This class provides static methods for formatting HTTP headers for both transmission and logging purposes. It handles the conversion of header maps to properly formatted HTTP header strings.
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final intRequest lines longer than this are cut at the query string.(package private) static final intHeader values longer than this are cut.private static final PatternSplits a header block at each CRLF.Constructor Summary
ConstructorsMethod Summary
Public MethodsModifier and TypeMethodDescriptionstatic StringformatHeaders(Map<String, List<String>> headers, StringBuilder command) Formats headers into an HTTP request/response string.static StringformatHeadersCompact(Map<String, List<String>> headers, StringBuilder command) Formats headers into a compact string for logging.static StringformatHeadersForLog(Map<String, List<String>> headers, StringBuilder command) Formats the request and headers for the log, in the house continuation form.static StringgetHostFromHeaders(String headers) Extracts the Host header value from raw HTTP headers.(package private) static StringThe eight-character head of a base32 address in brackets, the form the console log viewer turns into a link.(package private) static StringshortRequest(String request) Cuts a request line to what a reader can actually scan, keeping the method, the path and the protocol version.(package private) static StringshortValue(String value) Cuts an over-long header value, keeping its head so it stays recognizable and reporting the length that was hidden.(package private) static StringRemoves CR and LF from a header name, value, or request line.
Field Details
NEWLINE_SPLIT
Splits a header block at each CRLF.MAX_REQUEST
static final int MAX_REQUESTRequest lines longer than this are cut at the query string. A tracker or client request carries a whole base64 destination in the query, which is the same destination the headers already name.- See Also:
MAX_VALUE
static final int MAX_VALUEHeader values longer than this are cut. No log line is readable once a 500 character blob lands in it.- See Also:
Constructor Details
HttpHeaderFormatter
public HttpHeaderFormatter()Constructor. Formatting is a static function of the header map passed to it, so an instance carries no state.
Method Details
formatHeaders
Formats headers into an HTTP request/response string.This method takes a header map and formats it into a properly formatted HTTP header block suitable for transmission. The format is:
REQUEST_LINE Header-Name: value ...
- Parameters:
headers- the HTTP headers map (header name to list of values)command- the request/response command line (e.g., "GET / HTTP/1.1")- Returns:
- the formatted HTTP headers as a string
stripLineBreaks
Removes CR and LF from a header name, value, or request line.Header maps are rebuilt from text that was parsed line by line, so a line break can only reach this point through a value that was constructed elsewhere (a spoofed host, a security header, a server response rewrite). Stripping it keeps a hostile value from splitting the message into extra header lines or an injected body (HTTP response splitting).
- Parameters:
s- the header name, value, or request line, may be null- Returns:
- the input with CR and LF removed, or null if null was passed
formatHeadersCompact
Formats headers into a compact string for logging.This method creates a compact, human-readable representation of HTTP headers suitable for logging. It filters out sensitive or verbose headers like cookies, authentication headers, and excessive request details.
The following are excluded from the output:
- Headers containing "desthash", "destb64", "dnt"
- Connection, Accept, Cookie, Pragma, Cache-Control headers
- Referer header with full URL
- Content-Length: 0
- User-Agent containing "MYOB"
- HEAD requests (headers only)
Long request URLs containing "peer_id" are truncated.
- Parameters:
headers- the HTTP headers mapcommand- the request command line- Returns:
- a compact string representation suitable for logging
formatHeadersForLog
Formats the request and headers for the log, in the house continuation form. Unlike the wire form this is not what is sent to the server: the base64 and hash forms of the client destination are dropped as repeats of the base32 form, that form is shortened to the eight characters the log viewer links, and over-long values are cut.- Parameters:
headers- the HTTP headers map (header name to list of values)command- the request command line (e.g., "GET / HTTP/1.1")- Returns:
- a string of "\n* name: value" lines, never null
shortRequest
Cuts a request line to what a reader can actually scan, keeping the method, the path and the protocol version.- Parameters:
request- the request line, non-null- Returns:
- the request line, or its head plus an ellipsis when over
MAX_REQUESTcharacters
shortB32
The eight-character head of a base32 address in brackets, the form the console log viewer turns into a link. The full address is 60 characters naming the same destination.- Parameters:
b32- the base32 address, may be null- Returns:
- the bracketed head, or [unknown] if there is no address
shortValue
getHostFromHeaders
Extracts the Host header value from raw HTTP headers.This method parses raw HTTP header text and extracts the value of the Host header. If the host contains a port number, it is stripped and only the hostname is returned.
- Parameters:
headers- the raw HTTP headers as a string- Returns:
- the hostname from the Host header, or null if not found