org.cybergarage.http

## Class HTTPPacket

- java.lang.Object
- - org.cybergarage.http.HTTPPacket

- Direct Known Subclasses:
    HTTPRequest, HTTPResponse

      public class HTTPPacket
      extends Object

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `HTTPPacket()` Creates a new HTTP packet with default settings.
    `HTTPPacket(HTTPPacket httpPacket)` Creates a copy of an existing HTTP packet.
    `HTTPPacket(InputStream in)` Creates an HTTP packet by parsing from an input stream.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addHeader(HTTPHeader header)`
    `void` | `addHeader(String name, String value)`
    `void` | `clearHeaders()`
    `String` | `getCacheControl()`
    `String` | `getCharSet()`
    `String` | `getConnection()`
    `byte[]` | `getContent()` Gets the content of this HTTP packet.
    `InputStream` | `getContentInputStream()`
    `String` | `getContentLanguage()`
    `long` | `getContentLength()`
    `long[]` | `getContentRange()`
    `long` | `getContentRangeFirstPosition()`
    `long` | `getContentRangeInstanceLength()`
    `long` | `getContentRangeLastPosition()`
    `String` | `getContentString()` Gets the content of this HTTP packet as a string.
    `String` | `getContentType()`
    `String` | `getDate()`
    `protected String` | `getFirstLine()` Gets the first line of the HTTP packet.
    `protected String` | `getFirstLineToken(int num)` Gets a specific token from the first line using HTTP request line delimiters.
    `HTTPHeader` | `getHeader(int n)`
    `HTTPHeader` | `getHeader(String name)`
    `String` | `getHeaderString()`
    `String` | `getHeaderValue(String name)`
    `String` | `getHost()`
    `int` | `getIntegerHeaderValue(String name)`
    `long` | `getLongHeaderValue(String name)`
    `int` | `getNHeaders()`
    `String` | `getServer()`
    `String` | `getStringHeaderValue(String name)`
    `String` | `getStringHeaderValue(String name, String startWidth, String endWidth)`
    `String` | `getTransferEncoding()`
    `String` | `getVersion()` Gets the HTTP version of this packet.
    `boolean` | `hasConnection()`
    `boolean` | `hasContent()` Checks if this HTTP packet has content.
    `boolean` | `hasContentInputStream()`
    `boolean` | `hasContentRange()`
    `boolean` | `hasFirstLine()`
    `boolean` | `hasHeader(String name)`
    `boolean` | `hasTransferEncoding()`
    `void` | `init()` Initializes the HTTP packet to empty state.
    `boolean` | `isChunked()`
    `boolean` | `isCloseConnection()`
    `boolean` | `isKeepAliveConnection()`
    `boolean` | `read(HTTPSocket httpSock)` Reads and parses HTTP packet from a socket.
    `protected void` | `set(HTTPPacket httpPacket)` Copies all data from another HTTP packet to this packet.
    `protected boolean` | `set(HTTPSocket httpSock)` Parses HTTP packet data from an HTTP socket.
    `protected boolean` | `set(InputStream in)` Parses HTTP packet data from an input stream including content.
    `protected boolean` | `set(InputStream in, boolean onlyHeaders)` Parses HTTP packet data from an input stream.
    `void` | `setCacheControl(int value)`
    `void` | `setCacheControl(String directive)`
    `void` | `setCacheControl(String directive, int value)`
    `void` | `setConnection(String value)`
    `void` | `setContent(byte[] data)`
    `void` | `setContent(byte[] data, boolean updateWithContentLength)` Sets the content of this HTTP packet.
    `void` | `setContent(String data)` Sets the content of this HTTP packet from a string.
    `void` | `setContent(String data, boolean updateWithContentLength)`
    `void` | `setContentInputStream(InputStream in)`
    `void` | `setContentLanguage(String code)`
    `void` | `setContentLength(long len)`
    `void` | `setContentRange(long firstPos, long lastPos, long length)`
    `void` | `setContentType(String type)`
    `void` | `setDate(Calendar cal)`
    `void` | `setHeader(HTTPHeader header)`
    `void` | `setHeader(String name, int value)`
    `void` | `setHeader(String name, long value)`
    `void` | `setHeader(String name, String value)`
    `void` | `setHost(String host, int port)`
    `void` | `setIntegerHeader(String name, int value)`
    `void` | `setLongHeader(String name, long value)`
    `void` | `setServer(String name)`
    `void` | `setStringHeader(String name, String value)`
    `void` | `setStringHeader(String name, String value, String startWidth, String endWidth)`
    `void` | `setTransferEncoding(String value)`
    `void` | `setVersion(String ver)` Sets the HTTP version for this packet.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### HTTPPacket

          public HTTPPacket()

      Creates a new HTTP packet with default settings. Initializes with HTTP version and empty content.

    - #### HTTPPacket

          public HTTPPacket(HTTPPacket httpPacket)

      Creates a copy of an existing HTTP packet.
      Parameters:
        `httpPacket` - HTTP packet to copy

    - #### HTTPPacket

          public HTTPPacket(InputStream in)

      Creates an HTTP packet by parsing from an input stream.
      Parameters:
        `in` - input stream containing the HTTP packet data

  - ### Method Detail

    - #### addHeader

          public void addHeader(HTTPHeader header)

    - #### addHeader

          public void addHeader(String name,
                                String value)

    - #### clearHeaders

          public void clearHeaders()

    - #### getCacheControl

          public String getCacheControl()

    - #### getCharSet

          public String getCharSet()

    - #### getConnection

          public String getConnection()

    - #### getContent

          public byte[] getContent()

      Gets the content of this HTTP packet.
      Returns:
        content as byte array

    - #### getContentInputStream

          public InputStream getContentInputStream()

    - #### getContentLanguage

          public String getContentLanguage()

    - #### getContentLength

          public long getContentLength()

    - #### getContentRange

          public long[] getContentRange()

    - #### getContentRangeFirstPosition

          public long getContentRangeFirstPosition()

    - #### getContentRangeInstanceLength

          public long getContentRangeInstanceLength()

    - #### getContentRangeLastPosition

          public long getContentRangeLastPosition()

    - #### getContentString

          public String getContentString()

      Gets the content of this HTTP packet as a string. Uses the character set specified in Content-Type header if available.
      Returns:
        content as string, using UTF-8 as default charset

    - #### getContentType

          public String getContentType()

    - #### getDate

          public String getDate()

    - #### getFirstLine

          protected String getFirstLine()

      Gets the first line of the HTTP packet.

      For requests, this returns the request line containing method, URI, and HTTP version. For responses, this returns the status line containing HTTP version, status code, and reason phrase.

      Returns:
        the first line string, or empty string if not set

    - #### getFirstLineToken

          protected String getFirstLineToken(int num)

      Gets a specific token from the first line using HTTP request line delimiters.

      This method parses the first line (request line or status line) and returns the token at the specified position. Tokens are separated by spaces.

      For request lines: token 0 = method, token 1 = URI, token 2 = HTTP version

      For status lines: token 0 = HTTP version, token 1 = status code, token 2 = reason phrase

      Parameters:
        `num` - the zero-based token position to retrieve
      Returns:
        the token at the specified position, or empty string if not found

    - #### getHeader

          public HTTPHeader getHeader(int n)

    - #### getHeader

          public HTTPHeader getHeader(String name)

    - #### getHeaderString

          public String getHeaderString()

    - #### getHeaderValue

          public String getHeaderValue(String name)

    - #### getHost

          public String getHost()

    - #### getIntegerHeaderValue

          public int getIntegerHeaderValue(String name)

    - #### getLongHeaderValue

          public long getLongHeaderValue(String name)

    - #### getNHeaders

          public int getNHeaders()

    - #### getServer

          public String getServer()

    - #### getStringHeaderValue

          public String getStringHeaderValue(String name)

    - #### getStringHeaderValue

          public String getStringHeaderValue(String name,
                                             String startWidth,
                                             String endWidth)

    - #### getTransferEncoding

          public String getTransferEncoding()

    - #### getVersion

          public String getVersion()

      Gets the HTTP version of this packet.
      Returns:
        HTTP version string, or null if not set

    - #### hasConnection

          public boolean hasConnection()

    - #### hasContent

          public boolean hasContent()

      Checks if this HTTP packet has content.
      Returns:
        true if content length is greater than 0, false otherwise

    - #### hasContentInputStream

          public boolean hasContentInputStream()

    - #### hasContentRange

          public boolean hasContentRange()

    - #### hasFirstLine

          public boolean hasFirstLine()

    - #### hasHeader

          public boolean hasHeader(String name)

    - #### hasTransferEncoding

          public boolean hasTransferEncoding()

    - #### init

          public void init()

      Initializes the HTTP packet to empty state. Clears first line, headers, content, and content input stream.

    - #### isChunked

          public boolean isChunked()

    - #### isCloseConnection

          public boolean isCloseConnection()

    - #### isKeepAliveConnection

          public boolean isKeepAliveConnection()

    - #### read

          public boolean read(HTTPSocket httpSock)

      Reads and parses HTTP packet from a socket.
      Parameters:
        `httpSock` - socket containing the HTTP packet data
      Returns:
        true if packet was read successfully, false otherwise

    - #### set

          protected void set(HTTPPacket httpPacket)

      Copies all data from another HTTP packet to this packet.

      This method performs a deep copy by copying the first line, all headers, and content from the source packet. The target packet is first cleared before copying.

      Parameters:
        `httpPacket` - the source HTTP packet to copy from

    - #### set

          protected boolean set(HTTPSocket httpSock)

      Parses HTTP packet data from an HTTP socket.

      This is a convenience method that extracts the input stream from the HTTP socket and parses the complete HTTP packet including content.

      Parameters:
        `httpSock` - the HTTP socket containing the HTTP packet data
      Returns:
        true if parsing was successful, false otherwise

    - #### set

          protected boolean set(InputStream in)

      Parses HTTP packet data from an input stream including content.

      This is a convenience method that calls `set(InputStream, boolean)` with onlyHeaders set to false.

      Parameters:
        `in` - the input stream containing HTTP packet data
      Returns:
        true if parsing was successful, false otherwise

    - #### set

          protected boolean set(InputStream in,
                                boolean onlyHeaders)

      Parses HTTP packet data from an input stream.

      This method reads and parses the HTTP packet including the first line, headers, and optionally the content body. It handles special cases such as:

      - HTTP 100 Continue responses (IIS compatibility)
      - Chunked transfer encoding
      - Content length parsing
      Parameters:
        `in` - the input stream containing HTTP packet data
        `onlyHeaders` - if true, only parse headers and skip content body
      Returns:
        true if parsing was successful, false otherwise

    - #### setCacheControl

          public void setCacheControl(int value)

    - #### setCacheControl

          public void setCacheControl(String directive)

    - #### setCacheControl

          public void setCacheControl(String directive,
                                      int value)

    - #### setConnection

          public void setConnection(String value)

    - #### setContent

          public void setContent(byte[] data)

    - #### setContent

          public void setContent(byte[] data,
                                 boolean updateWithContentLength)

      Sets the content of this HTTP packet.
      Parameters:
        `data` - content data as byte array
        `updateWithContentLength` - whether to update Content-Length header automatically

    - #### setContent

          public void setContent(String data)

      Sets the content of this HTTP packet from a string. Automatically updates Content-Length header.
      Parameters:
        `data` - content data as string

    - #### setContent

          public void setContent(String data,
                                 boolean updateWithContentLength)

    - #### setContentInputStream

          public void setContentInputStream(InputStream in)

    - #### setContentLanguage

          public void setContentLanguage(String code)

    - #### setContentLength

          public void setContentLength(long len)

    - #### setContentRange

          public void setContentRange(long firstPos,
                                      long lastPos,
                                      long length)

    - #### setContentType

          public void setContentType(String type)

    - #### setDate

          public void setDate(Calendar cal)

    - #### setHeader

          public void setHeader(HTTPHeader header)

    - #### setHeader

          public void setHeader(String name,
                                int value)

    - #### setHeader

          public void setHeader(String name,
                                long value)

    - #### setHeader

          public void setHeader(String name,
                                String value)

    - #### setHost

          public void setHost(String host,
                              int port)

    - #### setIntegerHeader

          public void setIntegerHeader(String name,
                                       int value)

    - #### setLongHeader

          public void setLongHeader(String name,
                                    long value)

    - #### setServer

          public void setServer(String name)

    - #### setStringHeader

          public void setStringHeader(String name,
                                      String value)

    - #### setStringHeader

          public void setStringHeader(String name,
                                      String value,
                                      String startWidth,
                                      String endWidth)

    - #### setTransferEncoding

          public void setTransferEncoding(String value)

    - #### setVersion

          public void setVersion(String ver)

      Sets the HTTP version for this packet.
      Parameters:
        `ver` - HTTP version string (e.g., "1.0", "1.1")