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]

Bases: HTTP[HTTP, HTTP]

This class implements Hypertext Transfer Protocol (HTTP/1.*).

property alias: Literal['HTTP/0.9', 'HTTP/1.0', 'HTTP/1.1']

Acronym of current protocol.

property version: Literal['0.9', '1.0', '1.1']

Version of current protocol.

classmethod id()[source]

Index ID of the protocol.

Return type:

tuple[Literal['HTTP'], Literal['HTTPv1']]

Returns:

Index ID of the protocol.

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:
  • length (int | None) – Length of packet data.

  • **kwargs (Any) – Arbitrary keyword arguments.

Return type:

HTTP

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:
Return type:

HTTP

Returns:

Constructed packet data.

classmethod _make_data(data)[source]

Create key-value pairs from data for protocol construction.

Parameters:

data (HTTP) – protocol data

Return type:

dict[str, Any]

Returns:

Key-value pairs for protocol construction.

_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:

tuple[Header, OrderedMultiDict[str, str]]

Returns:

Parsed packet data.

Raises:

ProtocolError – If the packet is malformed.

_read_http_body(body, *, headers)[source]

Read HTTP/1.* body.

Parameters:
Return type:

Any

Returns:

Raw HTTP body.

pcapkit.protocols.application.httpv1._test_start_line(data)[source]

Whether data opens 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:

bool

Returns:

Whether the payload’s first line is a request-line or a status-line (RFC 9112 Section 2.1).

Note

The acceptance rule is deliberately the same one HTTP._read_http_header applies further down this module – _RE_METHOD with _RE_VERSION for a request, _RE_VERSION with _RE_STATUS for 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_parser pins 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\n is deliberately a well-formed HTTP/1.1 request line (RFC 9113 Section 3.4), so a predicate that tested only the first line would answer True for it. Split at the header/body separator first, as HTTP.read does, and the preface’s header is PRI * HTTP/2.0 with no CRLF left in it – which is exactly why the parser refuses it, and now why this does. Measured: without the separator split this returned True for 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

class pcapkit.protocols.application.httpv1.Type(*values)[source]

Bases: StrEnum

HTTP packet type.

REQUEST = 'request'

Request packet.

RESPONSE = 'response'

Response packet.

static _generate_next_value_(name, start, count, last_values)

Return the lower-cased version of the member name.

Header Schemas

class pcapkit.protocols.schema.application.httpv1.HTTP(*args: _VT, **kwargs: _VT)[source]

Bases: Schema

Header schema for HTTP/1.* packet.

data: bytes = <BytesField data>

Packet data.

Data Models

class pcapkit.protocols.data.application.httpv1.HTTP(*args: VT, **kwargs: VT)[source]

Bases: Protocol

Data model for HTTP/1.* protocol.

receipt: Header

HTTP receipt.

header: OrderedMultiDict[str, str]

HTTP header.

body: Any

HTTP body.

class pcapkit.protocols.data.application.httpv1.Header(dict_=None, **kwargs)[source]

Bases: Data

Data model for HTTP/1.* header line.

type: Type

Receipt type.

class pcapkit.protocols.data.application.httpv1.RequestHeader(*args: VT, **kwargs: VT)[source]

Bases: Header

Data model for HTTP/1.* request header line.

type: Type

HTTP request header line.

method: Method

HTTP method.

uri: str

HTTP request URI.

version: str

HTTP request version.

class pcapkit.protocols.data.application.httpv1.ResponseHeader(*args: VT, **kwargs: VT)[source]

Bases: Header

Data model for HTTP/1.* response header line.

type: Type

HTTP response header line.

version: str

HTTP response version.

status: StatusCode

HTTP response status.

message: str

HTTP response status message.

Footnotes