Skip to content

Commit 9c30caf

Browse files
authored
Alternate resolution of #7615 (#7763)
+ use presence of scheme to gate parsing as HttpURI Signed-off-by: Greg Wilkins <[email protected]>
1 parent f7d0bb4 commit 9c30caf

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

jetty-server/src/main/java/org/eclipse/jetty/server/Response.java

+17-15
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,18 @@ public boolean containsHeader(String name)
321321
@Override
322322
public String encodeURL(String url)
323323
{
324+
if (url == null)
325+
return null;
326+
324327
final Request request = _channel.getRequest();
325328
SessionHandler sessionManager = request.getSessionHandler();
326329

327330
if (sessionManager == null)
328331
return url;
329332

330333
HttpURI uri = null;
331-
if (sessionManager.isCheckingRemoteSessionIdEncoding() && URIUtil.hasScheme(url))
334+
boolean hasScheme = URIUtil.hasScheme(url);
335+
if (sessionManager.isCheckingRemoteSessionIdEncoding() && hasScheme)
332336
{
333337
uri = new HttpURI(url);
334338
String path = uri.getPath();
@@ -350,9 +354,6 @@ public String encodeURL(String url)
350354
if (sessionURLPrefix == null)
351355
return url;
352356

353-
if (url == null)
354-
return null;
355-
356357
// should not encode if cookies in evidence
357358
if ((sessionManager.isUsingCookies() && request.isRequestedSessionIdFromCookie()) || !sessionManager.isUsingURLs())
358359
{
@@ -383,9 +384,6 @@ public String encodeURL(String url)
383384

384385
String id = sessionManager.getExtendedId(session);
385386

386-
if (uri == null)
387-
uri = new HttpURI(url);
388-
389387
// Already encoded
390388
int prefix = url.indexOf(sessionURLPrefix);
391389
if (prefix != -1)
@@ -400,20 +398,24 @@ public String encodeURL(String url)
400398
url.substring(suffix);
401399
}
402400

401+
// check for a null path
402+
String nonNullPath = "";
403+
if (hasScheme)
404+
{
405+
if (uri == null)
406+
uri = new HttpURI(url);
407+
if (uri.getPath() == null)
408+
nonNullPath = "/";
409+
}
410+
403411
// edit the session
404412
int suffix = url.indexOf('?');
405413
if (suffix < 0)
406414
suffix = url.indexOf('#');
407415
if (suffix < 0)
408-
{
409-
return url +
410-
((HttpScheme.HTTPS.is(uri.getScheme()) || HttpScheme.HTTP.is(uri.getScheme())) && uri.getPath() == null ? "/" : "") + //if no path, insert the root path
411-
sessionURLPrefix + id;
412-
}
416+
return url + nonNullPath + sessionURLPrefix + id;
413417

414-
return url.substring(0, suffix) +
415-
((HttpScheme.HTTPS.is(uri.getScheme()) || HttpScheme.HTTP.is(uri.getScheme())) && uri.getPath() == null ? "/" : "") + //if no path so insert the root path
416-
sessionURLPrefix + id + url.substring(suffix);
418+
return url.substring(0, suffix) + nonNullPath + sessionURLPrefix + id + url.substring(suffix);
417419
}
418420

419421
@Override

jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1520,7 +1520,7 @@ public void testWriteCheckError() throws Exception
15201520
}
15211521

15221522
@Test
1523-
public void testEncodeRedirect()
1523+
public void testEncodeURLs()
15241524
throws Exception
15251525
{
15261526
Response response = getResponse();
@@ -1570,6 +1570,7 @@ public void testEncodeRedirect()
15701570
assertEquals("/;jsessionid=12345", response.encodeURL("/"));
15711571
assertEquals("/foo.html;jsessionid=12345#target", response.encodeURL("/foo.html#target"));
15721572
assertEquals(";jsessionid=12345", response.encodeURL(""));
1573+
assertEquals("../foo/bar.jsp;jsessionid=12345", response.encodeURL("../foo/bar.jsp"));
15731574
}
15741575

15751576
@Test

0 commit comments

Comments
 (0)