request_response
pigwig.request_response
Request(app: PigWig, method: str, path: str, query: typing.Mapping[str, str | list[str]], headers: HTTPHeaders, body: dict, cookies: http.cookies.BaseCookie, wsgi_environ: dict[str, typing.Any])
an instance of this class is passed to every route handler. has the following instance attrs:
app- an instance of :class:.PigWigmethod- the request method/verb (GET,POST, etc.)path- WSGI environPATH_INFO(/foo/bar)query- dict of parsed query string. duplicate keys appear as listsheaders- HTTPHeaders of the headersbody- dict of parsed body content. see PigWig.content_handlers for a list of supported content typescookies- an instance of http.cookies.SimpleCookiewsgi_environ- the raw WSGI environ handed down from the server
Source code in pigwig/request_response.py
34 35 36 37 38 39 40 41 42 43 44 | |
get_secure_cookie(key: str, max_time: datetime.timedelta) -> str | None
decode and verify a cookie set with Response.set_secure_cookie
Parameters:
-
key(str) –keypassed toset_secure_cookie -
max_time(datetime.timedelta) –amount of time since cookie was set that it should be considered valid for. this is normally equal to the
max_agepassed toset_secure_cookie. longer times mean larger windows during which a replay attack is valid. this can beNone, in which case no expiry check is performed
Source code in pigwig/request_response.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
Response(body: str | bytes | typing.Iterator[bytes] | None = None, code: int = 200, content_type: str = 'text/plain', location: str | None = None, extra_headers: list[tuple[str, str]] | None = None)
every route handler should return an instance of this class (or raise an exceptions.HTTPException)
Parameters:
-
body(str | bytes | typing.Iterator[bytes] | None, default:None) –- if
None, the response body is empty - if a
str, the response body is UTF-8 encoded - if a
bytes, the response body is sent as-is - if a generator, the response streams the yielded bytes
- if
-
code(int, default:200) –HTTP status code; the "reason phrase" is generated automatically from http.client.responses
-
content_type(str, default:'text/plain') –sets the Content-Type header
-
location(str | None, default:None) –if not
None, sets the Location header. you must still specify a 3xx code -
extra_headers(list[tuple[str, str]] | None, default:None) –if not
None, an iterable of extra header 2-tuples to be sent
Source code in pigwig/request_response.py
105 106 107 108 109 110 111 112 113 114 115 116 117 | |
json_encoder = jsonlib.JSONEncoder(indent='\t')
class-attribute
instance-attribute
body: str | bytes | typing.Iterator[bytes] | None = body
instance-attribute
code: int = code
instance-attribute
headers: list[tuple[str, str]] = headers
instance-attribute
set_cookie(key: str, value: typing.Any, domain: str | None = None, path: str = '/', expires: datetime.datetime | None = None, max_age: datetime.timedelta | None = None, secure: bool = False, http_only: bool = False) -> None
adds a Set-Cookie header
Parameters:
-
expires(datetime.datetime | None, default:None) –if set to a value in the past, the cookie is deleted. if this and
max_ageare not set, the cookie becomes a session cookie. -
max_age(datetime.timedelta | None, default:None) –according to the spec, has precedence over expires. if you specify both, both are sent.
-
secure(bool, default:False) –controls when the browser sends the cookie back - unrelated to set_secure_cookie
see the docs for an explanation of the other params
Source code in pigwig/request_response.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
set_secure_cookie(request: Request, key: str, value: typing.Any, **kwargs: typing.Any) -> None
this function accepts the same keyword arguments as set_cookie but stores a
timestamp and a signature based on request.app.cookie_secret. decode with
Request.get_secure_cookie.
the signature is a SHA-256 hmac of the key, value, and timestamp. the value is not
encrypted and is readable by the user, but is signed and tamper-proof (assuming the
cookie_secret is secure). because we store the signing time, expiry is checked with
Request.get_secure_cookie. you generally will want to pass this function a
max_age equal to max_time used when reading the cookie.
Source code in pigwig/request_response.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
json(obj: typing.Any) -> Response
classmethod
generate a streaming Response object from an object with an application/json
content type. the default json_encoder indents with tabs - override if you want
different indentation or need special encoding.
Source code in pigwig/request_response.py
166 167 168 169 170 171 172 173 174 | |
render(request: Request, template: str, context: dict[str, typing.Any]) -> 'Response'
classmethod
generate a streaming Response object from a template and a context with a
text/html content type.
Parameters:
-
request(Request) –the request to generate the response for
-
template(str) –the template name to render, relative to
request.app.template_dir -
context(dict[str, typing.Any]) –if you used the default jinja2 template engine, this is a dict
Source code in pigwig/request_response.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |