java.lang.Object
org.cybergarage.http.HTTPPacket
org.cybergarage.http.HTTPRequest
Direct Known Subclasses:
SOAPRequest, SSDPRequest, SubscriptionRequest

public class HTTPRequestextends HTTPPacket
HTTP request message with method, URI, headers, and content body.
  • Field Details

    • method

      private String method
      /////////////////////////////////////////////
    • uri

      private String uri
      /////////////////////////////////////////////
    • requestHost

      private String requestHost
      /////////////////////////////////////////////
    • requestPort

      private int requestPort
      Port the request was sent to, -1 until known.
    • httpSocket

      private HTTPSocket httpSocket
      /////////////////////////////////////////////
    • postSocket

      private Socket postSocket
      /////////////////////////////////////////////
    • bindTo

      private String bindTo
      Local address the connection is bound to.
  • Constructor Details

    • HTTPRequest

      public HTTPRequest()
      /////////////////////////////////////////////
    • HTTPRequest

      public HTTPRequest(InputStream in)
      Parses an HTTP request from the given input stream.
      Parameters:
      in - the stream supplying the raw request bytes
    • HTTPRequest

      public HTTPRequest(HTTPSocket httpSock)
      Creates an HTTP request from the given socket's input stream.
      Parameters:
      httpSock - the socket supplying the request bytes, retained on the new request
  • Method Details

    • setMethod

      public void setMethod(String value)
      Sets the HTTP method (e.g., GET, POST).
      Parameters:
      value - the HTTP method string
    • getMethod

      public String getMethod()
      Returns the HTTP method.
      Returns:
      the HTTP method string, or null if not set
    • isMethod

      public boolean isMethod(String method)
      Checks if the HTTP method matches the given string.
      Parameters:
      method - the method string to compare
      Returns:
      true if the methods match (case-insensitive), false otherwise
    • isGetRequest

      public boolean isGetRequest()
      Determines if this is a GET request.
      Returns:
      true if the method is GET, false otherwise
    • isPostRequest

      public boolean isPostRequest()
      Determines if this is a POST request.
      Returns:
      true if the method is POST, false otherwise
    • isHeadRequest

      public boolean isHeadRequest()
      Determines if this is a HEAD request.
      Returns:
      true if the method is HEAD, false otherwise
    • isSubscribeRequest

      public boolean isSubscribeRequest()
      Determines if this is a SUBSCRIBE request.
      Returns:
      true if the method is SUBSCRIBE, false otherwise
    • isUnsubscribeRequest

      public boolean isUnsubscribeRequest()
      Determines if this is an UNSUBSCRIBE request.
      Returns:
      true if the method is UNSUBSCRIBE, false otherwise
    • isNotifyRequest

      public boolean isNotifyRequest()
      Determines if this is a NOTIFY request.
      Returns:
      true if the method is NOTIFY, false otherwise
    • setURI

      public void setURI(String value, boolean isCheckRelativeURL)
      Sets the URI for this HTTP request.

      Sets the request URI and optionally converts it to a relative URL. This is used to handle both absolute and relative URLs in HTTP requests.

      Parameters:
      value - the URI string to set
      isCheckRelativeURL - if true, converts the URI to a relative URL using HTTP.toRelativeURL()
    • setURI

      public void setURI(String value)
      Sets the URI for this HTTP request without relative URL conversion.

      This is a convenience method that calls setURI(String, boolean) with isCheckRelativeURL set to false.

      Parameters:
      value - the URI string to set
    • getURI

      public String getURI()
      Returns the request URI.
      Returns:
      the URI string, or null if not set
    • getParameterList

      public ParameterList getParameterList()
      Parses the URI query string and returns the parameters as a ParameterList.
      Returns:
      the ParameterList containing all query parameters, or an empty list if none exist
    • getParameterValue

      public String getParameterValue(String name)
      Returns the value of the specified URI query parameter.
      Parameters:
      name - the parameter name
      Returns:
      the parameter value, or null if not found
    • isSOAPAction

      public boolean isSOAPAction()
      Checks if this request has a SOAPAction header.
      Returns:
      true if the SOAPAction header is present, false otherwise
    • setRequestHost

      public void setRequestHost(String host)
      Sets the host for this request.
      Parameters:
      host - the host string
    • getRequestHost

      public String getRequestHost()
      Returns the request host.
      Returns:
      the host string
    • setRequestPort

      public void setRequestPort(int host)
      Sets the port for this request.
      Parameters:
      host - the port number
    • getRequestPort

      public int getRequestPort()
      Returns the request port.
      Returns:
      the port number
    • setSocket

      public void setSocket(HTTPSocket value)
      Sets the HTTPSocket for this request.
      Parameters:
      value - the HTTPSocket to set
    • getSocket

      public HTTPSocket getSocket()
      Returns the HTTPSocket for this request.
      Returns:
      the HTTPSocket, or null if not set
    • getLocalAddress

      public String getLocalAddress()
      Returns the local address of the underlying socket.
      Returns:
      the local address string
    • getLocalPort

      public int getLocalPort()
      Returns the local port of the underlying socket.
      Returns:
      the local port number
    • parseRequestLine

      public boolean parseRequestLine(String lineStr)
      Parses an HTTP request line and extracts method, URI, and version.

      This method parses a request line in the format "METHOD URI VERSION" (e.g., "GET /path HTTP/1.1") and sets the corresponding properties on this HTTP request object.

      Parameters:
      lineStr - the request line string to parse
      Returns:
      true if parsing was successful and all three components were found, false otherwise
    • getHTTPVersion

      public String getHTTPVersion()
      Returns the HTTP version string for this request.
      Returns:
      the HTTP version (e.g., "HTTP/1.1")
    • getFirstLineString

      public String getFirstLineString()
      Returns the first line of the HTTP request (method, URI, and version).
      Returns:
      the request line string
    • getHeader

      public String getHeader()
      Returns the complete HTTP header as a string.
      Returns:
      the header string including the request line and headers
    • isKeepAlive

      public boolean isKeepAlive()
      Determines if this connection should be kept alive.
      Returns:
      true if keep-alive is enabled, false otherwise
    • read

      public boolean read()
      Reads the HTTP request from the underlying socket.
      Returns:
      true if the request was read successfully, false otherwise
    • post

      public boolean post(HTTPResponse httpRes)
      Posts an HTTP response back to the client.
      Parameters:
      httpRes - the HTTP response to send
      Returns:
      true if the response was sent successfully, false otherwise
    • setBindHost

      public void setBindHost(String host)
      I2P - bind HTTP socket to specified local host address
      Parameters:
      host - null to not bind to a particular local address
    • post

      public HTTPResponse post(String host, int port, boolean isKeepAlive)
      Posts this HTTP request to the specified host and port.
      Parameters:
      host - the target host
      port - the target port
      isKeepAlive - whether to keep the connection alive
      Returns:
      the HTTP response
    • post

      public HTTPResponse post(String host, int port)
      Posts this HTTP request to the specified host and port with no keep-alive.
      Parameters:
      host - the target host
      port - the target port
      Returns:
      the HTTP response
    • set

      public void set(HTTPRequest httpReq)
      Copies the contents of the given HTTP request into this request.
      Parameters:
      httpReq - the HTTP request to copy from
    • returnResponse

      public boolean returnResponse(int statusCode)
      Sends an HTTP response with the specified status code and no content.

      This method creates a new HTTP response with the given status code, sets content length to 0, and sends it back to the client using the post(HTTPResponse) method. This is commonly used for error responses or responses that don't require a body.

      Parameters:
      statusCode - the HTTP status code to send (e.g., 200, 404, 500)
      Returns:
      true if the response was sent successfully, false otherwise
    • returnOK

      public boolean returnOK()
      Sends an HTTP 200 OK response with no content.

      This is a convenience method that sends a 200 OK status code response, typically used to indicate successful request processing when no response body is needed.

      Returns:
      true if the response was sent successfully, false otherwise
    • returnBadRequest

      public boolean returnBadRequest()
      Sends an HTTP 400 Bad Request response with no content.

      This is a convenience method that sends a 400 Bad Request status code response, typically used when the client request is malformed or cannot be understood by the server.

      Returns:
      true if the response was sent successfully, false otherwise
    • toString

      public String toString()
      /////////////////////////////////////////////
      Overrides:
      toString in class Object
    • print

      public void print()
      Prints the HTTP request to the debug message stream.

      This method outputs the complete HTTP request including headers and content to the debug logging system using Debug.message(). Useful for debugging HTTP request processing.