HTTP/1.* - Hypertext Transfer Protocol¶
pcapkit.protocols.application.httpv1 contains
HTTP
only, which implements extractor for Hypertext Transfer
Protocol (HTTP/1.*) [*], whose structure is described
as below:
METHOD URL HTTP/VERSION\r\n :==: REQUEST LINE
<key> : <value>\r\n :==: REQUEST HEADER
............ (Ellipsis) :==: REQUEST HEADER
\r\n :==: REQUEST SEPARATOR
<body> :==: REQUEST BODY (optional)
HTTP/VERSION CODE DESP \r\n :==: RESPONSE LINE
<key> : <value>\r\n :==: RESPONSE HEADER
............ (Ellipsis) :==: RESPONSE HEADER
\r\n :==: RESPONSE SEPARATOR
<body> :==: RESPONSE BODY (optional)
- class pcapkit.protocols.application.httpv1.HTTP(file=None, length=None, **kwargs)[source]¶
-
This class implements Hypertext Transfer Protocol (HTTP/1.*).
- read(length=None, **kwargs)[source]¶
Read Hypertext Transfer Protocol (HTTP/1.*).
Structure of HTTP/1.* packet [RFC 7230]:
HTTP-message :==: start-line *( header-field CRLF ) CRLF [ message-body ]- Parameters:
- Return type:
- Returns:
Parsed packet data.
- Raises:
ProtocolError – If the packet is malformed.
- make(http_version='1.1', method=None, uri=None, status=None, status_default=None, status_namespace=None, status_reversed=False, message=None, headers=None, body=b'', **kwargs)[source]¶
Make (construct) packet data.
- Parameters:
http_version (
Literal['0.9','1.0','1.1',b'0.9',b'1.0',b'1.1']) – HTTP version.status (
StatusCode|str|bytes|int|None) – HTTP status code.status_namespace (
dict[str,int] |dict[int,str] |Type[IntEnum] |Type[IntEnum] |None) – Namespace of HTTP status code.status_reversed (
bool) – Whether to reverse the namespace.headers (
OrderedMultiDict[str,str] |None) – HTTP headers.body (
bytes) – HTTP body.**kwargs (
Any) – Arbitrary keyword arguments.
- Return type:
- Returns:
Constructed packet data.
- _read_http_header(header)[source]¶
Read HTTP/1.* header.
Structure of HTTP/1.* header [RFC 7230]:
start-line :==: request-line / status-line request-line :==: method SP request-target SP HTTP-version CRLF status-line :==: HTTP-version SP status-code SP reason-phrase CRLF header-field :==: field-name ":" OWS field-value OWS
- Parameters:
header (
bytes) – HTTP header data.- Return type:
- Returns:
Parsed packet data.
- Raises:
ProtocolError – If the packet is malformed.
- pcapkit.protocols.application.httpv1._test_start_line(data)[source]¶
Whether
dataopens with an HTTP/1.* start line.This is a classification predicate and parses nothing: it answers “is this HTTP/1?” for
HTTP._guess_version, which until #800 answered that question by trial-parsing every version in the family and keeping whichever one did not object – so a payload that is not HTTP at all was classified by which parser happened to fail less loudly.- Parameters:
data (
bytes) – Payload to classify.- Return type:
- Returns:
Whether the payload’s first line is a
request-lineor astatus-line(RFC 9112 Section 2.1).
Note
The acceptance rule is deliberately the same one
HTTP._read_http_headerapplies further down this module –_RE_METHODwith_RE_VERSIONfor a request,_RE_VERSIONwith_RE_STATUSfor a response – which is why this lives beside those three patterns rather than in the dispatcher that calls it. The two must accept the same start lines: a predicate looser than the parser classifies payloads the parser then refuses, and one tighter than the parser hands real HTTP/1 to a later arm.test_start_line_predicate_agrees_with_the_httpv1_parserpins that agreement.Both of the unpackings the parser performs before those patterns are mirrored too, and this is not pedantry – the second of them is the whole reason the HTTP/2 connection preface is not claimed here.
PRI * HTTP/2.0\r\n\r\nSM\r\n\r\nis deliberately a well-formed HTTP/1.1 request line (RFC 9113 Section 3.4), so a predicate that tested only the first line would answerTruefor it. Split at the header/body separator first, asHTTP.readdoes, and the preface’s header isPRI * HTTP/2.0with no CRLF left in it – which is exactly why the parser refuses it, and now why this does. Measured: without the separator split this returnedTruefor the preface.An HTTP/0.9 request line carries only two tokens and so is not recognised here either, matching the parser, which raises on fewer than three.
Auxiliary Data¶
Header Schemas¶
Data Models¶
- class pcapkit.protocols.data.application.httpv1.HTTP(*args: VT, **kwargs: VT)[source]¶
Bases:
ProtocolData model for HTTP/1.* protocol.
- header: OrderedMultiDict[str, str]¶
HTTP header.
- class pcapkit.protocols.data.application.httpv1.Header(dict_=None, **kwargs)[source]¶
Bases:
DataData model for HTTP/1.* header line.
- class pcapkit.protocols.data.application.httpv1.RequestHeader(*args: VT, **kwargs: VT)[source]¶
Bases:
HeaderData model for HTTP/1.* request header line.
- class pcapkit.protocols.data.application.httpv1.ResponseHeader(*args: VT, **kwargs: VT)[source]¶
Bases:
HeaderData model for HTTP/1.* response header line.
- status: StatusCode¶
HTTP response status.
Footnotes