edu.rit.http
Class HttpRequest
- java.lang.Object
-
- edu.rit.http.HttpRequest
-
public class HttpRequest extends java.lang.ObjectClass 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:
-
Call the isValid() method.
-
If isValid() returns false, send an HTTP response message indicating
the error.
- If isValid() returns true, call the other methods to examine the contents of the HTTP request message, and send an appropriate HTTP response message.
-
Call the isValid() method.
-
-
Field Summary
Fields Modifier and Type Field and Description static java.lang.StringGET_METHODThe GET method string, "GET".static java.lang.StringHEAD_METHODThe HEAD method string, "HEAD".static java.lang.StringHTTP_1_0_VERSIONThe HTTP/1.0 version string "HTTP/1.0".static java.lang.StringHTTP_1_1_VERSIONThe HTTP/1.1 version string, "HTTP/1.1".static java.lang.StringPOST_METHODThe POST method string, "POST".
-
Constructor Summary
Constructors Constructor and Description HttpRequest(java.net.Socket theSocket)Construct a new HTTP request.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description java.lang.StringgetHeader(java.lang.String theHeaderName)Obtain the value of the given header in this HTTP request.java.util.Collection<java.util.Map.Entry<java.lang.String,java.lang.String>>getHeaders()Obtain a collection of all the headers in this HTTP request.java.lang.StringgetHttpVersion()Obtain this HTTP request's version.java.lang.StringgetMethod()Obtain this HTTP request's method.java.lang.StringgetUri()Obtain this HTTP request's URI.booleanisValid()Determine if this HTTP request is valid.
-
-
-
Field Detail
-
GET_METHOD
public static final java.lang.String GET_METHOD
The GET method string, "GET".- See Also:
- Constant Field Values
-
HEAD_METHOD
public static final java.lang.String HEAD_METHOD
The HEAD method string, "HEAD".- See Also:
- Constant Field Values
-
POST_METHOD
public static final java.lang.String POST_METHOD
The POST method string, "POST".- See Also:
- Constant Field Values
-
HTTP_1_0_VERSION
public static final java.lang.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 java.lang.String HTTP_1_1_VERSION
The HTTP/1.1 version string, "HTTP/1.1".- See Also:
- Constant Field Values
-
-
Constructor Detail
-
HttpRequest
public HttpRequest(java.net.Socket theSocket)
Construct a new HTTP request. The request is read from the input stream of the given socket.- Parameters:
theSocket- Socket.- Throws:
java.lang.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 java.lang.String getMethod()
Obtain this HTTP request's method.- Returns:
- Method string, e.g. "GET", "POST".
- Throws:
java.lang.IllegalStateException- (unchecked exception) Thrown if this HTTP request is invalid.
-
getUri
public java.lang.String getUri()
Obtain this HTTP request's URI.- Returns:
- URI string.
- Throws:
java.lang.IllegalStateException- (unchecked exception) Thrown if this HTTP request is invalid.
-
getHttpVersion
public java.lang.String getHttpVersion()
Obtain this HTTP request's version.- Returns:
- HTTP version string, e.g. "HTTP/1.0", "HTTP/1.1".
- Throws:
java.lang.IllegalStateException- (unchecked exception) Thrown if this HTTP request is invalid.
-
getHeader
public java.lang.String getHeader(java.lang.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:
java.lang.IllegalStateException- (unchecked exception) Thrown if this HTTP request is invalid.
-
getHeaders
public java.util.Collection<java.util.Map.Entry<java.lang.String,java.lang.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:
java.lang.IllegalStateException- (unchecked exception) Thrown if this HTTP request is invalid.
-
-
DMelt 3.0 © DataMelt by jWork.ORG