Top Description Inners Fields Constructors Methods
org.apache.tomcat.util.http.parser

public Class HttpParser

extends Object
Class Inheritance
Imports
java.io.IOException, .Reader, org.apache.tomcat.util.res.StringManager

HTTP header value parser implementation. Parsing HTTP headers as per RFC2616 is not always as simple as it first appears. For headers that only use tokens the simple approach will normally be sufficient. However, for the other headers, while simple code meets 99.9% of cases, there are often some edge cases that make things far more complicated. The purpose of this parser is to let the parser worry about the edge cases. It provides tolerant (where safe to do so) parsing of HTTP header values assuming that wrapped header lines have already been unwrapped. (The Tomcat header processing code does the unwrapping.)

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static enum

Field Summary

Modifier and TypeField and Description
private static final int
private static final HttpParser
private final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private final boolean[]
private static final boolean[]
private final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final StringManager

Constructor Summary

AccessConstructor and Description
public
HttpParser(String relaxedPathChars, String relaxedQueryChars)

Method Summary

Modifier and TypeMethod and Description
public static boolean
public boolean
public static boolean
isAlpha(int c)

public static boolean
isControl(int c)

public static boolean
isHex(int c)

public static boolean
public static boolean
public boolean
public static boolean
isNumeric(int c)

public static boolean
isQuery(int c)

public boolean
private static boolean
isRelaxable(int c)

public static boolean
isScheme(int c)

public static boolean

Returns:

true if the string is a valid scheme, otherwise false
isScheme
(String
The string to test
s
)

Is the provided String a scheme as per RFC 3986?

public static boolean
isToken(int c)

public static boolean

Returns:

true if the string is a valid token, otherwise false
isToken
(String
The string to test
s
)

Is the provided String a token as per RFC 7230?

public static boolean
isUserInfo(int c)

pack-priv static String

Returns:

the digits if any were found, the empty string if no data was found or if data other than digits was found
readDigits
(Reader input)

pack-priv static int

Returns:

The position of ':' that separates the host from the port or -1 if it is not present
readHostDomainName
(Reader reader)

pack-priv static int

Returns:

If inIPv6 is false, the position of ':' that separates the host from the port or -1 if it is not present. If inIPv6 is true, the number of characters read
readHostIPv4
(Reader reader, boolean inIPv6)

pack-priv static int

Returns:

The position of ':' that separates the host from the port or -1 if it is not present
readHostIPv6
(Reader reader)

pack-priv static String

Returns:

the sequence of LHEX (minus any surrounding quotes) if any was found, or null if data other LHEX was found
readLhex
(Reader input)

LHEX can be read unambiguously with or without surrounding quotes so this parsing method for LHEX permits optional surrounding double quotes.

pack-priv static long

Returns:

the number if digits were found, -1 if no data was found or if data other than digits was found
readLong
(Reader input)

pack-priv static String

Returns:

the quoted string if one was found, null if data other than a quoted string was found or null if the end of data was reached before the quoted string was terminated
readQuotedString
(Reader input, boolean returnQuoted)

pack-priv static String

Returns:

the token if one was found, null if data other than a token or quoted token was found or null if the end of data was reached before a quoted token was terminated
readQuotedToken
(Reader input)

Token can be read unambiguously with or without surrounding quotes so this parsing method for token permits optional surrounding double quotes.

pack-priv static String

Returns:

the token if one was found, the empty string if no data was available to read or null if data other than a token was found
readToken
(Reader input)

pack-priv static String
readTokenOrQuotedString(Reader input, boolean returnQuoted)

pack-priv static double
readWeight(Reader input, char delimiter)

private void
relax(boolean[] flags, String relaxedChars)

pack-priv static SkipResult
skipConstant(Reader input, String constant)

pack-priv static int
skipLws(Reader input)

pack-priv static SkipResult
skipUntil(Reader input, int c, char target)

Skips all characters until EOF or the specified target is found.

public static String
unquote(String input)

