Skip to content

Commit 41c02bd

Browse files
authored
Merge pull request #1392 from ropalka/UNDERTOW-2123
[UNDERTOW-2123] always URL decode pathInfo and servletPath as required by Servlet specification
2 parents 84d8978 + 0552d58 commit 41c02bd

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

servlet/src/main/java/io/undertow/servlet/spec/AsyncContextImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import io.undertow.servlet.handlers.ServletDebugPageHandler;
6262
import io.undertow.servlet.handlers.ServletPathMatch;
6363
import io.undertow.servlet.handlers.ServletRequestContext;
64+
import io.undertow.util.CanonicalPathUtils;
6465
import io.undertow.util.Headers;
6566
import io.undertow.util.SameThreadExecutor;
6667
import io.undertow.util.StatusCodes;
@@ -178,8 +179,7 @@ public void dispatch() {
178179
//this should never happen
179180
throw UndertowServletMessages.MESSAGES.couldNotFindContextToDispatchTo(requestImpl.getOriginalContextPath());
180181
}
181-
//UNDERTOW-1591 use original, decoded value to dispatch, this should match: ServletInitialHandler.handleRequest
182-
String toDispatch = requestImpl.getExchange().getRelativePath();
182+
String toDispatch = CanonicalPathUtils.canonicalize(requestImpl.getOriginalRequestURI()).substring(requestImpl.getOriginalContextPath().length());
183183
String qs = requestImpl.getOriginalQueryString();
184184
if (qs != null && !qs.isEmpty()) {
185185
toDispatch = toDispatch + "?" + qs;

servlet/src/main/java/io/undertow/servlet/spec/HttpServletRequestImpl.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package io.undertow.servlet.spec;
2020

21+
import io.undertow.UndertowOptions;
2122
import io.undertow.security.api.SecurityContext;
2223
import io.undertow.security.idm.Account;
2324
import io.undertow.server.HttpServerExchange;
@@ -54,6 +55,7 @@
5455
import java.io.UnsupportedEncodingException;
5556
import java.net.InetAddress;
5657
import java.net.InetSocketAddress;
58+
import java.net.URLDecoder;
5759
import java.nio.charset.Charset;
5860
import java.nio.charset.StandardCharsets;
5961
import java.nio.charset.UnsupportedCharsetException;
@@ -286,16 +288,29 @@ public String getMethod() {
286288

287289
@Override
288290
public String getPathInfo() {
289-
ServletPathMatch match = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getServletPathMatch();
290-
if (match != null) {
291-
return match.getRemaining();
291+
final ServletPathMatch match = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getServletPathMatch();
292+
return match != null ? decodeURL(match.getRemaining()) : null;
293+
}
294+
295+
private String decodeURL(final String s) {
296+
try {
297+
return s != null && s.length() > 0 ? URLDecoder.decode(s, getURLEncoding()) : s;
298+
} catch (UnsupportedEncodingException ignored) {
299+
throw new IllegalStateException(); // cannot happen
292300
}
293-
return null;
301+
}
302+
303+
private String getURLEncoding() {
304+
return exchange.getConnection().getUndertowOptions().get(UndertowOptions.URL_CHARSET, StandardCharsets.UTF_8.name());
294305
}
295306

296307
@Override
297308
public String getPathTranslated() {
298-
return getRealPath(getPathInfo());
309+
ServletPathMatch match = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getServletPathMatch();
310+
if (match != null) {
311+
return getRealPath(match.getRemaining());
312+
}
313+
return null;
299314
}
300315

301316
@Override
@@ -413,11 +428,8 @@ public StringBuffer getRequestURL() {
413428

414429
@Override
415430
public String getServletPath() {
416-
ServletPathMatch match = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getServletPathMatch();
417-
if (match != null) {
418-
return match.getMatched();
419-
}
420-
return "";
431+
final ServletPathMatch match = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getServletPathMatch();
432+
return match != null ? decodeURL(match.getMatched()) : "";
421433
}
422434

423435
@Override
@@ -1156,7 +1168,11 @@ public String getOriginalServletPath() {
11561168
if(uri != null) {
11571169
return uri;
11581170
}
1159-
return getServletPath();
1171+
ServletPathMatch match = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getServletPathMatch();
1172+
if (match != null) {
1173+
return match.getMatched();
1174+
}
1175+
return "";
11601176
}
11611177

11621178
public String getOriginalPathInfo() {
@@ -1168,7 +1184,11 @@ public String getOriginalPathInfo() {
11681184
if(uri != null) {
11691185
return uri;
11701186
}
1171-
return getPathInfo();
1187+
ServletPathMatch match = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getServletPathMatch();
1188+
if (match != null) {
1189+
return match.getRemaining();
1190+
}
1191+
return null;
11721192
}
11731193

11741194
public String getOriginalContextPath() {

0 commit comments

Comments
 (0)