parse-http-header-line
Parse HTTP header field lines into their names and values, and split
type; name=value parameter lists.
Installation
pip install parse-http-header-line
Quick start
from parse_http_header_line import parse_name_value, parse_parameters
parse_name_value(u"Content-Type: text/html\r\n")
# ("content-type", "text/html")
parse_name_value(u"Host: example.com")
# ("host", "example.com")
parse_parameters(u"multipart/form-data; boundary=abc=def")
# ("multipart/form-data", [("boundary", "abc=def")])
parse_parameters(u'form-data; name="file"; filename="hello.txt"')
# ("form-data", [("name", "file"), ("filename", "hello.txt")])
API
parse_name_value(line)
Parses one header field line and returns a (name, value) tuple.
| Return | Description |
|---|---|
name |
Field name, lowercased. |
value |
Field value with leading/trailing spaces and tabs removed. |
lineis Latin-1 text, with or without a trailing CRLF.- On Python 3,
lineisstr; on Python 2, it isunicode. - Bytes are rejected with
TypeError.
parse_name_value(u"Host: example.com")
# ("host", "example.com")
parse_name_value(u"Host: example.com:8080\r\n")
# ("host", "example.com:8080")
parse_name_value(u"X-Empty:\r\n")
# ("x-empty", "")
parse_name_value(u"X: caf\xe9\r\n") # obs-text is allowed
# ("x", "café")
Names are lowercased and surrounding whitespace is trimmed from the value:
parse_name_value(u"CONTENT-TYPE:\t text/html \t\r\n")
# ("content-type", "text/html")
The value is returned exactly as written (minus surrounding whitespace); no value grammar is applied. To interpret a value, parse the line first and then choose a semantic parser based on the name:
name, value = parse_name_value(
u'Content-Disposition: form-data; name="file"; filename="hello.txt"\r\n'
)
if name == "content-disposition":
type_text, parameters = parse_parameters(value)
# type_text == "form-data"
# parameters == [("name", "file"), ("filename", "hello.txt")]
parse_parameters(value)
Splits a type; name=value; ... value into a (type_text, parameters) tuple.
type_textis the leading type (a token ortype/subtype), lowercased.parametersis an ordered list of(name, value)pairs. Order and duplicate names are preserved. Parameter names are lowercased.
parse_parameters(u"multipart/form-data; boundary=abc=def")
# ("multipart/form-data", [("boundary", "abc=def")])
parse_parameters(u'x; v="a;b"')
# ("x", [("v", "a;b")])
parse_parameters(u'form-data; name="a\\"b"')
# ("form-data", [("name", 'a"b')])
Quoted values are unquoted, and a backslash escapes the next character.
Unquoted values extend to the next ;, preserving = and other characters in
boundary values.
Do not use parse_parameters for every field that contains a semicolon.
Fields such as Set-Cookie, Link, Accept, and authentication headers have
their own grammars.
Scanner helpers
Two position-based helpers are exported for use with the module's state machines:
skip_ows(text, position)— returns the first position after spaces and tabs.consume_token(text, position)— returns the first position after RFC 7230 token characters.
Errors
parse_name_value raises MalformedHeaderLineError for invalid field-line
syntax:
parse_name_value(u"\r\n") # empty line
parse_name_value(u" folded\r\n") # obsolete folded line
parse_name_value(u"no-colon\r\n") # missing ':'
parse_name_value(u": value\r\n") # empty field name
parse_name_value(u"bad name: v\r\n") # space in field name
parse_name_value(u"X: bad\x7fvalue\r\n") # DEL in field value
parse_parameters raises MalformedParameterListError when its argument is
not a well-formed parameterized value:
parse_parameters(u"form-data; name") # parameter has no '='
Testing
python -m unittest discover -s tests
License
This project is licensed under the MIT License.
Release files for parse-http-header-line 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| parse_http_header_line-0.1.0.tar.gz | 7.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| parse_http_header_line-0.1.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 14.0 kB
Release files / parse_http_header_line-0.1.0.tar.gz
| Download URL | parse_http_header_line-0.1.0.tar.gz |
|---|---|
| Size | 7.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fb798e39fe89ec3eed6b97548b134c63e7d240230e3810cb68e25495285d7e2e
|
|
BLAKE2b-256 checksum How to use checksums |
3023f6bfd19566dc0e8cc6a9cba7a8ed6732ba6b11d731d430d0048a93e10ce7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.7
|
Release files / parse_http_header_line-0.1.0-py2.py3-none-any.whl
| Download URL | parse_http_header_line-0.1.0-py2.py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
9865039f12c53479448e0d50ce41edc7afbeb88b3153d5202ac097e1e91b1aba
|
|
BLAKE2b-256 checksum How to use checksums |
a1163b8cebc8536a71dd372ef36f754fbb456cbd77ba03f65525fd8d0bb2090c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.7
|