java.lang.Object
org.cybergarage.http.HTTPSocket

public class HTTPSocketextends Object
HTTP socket wrapper providing input/output stream management
  • Field Details

    • socket

      private Socket socket
      /////////////////////////////////////////////
    • sockIn

      private InputStream sockIn
      /////////////////////////////////////////////
    • sockOut

      private OutputStream sockOut
      Stream responses are written to.
  • Constructor Details

    • HTTPSocket

      public HTTPSocket(Socket socket)
      Creates an HTTP socket wrapper around a standard socket.
      Parameters:
      socket - underlying socket to wrap
    • HTTPSocket

      public HTTPSocket(HTTPSocket socket)
      Creates an HTTP socket wrapper around another HTTP socket.
      Parameters:
      socket - HTTP socket to wrap
  • Method Details

    • setSocket

      private void setSocket(Socket socket)
      Sets the connection the request arrived on.
      Parameters:
      socket - the socket to use
    • getSocket

      public Socket getSocket()
      Returns the socket this object wraps.
      Returns:
      the wrapped java.net.Socket
    • getLocalAddress

      public String getLocalAddress()
      Gets the local address of this socket.
      Returns:
      local host address
    • getLocalPort

      public int getLocalPort()
      Gets the local port of this socket.
      Returns:
      local port number
    • setInputStream

      private void setInputStream(InputStream in)
      Sets the stream requests are read from.
      Parameters:
      in - the stream to use
    • getInputStream

      public InputStream getInputStream()
      Get the input stream
      Returns:
      the input stream
    • setOutputStream

      private void setOutputStream(OutputStream out)
      Sets the stream responses are written to.
      Parameters:
      out - the stream to use
    • getOutputStream

      private OutputStream getOutputStream()
      Gives the stream responses are written to.
      Returns:
      the stream, null if the socket is closed
    • open

      public boolean open()
      Opens the socket streams for input/output operations.
      Returns:
      true if streams were opened successfully, false otherwise
    • close

      public boolean close()
      Closes the socket and all associated streams.
      Returns:
      true if closed successfully
    • post

      private boolean post(HTTPResponse httpRes, byte[] content, long contentOffset, long contentLength, boolean isOnlyHeader)
      Sends an HTTP response using byte array content.

      This private helper method sends an HTTP response with the specified content. It handles both regular and chunked transfer encoding, sets appropriate headers, and manages the content offset and length for partial content responses.

      Parameters:
      httpRes - the HTTP response to send
      content - the content data as byte array
      contentOffset - the starting offset in the content array
      contentLength - the length of content to send
      isOnlyHeader - if true, only send headers without content body
      Returns:
      true if the response was sent successfully, false otherwise
    • post

      private boolean post(HTTPResponse httpRes, InputStream in, long contentOffset, long contentLength, boolean isOnlyHeader)
      Sends an HTTP response using input stream content.

      This private helper method sends an HTTP response with content from an input stream. It handles both regular and chunked transfer encoding, reads content in chunks, and manages the content offset for partial content responses.

      Parameters:
      httpRes - the HTTP response to send
      in - the input stream containing content data
      contentOffset - the offset to skip in the input stream before reading
      contentLength - the total length of content to send
      isOnlyHeader - if true, only send headers without content body
      Returns:
      true if the response was sent successfully, false otherwise
    • post

      public boolean post(HTTPResponse httpRes, long contentOffset, long contentLength, boolean isOnlyHeader)
      Posts an HTTP response with the specified content parameters.
      Parameters:
      httpRes - the HTTP response to post
      contentOffset - the offset in the content to start from
      contentLength - the length of content to send
      isOnlyHeader - true if only headers should be sent
      Returns:
      true if posted successfully