public class HttpHeaderFormatter extends Object
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.
| Constructor and Description |
|---|
HttpHeaderFormatter() |
| Modifier and Type | Method and Description |
|---|---|
static String |
formatHeaders(Map<String,List<String>> headers,
StringBuilder command)
Formats headers into an HTTP request/response string.
|
static String |
formatHeadersCompact(Map<String,List<String>> headers,
StringBuilder command)
Formats headers into a compact string for logging.
|
static String |
getHostFromHeaders(String headers)
Extracts the Host header value from raw HTTP headers.
|
public static String formatHeaders(Map<String,List<String>> headers, StringBuilder command)
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 ...
headers - the HTTP headers map (header name to list of values)command - the request/response command line (e.g., "GET / HTTP/1.1")public static String formatHeadersCompact(Map<String,List<String>> headers, StringBuilder command)
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:
Long request URLs containing "peer_id" are truncated.
headers - the HTTP headers mapcommand - the request command linepublic static String getHostFromHeaders(String 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.
headers - the raw HTTP headers as a string