Package eu.bandm.tools.util.http
Class HttpHeader
java.lang.Object
eu.bandm.tools.util.http.HttpHeader
Consumes input from an Inputstream and stores it in
key/value pairs, as long as no empty line is found.
Typical usage pattern is
new HttpHeader().read(socket.getInputStream()).
After consuming the empty line, the InputStream is left for further reading.
The first line is split into the three pseudo-header-parameters:
method, uri, and httpVersion.
The code follows the specification in rfc 822.
Additionally this class provides simple uri decoding functions which are restricted as they do not allow the encoding of syntactically relevant characters, but sufficient for our current applications.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final char
One single blank characterprotected char
The global buffer.static final char
evidentstatic final char
evidentstatic final String
static final char
evidentstatic final char
evidentstatic final char
evidentprotected InputStream
Source to read from.static final String
Explicit key which can appear in a http header.static final String
Explicit key which can appear in a http header.static final String
Explicit key which can appear in a http header.static final String
Pseudo-key for storing the meta information "http version" from the http header line.static final String
Pseudo-key for storing the meta information "method" from the http header line.static final String
Pseudo-key for storing the meta information "uri" from the http header line.The decoded header information as key/value pairs.static final char
evidentstatic final String
evidentstatic final String
evidentstatic final String
evidentstatic final String
evidentstatic final String
evidentstatic final String
evidentstatic final String
evidentstatic final String
evidentstatic final String
Own separator for concatenating the values, used whenever one and the same key is found repeatedly.static final char
evidentstatic final char
evidentstatic final char
evidentstatic final String
Standard status line sent by a Http server when a request has failed.static final String
Standard status line sent by a Http server when a request has been fulfilled.static final char
evidentprotected String
Uri extracted from the header. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected String
afterFirstOrEmpty
(String s, char c) Return the suffix of s following the first occuring character c, exclusively, or the total string if no c is contained.protected String
afterLastOrAll
(String s, char c) Return the suffix of s following the last occuring character c, exclusively, or the total string if no c is contained.protected String
afterLastOrEmpty
(String s, char c) Return the suffix of s following the last occuring character c, exclusively, or an empty string if no c is contained.protected String
afterOrAlternative
(String s, String alt, int i) Return the suffix of s if i>=0, else the alternative "alt".protected String
beforeFirstOrAll
(String s, char c) Return the prefix of s up to the first occuring character c, exclusively, or the total string if no c is contained.protected String
beforeLastOrAll
(String s, char c) Return the prefix of s up to the last occuring character c, exclusively, or the total string if no c is contained.protected String
beforeOrAlternative
(String s, String alt, int i) Return the prefix of s if i>=0, else the alternative "alt".extractCookie
(String name) Return the value which is assigned to the given key in the set of all cookies.Return the value of a certain key from the html header, null in case there is no entry.int
Returns the value of the parameter with nameKEY_contentLength
.static boolean
isBlank
(char c) Returns whether the character is a white space or a tabulator.void
read
(InputStream in) Decode the http header, store its result inkeyValue
, and leave the InputStream in the state for further reading.protected void
readC()
Aux method for reading one character.protected String
Reads a maximal sequence of non-blanks/non-CRs and stores it with the given key in .toString()
String representation including all key/value pairs formkeyValue
.Return the fragment part (= location information = part starting with hash mark) maybe including paramters from the uri.Return the last file name of the directory path of the uri.Return the last file name of the directory path of the uri without its suffix, or completely, if it does not contain a dot.Return the suffix of the last file name of the directory path of the uri, or empty string, if there is no dot.Strip the fragment part (= location information = part starting with hash mark) from the uri.Strip the fragment part and all parameters (part starting with "?") from the uri.Return only the parameters part (= part starting with "?") of the uri, or empty string.Returns only the (local/file system)) path component of an uri.Return the complete directory path from the uri, but without the lowest suffix, if any.
-
Field Details
-
KEY_method
Pseudo-key for storing the meta information "method" from the http header line.- See Also:
-
KEY_uri
Pseudo-key for storing the meta information "uri" from the http header line. The value will be the complete uri, maybe with fragment id and parameters.- See Also:
-
KEY_httpVersion
Pseudo-key for storing the meta information "http version" from the http header line.- See Also:
-
KEY_contentLength
Explicit key which can appear in a http header.- See Also:
-
KEY_contentType
Explicit key which can appear in a http header.- See Also:
-
KEY_allcookies
Explicit key which can appear in a http header.- See Also:
-
LF
public static final char LFevident- See Also:
-
CR
public static final char CRevident- See Also:
-
colon
public static final char colonevident- See Also:
-
BL
public static final char BLOne single blank character- See Also:
-
TAB
public static final char TABevident- See Also:
-
MULTISEP
Own separator for concatenating the values, used whenever one and the same key is found repeatedly.- See Also:
-
QUEST
public static final char QUESTevident- See Also:
-
HASH
public static final char HASHevident- See Also:
-
SLASH
public static final char SLASHevident- See Also:
-
DOT
public static final char DOTevident- See Also:
-
EQUAL
public static final char EQUALevident- See Also:
-
SEMI
public static final char SEMIevident- See Also:
-
method_OPTIONS
evident- See Also:
-
method_GET
evident- See Also:
-
method_HEAD
evident- See Also:
-
method_POST
evident- See Also:
-
method_PUT
evident- See Also:
-
method_DELETE
evident- See Also:
-
method_TRACE
evident- See Also:
-
method_CONNECT
evident- See Also:
-
statusline_OK
Standard status line sent by a Http server when a request has been fulfilled.- See Also:
-
statusline_404
Standard status line sent by a Http server when a request has failed.- See Also:
-
CRLF
- See Also:
-
c
protected char cThe global buffer. Communcation channel betweenreadC()
andread(java.io.InputStream)
. -
in
Source to read from. Set byread(java.io.InputStream)
. -
uri
Uri extracted from the header. -
keyValue
The decoded header information as key/value pairs. Most keys are sent explicitly by the server; meta information is encoded by the meta-keysKEY_method
,KEY_uri
, andKEY_httpVersion
.
-
-
Constructor Details
-
HttpHeader
public HttpHeader()
-
-
Method Details
-
isBlank
public static boolean isBlank(char c) Returns whether the character is a white space or a tabulator. -
toString
String representation including all key/value pairs formkeyValue
. -
get
Return the value of a certain key from the html header, null in case there is no entry. -
getContentLength
Returns the value of the parameter with nameKEY_contentLength
. May only be called if such is present. ThrowsIOException
if not found in the keys, andNumberFormatException
if the string value is not a positive (or zero) integer.- Throws:
IOException
-
beforeOrAlternative
Return the prefix of s if i>=0, else the alternative "alt". -
afterOrAlternative
Return the suffix of s if i>=0, else the alternative "alt". -
beforeFirstOrAll
Return the prefix of s up to the first occuring character c, exclusively, or the total string if no c is contained. -
afterFirstOrEmpty
Return the suffix of s following the first occuring character c, exclusively, or the total string if no c is contained. -
beforeLastOrAll
Return the prefix of s up to the last occuring character c, exclusively, or the total string if no c is contained. -
afterLastOrAll
Return the suffix of s following the last occuring character c, exclusively, or the total string if no c is contained. -
afterLastOrEmpty
Return the suffix of s following the last occuring character c, exclusively, or an empty string if no c is contained. -
uriNoFragment
Strip the fragment part (= location information = part starting with hash mark) from the uri. -
uriNoParametersNorFragment
Strip the fragment part and all parameters (part starting with "?") from the uri. -
uriFragment
Return the fragment part (= location information = part starting with hash mark) maybe including paramters from the uri. If no hash mark, then the empty string. -
uriParameters
Return only the parameters part (= part starting with "?") of the uri, or empty string. -
uriPathOnly
Returns only the (local/file system)) path component of an uri. Assume a server identification must be thrown away ONLY when the string value starts with. "http://a.b.c./
". If not, uri must start with "/" or "*".- Throws:
IOException
-
uriLowestFilename
Return the last file name of the directory path of the uri.- Throws:
IOException
-
uriLowestSuffix
Return the suffix of the last file name of the directory path of the uri, or empty string, if there is no dot.- Throws:
IOException
-
uriLowestFilenameNoSuffix
Return the last file name of the directory path of the uri without its suffix, or completely, if it does not contain a dot.- Throws:
IOException
-
uriPathOnlyNoSuffix
Return the complete directory path from the uri, but without the lowest suffix, if any.- Throws:
IOException
-
extractCookie
Return the value which is assigned to the given key in the set of all cookies. Cookies are separated by semicolon ";". Assumes that cookies come in simple form, no attributes, paths etc. are decoded. Lead-in for cookie parameter isKEY_allcookies
. -
readC
Aux method for reading one character. The read character is stored globally inc
. Any CR must be followed by a LF, and only the CR is stored.- Throws:
IOException
-
readToken
Reads a maximal sequence of non-blanks/non-CRs and stores it with the given key in .- Throws:
IOException
- if the sequence is empty
-
read
Decode the http header, store its result inkeyValue
, and leave the InputStream in the state for further reading. A http REQUEST header looks likePOST http://server.com/dir/dir/dir/file.html#ask?a=b?c=d HTTP/1.0 param1:value1« param2:« param2:a very long value« extending over more than« one line« « DATA ---
A http STATUS header (in a server response) has a different start-line:HTTP/1.1 440 not found
i.e. version, status code, explanation text for status code.(See https://datatracker.ietf.org/doc/html/rfc822#section-3.1 for "folding"/multiple line values).
(This method gets the next character read by
readC()
via the bufferc
.)- Throws:
IOException
-