Class Request
This class is used to represent an HTTP request. It stores the method, path, HTTP version, headers and body of the request. It also provides methods to access these values. This class is used to parse an HTTP request from a socket.
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidanswer(byte[] response) Sends the provided response as the answer to the client.getBody()Returns the body of the HTTP request.Retrieves the form data from the HTTP request body.Returns the headers of the HTTP request.Returns the HTTP version of the HTTP request.Returns the method of the HTTP request.getPath()Returns the path of the HTTP request.Returns the query parameters of the HTTP request.booleanhasBody()Checks if the request has a body.booleanisValid()Checks if the request is valid.private voidparseBody(byte[] bytes) Parses the body of an HTTP request.private voidparseHeaders(byte[] bytes) Parses the headers from the provided byte array.
-
Field Details
-
method
-
path
-
queryParameters
-
httpVersion
-
bodyString
-
body
-
valid
boolean valid -
headers
-
clientSocket
-
-
Constructor Details
-
Request
Creates a new Request object by reading an HTTP request from a socket using the new binary logic.Reads the request from the socket in chunks of 1024 bytes so that binary files can be parsed as well.
The old logic used BufferedReader to read the request line by line as strings. This led to problems when parsing binary files.
Reads until a double newline is found which indicates the end of the headers. Then reads the body if the Content-Length header is present.
- Parameters:
clientSocket- The socket to read the request fromnewLogic- Flag to indicate that the new logic should be used
-
Request
Creates a new Request object by reading an HTTP request from a socket.This uses the old logic to read the request line by line as strings. This can lead to problems when parsing binary files.
Superseded by the new constructor which reads the request in chunks of 1024 bytes.
- Parameters:
clientSocket- The socket to read the request from- Throws:
IOException- if an error occurs while reading the request from the socket
-
-
Method Details
-
parseHeaders
private void parseHeaders(byte[] bytes) Parses the headers from the provided byte array.- Parameters:
bytes- The byte array containing the headers
-
parseBody
private void parseBody(byte[] bytes) Parses the body of an HTTP request.- Parameters:
bytes- The byte array containing the body of the request
-
getHeaders
Returns the headers of the HTTP request.- Returns:
- The headers of the HTTP request
-
hasBody
public boolean hasBody()Checks if the request has a body.- Returns:
- true if the request has a body, false otherwise
-
getBody
Returns the body of the HTTP request.- Returns:
- The body of the HTTP request
-
getMethod
Returns the method of the HTTP request.- Returns:
- The method of the HTTP request
-
getHttpVersion
Returns the HTTP version of the HTTP request.- Returns:
- The HTTP version of the HTTP request
-
getPath
Returns the path of the HTTP request.- Returns:
- The path of the HTTP request
-
isValid
public boolean isValid()Checks if the request is valid.- Returns:
- true if the request is valid, false otherwise
-
getQueryParameters
Returns the query parameters of the HTTP request.- Returns:
- The query parameters of the HTTP request
-
answer
public void answer(byte[] response) Sends the provided response as the answer to the client.For easy access to the response, use
ResponseHelperto generate the response.- Parameters:
response- The byte array containing the response to send (useResponseHelperto generate the response)
-
getFormData
Retrieves the form data from the HTTP request body.This method returns the form data stored in the body of the HTTP request.
- Returns:
- The form data as a FormData object or null if the body is not a form data object
-