Parse and serialise HTTP Structured Fields
Project description
HTTP Structured Fields in Python
This is a Python 3 library implementing parsing and serialisation of HTTP Structured Fields.
The library's initial purpose is to prove the algorithms in the specification; as a result, it is not at all optimised. It tracks the specification closely, but since it is not yet an RFC, may change at any time.
Currently, this implements draft-ietf-httpbis-sfbis-03.
Python API
Parsing
Textual HTTP headers can be parsed by calling parse
; the return value is a data structure that represents the field value.
>>> from http_sf import parse, ser
>>> parse(b"foo; a=1, bar; b=2", tltype="dictionary")
{'foo': (True, {'a': 1}), 'bar': (True, {'b': 2})}
parse()
takes a bytes-like object as the first argument. If you want to parse a string, please .encode()
it first.
Indicating Top-Level Type
Because the library needs to know which kind of field it is, you need to hint this when calling parse
. There are two ways to do this:
- Using a
tltype
parameter, whose value should be one of 'dictionary', 'list', or 'item'. - Using a
name
parameter to indicate a field name that has a registered type, per the retrofit draft.
Note that if you use name
, a KeyError
will be raised if the type associated with the name isn't known.
Types
In the returned data, Dictionaries are represented as Python dictionaries; Lists are represented as Python lists, and Items are the bare type.
Bare types are represented using the following Python types:
- Integers:
int
- Decimals:
float
- Strings:
str
- Tokens:
http_sf.Token
(aUserString
) - Byte Sequences:
bytes
- Booleans:
bool
- Dates:
datetime.datetime
- Display Strings:
http_sf.DisplayString
(aUserString
)
Inner Lists are represented as lists as well.
Parameters
Structured Types that can have parameters (including Dictionary and List members as well as singular Items and Inner Lists) are represented as a tuple of (value, parameters)
where parameters is a dictionary.
So, a single item that's a Token with one parameter whose value is an integer will be represented like this:
>>> parse(b"foo; a=1", tltype="item")
(Token("foo"), {'a': 1})
Note that even if there aren't parameters, a tuple will still be returned, as in some items on this List:
>>> parse(b"a, b; q=5, c", tltype="list")
[(Token("a"), {}), (Token("b"), {'q': 5}), (Token("c"), {})]
Serialisation
To serialise that data structure back to a textual Structured Field, use ser
:
>>> field = parse(b"a, b; q=5, c", tltype="list")
>>> ser(field)
'a, b;q=5, c'
When using ser
, if an Item or Inner List doesn't have parameters, they can be omitted; for example:
>>> structure = [5, 6, (7, {"with": "param"})]
>>> ser(structure)
'5, 6, 7;with="param"'
Note that ser
produces a string, not a bytes-like object.
Command Line Use
You can validate and examine the data model of a field value by calling the library on the command line, using -d
, -l
and -i
to denote dictionaries, lists or items respectively; e.g.,
> python3 -m http_sf -i "foo;bar=baz"
[
{
"__type": "token",
"value": "foo"
},
{
"bar": {
"__type": "token",
"value": "baz"
}
}
]
or:
> python3 -m http_sf -i "foo;&bar=baz"
FAIL: Key does not begin with lcalpha or * at: &bar=baz
Alternatively, you can pass the field name with the -n
option, provided that it is a compatible retrofit field:
> python3 -m http_sf -n "Cache-Control" "max-age=40, must-revalidate"
{
"max-age": [
40,
{}
],
"must-revalidate": [
true,
{}
]
}
Note that if successful, the output is in the JSON format used by the test suite.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file http-sf-1.0.0.tar.gz
.
File metadata
- Download URL: http-sf-1.0.0.tar.gz
- Upload date:
- Size: 17.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb8878d0316d1fca72bde6027f2d2671e3f51d816409d53e740babae4d42771a |
|
MD5 | c3f994d257327d20c41edfd92e6b2f42 |
|
BLAKE2b-256 | b35121140a6310366e652bd5c9dde283068d1a4c1aa75e1cab3e8309615f7df3 |
Provenance
File details
Details for the file http_sf-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: http_sf-1.0.0-py3-none-any.whl
- Upload date:
- Size: 22.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a41809e7152dd8bab2f22e85d2a7753482069f56911bccc74bb60268a1a864a6 |
|
MD5 | 738ed9a46082458d121240dbb8ad0126 |
|
BLAKE2b-256 | c656a360a953f08f82d735472724f4a89c6dad3aa7f05a8552c6471e7028ee9d |