Class Request

java.lang.Object
net.bjmsw.hda.vs.model.Request

public class Request extends Object
Represents an HTTP 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.

See Also:
  • Field Details

  • Constructor Details

    • Request

      public Request(Socket clientSocket, boolean newLogic)
      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 from
      newLogic - Flag to indicate that the new logic should be used
    • Request

      public Request(Socket clientSocket) throws IOException
      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

      public Map<String,String> 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

      public HttpBody getBody()
      Returns the body of the HTTP request.
      Returns:
      The body of the HTTP request
    • getMethod

      public Request.Method getMethod()
      Returns the method of the HTTP request.
      Returns:
      The method of the HTTP request
    • getHttpVersion

      public String getHttpVersion()
      Returns the HTTP version of the HTTP request.
      Returns:
      The HTTP version of the HTTP request
    • getPath

      public String 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

      public Map<String,String> 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 ResponseHelper to generate the response.

      Parameters:
      response - The byte array containing the response to send (use ResponseHelper to generate the response)
    • getFormData

      public FormData 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