pack-priv static int
validatePort(Reader reader, int colonPosition)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ARRAY_SIZEback to summary
private static final int ARRAY_SIZE
DEFAULTback to summary
private static final HttpParser DEFAULT
IS_ABSOLUTEPATH_RELAXEDback to summary
private final boolean[] IS_ABSOLUTEPATH_RELAXED
IS_ALPHAback to summary
private static final boolean[] IS_ALPHA
IS_CONTROLback to summary
private static final boolean[] IS_CONTROL
IS_HEXback to summary
private static final boolean[] IS_HEX
IS_HTTP_PROTOCOLback to summary
private static final boolean[] IS_HTTP_PROTOCOL
IS_NOT_REQUEST_TARGETback to summary
private final boolean[] IS_NOT_REQUEST_TARGET
IS_NUMERICback to summary
private static final boolean[] IS_NUMERIC
IS_QUERY_RELAXEDback to summary
private final boolean[] IS_QUERY_RELAXED
IS_RELAXABLEback to summary
private static final boolean[] IS_RELAXABLE
IS_SCHEMEback to summary
private static final boolean[] IS_SCHEME
IS_SEPARATORback to summary
private static final boolean[] IS_SEPARATOR
IS_SUBDELIMback to summary
private static final boolean[] IS_SUBDELIM
IS_TOKENback to summary
private static final boolean[] IS_TOKEN
IS_UNRESERVEDback to summary
private static final boolean[] IS_UNRESERVED
IS_USERINFOback to summary
private static final boolean[] IS_USERINFO
smback to summary
private static final StringManager sm

Constructor Detail

HttpParserback to summary
public HttpParser(String relaxedPathChars, String relaxedQueryChars)

Method Detail

isAbsolutePathback to summary
public static boolean isAbsolutePath(int c)
isAbsolutePathRelaxedback to summary
public boolean isAbsolutePathRelaxed(int c)
isAlphaback to summary
public static boolean isAlpha(int c)
isControlback to summary
public static boolean isControl(int c)
isHexback to summary
public static boolean isHex(int c)
isHttpProtocolback to summary
public static boolean isHttpProtocol(int c)
isNotRequestTargetback to summary
public static boolean isNotRequestTarget(int c)
isNotRequestTargetRelaxedback to summary
public boolean isNotRequestTargetRelaxed(int c)
isNumericback to summary
public static boolean isNumeric(int c)
isQueryback to summary
public static boolean isQuery(int c)
isQueryRelaxedback to summary
public boolean isQueryRelaxed(int c)
isRelaxableback to summary
private static boolean isRelaxable(int c)
isSchemeback to summary
public static boolean isScheme(int c)
isSchemeback to summary
public static boolean isScheme(String s)

Is the provided String a scheme as per RFC 3986?
Note: scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
Since a scheme requires at least 1 ALPHA, null and the empty string ("") are not considered to be valid tokens.

Parameters
s:String

The string to test

Returns:boolean

true if the string is a valid scheme, otherwise false

isTokenback to summary
public static boolean isToken(int c)
isTokenback to summary
public static boolean isToken(String s)

Is the provided String a token as per RFC 7230?
Note: token = 1 * tchar (RFC 7230)
Since a token requires at least 1 tchar, null and the empty string ("") are not considered to be valid tokens.

Parameters
s:String

The string to test

Returns:boolean

true if the string is a valid token, otherwise false

isUserInfoback to summary
public static boolean isUserInfo(int c)
readDigitsback to summary
pack-priv static String readDigits(Reader input) throws IOException
Returns:String

the digits if any were found, the empty string if no data was found or if data other than digits was found

readHostDomainNameback to summary
pack-priv static int readHostDomainName(Reader reader) throws IOException
Returns:int

The position of ':' that separates the host from the port or -1 if it is not present

readHostIPv4back to summary
pack-priv static int readHostIPv4(Reader reader, boolean inIPv6) throws IOException
Returns:int

If inIPv6 is false, the position of ':' that separates the host from the port or -1 if it is not present. If inIPv6 is true, the number of characters read

readHostIPv6back to summary
pack-priv static int readHostIPv6(Reader reader) throws IOException
Returns:int

The position of ':' that separates the host from the port or -1 if it is not present

readLhexback to summary
pack-priv static String readLhex(Reader input) throws IOException

LHEX can be read unambiguously with or without surrounding quotes so this parsing method for LHEX permits optional surrounding double quotes. Some buggy clients (libwww-perl for DIGEST auth) are known to send quoted LHEX when the specification requires just LHEX.

LHEX are, literally, lower-case hexadecimal digits. This implementation allows for upper-case digits as well, converting the returned value to lower-case.

