Class BrowserUpHttpUtil


  • public class BrowserUpHttpUtil
    extends java.lang.Object
    Utility class with static methods for processing HTTP requests and responses.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DECOMPRESS_BUFFER_SIZE
      Buffer size when decompressing content.
      static java.nio.charset.Charset DEFAULT_HTTP_CHARSET
      The default charset when the Content-Type header does not specify a charset.
      static java.lang.String UNKNOWN_CONTENT_TYPE
      Default MIME content type if no Content-Type header is present.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String base64EncodeBasicCredentials​(java.lang.String username, java.lang.String password)
      Base64-encodes the specified username and password for Basic Authorization for HTTP requests or upstream proxy authorization.
      static byte[] decompressBrotliContents​(byte[] fullMessage)
      Decompresses the brotli byte stream
      static byte[] decompressGZIPContents​(byte[] fullMessage)
      Decompresses the gzipped byte stream.
      static byte[] extractReadableBytes​(io.netty.buffer.ByteBuf content)
      Extracts all readable bytes from the ByteBuf as a byte array.
      static java.lang.String getContentAsString​(byte[] content, java.nio.charset.Charset charset)
      Converts the byte array into a String based on the specified charset.
      static long getHeaderSize​(io.netty.handler.codec.http.HttpHeaders headers)
      Returns the size of the headers, including the 2 CRLFs at the end of the header block.
      static java.lang.String getRawPathAndParamsFromRequest​(io.netty.handler.codec.http.HttpRequest httpRequest)
      Retrieves the raw (unescaped) path + query string from the specified request.
      static java.lang.String getRawPathAndParamsFromUri​(java.lang.String uriString)
      Retrieves the raw (unescaped) path and query parameters from the URI, stripping out the scheme, host, and port.
      static boolean hasTextualContent​(java.lang.String contentType)
      Returns true if the content type string indicates textual content.
      static boolean isRedirect​(io.netty.handler.codec.http.HttpResponse httpResponse)
      Returns true if the specified response is an HTTP redirect response, i.e.
      static java.nio.charset.Charset readCharsetInContentTypeHeader​(java.lang.String contentTypeHeader)
      Reads the charset directly from the Content-Type header string.
      static java.lang.String removeMatchingPort​(java.lang.String hostWithPort, int portNumber)
      Removes a port from a host+port if the string contains the specified port.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • UNKNOWN_CONTENT_TYPE

        public static final java.lang.String UNKNOWN_CONTENT_TYPE
        Default MIME content type if no Content-Type header is present. According to the HTTP 1.1 spec, section 7.2.1:
             Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media
             type of that body. If and only if the media type is not given by a Content-Type field, the recipient MAY
             attempt to guess the media type via inspection of its content and/or the name extension(s) of the URI used to
             identify the resource. If the media type remains unknown, the recipient SHOULD treat it as
             type "application/octet-stream".
         
        See Also:
        Constant Field Values
      • DEFAULT_HTTP_CHARSET

        public static final java.nio.charset.Charset DEFAULT_HTTP_CHARSET
        The default charset when the Content-Type header does not specify a charset. According to RFC 7231 Appendix B:
             The default charset of ISO-8859-1 for text media types has been
             removed; the default is now whatever the media type definition says.
             Likewise, special treatment of ISO-8859-1 has been removed from the
             Accept-Charset header field.
         
        Technically, we would have to determine the charset on a per-content-type basis, but generally speaking, UTF-8 is a pretty safe default. (NOTE: In the previous HTTP/1.1 spec, section 3.7.1, the default charset was defined as ISO-8859-1.)
      • DECOMPRESS_BUFFER_SIZE

        public static final int DECOMPRESS_BUFFER_SIZE
        Buffer size when decompressing content.
        See Also:
        Constant Field Values
    • Constructor Detail

      • BrowserUpHttpUtil

        public BrowserUpHttpUtil()
    • Method Detail

      • getHeaderSize

        public static long getHeaderSize​(io.netty.handler.codec.http.HttpHeaders headers)
        Returns the size of the headers, including the 2 CRLFs at the end of the header block.
        Parameters:
        headers - headers to size
        Returns:
        length of the headers, in bytes
      • decompressGZIPContents

        public static byte[] decompressGZIPContents​(byte[] fullMessage)
                                             throws DecompressionException
        Decompresses the gzipped byte stream.
        Parameters:
        fullMessage - gzipped byte stream to decompress
        Returns:
        decompressed bytes
        Throws:
        DecompressionException - thrown if the fullMessage cannot be read or decompressed for any reason
      • decompressBrotliContents

        public static byte[] decompressBrotliContents​(byte[] fullMessage)
                                               throws DecompressionException
        Decompresses the brotli byte stream
        Parameters:
        fullMessage - brotli byte stream to decompress
        Returns:
        decompressed bytes
        Throws:
        DecompressionException - thrown if the fullMessage cannot be read or decompressed for any reason
      • hasTextualContent

        public static boolean hasTextualContent​(java.lang.String contentType)
        Returns true if the content type string indicates textual content. Currently these are any Content-Types that start with one of the following:
             text/
             application/x-javascript
             application/javascript
             application/json
             application/xml
             application/xhtml+xml
         
        Parameters:
        contentType - contentType string to parse
        Returns:
        true if the content type is textual
      • extractReadableBytes

        public static byte[] extractReadableBytes​(io.netty.buffer.ByteBuf content)
        Extracts all readable bytes from the ByteBuf as a byte array.
        Parameters:
        content - ByteBuf to read
        Returns:
        byte array containing the readable bytes from the ByteBuf
      • getContentAsString

        public static java.lang.String getContentAsString​(byte[] content,
                                                          java.nio.charset.Charset charset)
        Converts the byte array into a String based on the specified charset. The charset cannot be null.
        Parameters:
        content - bytes to convert to a String
        charset - the character set of the content
        Returns:
        String containing the converted content
        Throws:
        java.lang.IllegalArgumentException - if charset is null
      • readCharsetInContentTypeHeader

        public static java.nio.charset.Charset readCharsetInContentTypeHeader​(java.lang.String contentTypeHeader)
                                                                       throws UnsupportedCharsetException
        Reads the charset directly from the Content-Type header string. If the Content-Type header does not contain a charset, is malformed or unparsable, or if the header is null or empty, this method returns null.
        Parameters:
        contentTypeHeader - the Content-Type header string; can be null or empty
        Returns:
        the character set indicated in the contentTypeHeader, or null if the charset is not present or is not parsable
        Throws:
        UnsupportedCharsetException - if there is a charset specified in the content-type header, but it is not supported on this platform
      • getRawPathAndParamsFromRequest

        public static java.lang.String getRawPathAndParamsFromRequest​(io.netty.handler.codec.http.HttpRequest httpRequest)
                                                               throws java.net.URISyntaxException
        Retrieves the raw (unescaped) path + query string from the specified request. The returned path will not include the scheme, host, or port.
        Parameters:
        httpRequest - HTTP request
        Returns:
        the unescaped path + query string from the HTTP request
        Throws:
        java.net.URISyntaxException - if the path could not be parsed (due to invalid characters in the URI, etc.)
      • getRawPathAndParamsFromUri

        public static java.lang.String getRawPathAndParamsFromUri​(java.lang.String uriString)
                                                           throws java.net.URISyntaxException
        Retrieves the raw (unescaped) path and query parameters from the URI, stripping out the scheme, host, and port. The path will begin with a leading '/'. For example, 'http://example.com/some/resource?param%20name=param%20value' would return '/some/resource?param%20name=param%20value'.
        Parameters:
        uriString - the URI to parse, containing a scheme, host, port, path, and query parameters
        Returns:
        the unescaped path and query parameters from the URI
        Throws:
        java.net.URISyntaxException - if the specified URI is invalid or cannot be parsed
      • isRedirect

        public static boolean isRedirect​(io.netty.handler.codec.http.HttpResponse httpResponse)
        Returns true if the specified response is an HTTP redirect response, i.e. a 300, 301, 302, 303, or 307.
        Parameters:
        httpResponse - HTTP response
        Returns:
        true if the response is a redirect, otherwise false
      • removeMatchingPort

        public static java.lang.String removeMatchingPort​(java.lang.String hostWithPort,
                                                          int portNumber)
        Removes a port from a host+port if the string contains the specified port. If the host+port does not contain a port, or contains another port, the string is returned unaltered. For example, if hostWithPort is the string www.website.com:443, this method will return www.website.com. Note: The hostWithPort string is not a URI and should not contain a scheme or resource. This method does not attempt to validate the specified host; it might throw IllegalArgumentException if there was a problem parsing the hostname, but makes no guarantees. In general, it should be validated externally, if necessary.
        Parameters:
        hostWithPort - string containing a hostname and optional port
        portNumber - port to remove from the string
        Returns:
        string with the specified port removed, or the original string if it did not contain the portNumber
      • base64EncodeBasicCredentials

        public static java.lang.String base64EncodeBasicCredentials​(java.lang.String username,
                                                                    java.lang.String password)
        Base64-encodes the specified username and password for Basic Authorization for HTTP requests or upstream proxy authorization. The format of Basic auth is "username:password" as a base64 string.
        Parameters:
        username - username to encode
        password - password to encode
        Returns:
        a base-64 encoded string containing username:password