Class HTTPTools

java.lang.Object
io.fusionauth.http.util.HTTPTools

public final class HTTPTools extends Object
  • Constructor Details

    • HTTPTools

      public HTTPTools()
  • Method Details

    • getMaxRequestBodySize

      public static int getMaxRequestBodySize(String contentType, Map<String,Integer> maxRequestBodySize)
      Return the maximum request body size for the requested content type.
      Parameters:
      contentType - the content-type of the request
      maxRequestBodySize - the maximum request size configuration
      Returns:
      the maximum request size, or -1 if no limit should be enforced.
    • initialize

      public static void initialize(LoggerFactory loggerFactory)
      Statically sets up the logger, mostly for trace logging.
      Parameters:
      loggerFactory - The logger factory.
    • isControlCharacter

      public static boolean isControlCharacter(byte ch)
      Parameters:
      ch - The character as a since HTTP is ASCII
      Returns:
      True if the character is an ASCII control character.
    • isDigitCharacter

      public static boolean isDigitCharacter(byte ch)
      Determines if the given character (byte) is a digit (i.e. 0-9)
      Parameters:
      ch - The character as a byte since HTTP is ASCII.
      Returns:
      True if the character is a digit.
    • isHexadecimalCharacter

      public static boolean isHexadecimalCharacter(byte ch)
      Determines if the given character (byte) is an allowed hexadecimal character (i.e. 0-9a-zA-Z)
      Parameters:
      ch - The character as a byte since HTTP is ASCII.
      Returns:
      True if the character is a hexadecimal character.
    • isTokenCharacter

      public static boolean isTokenCharacter(byte ch)
      Determines if the given character (byte) is an allowed HTTP token character (header field names, methods, etc).

      Covered by https://www.rfc-editor.org/rfc/rfc9110.html#name-fields

      Parameters:
      ch - The character as a byte since HTTP is ASCII.
      Returns:
      True if the character is a token character.
    • isURICharacter

      public static boolean isURICharacter(byte ch)
      Naively determines if the given character (byte) is an allowed URI character.
      Parameters:
      ch - The character as a byte since URIs are ASCII.
      Returns:
      True if the character is a URI character.
    • isValueCharacter

      public static boolean isValueCharacter(byte ch)
    • makeParseException

      public static ParseException makeParseException(byte b, Enum<? extends Enum<?>> state)
      Build a ParseException that can be thrown.
      Parameters:
      b - the byte that caused the exception
      state - the current parser state
      Returns:
      a throwable exception
    • parseEncodedData

      public static void parseEncodedData(byte[] data, int start, int length, Charset charset, Map<String,List<String>> result)
      Parses URL encoded data either from a URL parameter list in the query string or the form body.
      Parameters:
      data - The data as a character array.
      start - The start index to start parsing from.
      length - The length to parse.
      charset - The charset used to decode the key and value. May be null, and will default to UTF-8.
      result - The result Map to put the value into.
    • parseEncodedData

      public static void parseEncodedData(byte[] data, int start, int length, Map<String,List<String>> result)
      Parses URL encoded data either from a URL parameter list in the query string or the form body. Assumes the values are UTF-8 encoded.
      Parameters:
      data - The data as a character array.
      start - The start index to start parsing from.
      length - The length to parse.
      result - The result Map to put the value into.
    • parseHeaderValue

      public static HTTPTools.HeaderValue parseHeaderValue(String value)
      Parses an HTTP header value that is a standard semicolon separated list of values.
      Parameters:
      value - The header value.
      Returns:
      The HeaderValue record.
    • parseRequestPreamble

      public static void parseRequestPreamble(PushbackInputStream inputStream, int maxRequestHeaderSize, HTTPRequest request, byte[] requestBuffer, Runnable readObserver) throws IOException
      Parses the request preamble directly from the given InputStream.

      The HTTP request is made up of the request line, headers and an optional body. The request preamble comprises the Request line and the Headers. All that remains after the preamble is the optional body.

      Parameters:
      inputStream - The input stream to read the preamble from.
      maxRequestHeaderSize - The maximum number of bytes to read for the header. If exceed throw an exception.
      request - The HTTP request to populate.
      requestBuffer - A buffer used for reading to help reduce memory thrashing.
      readObserver - An observer that is called once one byte has been read.
      Throws:
      IOException - If the read fails.
    • writeResponsePreamble

      public static void writeResponsePreamble(HTTPResponse response, OutputStream outputStream) throws IOException
      Writes the HTTP response head section (status line, headers, etc).
      Parameters:
      response - The response.
      outputStream - The output stream to write the preamble to.
      Throws:
      IOException - If the stream threw an exception.