Returns:String

the sequence of LHEX (minus any surrounding quotes) if any was found, or null if data other LHEX was found

readLongback to summary
pack-priv static long readLong(Reader input) throws IOException
Returns:long

the number if digits were found, -1 if no data was found or if data other than digits was found

readQuotedStringback to summary
pack-priv static String readQuotedString(Reader input, boolean returnQuoted) throws IOException
Returns:String

the quoted string if one was found, null if data other than a quoted string was found or null if the end of data was reached before the quoted string was terminated

readQuotedTokenback to summary
pack-priv static String readQuotedToken(Reader input) throws IOException

Token can be read unambiguously with or without surrounding quotes so this parsing method for token permits optional surrounding double quotes. This is not defined in any RFC. It is a special case to handle data from buggy clients (known buggy clients for DIGEST auth include Microsoft IE 8 & 9, Apple Safari for OSX and iOS) that add quotes to values that should be tokens.

Returns:String

the token if one was found, null if data other than a token or quoted token was found or null if the end of data was reached before a quoted token was terminated

readTokenback to summary
pack-priv static String readToken(Reader input) throws IOException
Returns:String

the token if one was found, the empty string if no data was available to read or null if data other than a token was found

readTokenOrQuotedStringback to summary
pack-priv static String readTokenOrQuotedString(Reader input, boolean returnQuoted) throws IOException
readWeightback to summary
pack-priv static double readWeight(Reader input, char delimiter) throws IOException
relaxback to summary
private void relax(boolean[] flags, String relaxedChars)
skipConstantback to summary
pack-priv static SkipResult skipConstant(Reader input, String constant) throws IOException
skipLwsback to summary
pack-priv static int skipLws(Reader input) throws IOException
skipUntilback to summary
pack-priv static SkipResult skipUntil(Reader input, int c, char target) throws IOException

Skips all characters until EOF or the specified target is found. Normally used to skip invalid input until the next separator.

unquoteback to summary
public static String unquote(String input)
validatePortback to summary
pack-priv static int validatePort(Reader reader, int colonPosition) throws IOException
org.apache.tomcat.util.http.parser back to summary

private final Enum HttpParser.DomainParseState

extends Enum<HttpParser.DomainParseState>
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final boolean
private final boolean
private final boolean
public static final HttpParser.DomainParseState
public static final HttpParser.DomainParseState
public static final HttpParser.DomainParseState
private final String
public static final HttpParser.DomainParseState
private final boolean
public static final HttpParser.DomainParseState
public static final HttpParser.DomainParseState
public static final HttpParser.DomainParseState

Constructor Summary

AccessConstructor and Description
private
DomainParseState(boolean mayContinue, boolean allowsHyphen, boolean allowsPeriod, boolean allowsEnd, String errorMsg)

Method Summary

Modifier and TypeMethod and Description
public boolean
public HttpParser.DomainParseState
next(int c)

public static HttpParser.DomainParseState
public static HttpParser.DomainParseState[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

allowsEndback to summary
private final boolean allowsEnd
allowsHyphenback to summary
private final boolean allowsHyphen
allowsPeriodback to summary
private final boolean allowsPeriod
ALPHAback to summary
public static final HttpParser.DomainParseState ALPHA
COLONback to summary
public static final HttpParser.DomainParseState COLON
ENDback to summary
public static final HttpParser.DomainParseState END
errorMsgback to summary
private final String errorMsg
HYPHENback to summary
public static final HttpParser.DomainParseState HYPHEN
mayContinueback to summary
private final boolean mayContinue
NEWback to summary
public static final HttpParser.DomainParseState NEW
NUMERICback to summary
public static final HttpParser.DomainParseState NUMERIC
PERIODback to summary
public static final HttpParser.DomainParseState PERIOD

Constructor Detail

DomainParseStateback to summary
private DomainParseState(boolean mayContinue, boolean allowsHyphen, boolean allowsPeriod, boolean allowsEnd, String errorMsg)

Method Detail

mayContinueback to summary
public boolean mayContinue()
nextback to summary
public HttpParser.DomainParseState next(int c)
valueOfback to summary
public static HttpParser.DomainParseState valueOf(String name)
valuesback to summary
public static HttpParser.DomainParseState[] values()