Class QueryStringDecoder
java.lang.Object
io.netty.handler.codec.http.QueryStringDecoder
public class QueryStringDecoder
extends java.lang.Object
Splits an HTTP query string into a path string and key-value parameter pairs.
This decoder is for one time use only. Create a new instance for each URI:
This decoder can also decode the content of an HTTP POST request whose content type is application/x-www-form-urlencoded:QueryStringDecoderdecoder = newQueryStringDecoder("/hello?recipient=worldinvalid input: '&x'=1;y=2"); assert decoder.path().equals("/hello"); assert decoder.parameters().get("recipient").get(0).equals("world"); assert decoder.parameters().get("x").get(0).equals("1"); assert decoder.parameters().get("y").get(0).equals("2");
QueryStringDecoderdecoder = newQueryStringDecoder("recipient=worldinvalid input: '&x'=1;y=2", false); ...
HashDOS vulnerability fix
As a workaround to the HashDOS vulnerability, the decoder limits the maximum number of decoded key-value parameter pairs, up to 1024 by default, and you can configure it when you construct the decoder by passing an additional integer parameter.- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final java.nio.charset.Charsetprivate static final intprivate final booleanprivate final intprivate java.util.Map<java.lang.String, java.util.List<java.lang.String>> private java.lang.Stringprivate intprivate final booleanprivate final java.lang.String -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateQueryStringDecoder(QueryStringDecoder.Builder builder, java.lang.String uri) privateQueryStringDecoder(QueryStringDecoder.Builder builder, java.net.URI uri) QueryStringDecoder(java.lang.String uri) Creates a new decoder that decodes the specified URI.QueryStringDecoder(java.lang.String uri, boolean hasPath) Creates a new decoder that decodes the specified URI encoded in the specified charset.QueryStringDecoder(java.lang.String uri, java.nio.charset.Charset charset) Creates a new decoder that decodes the specified URI encoded in the specified charset.QueryStringDecoder(java.lang.String uri, java.nio.charset.Charset charset, boolean hasPath) Creates a new decoder that decodes the specified URI encoded in the specified charset.QueryStringDecoder(java.lang.String uri, java.nio.charset.Charset charset, boolean hasPath, int maxParams) Creates a new decoder that decodes the specified URI encoded in the specified charset.QueryStringDecoder(java.lang.String uri, java.nio.charset.Charset charset, boolean hasPath, int maxParams, boolean semicolonIsNormalChar) Creates a new decoder that decodes the specified URI encoded in the specified charset.QueryStringDecoder(java.net.URI uri) Creates a new decoder that decodes the specified URI.QueryStringDecoder(java.net.URI uri, java.nio.charset.Charset charset) Creates a new decoder that decodes the specified URI encoded in the specified charset.QueryStringDecoder(java.net.URI uri, java.nio.charset.Charset charset, int maxParams) Creates a new decoder that decodes the specified URI encoded in the specified charset.QueryStringDecoder(java.net.URI uri, java.nio.charset.Charset charset, int maxParams, boolean semicolonIsNormalChar) Creates a new decoder that decodes the specified URI encoded in the specified charset. -
Method Summary
Modifier and TypeMethodDescriptionprivate booleanaddParam(java.lang.String s, int nameStart, int valueStart, int valueEnd, java.util.Map<java.lang.String, java.util.List<java.lang.String>> params, java.nio.charset.Charset charset) static QueryStringDecoder.Builderbuilder()static java.lang.StringdecodeComponent(java.lang.String s) Decodes a bit of a URL encoded by a browser.private static java.lang.StringdecodeComponent(java.lang.String s, int from, int toExcluded, java.nio.charset.Charset charset, boolean plusToSpace) static java.lang.StringdecodeComponent(java.lang.String s, java.nio.charset.Charset charset) Decodes a bit of a URL encoded by a browser.private java.util.Map<java.lang.String, java.util.List<java.lang.String>> decodeParams(java.lang.String s, int from, java.nio.charset.Charset charset, int paramsLimit) private static intfindPathEndIndex(java.lang.String uri) java.util.Map<java.lang.String, java.util.List<java.lang.String>> Returns the decoded key-value parameter pairs of the URI.java.lang.Stringpath()Returns the decoded path string of the URI.private intjava.lang.StringrawPath()Returns the raw path string of the URI.java.lang.StringrawQuery()Returns raw query string of the URI.java.lang.StringtoString()java.lang.Stringuri()Returns the uri used to initialize thisQueryStringDecoder.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Field Details
-
DEFAULT_MAX_PARAMS
private static final int DEFAULT_MAX_PARAMS- See Also:
-
charset
private final java.nio.charset.Charset charset -
uri
private final java.lang.String uri -
maxParams
private final int maxParams -
semicolonIsNormalChar
private final boolean semicolonIsNormalChar -
htmlQueryDecoding
private final boolean htmlQueryDecoding -
pathEndIdx
private int pathEndIdx -
path
private java.lang.String path -
params
private java.util.Map<java.lang.String, java.util.List<java.lang.String>> params
-
-
Constructor Details
-
QueryStringDecoder
public QueryStringDecoder(java.lang.String uri) Creates a new decoder that decodes the specified URI. The decoder will assume that the query string is encoded in UTF-8. -
QueryStringDecoder
public QueryStringDecoder(java.lang.String uri, boolean hasPath) Creates a new decoder that decodes the specified URI encoded in the specified charset. -
QueryStringDecoder
public QueryStringDecoder(java.lang.String uri, java.nio.charset.Charset charset) Creates a new decoder that decodes the specified URI encoded in the specified charset. -
QueryStringDecoder
public QueryStringDecoder(java.lang.String uri, java.nio.charset.Charset charset, boolean hasPath) Creates a new decoder that decodes the specified URI encoded in the specified charset. -
QueryStringDecoder
public QueryStringDecoder(java.lang.String uri, java.nio.charset.Charset charset, boolean hasPath, int maxParams) Creates a new decoder that decodes the specified URI encoded in the specified charset. -
QueryStringDecoder
public QueryStringDecoder(java.lang.String uri, java.nio.charset.Charset charset, boolean hasPath, int maxParams, boolean semicolonIsNormalChar) Creates a new decoder that decodes the specified URI encoded in the specified charset. -
QueryStringDecoder
public QueryStringDecoder(java.net.URI uri) Creates a new decoder that decodes the specified URI. The decoder will assume that the query string is encoded in UTF-8. -
QueryStringDecoder
public QueryStringDecoder(java.net.URI uri, java.nio.charset.Charset charset) Creates a new decoder that decodes the specified URI encoded in the specified charset. -
QueryStringDecoder
public QueryStringDecoder(java.net.URI uri, java.nio.charset.Charset charset, int maxParams) Creates a new decoder that decodes the specified URI encoded in the specified charset. -
QueryStringDecoder
public QueryStringDecoder(java.net.URI uri, java.nio.charset.Charset charset, int maxParams, boolean semicolonIsNormalChar) Creates a new decoder that decodes the specified URI encoded in the specified charset. -
QueryStringDecoder
-
QueryStringDecoder
-
-
Method Details
-
toString
public java.lang.String toString()- Overrides:
toStringin classjava.lang.Object
-
uri
public java.lang.String uri()Returns the uri used to initialize thisQueryStringDecoder. -
path
public java.lang.String path()Returns the decoded path string of the URI. -
parameters
public java.util.Map<java.lang.String, java.util.List<java.lang.String>> parameters()Returns the decoded key-value parameter pairs of the URI. -
rawPath
public java.lang.String rawPath()Returns the raw path string of the URI. -
rawQuery
public java.lang.String rawQuery()Returns raw query string of the URI. -
pathEndIdx
private int pathEndIdx() -
decodeParams
private java.util.Map<java.lang.String, java.util.List<java.lang.String>> decodeParams(java.lang.String s, int from, java.nio.charset.Charset charset, int paramsLimit) -
addParam
private boolean addParam(java.lang.String s, int nameStart, int valueStart, int valueEnd, java.util.Map<java.lang.String, java.util.List<java.lang.String>> params, java.nio.charset.Charset charset) -
decodeComponent
public static java.lang.String decodeComponent(java.lang.String s) Decodes a bit of a URL encoded by a browser.This is equivalent to calling
decodeComponent(String, Charset)with the UTF-8 charset (recommended to comply with RFC 3986, Section 2).- Parameters:
s- The string to decode (can be empty).- Returns:
- The decoded string, or
sif there's nothing to decode. If the string to decode isnull, returns an empty string. - Throws:
java.lang.IllegalArgumentException- if the string contains a malformed escape sequence.
-
decodeComponent
public static java.lang.String decodeComponent(java.lang.String s, java.nio.charset.Charset charset) Decodes a bit of a URL encoded by a browser.The string is expected to be encoded as per RFC 3986, Section 2. This is the encoding used by JavaScript functions
encodeURIandencodeURIComponent, but notescape. For example in this encoding, é (in UnicodeU+00E9or in UTF-80xC3 0xA9) is encoded as%C3%A9or%c3%a9.This is essentially equivalent to calling
URLDecoder.decode(String, String)except that it's over 2x faster and generates less garbage for the GC. Actually this function doesn't allocate any memory if there's nothing to decode, the argument itself is returned.- Parameters:
s- The string to decode (can be empty).charset- The charset to use to decode the string (should really beCharsetUtil.UTF_8.- Returns:
- The decoded string, or
sif there's nothing to decode. If the string to decode isnull, returns an empty string. - Throws:
java.lang.IllegalArgumentException- if the string contains a malformed escape sequence.
-
decodeComponent
private static java.lang.String decodeComponent(java.lang.String s, int from, int toExcluded, java.nio.charset.Charset charset, boolean plusToSpace) -
findPathEndIndex
private static int findPathEndIndex(java.lang.String uri) -
builder
-