HttpRequest - wrvenkat/request_parser GitHub Wiki

Usage:

The __init__ method takes two keyword arguments, the request_stream which is the iterable request bytes and a settings object that affects certain behavior and parameters of the parser as described in Settings. settings is optional and if it is None, Settings.default() is used.

http_request = HttpRequest(request_stream=req_stream, settings=None)

Methods

Parsing methods

parse() parse the entire request
parse_header() parse the HTTP request header
parse_body() parse the HTTP request body

Accessing HTTP Information

Attributes
GET holds the URL query string in a dictionary of the format key: [value1, value2]
POST holds the POST parameters in a form data and multipart request in a dictionary of the format key: [value1, value2]

For a multipart/form-data request, the POST attribute takes the form,
param_name: [{'data' : value, key1: value1, key2: value2}]  where the mapping 'data': value holds the value of param_name.
FILES holds the uploaded files in a multipart/form-data request in a dictionary of the format name: <UploadedFileObject>  where name is the value of the name attribute in the request
META holds the raw information about a request obtained from the request header in a dictionary. For example, the dictionary of request headers can be accessed by http_request.META[MetaDict.Info.REQ_HEADERS]  where MetaDict is a class which holds the keys to the META dictionary.

Accessing the HTTP request headers would be the most frequent and expected usage for the META attribute. For a sample of the information contained in this attribute, please image below.
alt text

Access information in request line
get_uri(raw=False) return the request URI includeing the scheme, host, path and query string
raw  - if false encodes the path and query in ascii; returns as is otherwise.
get_scheme() return the scheme i.e http, https etc.
For proxy requests, the scheme is retrieved from the request line. For normal requests, the return value is UNKNOWN
get_host() return the host to which the request is sent. Note that for requests that are HTTP proxt compliant, the value returned is one from the request line. Otherwise, this value is the one sent in the Host header.
get_path() return the path component of the request
get_full_path(force_append_slash=False, raw=False) return the full path including the query string
force_append_slash - add a '/' if force_append_slash is true and the path doesn't end with '/'
raw  - if false encodes the path and query in ascii; return as is otherwise.
get_protocol_info() returns information about the protocol in use from the request line. For example, HTTP/1.1

Most of the above methods have setters to modify the information in the object.

Accessing request body

body()

for requests of type application/x-www-form-urlencoded and multipart/form-data, the request information can be directly accessed from the attribtues. However, for requests of other type, body() can be used to get the raw request body.

For the above mentioned request types, reading the body using body() after the body has been parsed raises RawPostDataException. This is because the raw body has already been consumed. If at all access to the raw body is required, it can be accessed right after a call to parse_header().

body() raises RequestDataTooBig whent the size of the raw request body exceeds the value of Settings attribute DATA_UPLOAD_MAX_MEMORY_SIZE.

Exceptions

This section lists the exceptions that may be raised while parsing an HTTP request or when accessing the information from an HttpRequest object.

Exception
UnreadablePostError raised when any IOError occurs when reading from the request body
InvalidHttpRequest raised when the request doesn't conform to a proper HTTP request format
RawPostDataException raised when the request body is accessed with a call to body() after the request body has been parsed and the request content type is either application/x-www-form-urlencoded or multipart/form-data
RequestHeaderParseException raised in a call to parse_request_body() when is is called before the request header has been parsed
NoHostFoundException raised when parsing the request header in a call to parse_request_header() when the request header doesn't have a Host header
⚠️ **GitHub.com Fallback** ⚠️