HTTP/1.* - Hypertext Transfer Protocol

pcapkit.protocols.application.httpv1 contains HTTP, which implements extractor for Hypertext Transfer Protocol (HTTP/1.*) [*], and its Type enumeration. The structure of an HTTP/1.* message 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)

Changed in version 1.5.0: A message rebuilds byte for byte: the whitespace of the start line and header lines, and octets in them that are not UTF-8, are kept as captured rather than normalised or re-encoded (#1241, #1302, #1333).

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'', charset=None, raw_line=None, raw_header=(), **kwargs)[source]

Make (construct) packet data.

The start line and the field lines are built from the parsed values with single SPs, name: value and UTF-8 field values. raw_line is written in place of the start line, and each raw_header entry in place of the header field at the same position, whenever reading it back gives the same values – so a message rebuilds byte for byte, whitespace, obs-fold and field-value charset included, while a value that has been changed is written from the change. Field lines are matched to headers by content and in order, so adding or removing a field leaves the others as received.

Parameters:
  • http_version (Literal['0.9', '1.0', '1.1', b'0.9', b'1.0', b'1.1']) – HTTP version.

  • method (Method | str | bytes | None) – HTTP method.

  • uri (str | bytes | None) – HTTP request URI.

  • status (StatusCode | str | bytes | int | None) – HTTP status code.

  • status_default (int | None) – Default 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.

  • message (str | bytes | None) – HTTP status message.

  • headers (OrderedMultiDict[str, str] | None) – HTTP headers.

  • body (bytes) – HTTP body.

  • charset (str | None) – Encoding for a str request URI or status message, UTF-8 if not given.

  • raw_line (bytes | None) – HTTP start line as received, CRLF excluded.

  • raw_header (Sequence[bytes]) – HTTP field lines as received, CRLF excluded, one per header field and in the order of headers.

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

Return type:

HTTP

Returns:

Constructed packet data.

Raises:

ProtocolError – If the values are neither a request’s nor a response’s, or if raw_line or a raw_header entry that reads back as the given values carries a CR or LF that the values do not (other than the CRLF of an obs-fold).

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.

Auxiliary Data

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

Bases: EnumLookup, StrEnum

HTTP packet type.

Built on EnumLookup, the lookup contract shared by every non-registry enumeration (#877). The class defines neither get nor _missing_ of its own.

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: OrderedMultiInfo[str, str]

HTTP header.

body: Any

HTTP body.

raw_header: tuple[bytes, ...]

HTTP field lines as received, CRLF excluded and obs-fold continuations kept: one per field in header, in order.

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.

charset: str | None

Charset to encode uri with, None for UTF-8.

raw_line: bytes

HTTP request line as received, CRLF excluded.

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.

charset: str | None

Charset to encode message with, None for UTF-8.

raw_line: bytes

HTTP status line as received, CRLF excluded.

Footnotes