hio.core.http.httping
hio.core.http.httping module http async io (nonblocking) support
Module Contents
Classes
Server Sent Event Stream Client parser |
|
Base class for objects that parse HTTP messages |
Functions
|
Return a string representation of a date according to RFC 1123 |
|
Given hostname host which could also be netloc which includes port |
|
Return dict of parsed query string. |
|
Returns duple of updated (qargs, query) |
|
Returns query string with unquoted values |
|
Format and return a header line. |
|
Return msg bytes in a chunk |
|
Generator to parse line from raw bytearray |
|
Generator to parse entire leader of header lines from raw bytearray |
|
Generator to parse next chunk from raw bytearray |
|
Generator to parse bom from raw bytearray |
|
Parse the response status line |
|
Parse the request start line |
Attributes
- hio.core.http.httping.METHODS = ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE', 'OPTIONS', 'TRACE', 'CONNECT'][source]
- exception hio.core.http.httping.HTTPException[source]
Bases:
ExceptionCommon base class for all non-exit exceptions.
- exception hio.core.http.httping.InvalidURL[source]
Bases:
HTTPExceptionCommon base class for all non-exit exceptions.
- exception hio.core.http.httping.UnknownProtocol(version)[source]
Bases:
HTTPExceptionCommon base class for all non-exit exceptions.
- exception hio.core.http.httping.BadStatusLine(line)[source]
Bases:
HTTPExceptionCommon base class for all non-exit exceptions.
- exception hio.core.http.httping.BadRequestLine(line)[source]
Bases:
BadStatusLineCommon base class for all non-exit exceptions.
- exception hio.core.http.httping.BadMethod(method)[source]
Bases:
HTTPExceptionCommon base class for all non-exit exceptions.
- exception hio.core.http.httping.LineTooLong(kind)[source]
Bases:
HTTPExceptionCommon base class for all non-exit exceptions.
- exception hio.core.http.httping.PrematureClosure(msg)[source]
Bases:
HTTPExceptionCommon base class for all non-exit exceptions.
- exception hio.core.http.httping.HTTPError(status, reason='', title='', detail='', fault=None, headers=None)[source]
Bases:
ExceptionHTTP error for use with Valet or Other WSGI servers to raise exceptions caught by the WSGI server.
- status is int HTTP status code, e.g. 400
- reason is str HTTP status text, "Unknown Error"
- title is str title of error
- headers is dict of extra headers to add to the response
- error
An internal application error code
- Type
int
- hio.core.http.httping.httpDate1123(dt)[source]
Return a string representation of a date according to RFC 1123 (HTTP/1.1).
The supplied date must be in UTC. import datetime httpDate1123(datetime.datetime.utcnow()) ‘Wed, 30 Sep 2015 14:29:18 GMT’
- hio.core.http.httping.normalizeHostPort(host, port=None, defaultPort=80)[source]
Given hostname host which could also be netloc which includes port and or port generate and return tuple (hostname, port) priority is if port is provided in hostname as host:port then use otherwise use port otherwise use defaultPort
- hio.core.http.httping.parseQuery(query)[source]
Return dict of parsed query string. Utility function
- hio.core.http.httping.updateQargsQuery(qargs=None, query='')[source]
Returns duple of updated (qargs, query) Where qargs parameter is dict of query arguments and query parameter is query string The returned qargs is updated with query string arguments and the returned query string is generated from the updated qargs If provided, qargs may have additional fields not in query string This allows combining query args from two sources, a dict and a string
https://www.w3.org/TR/2014/REC-html5-20141028/forms.html#url-encoded-form-data
- hio.core.http.httping.packHeader(name, *values)[source]
Format and return a header line.
For example: h.packHeader(‘Accept’, ‘text/html’)
- hio.core.http.httping.parseLine(raw, eols=(CRLF, LF, CR), kind='event line')[source]
Generator to parse line from raw bytearray Each line demarcated by one of eols kind is line type string for error message
Yields None If waiting for more to parse Yields line Otherwise
Consumes parsed portions of raw bytearray
Raise error if eol not found before MAX_LINE_SIZE
- hio.core.http.httping.parseLeader(raw, eols=(CRLF, LF), kind='leader header line', headers=None)[source]
Generator to parse entire leader of header lines from raw bytearray Each line demarcated by one of eols Yields None If more to parse Yields cimdict of headers Otherwise as indicated by empty headers
Raise error if eol not found before MAX_LINE_SIZE
- hio.core.http.httping.parseChunk(raw)[source]
Generator to parse next chunk from raw bytearray Consumes used portions of raw Yields None If waiting for more bytes Yields tuple (size, parms, trails, chunk) Otherwise Where:
size is int size of the chunk parms is dict of chunk extension parameters trails is dict of chunk trailer headers (only on last chunk if any) chunk is chunk if any or empty if not
- Chunked-Body = *chunk
last-chunk trailer CRLF
- chunk = chunk-size [ chunk-extension ] CRLF
chunk-data CRLF
chunk-size = 1*HEX last-chunk = 1*(“0”) [ chunk-extension ] CRLF chunk-extension= *( “;” chunk-ext-name [ “=” chunk-ext-val ] ) chunk-ext-name = token chunk-ext-val = token | quoted-string chunk-data = chunk-size(OCTET) trailer = *(entity-header CRLF)
- hio.core.http.httping.parseBom(raw, bom=codecs.BOM_UTF8)[source]
Generator to parse bom from raw bytearray Yields None If waiting for more to parse Yields bom If found Yields empty bytearray Otherwise Consumes parsed portions of raw bytearray
- class hio.core.http.httping.EventSource(raw=None, events=None, dictable=False)[source]
Bases:
objectServer Sent Event Stream Client parser
- parseEvents(self)[source]
Generator to parse events from .raw bytearray and append to .events Each event is dict with the following items:
id: event id utf-8 decoded or empty
name: event name utf-8 decoded or empty data: event data utf-8 decoded json: event data deserialized to dict when applicable pr None
assigns .retry if any
Yields None If waiting for more bytes Yields True When done
event = *( comment / field ) end-of-line comment = colon *any-char end-of-line field = 1*name-char [ colon [ space ] *any-char ] end-of-line end-of-line = ( cr lf / cr / lf / eof ) eof = < matches repeatedly at the end of the stream > lf =
- 0xA
cr =
- 0xD
space = 0x20 colon = 0x3A bom = when encoded as utf-8 b’’ name-char = a Unicode character other than LF, CR, or : any-char = a Unicode character other than LF or CR Event streams in this format must always be encoded as UTF-8. [RFC3629]
- parseEventStream(self)[source]
Generator to parse event stream from .raw bytearray stream appends each event to .events deque. assigns .bom if any assigns .retry if any Parses until connection closed
- Each event is dict with the following items:
id: event id utf-8 decoded or empty
name: event name utf-8 decoded or empty data: event data utf-8 decoded json: event data deserialized to dict when applicable pr None
Yields None If waiting for more bytes Yields True When completed and sets .ended to True If BOM present at beginning of event stream then assigns to .bom and deletes. Consumes bytearray as it parses
stream = [ bom ] *event event = *( comment / field ) end-of-line comment = colon *any-char end-of-line field = 1*name-char [ colon [ space ] *any-char ] end-of-line end-of-line = ( cr lf / cr / lf / eof ) eof = < matches repeatedly at the end of the stream > lf =
- 0xA
cr =
- 0xD
space = 0x20 colon = 0x3A bom = when encoded as utf-8 b’’ name-char = a Unicode character other than LF, CR, or : any-char = a Unicode character other than LF or CR Event streams in this format must always be encoded as UTF-8. [RFC3629]
- class hio.core.http.httping.Parsent(msg=None, dictable=None, method='GET')[source]
Bases:
objectBase class for objects that parse HTTP messages
- reinit(self, msg=None, dictable=None, method='GET')[source]
Reinitialize Instance msg = bytearray of request msg to parse dictable = Boolean flag If True attempt to convert json body method = method verb of associated request
- checkPersisted(self)[source]
Checks headers to determine if connection should be kept open until client closes it Sets the .persisted flag
- parseHead(self)[source]
Generator to parse headers in heading of .msg Yields None if more to parse Yields True if done parsing
- parseMessage(self)[source]
Generator to parse message bytearray. Parses msg if not None Otherwise parse .msg
- makeParser(self, msg=None)[source]
Make message parser generator and assign to .parser Assign msg to .msg If provided