Class RequestConsumer

All Implemented Interfaces:
ByteConsumer, Header, Segment

public class RequestConsumer extends HeaderConsumer
The RequestConsumer object is used to parse the HTTP request line followed by the HTTP message headers. This parses the request URI such that the query parameters and path are extracted and normalized. It performs this using external parsers, which will remove and escaped characters and normalize the path segments. Finally this exposes the HTTP version used using the major and minor numbers sent with the HTTP request.
  • Field Details

    • parser

      protected AddressParser parser
      This is the address parser used to parse the request URI.
    • method

      protected String method
      This is the method token send with the HTTP request header.
    • target

      protected String target
      This represents the raw request URI in an unparsed form.
    • major

      protected int major
      This is the major version number of the HTTP request header.
    • minor

      protected int minor
      This is the minor version number of the HTTP request header.
  • Constructor Details

    • RequestConsumer

      public RequestConsumer()
      Constructor for the RequestConsumer object. This is used to create a consumer which can consume a HTTP request header and provide the consumed contents via a known interface. This also further breaks down the request URI for convenience.
  • Method Details

    • getTarget

      public String getTarget()
      This can be used to get the target specified for this HTTP request. This corresponds to the URI sent in the request line. Typically this will be the path part of the URI, but can be the full URI if the request is a proxy request.
      Returns:
      the target URI that this HTTP request specifies
    • getAddress

      public Address getAddress()
      This is used to acquire the address from the request line. An address is the full URI including the scheme, domain, port and the query parts. This allows various parameters to be acquired without having to parse the target.
      Returns:
      this returns the address of the request line
    • getQuery

      public Query getQuery()
      This method is used to acquire the query part from the HTTP request URI target. This will return only the values that have been extracted from the request URI target.
      Returns:
      the query associated with the HTTP target URI
    • getPath

      public Path getPath()
      This is used to acquire the path as extracted from the the HTTP request URI. The Path object that is provided by this method is immutable, it represents the normalized path only part from the request URI.
      Returns:
      this returns the normalized path for the request
    • getMethod

      public String getMethod()
      This can be used to get the HTTP method for this request. The HTTP specification RFC 2616 specifies the HTTP request methods in section 9, Method Definitions. Typically this will be a GET or POST method, but can be any valid alphabetic token.
      Returns:
      the HTTP method that this request has specified
    • getMajor

      public int getMajor()
      This can be used to get the major number from a HTTP version. The major version corrosponds to the major protocol type, that is the 1 of a HTTP/1.0 version string. Typically the major type is 1, by can be 0 for HTTP/0.9 clients.
      Returns:
      the major version number for the HTTP message
    • getMinor

      public int getMinor()
      This can be used to get the minor number from a HTTP version. The minor version corrosponds to the minor protocol type, that is the 0 of a HTTP/1.0 version string. This number is typically used to determine whether persistent connections are supported.
      Returns:
      the minor version number for the HTTP message
    • getDate

      public long getDate(String name)
      This can be used to get the date of the first message header that has the specified name. This is a convenience method that avoids having to deal with parsing the value of the requested HTTP message header. This returns -1 if theres no HTTP header value for the specified name.
      Specified by:
      getDate in interface Header
      Overrides:
      getDate in class HeaderConsumer
      Parameters:
      name - the HTTP message header to get the value from
      Returns:
      this returns the date as a long from the header value
    • getInteger

      public int getInteger(String name)
      This can be used to get the integer of the first message header that has the specified name. This is a convenience method that avoids having to deal with parsing the value of the requested HTTP message header. This returns -1 if theres no HTTP header value for the specified name.
      Specified by:
      getInteger in interface Header
      Overrides:
      getInteger in class HeaderConsumer
      Parameters:
      name - the HTTP message header to get the value from
      Returns:
      this returns the date as a long from the header value
    • getNames

      public List<String> getNames()
      This method is used to get a List of the names for the headers. This will provide the original names for the HTTP headers for the message. Modifications to the provided list will not affect the header, the list is a simple copy.
      Specified by:
      getNames in interface Header
      Overrides:
      getNames in class HeaderConsumer
      Returns:
      this returns a list of the names within the header
    • process

      protected void process()
      This method is invoked after the terminal token has been read. It is used to process the consumed data and is typically used to parse the input such that it can be used by the subclass for some useful puropse. This is called only once by the consumer.
      Overrides:
      process in class SegmentConsumer
    • target

      private void target()
      This will parse URI target from the first line of the header and store the parsed string internally. The target token is used to create an Address object which provides all the details of the target including the query part.
    • method

      private void method()
      This will parse HTTP method from the first line of the header and store the parsed string internally. The method is used to determine what action to take with the request, it also acts as a means to determine the semantics of the request.
    • version

      protected void version()
      This will parse HTTP version from the first line of the header and store the parsed string internally. The method is used to determine what version of HTTP is being used. Typically this will be HTTP/1.1 however HTTP/1.0 must be supported and this has different connection semantics with regards to pipelines.
    • major

      private void major()
      This will parse the header from the current offset and convert the bytes found into an int as it parses the digits it comes accross. This will cease to parse bytes when it encounters a non digit byte or the end of the readable bytes.
    • minor

      private void minor()
      This will parse the header from the current offset and convert the bytes found into an int as it parses the digits it comes accross. This will cease to parse bytes when it encounters a non digit byte or the end of the readable bytes.
    • digit

      protected boolean digit(byte octet)
      This is used to determine if a given ISO-8859-1 byte is a digit character, between an ISO-8859-1 0 and 9. If it is, this will return true otherwise it returns false.
      Parameters:
      octet - this is to be checked to see if it is a digit
      Returns:
      true if the byte is a digit character, false otherwise
    • getHeader

      public CharSequence getHeader()
      This method returns a CharSequence holding the data consumed for the request. A character sequence is returned as it can provide a much more efficient means of representing the header data by just wrapping the consumed byte array.
      Returns:
      this returns the characters consumed for the header
    • toString

      public String toString()
      This is used to convert the byte range to a string. This will use UTF-8 encoding for the string which is compatible with the HTTP default header encoding of ISO-8859-1.
      Specified by:
      toString in interface Header
      Overrides:
      toString in class SegmentConsumer
      Returns:
      the encoded string representing the token