Package edu.rit.http
Class HttpRequest
java.lang.Object
edu.rit.http.HttpRequest
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:
-
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.
- Version:
- 29-Jul-2010
- Author:
- Alan Kaminsky
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The GET method string,"GET"
.static final String
The HEAD method string,"HEAD"
.static final String
The HTTP/1.0 version string"HTTP/1.0"
.static final String
The HTTP/1.1 version string,"HTTP/1.1"
.static final String
The POST method string,"POST"
. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionObtain the value of the given header in this HTTP request.Obtain a collection of all the headers in this HTTP request.Obtain this HTTP request's version.Obtain this HTTP request's method.getUri()
Obtain this HTTP request's URI.boolean
isValid()
Determine if this HTTP request is valid.
-
Field Details
-
GET_METHOD
The GET method string,"GET"
.- See Also:
-
HEAD_METHOD
The HEAD method string,"HEAD"
.- See Also:
-
POST_METHOD
The POST method string,"POST"
.- See Also:
-
HTTP_1_0_VERSION
The HTTP/1.0 version string"HTTP/1.0"
.- See Also:
-
HTTP_1_1_VERSION
The HTTP/1.1 version string,"HTTP/1.1"
.- See Also:
-
-
Constructor Details
-
HttpRequest
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 iftheSocket
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
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
Obtain this HTTP request's URI.- Returns:
- URI string.
- Throws:
IllegalStateException
- (unchecked exception) Thrown if this HTTP request is invalid.
-
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
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
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.
-