Package net.targetr.wtm3.net.http
Class HttpRequest
java.lang.Object
net.targetr.wtm3.net.http.HttpRequest
Parses, stores and reads HTTP requests.
- Author:
- Dr Michael Gardiner
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new HttpRequest object with an empty Headers object.HttpRequest
(String raw) Constructs a new HttpRequest object by parsing the given raw HTTP request string.HttpRequest
(String method, String uri, String version, Headers headers) Constructs a new HttpRequest object with the specified method, URI, version, and headers. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Consumes the body of this HTTP request by reading and discarding the remaining data.byte[]
getBody()
Returns the body of this HTTP request as a byte array.Returns an InputStream for reading the body of this HTTP request.long
Returns the Content-Length value of this request, or -1 if not present.Returns a Map containing the cookies of this request.getPath()
Returns the path portion of the URI of this request.getQuery()
Returns a Map containing the query parameters of this request.boolean
isGet()
Returns true if this is a GET request, false otherwise.boolean
isHead()
Returns true if this is a HEAD request, false otherwise.boolean
Returns true if this is an OPTIONS request, false otherwise.boolean
isPost()
Returns true if this is a POST request, false otherwise.boolean
isPut()
Returns true if this is a PUT request, false otherwise.static void
parseRequest
(PushbackInputStream in, HttpRequest req) Parses the HTTP request from the given PushbackInputStream and stores the parsed data in the given HttpRequest object.toString()
Returns a string representation of this HTTP request.
-
Field Details
-
socket
Socket the request was read from. -
method
Method of operation.. -
uri
The URI requested. -
version
The HTTP version. -
headers
The HTTP headers.
-
-
Constructor Details
-
HttpRequest
public HttpRequest()Constructs a new HttpRequest object with an empty Headers object. -
HttpRequest
Constructs a new HttpRequest object with the specified method, URI, version, and headers.- Parameters:
method
- the HTTP method of the requesturi
- the URI of the requestversion
- the HTTP version of the requestheaders
- the headers of the request
-
HttpRequest
Constructs a new HttpRequest object by parsing the given raw HTTP request string.- Parameters:
raw
- the raw HTTP request string- Throws:
IOException
- if an I/O error occurs while parsing the request
-
-
Method Details
-
parseRequest
Parses the HTTP request from the given PushbackInputStream and stores the parsed data in the given HttpRequest object.- Parameters:
in
- the PushbackInputStream containing the HTTP request datareq
- the HttpRequest object to store the parsed data- Throws:
IOException
- if an I/O error occurs while parsing the request
-
getBodyInputStream
Returns an InputStream for reading the body of this HTTP request.- Returns:
- an InputStream for reading the request body
-
getBody
Returns the body of this HTTP request as a byte array.Caches body for subsequent processing.
- Returns:
- the request body as a byte array
- Throws:
IOException
- if an I/O error occurs while reading the request body
-
consumeBody
Consumes the body of this HTTP request by reading and discarding the remaining data.- Throws:
IOException
- if an I/O error occurs while consuming the request body
-
isHead
public boolean isHead()Returns true if this is a HEAD request, false otherwise.- Returns:
- true if this is a HEAD request, false otherwise
-
isOptions
public boolean isOptions()Returns true if this is an OPTIONS request, false otherwise.- Returns:
- true if this is an OPTIONS request, false otherwise
-
isGet
public boolean isGet()Returns true if this is a GET request, false otherwise.- Returns:
- true if this is a GET request, false otherwise
-
isPost
public boolean isPost()Returns true if this is a POST request, false otherwise.- Returns:
- true if this is a POST request, false otherwise
-
isPut
public boolean isPut()Returns true if this is a PUT request, false otherwise.- Returns:
- true if this is a PUT request, false otherwise
-
getPath
Returns the path portion of the URI of this request.- Returns:
- the path portion of the URI
-
getContentLength
public long getContentLength()Returns the Content-Length value of this request, or -1 if not present.- Returns:
- the Content-Length value, or -1 if not present
-
getQuery
Returns a Map containing the query parameters of this request.- Returns:
- a Map containing the query parameters
-
getCookies
Returns a Map containing the cookies of this request.- Returns:
- a Map containing the cookies
-
toString
Returns a string representation of this HTTP request.
-