def set_cookie(self, name: str, value: str, **options): """ set cookie to http resposne :param options: enable to include following options :param max_age: maximum age in seconds. (default: None) :param expires: a datetime object or UNIX timestamp. (default: None) :param domain: the domain that is allowed to read the cookie. (default: current domain) :param path: limits the cookie to a given path (default: current path) :param secure: limit the cookie to HTTPS connections (default: off). :param httponly: prevents client-side javascript to read this cookie (default: off, requires Python 2.6 or newer). """ if len(value) > 4096: raise ValueError('cookie value is too long.') self.cookies[name.strip()] = hval(value) for key, value in options.items(): key = hkey(key) if key == 'max_age': if isinstance(value, timedelta): value = value.seconds + value.days * 24 * 3600 if key == 'expires': if isinstance(value, (date_t, datetime)): value = value.timetuple() elif isinstance(value, (int, float)): value = time.gmtime(value) assert isinstance(value, tuple) value = time.strftime("%a, %d %b %Y %H:%M:%S GMT", value) self.cookies[name][key] = value
def _generate_request(self, method=b'GET', path=b'/', version=b'1.1', accept=b'*/*', accept_encoding=b'gzip, deflate', connection=b'Keep-alive', data=b'', **options): headers = [ b'%b: %b\r\n' % (tob(hkey(key)), tob(value)) for key, value in options.items() ] return (b'%b %b HTTP/%b\r\n' b'Accept: %b\r\n' b'Accept-Encoding: %b\r\n' b'Connection: %b\r\n' b'%b' b'\r\n' b'%b' % (method, path, version, accept, accept_encoding, connection, b''.join(headers), data))
def get_all(self, key): return super().get_all(hkey(key))
def get(self, key, default=None, index=-1): return super().get(hkey(key), default, index)
def __setitem__(self, key, value): return super().__setitem__(hkey(key), hval(value))
def __getitem__(self, key): return super().__getitem__(hkey(key))
def __contains__(self, key): return super().__contains__(hkey(key))
def __init__(self, *a, **k): """TODO """ self.data = {hkey(k): [v] for (k, v) in dict(*a, **k).items()}
def set_header(self, name: str, value: str): """ set response header """ self.response.headers[hkey(name)] = hval(value)