edu.rit.http
Class HttpRequest

java.lang.Object
  extended by 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.


Field Summary
static String GET_METHOD
          The GET method string, "GET".
static String HEAD_METHOD
          The HEAD method string, "HEAD".
static String HTTP_1_0_VERSION
          The HTTP/1.0 version string "HTTP/1.0".
static String HTTP_1_1_VERSION
          The HTTP/1.1 version string, "HTTP/1.1".
static String POST_METHOD
          The POST method string, "POST".
 
Constructor Summary
HttpRequest(Socket theSocket)
          Construct a new HTTP request.
 
Method Summary
 String getHeader(String theHeaderName)
          Obtain the value of the given header in this HTTP request.
 Collection<Map.Entry<String,String>> getHeaders()
          Obtain a collection of all the headers in this HTTP request.
 String getHttpVersion()
          Obtain this HTTP request's version.
 String getMethod()
          Obtain this HTTP request's method.
 String getUri()
          Obtain this HTTP request's URI.
 boolean isValid()
          Determine if this HTTP request is valid.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GET_METHOD

public static final String GET_METHOD
The GET method string, "GET".

See Also:
Constant Field Values

HEAD_METHOD

public static final String HEAD_METHOD
The HEAD method string, "HEAD".

See Also:
Constant Field Values

POST_METHOD

public static final String POST_METHOD
The POST method string, "POST".

See Also:
Constant Field Values

HTTP_1_0_VERSION

public static final String HTTP_1_0_VERSION
The HTTP/1.0 version string "HTTP/1.0".

See Also:
Constant Field Values

HTTP_1_1_VERSION

public static final String HTTP_1_1_VERSION
The HTTP/1.1 version string, "HTTP/1.1".

See Also:
Constant Field Values
Constructor Detail

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 Detail

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.


Copyright © 2005-2012 by Alan Kaminsky. All rights reserved. Send comments to ark­@­cs.rit.edu.