Top Description Fields Constructors Methods
io.netty5.handler.codec.http

public Class QueryStringDecoder

extends Object
Class Inheritance
Imports
io.netty5.util.CharsetUtil, io.netty5.util.internal.PlatformDependent, java.net.URI, .URLDecoder, java.nio.charset.Charset, java.util.ArrayList, .Collections, .LinkedHashMap, .List, .Map

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:
QueryStringDecoder decoder = new QueryStringDecoder("/hello?recipient=world&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");
This decoder can also decode the content of an HTTP POST request whose content type is application/x-www-form-urlencoded:
QueryStringDecoder decoder = new QueryStringDecoder("recipient=world&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
QueryStringEncoder

Field Summary

Modifier and TypeField and Description
private final Charset
private static final int
private final int
private Map<String, List<String>>
private String
private int
private final boolean
private final String

Constructor Summary

AccessConstructor and Description
public
QueryStringDecoder(String uri)

Creates a new decoder that decodes the specified URI.

public
QueryStringDecoder(String uri, boolean hasPath)

Creates a new decoder that decodes the specified URI encoded in the specified charset.

public
QueryStringDecoder(String uri, Charset charset)

Creates a new decoder that decodes the specified URI encoded in the specified charset.

public
QueryStringDecoder(String uri, Charset charset, boolean hasPath)

Creates a new decoder that decodes the specified URI encoded in the specified charset.

public
QueryStringDecoder(String uri, Charset charset, boolean hasPath, int maxParams)

Creates a new decoder that decodes the specified URI encoded in the specified charset.

public
QueryStringDecoder(String uri, Charset charset, boolean hasPath, int maxParams, boolean semicolonIsNormalChar)

Creates a new decoder that decodes the specified URI encoded in the specified charset.

public
QueryStringDecoder(URI uri)

Creates a new decoder that decodes the specified URI.

public
QueryStringDecoder(URI uri, Charset charset)

Creates a new decoder that decodes the specified URI encoded in the specified charset.

public
QueryStringDecoder(URI uri, Charset charset, int maxParams)

Creates a new decoder that decodes the specified URI encoded in the specified charset.

public
QueryStringDecoder(URI uri, Charset charset, int maxParams, boolean semicolonIsNormalChar)

Creates a new decoder that decodes the specified URI encoded in the specified charset.

Method Summary

Modifier and TypeMethod and Description
private static boolean
addParam(String s, int nameStart, int valueStart, int valueEnd, Map<String, List<String>> params, Charset charset)

public static String

Returns:

The decoded string, or s if there's nothing to decode. If the string to decode is null, returns an empty string.
decodeComponent
(final String
The string to decode (can be empty).
s
)

Decodes a bit of a URL encoded by a browser.

public static String

Returns:

The decoded string, or s if there's nothing to decode. If the string to decode is null, returns an empty string.
decodeComponent
(final String
The string to decode (can be empty).
s
,
final Charset
The charset to use to decode the string (should really be java.nio.charset.StandardCharsets#UTF_8.
charset
)

Decodes a bit of a URL encoded by a browser.

private static String
decodeComponent(String s, int from, int toExcluded, Charset charset, boolean isPath)

private static Map<String, List<String>>
decodeParams(String s, int from, Charset charset, int paramsLimit, boolean semicolonIsNormalChar)

private static int
public Map<String, List<String>>
parameters()

Returns the decoded key-value parameter pairs of the URI.

public String
path()

Returns the decoded path string of the URI.

private int
public String
rawPath()

Returns the raw path string of the URI.

public String
rawQuery()

Returns raw query string of the URI.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public String
uri()

Returns the uri used to initialize this QueryStringDecoder.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

charsetback to summary
private final Charset charset
DEFAULT_MAX_PARAMSback to summary
private static final int DEFAULT_MAX_PARAMS
maxParamsback to summary
private final int maxParams
paramsback to summary
private Map<String, List<String>> params
pathback to summary
private String path
pathEndIdxback to summary
private int pathEndIdx
semicolonIsNormalCharback to summary
private final boolean semicolonIsNormalChar
uriback to summary
private final String uri

Constructor Detail

QueryStringDecoderback to summary
public QueryStringDecoder(String uri)

Creates a new decoder that decodes the specified URI. The decoder will assume that the query string is encoded in UTF-8.

QueryStringDecoderback to summary
public QueryStringDecoder(String uri, boolean hasPath)

Creates a new decoder that decodes the specified URI encoded in the specified charset.

QueryStringDecoderback to summary
public QueryStringDecoder(String uri, Charset charset)

Creates a new decoder that decodes the specified URI encoded in the specified charset.

QueryStringDecoderback to summary
public QueryStringDecoder(String uri, Charset charset, boolean hasPath)

Creates a new decoder that decodes the specified URI encoded in the specified charset.

QueryStringDecoderback to summary
public QueryStringDecoder(String uri, Charset charset, boolean hasPath, int maxParams)

Creates a new decoder that decodes the specified URI encoded in the specified charset.

QueryStringDecoderback to summary
public QueryStringDecoder(String uri, Charset charset, boolean hasPath, int maxParams, boolean semicolonIsNormalChar)

Creates a new decoder that decodes the specified URI encoded in the specified charset.

QueryStringDecoderback to summary
public QueryStringDecoder(URI uri)

Creates a new decoder that decodes the specified URI. The decoder will assume that the query string is encoded in UTF-8.

QueryStringDecoderback to summary
public QueryStringDecoder(URI uri, Charset charset)

Creates a new decoder that decodes the specified URI encoded in the specified charset.

QueryStringDecoderback to summary
public QueryStringDecoder(URI uri, Charset charset, int maxParams)

Creates a new decoder that decodes the specified URI encoded in the specified charset.

QueryStringDecoderback to summary
public QueryStringDecoder(URI uri, Charset charset, int maxParams, boolean semicolonIsNormalChar)

Creates a new decoder that decodes the specified URI encoded in the specified charset.

Method Detail

addParamback to summary
private static boolean addParam(String s, int nameStart, int valueStart, int valueEnd, Map<String, List<String>> params, Charset charset)
decodeComponentback to summary
public static String decodeComponent(final 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:String

The string to decode (can be empty).

Returns:String

The decoded string, or s if there's nothing to decode. If the string to decode is null, returns an empty string.

Exceptions
IllegalArgumentException:
if the string contains a malformed escape sequence.
decodeComponentback to summary
public static String decodeComponent(final String s, final 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 encodeURI and encodeURIComponent, but not escape. For example in this encoding, é (in Unicode U+00E9 or in UTF-8 0xC3 0xA9) is encoded as %C3%A9 or %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:String

The string to decode (can be empty).

charset:Charset

The charset to use to decode the string (should really be java.nio.charset.StandardCharsets#UTF_8.

Returns:String

The decoded string, or s if there's nothing to decode. If the string to decode is null, returns an empty string.

Exceptions
IllegalArgumentException:
if the string contains a malformed escape sequence.
decodeComponentback to summary
private static String decodeComponent(String s, int from, int toExcluded, Charset charset, boolean isPath)
decodeParamsback to summary
private static Map<String, List<String>> decodeParams(String s, int from, Charset charset, int paramsLimit, boolean semicolonIsNormalChar)
findPathEndIndexback to summary
private static int findPathEndIndex(String uri)
parametersback to summary
public Map<String, List<String>> parameters()

Returns the decoded key-value parameter pairs of the URI.

pathback to summary
public String path()

Returns the decoded path string of the URI.

pathEndIdxback to summary
private int pathEndIdx()
rawPathback to summary
public String rawPath()

Returns the raw path string of the URI.

rawQueryback to summary
public String rawQuery()

Returns raw query string of the URI.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override
uriback to summary
public String uri()

Returns the uri used to initialize this QueryStringDecoder.