java.lang.Object
org.cybergarage.http.HTTPPacket
Direct Known Subclasses:
HTTPRequest, HTTPResponse

public class HTTPPacketextends Object
Represents a parsed HTTP packet with header and body handling.
  • Field Details

    • version

      private String version
      /////////////////////////////////////////////
    • firstLine

      private String firstLine
      /////////////////////////////////////////////
    • httpHeaderList

      private Vector<HTTPHeader> httpHeaderList
      /////////////////////////////////////////////
    • content

      private byte[] content
      /////////////////////////////////////////////
    • contentInput

      private InputStream contentInput
      /////////////////////////////////////////////
  • Constructor Details

    • 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 Details

    • init

      public void init()
      Initializes the HTTP packet to empty state. Clears first line, headers, content, and content input stream.
    • setVersion

      public void setVersion(String ver)
      Sets the HTTP version for this packet.
      Parameters:
      ver - HTTP version string (e.g., "1.0", "1.1")
    • getVersion

      public String getVersion()
      Gets the HTTP version of this packet.
      Returns:
      HTTP version string, or null if not set
    • readLine

      private String readLine(BufferedInputStream in)
      Reads a single line from the buffered input stream.

      This method reads characters until a line feed (LF) is encountered. Carriage return (CR) characters are ignored. The method handles interrupted IO exceptions gracefully as they are used to break HTTP connections.

      Parameters:
      in - the buffered input stream to read from
      Returns:
      the line as a UTF-8 string, or empty string if end of stream is reached
    • 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
    • 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(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 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
    • 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
    • setFirstLine

      private void setFirstLine(String value)
      Sets the first line of the HTTP packet.

      For requests, this is typically the request line (e.g., "GET /path HTTP/1.1"). For responses, this is the status line (e.g., "HTTP/1.1 200 OK").

      Parameters:
      value - the first line string to set
    • 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
    • hasFirstLine

      public boolean hasFirstLine()
      Checks if the first line of the HTTP packet has been set.
      Returns:
      true if the first line has content, false otherwise
    • getNHeaders

      public int getNHeaders()
      Gets the number of headers in this HTTP packet.
      Returns:
      the number of headers
    • addHeader

      public void addHeader(HTTPHeader header)
      Adds a header to this HTTP packet.
      Parameters:
      header - the header to add
    • addHeader

      public void addHeader(String name, String value)
      Adds a header with the specified name and value.
      Parameters:
      name - the header name
      value - the header value
    • getHeader

      public HTTPHeader getHeader(int n)
      Gets the header at the specified index.
      Parameters:
      n - the index of the header to retrieve
      Returns:
      the header at the specified index
    • getHeader

      public HTTPHeader getHeader(String name)
      Gets the header with the specified name, ignoring case.
      Parameters:
      name - the header name
      Returns:
      the matching header, or null if not found
    • clearHeaders

      public void clearHeaders()
      Clears all headers from this HTTP packet.
    • hasHeader

      public boolean hasHeader(String name)
      Checks if a header with the specified name exists.
      Parameters:
      name - the header name
      Returns:
      true if the header exists, false otherwise
    • setHeader

      public void setHeader(String name, String value)
      Sets the value of a header. If the header does not exist, it is created.
      Parameters:
      name - the header name
      value - the header value
    • setHeader

      public void setHeader(String name, int value)
      Sets the value of a header from an integer.
      Parameters:
      name - the header name
      value - the header value as integer
    • setHeader

      public void setHeader(String name, long value)
      Sets the value of a header from a long.
      Parameters:
      name - the header name
      value - the header value as long
    • setHeader

      public void setHeader(HTTPHeader header)
      Sets the value of a header from an HTTP header object.
      Parameters:
      header - the header to set
    • getHeaderValue

      public String getHeaderValue(String name)
      Gets the value of a header by name.
      Parameters:
      name - the header name
      Returns:
      the header value, or empty string if not found
    • setStringHeader

      public void setStringHeader(String name, String value, String startWidth, String endWidth)
      Sets a string header value, wrapping it with the specified delimiters if not already wrapped.
      Parameters:
      name - the header name
      value - the header value
      startWidth - the start delimiter
      endWidth - the end delimiter
    • setStringHeader

      public void setStringHeader(String name, String value)
      Sets a string header value, wrapping it with double quotes if not already wrapped.
      Parameters:
      name - the header name
      value - the header value
    • getStringHeaderValue

      public String getStringHeaderValue(String name, String startWidth, String endWidth)
      Gets a string header value, stripping the specified delimiters if present.
      Parameters:
      name - the header name
      startWidth - the start delimiter to strip
      endWidth - the end delimiter to strip
      Returns:
      the header value with delimiters stripped
    • getStringHeaderValue

      public String getStringHeaderValue(String name)
      Gets a string header value, stripping double quotes if present.
      Parameters:
      name - the header name
      Returns:
      the header value with double quotes stripped
    • setIntegerHeader

      public void setIntegerHeader(String name, int value)
      Sets an integer header value.
      Parameters:
      name - the header name
      value - the header value as integer
    • setLongHeader

      public void setLongHeader(String name, long value)
      Sets a long header value.
      Parameters:
      name - the header name
      value - the header value as long
    • getIntegerHeaderValue

      public int getIntegerHeaderValue(String name)
      Gets an integer header value by name.
      Parameters:
      name - the header name
      Returns:
      the header value as integer, or 0 if not found
    • getLongHeaderValue

      public long getLongHeaderValue(String name)
      Gets a long header value by name.
      Parameters:
      name - the header name
      Returns:
      the header value as long, or 0 if not found
    • getHeaderString

      public String getHeaderString()
      Gets all headers as a formatted string.
      Returns:
      the header string in "Name: Value" format, each header on its own line
    • 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(byte[] data)
      Sets the content of this HTTP packet. Automatically updates Content-Length header.
      Parameters:
      data - content data as byte array
    • setContent

      public void setContent(String data, boolean updateWithContentLength)
      Sets the content of this HTTP packet from a string.
      Parameters:
      data - content data as string
      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
    • getContent

      public byte[] getContent()
      Gets the content of this HTTP packet.
      Returns:
      content as byte array
    • 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
    • hasContent

      public boolean hasContent()
      Checks if this HTTP packet has content.
      Returns:
      true if content length is greater than 0, false otherwise
    • setContentInputStream

      public void setContentInputStream(InputStream in)
      Sets the content input stream.
      Parameters:
      in - the input stream to use as content source
    • getContentInputStream

      public InputStream getContentInputStream()
      Gets the content input stream.
      Returns:
      the content input stream, or null if not set
    • hasContentInputStream

      public boolean hasContentInputStream()
      Checks if a content input stream has been set.
      Returns:
      true if a content input stream is set, false otherwise
    • setContentType

      public void setContentType(String type)
      Sets the Content-Type header.
      Parameters:
      type - the content type string
    • getContentType

      public String getContentType()
      Gets the Content-Type header value.
      Returns:
      the content type, or empty string if not set
    • setContentLanguage

      public void setContentLanguage(String code)
      Sets the Content-Language header.
      Parameters:
      code - the language code
    • getContentLanguage

      public String getContentLanguage()
      Gets the Content-Language header value.
      Returns:
      the content language, or empty string if not set
    • getCharSet

      public String getCharSet()
      Gets the character set from the Content-Type header.
      Returns:
      the charset string, or empty string if not specified
    • setContentLength

      public void setContentLength(long len)
      Sets the Content-Length header.
      Parameters:
      len - the content length
    • getContentLength

      public long getContentLength()
      Gets the content length from the Content-Length header.
      Returns:
      the content length
    • hasConnection

      public boolean hasConnection()
      Checks if the Connection header is set.
      Returns:
      true if the Connection header exists, false otherwise
    • setConnection

      public void setConnection(String value)
      Sets the Connection header.
      Parameters:
      value - the connection directive
    • getConnection

      public String getConnection()
      Gets the Connection header value.
      Returns:
      the connection directive, or empty string if not set
    • isCloseConnection

      public boolean isCloseConnection()
      Checks if the Connection header is set to close.
      Returns:
      true if close, false otherwise
    • isKeepAliveConnection

      public boolean isKeepAliveConnection()
      Checks if the Connection header is set to keep-alive.
      Returns:
      true if keep-alive, false otherwise
    • hasContentRange

      public boolean hasContentRange()
      Checks if the Content-Range or Range header is set.
      Returns:
      true if a range header exists, false otherwise
    • setContentRange

      public void setContentRange(long firstPos, long lastPos, long length)
      Sets the Content-Range header.
      Parameters:
      firstPos - the first byte position
      lastPos - the last byte position
      length - the total length, or 0 for unknown
    • getContentRange

      public long[] getContentRange()
      Gets the content range values as an array.
      Returns:
      array containing first position, last position, and length
    • getContentRangeFirstPosition

      public long getContentRangeFirstPosition()
      Get the first position from the content range.
      Returns:
      the first byte position
    • getContentRangeLastPosition

      public long getContentRangeLastPosition()
      Get the last position from the content range.
      Returns:
      the last byte position
    • getContentRangeInstanceLength

      public long getContentRangeInstanceLength()
      Get the instance length from the content range.
      Returns:
      the instance length
    • setCacheControl

      public void setCacheControl(String directive)
      Set the Cache-Control header.
      Parameters:
      directive - the cache directive
    • setCacheControl

      public void setCacheControl(String directive, int value)
      Set the Cache-Control header with a directive and value.
      Parameters:
      directive - the cache directive
      value - the directive value
    • setCacheControl

      public void setCacheControl(int value)
      Set the Cache-Control header with a max-age value.
      Parameters:
      value - the max age in seconds
    • getCacheControl

      public String getCacheControl()
      Get the Cache-Control header value.
      Returns:
      the cache control string
    • setServer

      public void setServer(String name)
      Set the Server header.
      Parameters:
      name - the server name
    • getServer

      public String getServer()
      Get the Server header value.
      Returns:
      the server string
    • setHost

      public void setHost(String host, int port)
      Set the Host header.
      Parameters:
      host - the host name
      port - the port number
    • getHost

      public String getHost()
      Get the Host header value.
      Returns:
      the host string including port
    • setDate

      public void setDate(Calendar cal)
      Set the Date header.
      Parameters:
      cal - the calendar to set as the date
    • getDate

      public String getDate()
      Get the Date header value.
      Returns:
      the date string
    • hasTransferEncoding

      public boolean hasTransferEncoding()
      Check if the Transfer-Encoding header is set.
      Returns:
      true if Transfer-Encoding is present
    • setTransferEncoding

      public void setTransferEncoding(String value)
      Set the Transfer-Encoding header.
      Parameters:
      value - the transfer encoding value
    • getTransferEncoding

      public String getTransferEncoding()
      Get the Transfer-Encoding header value.
      Returns:
      the transfer encoding string
    • isChunked

      public boolean isChunked()
      Check if the content is chunked.
      Returns:
      true if chunked transfer encoding is set