-
Notifications
You must be signed in to change notification settings - Fork 671
HttpServerRequest supports query parameters. #2696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jchenga
wants to merge
1
commit into
reactor:main
Choose a base branch
from
jchenga:68-query-parameters-in-request
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
203 changes: 203 additions & 0 deletions
203
reactor-netty-http/src/main/java/reactor/netty/http/QueryStringDecoder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
/* | ||
* Copyright (c) 2012-2023 VMware, Inc. or its affiliates, All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package reactor.netty.http; | ||
|
||
import static io.netty.util.internal.StringUtil.EMPTY_STRING; | ||
import static io.netty.util.internal.StringUtil.SPACE; | ||
import static io.netty.util.internal.StringUtil.decodeHexByte; | ||
|
||
import io.netty.handler.codec.http.HttpConstants; | ||
import io.netty.util.internal.PlatformDependent; | ||
|
||
import java.nio.charset.Charset; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Provides utility methods to split an HTTP query string into key-value parameter pairs. | ||
* <pre> | ||
* {@link Map} parameters = {@link QueryStringDecoder}.decodeParams("/hello?recipient=world&x=1;y=2"); | ||
* assert parameters.get("recipient").get(0).equals("world"); | ||
* assert parameters.get.get("x").get(0).equals("1"); | ||
* assert parameters.get.get("y").get(0).equals("2"); | ||
* </pre> | ||
* | ||
* | ||
* <h3>HashDOS vulnerability fix</h3> | ||
* | ||
* As a workaround to the <a href="https://netty.io/s/hashdos">HashDOS</a> vulnerability, the decoder | ||
* limits the maximum number of decoded key-value parameter pairs, up to {@literal 1024} by | ||
* default. you can configure it when you construct the decoder by passing an additional | ||
* integer parameter. | ||
* | ||
*/ | ||
public class QueryStringDecoder { | ||
|
||
private static final int DEFAULT_MAX_PARAMS = 1024; | ||
|
||
public static Map<String, List<String>> decodeParams(final String uri) { | ||
|
||
return decodeParams(uri, HttpConstants.DEFAULT_CHARSET, | ||
DEFAULT_MAX_PARAMS, true); | ||
} | ||
|
||
public static Map<String, List<String>> decodeParams(final String uri, final boolean semiColonIsNormalChar) { | ||
|
||
return decodeParams(uri, HttpConstants.DEFAULT_CHARSET, | ||
DEFAULT_MAX_PARAMS, semiColonIsNormalChar); | ||
} | ||
|
||
public static Map<String, List<String>> decodeParams(final String uri, final Charset charset, final int maxParams, final boolean semicolonIsNormalChar) { | ||
Objects.requireNonNull(uri, "uri"); | ||
Objects.requireNonNull(charset, "charset"); | ||
if (maxParams < 1) { | ||
throw new IllegalArgumentException("maxParams must be positive"); | ||
} | ||
|
||
int from = findPathEndIndex(uri); | ||
return decodeParams(uri, from, charset, | ||
maxParams, semicolonIsNormalChar); | ||
} | ||
|
||
private static Map<String, List<String>> decodeParams(String s, int from, Charset charset, int paramsLimit, | ||
boolean semicolonIsNormalChar) { | ||
int len = s.length(); | ||
if (from >= len) { | ||
return Collections.emptyMap(); | ||
} | ||
if (s.charAt(from) == '?') { | ||
from++; | ||
} | ||
Map<String, List<String>> params = new LinkedHashMap<String, List<String>>(); | ||
int nameStart = from; | ||
int valueStart = -1; | ||
int i; | ||
loop: | ||
for (i = from; i < len; i++) { | ||
switch (s.charAt(i)) { | ||
case '=': | ||
if (nameStart == i) { | ||
nameStart = i + 1; | ||
} | ||
else if (valueStart < nameStart) { | ||
valueStart = i + 1; | ||
} | ||
break; | ||
case ';': | ||
if (semicolonIsNormalChar) { | ||
continue; | ||
} | ||
// fall-through | ||
case '&': | ||
if (addParam(s, nameStart, valueStart, i, params, charset)) { | ||
paramsLimit--; | ||
if (paramsLimit == 0) { | ||
return params; | ||
} | ||
} | ||
nameStart = i + 1; | ||
break; | ||
case '#': | ||
break loop; | ||
default: | ||
// continue | ||
} | ||
} | ||
addParam(s, nameStart, valueStart, i, params, charset); | ||
return params; | ||
} | ||
|
||
private static boolean addParam(String s, int nameStart, int valueStart, int valueEnd, | ||
Map<String, List<String>> params, Charset charset) { | ||
if (nameStart >= valueEnd) { | ||
return false; | ||
} | ||
if (valueStart <= nameStart) { | ||
valueStart = valueEnd + 1; | ||
} | ||
String name = decodeComponent(s, nameStart, valueStart - 1, charset, false); | ||
String value = decodeComponent(s, valueStart, valueEnd, charset, false); | ||
List<String> values = params.get(name); | ||
if (values == null) { | ||
values = new ArrayList<String>(1); // Often there's only 1 value. | ||
params.put(name, values); | ||
} | ||
values.add(value); | ||
return true; | ||
} | ||
|
||
private static String decodeComponent(String s, int from, int toExcluded, Charset charset, boolean isPath) { | ||
int len = toExcluded - from; | ||
if (len <= 0) { | ||
return EMPTY_STRING; | ||
} | ||
int firstEscaped = -1; | ||
for (int i = from; i < toExcluded; i++) { | ||
char c = s.charAt(i); | ||
if (c == '%' || c == '+' && !isPath) { | ||
firstEscaped = i; | ||
break; | ||
} | ||
} | ||
if (firstEscaped == -1) { | ||
return s.substring(from, toExcluded); | ||
} | ||
|
||
// Each encoded byte takes 3 characters (e.g. "%20") | ||
int decodedCapacity = (toExcluded - firstEscaped) / 3; | ||
byte[] buf = PlatformDependent.allocateUninitializedArray(decodedCapacity); | ||
int bufIdx; | ||
|
||
StringBuilder strBuf = new StringBuilder(len); | ||
strBuf.append(s, from, firstEscaped); | ||
|
||
for (int i = firstEscaped; i < toExcluded; i++) { | ||
char c = s.charAt(i); | ||
if (c != '%') { | ||
strBuf.append(c != '+' || isPath ? c : SPACE); | ||
continue; | ||
} | ||
|
||
bufIdx = 0; | ||
do { | ||
if (i + 3 > toExcluded) { | ||
throw new IllegalArgumentException("unterminated escape sequence at index " + i + " of: " + s); | ||
} | ||
buf[bufIdx++] = decodeHexByte(s, i + 1); | ||
i += 3; | ||
} while (i < toExcluded && s.charAt(i) == '%'); | ||
i--; | ||
|
||
strBuf.append(new String(buf, 0, bufIdx, charset)); | ||
} | ||
return strBuf.toString(); | ||
} | ||
|
||
private static int findPathEndIndex(String uri) { | ||
int len = uri.length(); | ||
for (int i = 0; i < len; i++) { | ||
char c = uri.charAt(i); | ||
if (c == '?' || c == '#') { | ||
return i; | ||
} | ||
} | ||
return len; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.