Skip to content

Commit d4b3aba

Browse files
authored
Merge pull request #1698 from fl4via/UNDERTOW-2519
[UNDERTOW-2519] At ProxyHandler append the non-decoded query string to the request
2 parents 56ca730 + da8c370 commit d4b3aba

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

core/src/main/java/io/undertow/server/HttpServerExchange.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ public final class HttpServerExchange extends AbstractAttachable {
211211
* the query string
212212
*/
213213
private String queryString = "";
214+
/**
215+
* the non-decoded query string. Set only when query string goes through decoding
216+
*/
217+
private String nonDecodedQueryString = null;
214218

215219
private int requestWrapperCount = 0;
216220
private ConduitWrapper<StreamSourceConduit>[] requestWrappers; //we don't allocate these by default, as for get requests they are not used
@@ -466,6 +470,7 @@ public String getRequestId() {
466470
* Examples:
467471
* GET http://localhost:8080/myFile.jsf?foo=bar HTTP/1.1 -&gt; 'http://localhost:8080/myFile.jsf'
468472
* POST /my+File.jsf?foo=bar HTTP/1.1 -&gt; '/my+File.jsf'
473+
* For the query string, see {@link #getQueryString} and {@link #getNonDecodedQueryString} .
469474
*/
470475
public String getRequestURI() {
471476
return requestURI;
@@ -584,15 +589,45 @@ public String getQueryString() {
584589
}
585590

586591
/**
587-
* Set query string. Leading ? char will be removed automatically.
592+
* Set query string. Leading {@code '?'} char will be removed automatically.
593+
*
594+
* @return this http server exchange
588595
*/
589596
public HttpServerExchange setQueryString(final String queryString) {
590597
// Clean leading ?
591598
if( queryString.length() > 0 && queryString.charAt(0) == '?' ) {
592-
this.queryString = queryString.substring(1);
593-
} else {
594-
this.queryString = queryString;
595-
}
599+
this.queryString = queryString.substring(1);
600+
} else {
601+
this.queryString = queryString;
602+
}
603+
return this;
604+
}
605+
606+
/**
607+
* Returns the query string as originally contained in the request, without any decoding.
608+
* The returned string does not contain the leading {@code '?'} char.
609+
*
610+
* @return The request query string, without the leading {@code '?'}, non-decoded.
611+
*/
612+
public String getNonDecodedQueryString() {
613+
return this.nonDecodedQueryString == null? this.queryString: this.nonDecodedQueryString;
614+
}
615+
616+
/**
617+
* Sets the non-decoded query string. Leading {@code '?'} char will be removed automatically.<p>
618+
* Must be invoked only if the {@link #getQueryString() query string} has gone through decoding. In such case, we expect
619+
* that both forms of the query string will be set in the exchange: {@link #setQueryString decoded} and non-decoded.
620+
*
621+
* @param nonDecodedQueryString the query string as originally contained in the request, without any decoding
622+
* @return this http server exchange
623+
*/
624+
public HttpServerExchange setNonDecodedQueryString(String nonDecodedQueryString) {
625+
// Clean leading ?
626+
if( nonDecodedQueryString.length() > 0 && nonDecodedQueryString.charAt(0) == '?' ) {
627+
this.nonDecodedQueryString = nonDecodedQueryString.substring(1);
628+
} else {
629+
this.nonDecodedQueryString = nonDecodedQueryString;
630+
}
596631
return this;
597632
}
598633

core/src/main/java/io/undertow/server/handlers/proxy/ProxyHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public void run() {
449449
}
450450
requestURI.append(targetURI);
451451

452-
String qs = exchange.getQueryString();
452+
String qs = exchange.getNonDecodedQueryString();
453453
if (qs != null && !qs.isEmpty()) {
454454
requestURI.append('?');
455455
requestURI.append(qs);

core/src/main/java/io/undertow/server/protocol/http/HttpRequestParser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ final void handleQueryParameters(ByteBuffer buffer, ParseState state, HttpServer
568568
if (next == ' ' || next == '\t') {
569569
String queryString = stringBuilder.toString();
570570
if(urlDecodeRequired && this.allowUnescapedCharactersInUrl) {
571+
exchange.setNonDecodedQueryString(queryString);
571572
queryString = decode(queryString, urlDecodeRequired, state, slashDecodingFlag, false);
572573
}
573574
exchange.setQueryString(queryString);

0 commit comments

Comments
 (0)