httpmsg

package
v0.683.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package httpmsg decodes HTTP/1.x messages per RFC 9112 + RFC 9110. HTTP is the foundational application-layer protocol of the web — every browser-server interaction, every REST API call, every webhook delivery, every internal microservice-to-service call (when not gRPC) speaks it.

Wrap-vs-native judgement

Native. HTTP/1.x is a plain-text request/response protocol with a start line + header field list + blank CRLF line + optional body. Body framing is either Content-Length (fixed-size) or Transfer-Encoding: chunked (length lines in hex + CRLF + data chunks). Pasting a message from Wireshark "Follow Stream" / mitmproxy export / Burp / curl -v output / a web-server access log / an internal API trace is enough — no HTTP stack, no socket, no network attach.

What this package covers

  • **Start-line dispatch**: auto-detect request (METHOD URI VERSION) vs response (VERSION CODE REASON) by whether the first token starts with "HTTP/" (response) or any other text (request).
  • **Request methods** (10 documented): GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH (RFC 5789), plus PROPFIND / PROPPATCH / MKCOL / COPY / MOVE / LOCK / UNLOCK (WebDAV per RFC 4918) recognised.
  • **Response status-code lookup** (~50 entries):
  • 1xx Informational: 100 Continue, 101 Switching Protocols, 102 Processing, 103 Early Hints.
  • 2xx Success: 200 OK, 201 Created, 202 Accepted, 203 Non-Authoritative Information, 204 No Content, 205 Reset Content, 206 Partial Content, 207 Multi- Status (WebDAV), 208 Already Reported, 226 IM Used.
  • 3xx Redirection: 300 Multiple Choices, 301 Moved Permanently, 302 Found, 303 See Other, 304 Not Modified, 305 Use Proxy, 307 Temporary Redirect, 308 Permanent Redirect.
  • 4xx Client error: 400 Bad Request, 401 Unauthorized, 402 Payment Required, 403 Forbidden, 404 Not Found, 405 Method Not Allowed, 406 Not Acceptable, 407 Proxy Authentication Required, 408 Request Timeout, 409 Conflict, 410 Gone, 411 Length Required, 412 Precondition Failed, 413 Payload Too Large, 414 URI Too Long, 415 Unsupported Media Type, 416 Range Not Satisfiable, 417 Expectation Failed, 418 I'm a teapot (RFC 2324), 421 Misdirected Request, 422 Unprocessable Entity (WebDAV), 423 Locked, 424 Failed Dependency, 425 Too Early, 426 Upgrade Required, 428 Precondition Required, 429 Too Many Requests, 431 Request Header Fields Too Large, 451 Unavailable For Legal Reasons.
  • 5xx Server error: 500 Internal Server Error, 501 Not Implemented, 502 Bad Gateway, 503 Service Unavailable, 504 Gateway Timeout, 505 HTTP Version Not Supported, 506 Variant Also Negotiates, 507 Insufficient Storage, 508 Loop Detected, 510 Not Extended, 511 Network Authentication Required.
  • **Header field parsing**: case-insensitive name match, line continuation (deprecated but still seen — folded into previous header), multi-value preserved as ordered list.
  • **Typed envelope fields surfaced**: Host, User-Agent, Server, Content-Type, Content-Length, Transfer- Encoding, Authorization (Basic / Bearer / Digest scheme detection), Cookie (parsed into key=value pairs), Set-Cookie (parsed into name, value, and attribute list).
  • **JA4H fingerprint** (FoxIO, for requests): the HTTP member of the JA4+ family — method + version + cookie/ referer flags + header count + Accept-Language, then the truncated SHA-256 of the header names (in wire case + order, excluding Cookie/Referer/pseudo), the sorted cookie names, and the sorted cookie name=value pairs. Fingerprints the HTTP client stack (browser / library / bot / malware). Verified byte-for-byte against FoxIO snapshot outputs.
  • **Body handling**:
  • Content-Length: read exactly N bytes from the body block.
  • Transfer-Encoding: chunked — decode hex-length- prefixed chunks (terminated by 0-length chunk per RFC 9112 §7.1).
  • Both: surface raw text if printable; hex otherwise.

What this package does NOT cover (deliberately out of scope)

  • HTTP/2 binary framing (RFC 9113) and HTTP/3 (RFC 9114) — entirely different wire formats; separate Specs.
  • HPACK header decompression (RFC 7541) — only relevant to HTTP/2.
  • WebSocket upgrades (RFC 6455) — the Upgrade: websocket header is preserved verbatim; the post-upgrade WebSocket frames are a separate Spec.
  • TLS layer — feed the inner cleartext after decryption.
  • Trailer headers — RFC 9112 §7.1.2 trailer field after the final 0-length chunk; surfaced as raw text in the trailing body.
  • Multipart bodies (multipart/form-data, multipart/ mixed) — body is surfaced as raw bytes; multipart parsing is a separate ~150 LoC effort.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthHeader

type AuthHeader struct {
	Scheme     string `json:"scheme"`
	Parameters string `json:"parameters,omitempty"`
}

AuthHeader is a parsed Authorization request-header field.

type Chunk

type Chunk struct {
	LengthHex string `json:"length_hex"`
	Length    int    `json:"length"`
	DataHex   string `json:"data_hex,omitempty"`
	DataText  string `json:"data_text,omitempty"`
}

Chunk is one decoded chunk from a Transfer-Encoding: chunked body.

type Cookie struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

Cookie is one name=value pair from a Cookie request header.

type Header struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

Header is one header field.

type Message

type Message struct {
	IsRequest        bool         `json:"is_request"`
	IsResponse       bool         `json:"is_response"`
	Method           string       `json:"method,omitempty"`
	RequestURI       string       `json:"request_uri,omitempty"`
	Version          string       `json:"version"`
	StatusCode       int          `json:"status_code,omitempty"`
	StatusReason     string       `json:"status_reason,omitempty"`
	StatusName       string       `json:"status_name,omitempty"`
	Headers          []*Header    `json:"headers"`
	Host             string       `json:"host,omitempty"`
	UserAgent        string       `json:"user_agent,omitempty"`
	Server           string       `json:"server,omitempty"`
	ContentType      string       `json:"content_type,omitempty"`
	ContentLength    *int64       `json:"content_length,omitempty"`
	TransferEncoding string       `json:"transfer_encoding,omitempty"`
	Authorization    *AuthHeader  `json:"authorization,omitempty"`
	Cookies          []Cookie     `json:"cookies,omitempty"`
	SetCookies       []*SetCookie `json:"set_cookies,omitempty"`
	BodyRaw          string       `json:"body_raw,omitempty"`
	BodyHex          string       `json:"body_hex,omitempty"`
	ChunkedBody      []*Chunk     `json:"chunked_body,omitempty"`
	JA4H             string       `json:"ja4h,omitempty"`
}

Message is the decoded HTTP/1.x message view.

func Decode

func Decode(input string) (*Message, error)

Decode parses an HTTP/1.x message (single envelope with optional body).

type SetCookie

type SetCookie struct {
	Name       string            `json:"name"`
	Value      string            `json:"value"`
	Attributes map[string]string `json:"attributes,omitempty"`
}

SetCookie is the decoded Set-Cookie response header.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL