hio.core.http.httping

hio.core.http.httping module http async io (nonblocking) support

Module Contents

exception hio.core.http.httping.HTTPException

Bases: Exception

Common base class for all non-exit exceptions.

exception hio.core.http.httping.InvalidURL

Bases: HTTPException

Common base class for all non-exit exceptions.

exception hio.core.http.httping.UnknownProtocol(version)

Bases: HTTPException

Common base class for all non-exit exceptions.

exception hio.core.http.httping.BadStatusLine(line)

Bases: HTTPException

Common base class for all non-exit exceptions.

exception hio.core.http.httping.BadRequestLine(line)

Bases: BadStatusLine

Common base class for all non-exit exceptions.

exception hio.core.http.httping.BadMethod(method)

Bases: HTTPException

Common base class for all non-exit exceptions.

exception hio.core.http.httping.LineTooLong(kind)

Bases: HTTPException

Common base class for all non-exit exceptions.

exception hio.core.http.httping.PrematureClosure(msg)

Bases: HTTPException

Common base class for all non-exit exceptions.

exception hio.core.http.httping.HTTPError(status, reason='', title='', detail='', fault=None, headers=None)

Bases: Exception

HTTP 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

render(jsonify=False)

Render and return the attributes as a bytes If jsonify then render as serialized json

hio.core.http.httping.httpDate1123(dt)

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.now(datetime.UTC)) ‘Wed, 30 Sep 2015 14:29:18 GMT’

hio.core.http.httping.normalizeHostPort(host, port=None, defaultPort=80)

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)

Return dict of parsed query string. Utility function

hio.core.http.httping.updateQargsQuery(qargs=None, query='')

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.unquoteQuery(query)

Returns query string with unquoted values

hio.core.http.httping.packHeader(name, *values)

Format and return a header line.

For example: h.packHeader(‘Accept’, ‘text/html’)

hio.core.http.httping.packChunk(msg)

Return msg bytes in a chunk

hio.core.http.httping.parseLine(raw, eols=(CRLF, LF, CR), kind='event line')

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)

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)

Generator to parse next chunk from raw bytearray. Consumes used portions of raw. Yields None while waiting for more bytes. Yields tuple (size, parms, trails, chunk) otherwise.

Tuple values: - size: integer size of the chunk. - parms: dict of chunk extension parameters. - trails: dict of chunk trailer headers (only on last chunk if any). - chunk: chunk data or empty if not present.

ABNF:

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)

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

hio.core.http.httping.parseStatusLine(line)

Parse the response status line

hio.core.http.httping.parseRequestLine(line)

Parse the request start line

class hio.core.http.httping.EventSource(raw=None, events=None, dictable=False)

Bases: object

Server Sent Event Stream Client parser

close()

Assign True to .closed

parseEvents()

Generator to parse events from .raw bytearray and append to .events.

Each event is a dict with: - 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 or None.

Assigns .retry if present.

Yields None while waiting for more bytes. Yields True when done.

ABNF:

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            = %x0A
cr            = %x0D
space         = %x20
colon         = %x3A
bom           = U+FEFF (UTF-8 bytes EF BB BF)
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()

Generator to parse event stream from .raw bytearray stream. Appends each event to .events deque. Assigns .bom and .retry if present. Parses until the connection is closed.

Each event is a dict with: - 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 or None.

Yields None while waiting for more bytes. Yields True when completed and sets .ended to True. If BOM is present at the beginning, assigns .bom and consumes it. Consumes the bytearray as it parses.

ABNF:

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            = %x0A
cr            = %x0D
space         = %x20
colon         = %x3A
bom           = U+FEFF (UTF-8 bytes EF BB BF)
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]

makeParser(raw=None)

Make event stream parser generator and assign to .parser Assign msg to .msg If provided

parse()

Service the event stream parsing. Call .makeParser to set up the parser. When done parsing, .parser is None and .ended is True.

class hio.core.http.httping.Parsent(msg=None, dictable=None, method='GET')

Bases: object

Base class for objects that parse HTTP messages

reinit(msg=None, dictable=None, method='GET')

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

close()

Assign True to .closed and close parser

checkPersisted()

Checks headers to determine if connection should be kept open until client closes it Sets the .persisted flag

parseHead()

Generator to parse headers in heading of .msg Yields None if more to parse Yields True if done parsing

parseBody()

Parse body

parseMessage()

Generator to parse message bytearray. Parses msg if not None Otherwise parse .msg

makeParser(msg=None)

Make message parser generator and assign to .parser Assign msg to .msg If provided

parse()

Service the message parsing. Call .makeParser to set up the parser. When done parsing, .parser is None and .ended is True.

dictify()

Attempt to convert body to dict data if .dictable or json content-type