'Sets a cookie.
``expires`` can be a string in the correct format or a
``datetime.datetime`` object in UTC. If ``expires`` is a datetime
object then ``max_age`` will be calculated.'

def set_cookie(self, key, value='', max_age=None, expires=None, path='/', domain=None, secure=False, httponly=False):

    self.cookies[key] = value
    if (expires is not None):
        if isinstance(expires, datetime.datetime):
            delta = (expires - expires.utcnow())
            delta = (delta + datetime.timedelta(seconds=1))
            expires = None
            max_age = max(0, ((delta.days * 86400) + delta.seconds))
        else:
            self.cookies[key]['expires'] = expires
    if (max_age is not None):
        self.cookies[key]['max-age'] = max_age
        if (not expires):
            self.cookies[key]['expires'] = cookie_date((time.time() + max_age))
    if (path is not None):
        self.cookies[key]['path'] = path
    if (domain is not None):
        self.cookies[key]['domain'] = domain
    if secure:
        self.cookies[key]['secure'] = True
    if httponly:
        self.cookies[key]['httponly'] = True
