- Direct Known Subclasses:
HTTPRequest, HTTPResponse
Field Summary
FieldsModifier and TypeFieldDescriptionprivate byte[]/////////////////////////////////////////////private InputStream/////////////////////////////////////////////private String/////////////////////////////////////////////private Vector<HTTPHeader> /////////////////////////////////////////////private String/////////////////////////////////////////////Constructor Summary
ConstructorsConstructorDescriptionCreates a new HTTP packet with default settings.Creates an HTTP packet by parsing from an input stream.HTTPPacket(HTTPPacket httpPacket) Creates a copy of an existing HTTP packet.Method Summary
Modifier and TypeMethodDescriptionvoidAdds a header with the specified name and value.voidaddHeader(HTTPHeader header) Adds a header to this HTTP packet.voidClears all headers from this HTTP packet.Get the Cache-Control header value.Gets the character set from the Content-Type header.Gets the Connection header value.byte[]Gets the content of this HTTP packet.Gets the content input stream.Gets the Content-Language header value.longGets the content length from the Content-Length header.long[]Gets the content range values as an array.longGet the first position from the content range.longGet the instance length from the content range.longGet the last position from the content range.Gets the content of this HTTP packet as a string.Gets the Content-Type header value.getDate()Get the Date header value.protected StringGets the first line of the HTTP packet.protected StringgetFirstLineToken(int num) Gets a specific token from the first line using HTTP request line delimiters.getHeader(int n) Gets the header at the specified index.Gets the header with the specified name, ignoring case.Gets all headers as a formatted string.getHeaderValue(String name) Gets the value of a header by name.getHost()Get the Host header value.intgetIntegerHeaderValue(String name) Gets an integer header value by name.longgetLongHeaderValue(String name) Gets a long header value by name.intGets the number of headers in this HTTP packet.Get the Server header value.getStringHeaderValue(String name) Gets a string header value, stripping double quotes if present.getStringHeaderValue(String name, String startWidth, String endWidth) Gets a string header value, stripping the specified delimiters if present.Get the Transfer-Encoding header value.Gets the HTTP version of this packet.booleanChecks if the Connection header is set.booleanChecks if this HTTP packet has content.booleanChecks if a content input stream has been set.booleanChecks if the Content-Range or Range header is set.booleanChecks if the first line of the HTTP packet has been set.booleanChecks if a header with the specified name exists.booleanCheck if the Transfer-Encoding header is set.voidinit()Initializes the HTTP packet to empty state.booleanCheck if the content is chunked.booleanChecks if the Connection header is set to close.booleanChecks if the Connection header is set to keep-alive.booleanread(HTTPSocket httpSock) Reads and parses HTTP packet from a socket.private StringReads a single line from the buffered input stream.protected booleanset(InputStream in) Parses HTTP packet data from an input stream including content.protected booleanset(InputStream in, boolean onlyHeaders) Parses HTTP packet data from an input stream.protected voidset(HTTPPacket httpPacket) Copies all data from another HTTP packet to this packet.protected booleanset(HTTPSocket httpSock) Parses HTTP packet data from an HTTP socket.voidsetCacheControl(int value) Set the Cache-Control header with a max-age value.voidsetCacheControl(String directive) Set the Cache-Control header.voidsetCacheControl(String directive, int value) Set the Cache-Control header with a directive and value.voidsetConnection(String value) Sets the Connection header.voidsetContent(byte[] data) Sets the content of this HTTP packet.voidsetContent(byte[] data, boolean updateWithContentLength) Sets the content of this HTTP packet.voidsetContent(String data) Sets the content of this HTTP packet from a string.voidsetContent(String data, boolean updateWithContentLength) Sets the content of this HTTP packet from a string.voidSets the content input stream.voidsetContentLanguage(String code) Sets the Content-Language header.voidsetContentLength(long len) Sets the Content-Length header.voidsetContentRange(long firstPos, long lastPos, long length) Sets the Content-Range header.voidsetContentType(String type) Sets the Content-Type header.voidSet the Date header.private voidsetFirstLine(String value) Sets the first line of the HTTP packet.voidSets the value of a header from an integer.voidSets the value of a header from a long.voidSets the value of a header.voidsetHeader(HTTPHeader header) Sets the value of a header from an HTTP header object.voidSet the Host header.voidsetIntegerHeader(String name, int value) Sets an integer header value.voidsetLongHeader(String name, long value) Sets a long header value.voidSet the Server header.voidsetStringHeader(String name, String value) Sets a string header value, wrapping it with double quotes if not already wrapped.voidsetStringHeader(String name, String value, String startWidth, String endWidth) Sets a string header value, wrapping it with the specified delimiters if not already wrapped.voidsetTransferEncoding(String value) Set the Transfer-Encoding header.voidsetVersion(String ver) Sets the HTTP version for this packet.
Field Details
version
/////////////////////////////////////////////firstLine
/////////////////////////////////////////////httpHeaderList
/////////////////////////////////////////////- private byte[] content/////////////////////////////////////////////
contentInput
/////////////////////////////////////////////
Constructor Details
HTTPPacket
public HTTPPacket()Creates a new HTTP packet with default settings. Initializes withHTTPversion and empty content.HTTPPacket
Creates a copy of an existing HTTP packet.- Parameters:
httpPacket- HTTP packet to copy
HTTPPacket
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
Sets the HTTP version for this packet.- Parameters:
ver- HTTP version string (e.g., "1.0", "1.1")
getVersion
Gets the HTTP version of this packet.- Returns:
- HTTP version string, or null if not set
readLine
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
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 dataonlyHeaders- if true, only parse headers and skip content body- Returns:
- true if parsing was successful, false otherwise
set
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
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
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
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
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
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
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
Adds a header to this HTTP packet.- Parameters:
header- the header to add
addHeader
getHeader
Gets the header at the specified index.- Parameters:
n- the index of the header to retrieve- Returns:
- the header at the specified index
getHeader
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
Checks if a header with the specified name exists.- Parameters:
name- the header name- Returns:
- true if the header exists, false otherwise
setHeader
setHeader
Sets the value of a header from an integer.- Parameters:
name- the header namevalue- the header value as integer
setHeader
Sets the value of a header from a long.- Parameters:
name- the header namevalue- the header value as long
setHeader
Sets the value of a header from an HTTP header object.- Parameters:
header- the header to set
getHeaderValue
setStringHeader
Sets a string header value, wrapping it with the specified delimiters if not already wrapped.- Parameters:
name- the header namevalue- the header valuestartWidth- the start delimiterendWidth- the end delimiter
setStringHeader
getStringHeaderValue
Gets a string header value, stripping the specified delimiters if present.- Parameters:
name- the header namestartWidth- the start delimiter to stripendWidth- the end delimiter to strip- Returns:
- the header value with delimiters stripped
getStringHeaderValue
setIntegerHeader
Sets an integer header value.- Parameters:
name- the header namevalue- the header value as integer
setLongHeader
Sets a long header value.- Parameters:
name- the header namevalue- the header value as long
getIntegerHeaderValue
Gets an integer header value by name.- Parameters:
name- the header name- Returns:
- the header value as integer, or 0 if not found
getLongHeaderValue
Gets a long header value by name.- Parameters:
name- the header name- Returns:
- the header value as long, or 0 if not found
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 arrayupdateWithContentLength- 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
Sets the content of this HTTP packet from a string.- Parameters:
data- content data as stringupdateWithContentLength- whether to update Content-Length header automatically
setContent
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
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
Sets the content input stream.- Parameters:
in- the input stream to use as content source
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
Sets the Content-Type header.- Parameters:
type- the content type string
getContentType
Gets the Content-Type header value.- Returns:
- the content type, or empty string if not set
setContentLanguage
Sets the Content-Language header.- Parameters:
code- the language code
getContentLanguage
Gets the Content-Language header value.- Returns:
- the content language, or empty string if not set
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
Sets the Connection header.- Parameters:
value- the connection directive
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 positionlastPos- the last byte positionlength- 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
Set the Cache-Control header.- Parameters:
directive- the cache directive
setCacheControl
Set the Cache-Control header with a directive and value.- Parameters:
directive- the cache directivevalue- 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
setServer
getServer
setHost
Set the Host header.- Parameters:
host- the host nameport- the port number
getHost
setDate
Set the Date header.- Parameters:
cal- the calendar to set as the date
getDate
hasTransferEncoding
public boolean hasTransferEncoding()Check if the Transfer-Encoding header is set.- Returns:
- true if Transfer-Encoding is present
setTransferEncoding
Set the Transfer-Encoding header.- Parameters:
value- the transfer encoding value
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