public class HTTPPacket extends Object
| 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. |
| Modifier and Type | Method and Description |
|---|---|
void | addHeader(HTTPHeader header)Adds a header to this HTTP packet. |
void | addHeader(String name,
String value)Adds a header with the specified name and value. |
void | clearHeaders()Clears all headers from this HTTP packet. |
String | getCacheControl()Get the Cache-Control header value. |
String | getCharSet()Gets the character set from the Content-Type header. |
String | getConnection()Gets the Connection header value. |
byte[] | getContent()Gets the content of this HTTP packet. |
InputStream | getContentInputStream()Gets the content input stream. |
String | getContentLanguage()Gets the Content-Language header value. |
long | getContentLength()Gets the content length from the Content-Length header. |
long[] | getContentRange()Gets the content range values as an array. |
long | getContentRangeFirstPosition()Get the first position from the content range. |
long | getContentRangeInstanceLength()Get the instance length from the content range. |
long | getContentRangeLastPosition()Get the last position from the content range. |
String | getContentString()Gets the content of this HTTP packet as a string. |
String | getContentType()Gets the Content-Type header value. |
String | getDate()Get the Date header value. |
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)Gets the header at the specified index. |
HTTPHeader | getHeader(String name)Gets the header with the specified name, ignoring case. |
String | getHeaderString()Gets all headers as a formatted string. |
String | getHeaderValue(String name)Gets the value of a header by name. |
String | getHost()Get the Host header value. |
int | getIntegerHeaderValue(String name)Gets an integer header value by name. |
long | getLongHeaderValue(String name)Gets a long header value by name. |
int | getNHeaders()Gets the number of headers in this HTTP packet. |
String | getServer()Get the Server header value. |
String | getStringHeaderValue(String name)Gets a string header value, stripping double quotes if present. |
String | getStringHeaderValue(String name,
String startWidth,
String endWidth)Gets a string header value, stripping the specified delimiters if present. |
String | getTransferEncoding()Get the Transfer-Encoding header value. |
String | getVersion()Gets the HTTP version of this packet. |
boolean | hasConnection()Checks if the Connection header is set. |
boolean | hasContent()Checks if this HTTP packet has content. |
boolean | hasContentInputStream()Checks if a content input stream has been set. |
boolean | hasContentRange()Checks if the Content-Range or Range header is set. |
boolean | hasFirstLine()Checks if the first line of the HTTP packet has been set. |
boolean | hasHeader(String name)Checks if a header with the specified name exists. |
boolean | hasTransferEncoding()Check if the Transfer-Encoding header is set. |
void | init()Initializes the HTTP packet to empty state. |
boolean | isChunked()Check if the content is chunked. |
boolean | isCloseConnection()Checks if the Connection header is set to close. |
boolean | isKeepAliveConnection()Checks if the Connection header is set to keep-alive. |
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)Set the Cache-Control header with a max-age value. |
void | setCacheControl(String directive)Set the Cache-Control header. |
void | setCacheControl(String directive,
int value)Set the Cache-Control header with a directive and value. |
void | setConnection(String value)Sets the Connection header. |
void | setContent(byte[] data)Sets the content of this HTTP packet. |
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)Sets the content of this HTTP packet from a string. |
void | setContentInputStream(InputStream in)Sets the content input stream. |
void | setContentLanguage(String code)Sets the Content-Language header. |
void | setContentLength(long len)Sets the Content-Length header. |
void | setContentRange(long firstPos,
long lastPos,
long length)Sets the Content-Range header. |
void | setContentType(String type)Sets the Content-Type header. |
void | setDate(Calendar cal)Set the Date header. |
void | setHeader(HTTPHeader header)Sets the value of a header from an HTTP header object. |
void | setHeader(String name,
int value)Sets the value of a header from an integer. |
void | setHeader(String name,
long value)Sets the value of a header from a long. |
void | setHeader(String name,
String value)Sets the value of a header. |
void | setHost(String host,
int port)Set the Host header. |
void | setIntegerHeader(String name,
int value)Sets an integer header value. |
void | setLongHeader(String name,
long value)Sets a long header value. |
void | setServer(String name)Set the Server header. |
void | setStringHeader(String name,
String value)Sets a string header value, wrapping it with double quotes if not already wrapped. |
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. |
void | setTransferEncoding(String value)Set the Transfer-Encoding header. |
void | setVersion(String ver)Sets the HTTP version for this packet. |
public HTTPPacket()
public HTTPPacket(HTTPPacket httpPacket)
httpPacket - HTTP packet to copypublic HTTPPacket(InputStream in)
in - input stream containing the HTTP packet datapublic void addHeader(HTTPHeader header)
header - the header to addpublic void addHeader(String name, String value)
name - the header namevalue - the header valuepublic void clearHeaders()
public String getCacheControl()
public String getCharSet()
public String getConnection()
public byte[] getContent()
public InputStream getContentInputStream()
public String getContentLanguage()
public long getContentLength()
public long[] getContentRange()
public long getContentRangeFirstPosition()
public long getContentRangeInstanceLength()
public long getContentRangeLastPosition()
public String getContentString()
public String getContentType()
public String getDate()
protected String getFirstLine()
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.
protected String getFirstLineToken(int num)
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
num - the zero-based token position to retrievepublic HTTPHeader getHeader(int n)
n - the index of the header to retrievepublic HTTPHeader getHeader(String name)
name - the header namepublic String getHeaderString()
public String getHeaderValue(String name)
name - the header namepublic String getHost()
public int getIntegerHeaderValue(String name)
name - the header namepublic long getLongHeaderValue(String name)
name - the header namepublic int getNHeaders()
public String getServer()
public String getStringHeaderValue(String name)
name - the header namepublic String getStringHeaderValue(String name, String startWidth, String endWidth)
name - the header namestartWidth - the start delimiter to stripendWidth - the end delimiter to strippublic String getTransferEncoding()
public String getVersion()
public boolean hasConnection()
public boolean hasContent()
public boolean hasContentInputStream()
public boolean hasContentRange()
public boolean hasFirstLine()
public boolean hasHeader(String name)
name - the header namepublic boolean hasTransferEncoding()
public void init()
public boolean isChunked()
public boolean isCloseConnection()
public boolean isKeepAliveConnection()
public boolean read(HTTPSocket httpSock)
httpSock - socket containing the HTTP packet dataprotected void set(HTTPPacket httpPacket)
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.
httpPacket - the source HTTP packet to copy fromprotected boolean set(HTTPSocket httpSock)
This is a convenience method that extracts the input stream from the HTTP socket and parses the complete HTTP packet including content.
httpSock - the HTTP socket containing the HTTP packet dataprotected boolean set(InputStream in)
This is a convenience method that calls set(InputStream, boolean) with
onlyHeaders set to false.
in - the input stream containing HTTP packet dataprotected boolean set(InputStream in, boolean onlyHeaders)
This method reads and parses the HTTP packet including the first line, headers, and optionally the content body. It handles special cases such as:
in - the input stream containing HTTP packet dataonlyHeaders - if true, only parse headers and skip content bodypublic void setCacheControl(int value)
value - the max age in secondspublic void setCacheControl(String directive)
directive - the cache directivepublic void setCacheControl(String directive, int value)
directive - the cache directivevalue - the directive valuepublic void setConnection(String value)
value - the connection directivepublic void setContent(byte[] data)
data - content data as byte arraypublic void setContent(byte[] data,
boolean updateWithContentLength)data - content data as byte arrayupdateWithContentLength - whether to update Content-Length header automaticallypublic void setContent(String data)
data - content data as stringpublic void setContent(String data, boolean updateWithContentLength)
data - content data as stringupdateWithContentLength - whether to update Content-Length header automaticallypublic void setContentInputStream(InputStream in)
in - the input stream to use as content sourcepublic void setContentLanguage(String code)
code - the language codepublic void setContentLength(long len)
len - the content lengthpublic void setContentRange(long firstPos,
long lastPos,
long length)firstPos - the first byte positionlastPos - the last byte positionlength - the total length, or 0 for unknownpublic void setContentType(String type)
type - the content type stringpublic void setDate(Calendar cal)
cal - the calendar to set as the datepublic void setHeader(HTTPHeader header)
header - the header to setpublic void setHeader(String name, int value)
name - the header namevalue - the header value as integerpublic void setHeader(String name, long value)
name - the header namevalue - the header value as longpublic void setHeader(String name, String value)
name - the header namevalue - the header valuepublic void setHost(String host, int port)
host - the host nameport - the port numberpublic void setIntegerHeader(String name, int value)
name - the header namevalue - the header value as integerpublic void setLongHeader(String name, long value)
name - the header namevalue - the header value as longpublic void setServer(String name)
name - the server namepublic void setStringHeader(String name, String value)
name - the header namevalue - the header valuepublic void setStringHeader(String name, String value, String startWidth, String endWidth)
name - the header namevalue - the header valuestartWidth - the start delimiterendWidth - the end delimiterpublic void setTransferEncoding(String value)
value - the transfer encoding valuepublic void setVersion(String ver)
ver - HTTP version string (e.g., "1.0", "1.1")