java.lang.Object
net.i2p.i2ptunnel.HttpHeaderFormatter

public class HttpHeaderFormatterextends Object
Utility class for formatting HTTP headers.

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 Details

    • NEWLINE_SPLIT

      private static final Pattern NEWLINE_SPLIT
      Splits a header block at each CRLF.
    • MAX_REQUEST

      static final int MAX_REQUEST
      Request 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_VALUE
      Header 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

      public static String formatHeaders(Map<String, List<String>> headers, StringBuilder command)
      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

      static String stripLineBreaks(String s)
      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

      public static String formatHeadersCompact(Map<String, List<String>> headers, StringBuilder command)
      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 map
      command - the request command line
      Returns:
      a compact string representation suitable for logging
    • formatHeadersForLog

      public static String formatHeadersForLog(Map<String, List<String>> headers, StringBuilder command)
      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

      static String shortRequest(String request)
      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_REQUEST characters
    • shortB32

      static String shortB32(String b32)
      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

      static String shortValue(String value)
      Cuts an over-long header value, keeping its head so it stays recognizable and reporting the length that was hidden.
      Parameters:
      value - the header value, non-null
      Returns:
      the value, or its head plus an ellipsis and the full length
    • getHostFromHeaders

      public static String getHostFromHeaders(String headers)
      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