Package edu.rit.http

Class HttpRequest

java.lang.Object
edu.rit.http.HttpRequest

public class HttpRequest extends Object
Class HttpRequest encapsulates an HTTP request received from a web browser.

HTTP/1.0 and HTTP/1.1 requests are supported. The obsolete HTTP/0.9 requests are not supported.

This class provides methods for examining the request line and the headers. This class does not support reading the entity body if any.

To receive an HTTP request message:

  1. Call the isValid() method.
     
  2. If isValid() returns false, send an HTTP response message indicating the error.
     
  3. If isValid() returns true, call the other methods to examine the contents of the HTTP request message, and send an appropriate HTTP response message.
Version:
29-Jul-2010
Author:
Alan Kaminsky
  • Field Details

  • Constructor Details

    • HttpRequest

      public HttpRequest(Socket theSocket)
      Construct a new HTTP request. The request is read from the input stream of the given socket.
      Parameters:
      theSocket - Socket.
      Throws:
      NullPointerException - (unchecked exception) Thrown if theSocket is null.
  • Method Details

    • isValid

      public boolean isValid()
      Determine if this HTTP request is valid. If the data read from the input stream of the socket given to the constructor represents a valid HTTP request message, true is returned, otherwise false is returned. If an I/O exception is thrown while reading the input, this HTTP request is marked as invalid, but the I/O exception is not propagated to the caller.
      Returns:
      True if this HTTP request is valid, false otherwise.
    • getMethod

      public String getMethod()
      Obtain this HTTP request's method.
      Returns:
      Method string, e.g. "GET", "POST".
      Throws:
      IllegalStateException - (unchecked exception) Thrown if this HTTP request is invalid.
    • getUri

      public String getUri()
      Obtain this HTTP request's URI.
      Returns:
      URI string.
      Throws:
      IllegalStateException - (unchecked exception) Thrown if this HTTP request is invalid.
    • getHttpVersion

      public String getHttpVersion()
      Obtain this HTTP request's version.
      Returns:
      HTTP version string, e.g. "HTTP/1.0", "HTTP/1.1".
      Throws:
      IllegalStateException - (unchecked exception) Thrown if this HTTP request is invalid.
    • getHeader

      public String getHeader(String theHeaderName)
      Obtain the value of the given header in this HTTP request.
      Parameters:
      theHeaderName - Header name.
      Returns:
      Header value, or null if there is no header for theHeaderName.
      Throws:
      IllegalStateException - (unchecked exception) Thrown if this HTTP request is invalid.
    • getHeaders

      public Collection<Map.Entry<String,String>> getHeaders()
      Obtain a collection of all the headers in this HTTP request. The returned object is an unmodifiable collection of zero or more map entries. Each map entry's key is the header name. Each map entry's value is the corresponding header value.
      Returns:
      Unmodifiable collection of header name-value mappings.
      Throws:
      IllegalStateException - (unchecked exception) Thrown if this HTTP request is invalid.