blinkenxmas.http
The blinkenxmas.http module defines classes for parsing HTTP requests
and constructing a response. This is a very basic implementation intended for
use with the standard library’s http.server.
HTTPResponse, in particular, accepts a wide variety of inputs
including file-like objects, pathlib.Path instances, strings, or
byte-strings and will attempt to handle determination of encodings, content
types automatically. Furthermore, given the originating request, it will handle
cache and range-request response headers implicitly.
Several other functions are also provided for parsing HTTP Content- headers, and MIME multipart form-data.
Classes
- class blinkenxmas.http.DummyResponse(request, **kwargs)[source]
An HTTP response that does nothing; useful for things that need to keep a client connection for whatever reason.
- class blinkenxmas.http.HTTPResponse(request, body=None, *, status_code=HTTPStatus.OK, content_length=None, accept_ranges=True, filename=None, mime_type=None, encoding=None, last_modified=None, etag=None, headers=None)[source]
An HTTP response.
The request is the
http.server.BaseHTTPRequestHandlerinstance representing the original request. The body (which forms the body of the response) may contain astr,bytes, or a file-like object.Other parameters represent typical HTTP headers and, if not given, will be derived from the body where possible.
- Parameters:
request (http.server.BaseHTTPRequestHandler) – The request instance representing the client’s request.
body –
The object containing the body of the response. Ultimately this will be converted to a file-like object (
IOBasedescendent) in thestreamattribute. This can be:A file-like object in which case it will be used directly as the value of
stream.A
str. This will be converted to aio.BytesIOstream with UTF-8 encoding.A
bytesstring. This will be used verbatim as the content of aio.BytesIOstream.A
pathlib.Path. This will be opened as a binary file.
status_code (http.HTTPStatus) – The HTTP status code of the response. Expected to be a
http.HTTPStatusattribute. Defaults tohttp.HTTPStatus.OK.content_length (int) – The number of bytes in the response body. If not specified, and the body stream is seekable, this will be filled out automatically.
accept_ranges (bool) –
If
True(the default), handle “bytes” ranges automatically.Specifically, if this is set, the response will automatically handle sending only those ranges requested. It will re-write the “Content-Length”, “Content-Type”, “Content-Range”, and “Accept-Ranges” headers accordingly as necessary.
filename (str) – The original filename of the body (if any). If not specified, will be filled out automatically from the
nameproperty of thestreamif it exists.mime_type (str) – The MIME type of the response body. If not specified, will be determined automatically from the filename (if that was specified or determined).
encoding (str) – The encoding of the response body. If not specified, will be determined automatically from the filename (if that was specified or determined).
last_modified (datetime.datetime) – The last modification date of the response body. If this is specified it must be a time-zone aware datetime instance. If not specified, will be determined automatically (if possible) from the last modification date of the file containing the body content.
headers (dict) – Additional headers to include in the response.
- check_cached()[source]
Check if the response is fresh in the client’s cache.
If the request is GET or HEAD with appropriate caching tests (
If-Modified-Sinceand/orIf-None-Match), and the response has appropriate caching responses then this method will (if the response is still “fresh” in the client’s cache), modify thestatus_codetohttp.HTTPStatus.NOT_MODIFIEDand setstreamtoNone.
- check_ranges()[source]
Check if the request wanted a partial response (if the “Range:” header was included).
If the accept_ranges parameter was
Trueat construction (the default), and a range or ranges were requested, this handles re-writing the response accordingly (this may include re-writing the status code, “Content-Length”, “Content-Type”, “Content-Range”, and “Accept-Ranges” headers).
Functions
- blinkenxmas.http.parse_content_value(s)[source]
Parse the content of an HTTP Content-* header’s value, s. The result is a tuple of (value, attrs) where value is the principal value (the part before the first semi-colon, if any), and attrs is a dictionary of attributes that follow the principal value.
- blinkenxmas.http.split_multipart(request)[source]
Given request, a
HTTPRequestHandler, which must have a Content-Type of multipart/, yield each part of the multipart body as a separate (headers, content) tuple. The *headers are returned as aHTTPHeadersinstance, and the content as a file-like object.
- blinkenxmas.http.parse_formdata(request)[source]
Given request, a
HTTPRequestHandler, which must have a Content-Type of “multipart/form-data”, split the multipart body into its constituent parts, and return adictmapping form names to their corresponding content.Short text or binary values are returned as
strorbytesvalues respectively. Anything with a “filename” attribute, or which exceeds a relatively large string size (currently 64KB) will be returned as a file-like object.