Lint for HTTP messages.
Project description
httplint
This Python library lints HTTP messages; it checks them for correctness and reports any issues it finds.
It has been extracted from REDbot, which will eventually depend upon it. Unlike REDbot, it does not perform any 'active' checks by making requests to the network, and it does not have a Web user interface.
Using httplint as a Library
httplint exposes two classes for linting: HttpRequestLinter
and HttpResponseLinter
. They expose the following methods for telling the linter about the HTTP message:
- As appropriate:
process_request_topline
, which takes threebytes
arguments:method
,uri
, andversion
process_response_topline
, which takes threebytes
arguments:version
,status_code
, andstatus_phrase
process_headers
for the headers, taking a list of (name
,value
) tuples (bothbytes
)feed_content
for the body (which can be called zero to many times), taking aninbytes
argumentfinish_content
when done, which has two arguments; abool
indicating whether the response was complete, and an optional list of tuples for the trailers, in the same format thatprocess_headers
takes.
For example:
from httplint import HttpResponseLinter
linter = HttpResponseLinter()
linter.process_response_topline(b'HTTP/1.1', b'200', b'OK')
linter.process_headers([
(b'Content-Type', b'text/plain'),
(b'Content-Length', b'10'),
(b'Cache-Control', b'max-age=60')
])
linter.feed_content(b'12345')
linter.feed_content(b'67890')
linter.finish_content(True)
Using httplint from the Command Line
httplint can also be used from the command line. For example:
> curl -s -i --raw https://www.mnot.net/ | httplint -n
* The Content-Length header is correct.
* The resource last changed 8 days 6 hr ago.
* This response allows all caches to store it.
* The server's clock is correct.
* This response is fresh until 3 hr from now.
* This response may still be served by a cache once it becomes stale.
Interpreting Notes
Once a message has been linted, the results will appear on the notes
property. This is a list of Note
objects, each having the following attributes:
category
- the Note's category; seenote.categories
level
- seenote.levels
summary
- a brief, one-line description of the notedetail
- a longer explanation
Note that summary
is textual, and needs to be escaped in a markup environment; detail
, however, is already escaped HTML.
Continuing our example:
for note in linter.notes:
print(note.summary)
and the output should be:
The Content-Length header is correct.
This response allows all caches to store it.
This response doesn't have a Date header.
This response is fresh until 1 min from now.
This response may still be served by a cache once it becomes stale.
Field Descriptions
The description of any field can be found by calling get_field_description
. For example:
>>> from httplint import get_field_description
>>> get_field_description("Allow")
'The `Allow` response header advertises the set of methods that are supported by the resource.'
If a description cannot be found it will return None
.
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
Hashes for httplint-2023.12.12-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18eb857fa6810a5649eae8988cf6d226312e6c235f3fbbfd66b62b862941ac54 |
|
MD5 | 8d474f009cf51655a941be1d2441a726 |
|
BLAKE2b-256 | 93ec1d902e9e3e7fe5bd3f3f35fb0555021bfb534787f25671425f0bafd4002b |