Skip to content

Commit

Permalink
HttpServerRequest supports query parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
jchenga committed Feb 15, 2023
1 parent b1b8836 commit 1e693a9
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -59,6 +60,7 @@
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.http.QueryStringDecoder;
import io.netty.handler.codec.http.TooLongHttpHeaderException;
import io.netty.handler.codec.http.TooLongHttpLineException;
import io.netty.handler.codec.http.cookie.Cookie;
Expand Down Expand Up @@ -121,6 +123,7 @@ class HttpServerOperations extends HttpOperations<HttpServerRequest, HttpServerR
final String scheme;
final ZonedDateTime timestamp;

Map<String, List<String>> queryParamsMap;
BiPredicate<HttpServerRequest, HttpServerResponse> compressionPredicate;
Function<? super String, Map<String, String>> paramsResolver;
String path;
Expand Down Expand Up @@ -459,6 +462,19 @@ public HttpHeaders requestHeaders() {
throw new IllegalStateException("request not parsed");
}

@Override
public Map<String, List<String>> queryParams() {
if (queryParamsMap == null) {
queryParamsMap = Collections.unmodifiableMap(parseQueryParams(this.nettyRequest));
}
return queryParamsMap;
}

private Map<String, List<String>> parseQueryParams(HttpRequest request) {
QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
return decoder.parameters();
}

@Override
public String scheme() {
if (connectionInfo != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.net.InetSocketAddress;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -143,6 +144,13 @@ default Flux<HttpContent> receiveContent() {
*/
HttpHeaders requestHeaders();

/**
* return parsed and decoded query parameter name value pairs
*
* @return query parameters {@link Map&lt;String,List&lt;String&gt;&gt;}
*/
Map<String, List<String>> queryParams();

/**
* Returns the inbound protocol and version.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2689,6 +2689,58 @@ void testUseComparatorOrderRoutes() {
.verifyComplete();
}

@Test
void testRouteQuery() {
HttpServerRoutes serverRoutes = HttpServerRoutes.newRoutes()
.get("/yes/value",
(request, response) -> {
StringBuilder sbuf = new StringBuilder();
request.queryParams().forEach((key, list) -> {

for(String value : list) {
sbuf.append(key);
sbuf.append('=');
sbuf.append(value);
sbuf.append('&');
}
sbuf.deleteCharAt(sbuf.length()-1);
});
return response.sendString(Mono.just("/yes/value?" + sbuf));
}).route(r -> true, (req, resp) -> {
return resp.sendString(Mono.just("/default"));
});

try {
disposableServer = HttpServer
.create()
.handle(serverRoutes)
.bindNow();

// verify HttpServerRequest has query parameters as the incoming request has query parameters
StepVerifier
.create(createClient(disposableServer.port())
.get()
.uri("/yes/value?id=a&id=b")
.responseSingle((response, mono) -> mono.asString()))
.expectNext("/yes/value?id=a&id=b")
.verifyComplete();

// verify HttpServerRequest does not have query parameters if the http request does not have query
// parameters
StepVerifier
.create(createClient(disposableServer.port())
.get()
.uri("/yes/value")
.responseSingle((response, mono) -> mono.asString()))
.expectNext("/yes/value?")
.verifyComplete();
} finally {
if (disposableServer != null) {
disposableServer.disposeNow();
}
}
}

@Test
void testOverrideRouteOrder() {
HttpServerRoutes serverRoutes = HttpServerRoutes.newRoutes()
Expand Down

0 comments on commit 1e693a9

Please sign in to comment.