Skip to content

Commit cab8d63

Browse files
authored
Issue #12828 support relative uris for Response.encodeURI (#12833)
1 parent 428b79b commit cab8d63

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

jetty-core/jetty-session/src/main/java/org/eclipse/jetty/session/AbstractSessionManager.java

+27-9
Original file line numberDiff line numberDiff line change
@@ -714,9 +714,6 @@ public String encodeURI(Request request, String uri, boolean cookiesInUse)
714714

715715
String id = session.getExtendedId();
716716

717-
if (httpURI == null)
718-
httpURI = HttpURI.from(uri);
719-
720717
// Already encoded
721718
int prefix = uri.indexOf(sessionURLPrefix);
722719
if (prefix != -1)
@@ -735,16 +732,37 @@ public String encodeURI(Request request, String uri, boolean cookiesInUse)
735732
int suffix = uri.indexOf('?');
736733
if (suffix < 0)
737734
suffix = uri.indexOf('#');
735+
738736
if (suffix < 0)
739737
{
740-
return uri +
741-
((HttpScheme.HTTPS.is(httpURI.getScheme()) || HttpScheme.HTTP.is(httpURI.getScheme())) && httpURI.getPath() == null ? "/" : "") + //if no path, insert the root path
742-
sessionURLPrefix + id;
738+
if (URIUtil.isRelative(uri))
739+
{
740+
return uri + sessionURLPrefix + id;
741+
}
742+
else
743+
{
744+
if (httpURI == null)
745+
httpURI = HttpURI.from(uri);
746+
747+
return uri +
748+
((HttpScheme.HTTPS.is(httpURI.getScheme()) || HttpScheme.HTTP.is(httpURI.getScheme())) && httpURI.getPath() == null ? "/" : "") + //if no path, insert the root path
749+
sessionURLPrefix + id;
750+
}
743751
}
744752

745-
return uri.substring(0, suffix) +
746-
((HttpScheme.HTTPS.is(httpURI.getScheme()) || HttpScheme.HTTP.is(httpURI.getScheme())) && httpURI.getPath() == null ? "/" : "") + //if no path so insert the root path
747-
sessionURLPrefix + id + uri.substring(suffix);
753+
if (URIUtil.isRelative(uri))
754+
{
755+
return uri.substring(0, suffix) + sessionURLPrefix + id + uri.substring(suffix);
756+
}
757+
else
758+
{
759+
if (httpURI == null)
760+
httpURI = HttpURI.from(uri);
761+
762+
return uri.substring(0, suffix) +
763+
((HttpScheme.HTTPS.is(httpURI.getScheme()) || HttpScheme.HTTP.is(httpURI.getScheme())) && httpURI.getPath() == null ? "/" : "") + //if no path so insert the root path
764+
sessionURLPrefix + id + uri.substring(suffix);
765+
}
748766
}
749767

750768
@Override

jetty-core/jetty-session/src/test/java/org/eclipse/jetty/session/SessionHandlerTest.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,13 @@ public boolean handle(Request request, Response response, Callback callback)
155155
out.append("Attribute ").append(name).append(" = ").append(session.getAttribute(name)).append('\n');
156156
out.append("URI [")
157157
.append(session.encodeURI(request, "/some/path", request.getHeaders().contains(HttpHeader.COOKIE)))
158-
.append("]");
158+
.append("]\n");
159+
out.append("RELATIVE URI [")
160+
.append(session.encodeURI(request, "../", request.getHeaders().contains(HttpHeader.COOKIE)))
161+
.append("]\n");
162+
out.append("ABSOLUTE URI [")
163+
.append(session.encodeURI(request, "http://localhost:80/foo/bar/", request.getHeaders().contains(HttpHeader.COOKIE)))
164+
.append("]\n");
159165
}
160166
else
161167
{
@@ -488,6 +494,8 @@ public void testCookieAndURI() throws Exception
488494
String content = response.getContent();
489495
assertThat(content, startsWith("Session="));
490496
assertThat(content, containsString("URI [/some/path;session_id=%s]".formatted(id))); // Cookies not known to be in use
497+
assertThat(content, containsString("RELATIVE URI [../;session_id=%s]".formatted(id))); // Cookies not known to be in use
498+
assertThat(content, containsString("ABSOLUTE URI [http://localhost:80/foo/bar/;session_id=%s]".formatted(id))); // Cookies not known to be in use
491499

492500
// Get with cookie
493501
endPoint.addInput("""
@@ -503,6 +511,8 @@ public void testCookieAndURI() throws Exception
503511
content = response.getContent();
504512
assertThat(content, containsString("Session=" + id.substring(0, id.indexOf(".node0"))));
505513
assertThat(content, containsString("URI [/some/path]")); // Cookies known to be in use
514+
assertThat(content, containsString("RELATIVE URI [../]"));
515+
assertThat(content, containsString("ABSOLUTE URI [http://localhost:80/foo/bar/"));
506516

507517
// Get with parameter
508518
endPoint.addInput("""
@@ -517,6 +527,8 @@ public void testCookieAndURI() throws Exception
517527
content = response.getContent();
518528
assertThat(content, containsString("Session=" + id.substring(0, id.indexOf(".node0"))));
519529
assertThat(content, containsString("URI [/some/path;session_id=%s]".formatted(id))); // Cookies not in use
530+
assertThat(content, containsString("RELATIVE URI [../;session_id=%s]".formatted(id)));
531+
assertThat(content, containsString("ABSOLUTE URI [http://localhost:80/foo/bar/;session_id=%s]".formatted(id)));
520532

521533
// Get with both, but param wrong
522534
endPoint.addInput("""
@@ -532,6 +544,8 @@ public void testCookieAndURI() throws Exception
532544
content = response.getContent();
533545
assertThat(content, containsString("Session=" + id.substring(0, id.indexOf(".node0"))));
534546
assertThat(content, containsString("URI [/some/path]")); // Cookies known to be in use
547+
assertThat(content, containsString("RELATIVE URI [../]"));
548+
assertThat(content, containsString("ABSOLUTE URI [http://localhost:80/foo/bar/]"));
535549

536550
// Get with both, but cookie wrong
537551
endPoint.addInput("""
@@ -547,6 +561,8 @@ public void testCookieAndURI() throws Exception
547561
content = response.getContent();
548562
assertThat(content, containsString("Session=" + id.substring(0, id.indexOf(".node0"))));
549563
assertThat(content, containsString("URI [/some/path]")); // Cookies known to be in use
564+
assertThat(content, containsString("RELATIVE URI [../]"));
565+
assertThat(content, containsString("ABSOLUTE URI [http://localhost:80/foo/bar/]"));
550566
}
551567
}
552568

@@ -789,5 +805,4 @@ public void testFlushOnResponseCommit() throws Exception
789805
assertThat(content, containsString("Session=" + id.substring(0, id.indexOf(".node0"))));
790806
assertThat(content, containsString("attribute = value"));
791807
}
792-
793808
}

0 commit comments

Comments
 (0)