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
FieldsModifier and TypeFieldDescriptionfinal AtomicLongNumber of bytes read from the request.longNumber of bytes in the request headers.final HeadersThe HTTP headers.Method of operation..Socket the request was read from.booleanWhether the request was upgraded to WebSocket.The URI requested.The HTTP version. -
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 TypeMethodDescriptionvoidConsumes the body of this HTTP request by reading and discarding the remaining data.static StringgenerateQueryString(Map<String, String> query) Generates a query string from a Map of key-value pairs.byte[]getBody()Returns the body of this HTTP request as a byte array.Returns an InputStream for reading the body of this HTTP request.longReturns 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.booleanisGet()Returns true if this is a GET request, false otherwise.booleanisHead()Returns true if this is a HEAD request, false otherwise.booleanReturns true if this is an OPTIONS request, false otherwise.booleanisPost()Returns true if this is a POST request, false otherwise.booleanisPut()Returns true if this is a PUT request, false otherwise.static voidParses the HTTP request header 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. -
upgradedToWebSocket
public boolean upgradedToWebSocketWhether the request was upgraded to WebSocket. -
headers
The HTTP headers. -
bytesRead
Number of bytes read from the request. -
headerLength
public long headerLengthNumber of bytes in the request 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
-
parseRequestHeader
Parses the HTTP request header 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. (unless upgraded to websocket)- 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
-
generateQueryString
Generates a query string from a Map of key-value pairs.- Parameters:
query- the Map of key-value pairs to generate a query string from- Returns:
- the query string
-
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.
-