Utility functions to parse and manipulate HTTP messages.
Dictionary of HTTP headers for both request and response. Value of each key, lower-cased string, is represented as a byte-string of comma separated values. Refer RFC2616 for more information.
Calculate port based on scheme name. If scheme and port matches, port is left empty. Otherwise port is explicitly set to port number and returned as a string.
scheme, byte-string.
Every HTTP request starts with a start line specifying method, uri and version. Parse them and return a tuple of (method, uri, version).
startline is expected in bytes and all the elements in returned tuple will be in byte-strings as well.
Using stdlib’s urllib.parse.urlsplit() API, parse uri into its component parts.
byte-string from HTTP Host header. Many times uri, as found in the request startline, have abs_path alone, in which case, optional host name as found in the Host header can be supplied. It will be applied on the urlsplit() result.
Note that as per RFC definition Host header can also contain port address.
Refer to Section 5.2 in RFC 2616.txt.
Using the baseurl and the remaining variable part of a url namely path, query, fragment construct a full url that can be sent in response and interpreted by clients.
Return relative-url or absolute-url as a string.
Compare two URLs url1 and url2 based on RFC2616 specification.
Parse netpath string containing host-name and script-path into a tuple of (netloc, script-path). If script-path is absent, return (netloc, '').
HTML form values can be submited via POST or PUT methods, in which case, request Content-Type will be appropriately set. This function supports, application/x-www-form-urlencoded, multipart/form-data media-types. Note that files are submitted using multipart/form-data media-type. Returns a dictionary of arguments.
Return a list of lowercased token values, in byte-strings, from Connection header field.
HTTP applications have historically allowed three different formats for the representation of date/time stamps:
Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
The first format is preferred as an Internet standard. This function heuristically parses the date format (from request header) and changes the format to RFC 1123 format. Returns datetime object
Convert timestamp adjusting it to GMT using RFC 1123 date format. Return string.
Convert date-time string, RFC 1123 normalized format, to python datetime object.
Parse Transfer-Encoding header value,
1 2 | transfer-coding = "chunked" | transfer-extension
transfer-extension = token *( ";" parameter )
|
Returns [ ( token, param ), ... ], where, token and param are byte-strings. token is also lower-cased.
Parse Accept header value,:
Accept = "Accept" ":"
#( media-range [ accept-params ] )
media-range = ( "*/*"
| ( type "/" "*" )
| ( type "/" subtype )
) *( ";" parameter )
accept-params = ";" "q" "=" qvalue *( accept-extension )
accept-extension = ";" token [ "=" ( token | quoted-string ) ]
Returns [ ( '<type>/<subtype>', q, param ), ... ], where, param is a byte-string representing media-type parameters and q is quality value in float for media-type.
Parse Accept-Charset header value,:
Accept-Charset = "Accept-Charset" ":"
1#( ( charset | "*" )[ ";" "q" "=" qvalue ] )
Returns, [ (charset, qvalue), ... ], where, charset is in string and qvalue is in float. The returned list is sorted by qvalue which is in float.
Parse Accept-Encoding header value,:
Accept-Encoding = "Accept-Encoding" ":"
1#( codings [ ";" "q" "=" qvalue ] )
codings = ( content-coding | "*" )
Returns, [ (content-coding, qvalue), ... ], where content-coding is in string and qvalue is in float. Returned list is sorted by qvalue.
Parse content type using grammar,:
Content-Type = "Content-Type" ":" media-type
media-type = type "/" subtype *( ";" parameter )
type = token
subtype = token
parameter = attribute "=" value
attribute = token
value = token | quoted-string
Returns, [ type, subtype, [ (attr, value), ... ] where all elements are in byte-string.
Parse content disposition using grammar,:
content-disposition = "Content-Disposition" ":"
disposition-type *( ";" disposition-parm )
disposition-type = "attachment" | disp-extension-token
disposition-parm = filename-parm | disp-extension-parm
filename-parm = "filename" "=" quoted-string
disp-extension-token = token
disp-extension-parm = token "=" ( token | quoted-string )
Returns, ( token, [ (attr, value), ... ] ), where all elements are in byte-string.