diff options
author | Henri Sara <hesara@vaadin.com> | 2013-10-10 12:56:09 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2014-04-09 06:31:10 +0000 |
commit | 3edf6bd933804413094aca2cd678a0f58369d7d0 (patch) | |
tree | 6ea3322580242ab52cb20537e80e23a88df9dc1a /server | |
parent | eb5ffa3a414c2def6d669067f647c06c70c3c5eb (diff) | |
download | vaadin-framework-3edf6bd933804413094aca2cd678a0f58369d7d0.tar.gz vaadin-framework-3edf6bd933804413094aca2cd678a0f58369d7d0.zip |
Don't add slash after jsessionid in URL (#12307)
Change-Id: Ic329b4307bcc0613e6c0160375003d4b9f7e7ee1
Diffstat (limited to 'server')
-rw-r--r-- | server/src/com/vaadin/server/VaadinServlet.java | 46 | ||||
-rw-r--r-- | server/tests/src/com/vaadin/server/VaadinServletTest.java | 60 |
2 files changed, 104 insertions, 2 deletions
diff --git a/server/src/com/vaadin/server/VaadinServlet.java b/server/src/com/vaadin/server/VaadinServlet.java index 3da264e0e7..2d4d11785a 100644 --- a/server/src/com/vaadin/server/VaadinServlet.java +++ b/server/src/com/vaadin/server/VaadinServlet.java @@ -258,14 +258,22 @@ public class VaadinServlet extends HttpServlet implements Constants { */ protected boolean handleContextRootWithoutSlash(HttpServletRequest request, HttpServletResponse response) throws IOException { + // Query parameters like "?a=b" are handled by the servlet container but + // path parameter (e.g. ;jsessionid=) needs to be handled here + String location = request.getRequestURI(); + + String lastPathParameter = getLastPathParameter(location); + location = location.substring(0, + location.length() - lastPathParameter.length()); + if ((request.getPathInfo() == null || "/".equals(request.getPathInfo())) && "".equals(request.getServletPath()) - && !request.getRequestURI().endsWith("/")) { + && !location.endsWith("/")) { /* * Path info is for the root but request URI doesn't end with a * slash -> redirect to the same URI but with an ending slash. */ - String location = request.getRequestURI() + "/"; + location = location + "/" + lastPathParameter; String queryString = request.getQueryString(); if (queryString != null) { location += '?' + queryString; @@ -277,6 +285,40 @@ public class VaadinServlet extends HttpServlet implements Constants { } } + /** + * Finds any path parameter added to the last part of the uri. A path + * parameter is any string separated by ";" from the path and ends in / or + * at the end of the string. + * <p> + * For example the uri http://myhost.com/foo;a=1/bar;b=1 contains two path + * parameters, {@literal a=1} related to {@literal /foo} and {@literal b=1} + * related to /bar. + * <p> + * For http://myhost.com/foo;a=1/bar;b=1 this method will return ;b=1 + * + * @since 7.2 + * @param uri + * a URI + * @return the last path parameter of the uri including the semicolon or an + * empty string. Never null. + */ + protected static String getLastPathParameter(String uri) { + int lastPathStart = uri.lastIndexOf('/'); + if (lastPathStart == -1) { + return ""; + } + + int semicolonPos = uri.indexOf(';', lastPathStart); + if (semicolonPos < 0) { + // No path parameter for the last part + return ""; + } else { + // This includes the semicolon. + String semicolonString = uri.substring(semicolonPos); + return semicolonString; + } + } + private VaadinServletResponse createVaadinResponse( HttpServletResponse response) { return new VaadinServletResponse(response, getService()); diff --git a/server/tests/src/com/vaadin/server/VaadinServletTest.java b/server/tests/src/com/vaadin/server/VaadinServletTest.java new file mode 100644 index 0000000000..be21586be4 --- /dev/null +++ b/server/tests/src/com/vaadin/server/VaadinServletTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.server; + +import org.junit.Assert; +import org.junit.Test; + +public class VaadinServletTest { + + @Test + public void testGetLastPathParameter() { + Assert.assertEquals("", + VaadinServlet.getLastPathParameter("http://myhost.com")); + Assert.assertEquals(";a", + VaadinServlet.getLastPathParameter("http://myhost.com;a")); + Assert.assertEquals("", + VaadinServlet.getLastPathParameter("http://myhost.com/hello")); + Assert.assertEquals(";b=c", VaadinServlet + .getLastPathParameter("http://myhost.com/hello;b=c")); + Assert.assertEquals("", + VaadinServlet.getLastPathParameter("http://myhost.com/hello/")); + Assert.assertEquals("", VaadinServlet + .getLastPathParameter("http://myhost.com/hello;a/")); + Assert.assertEquals("", VaadinServlet + .getLastPathParameter("http://myhost.com/hello;a=1/")); + Assert.assertEquals(";b", VaadinServlet + .getLastPathParameter("http://myhost.com/hello/;b")); + Assert.assertEquals(";b=1", VaadinServlet + .getLastPathParameter("http://myhost.com/hello/;b=1")); + Assert.assertEquals(";b=1,c=2", VaadinServlet + .getLastPathParameter("http://myhost.com/hello/;b=1,c=2")); + Assert.assertEquals("", VaadinServlet + .getLastPathParameter("http://myhost.com/hello/;b=1,c=2/")); + Assert.assertEquals("", VaadinServlet + .getLastPathParameter("http://myhost.com/a;hello/;a/")); + Assert.assertEquals("", VaadinServlet + .getLastPathParameter("http://myhost.com/a;hello/;a=1/")); + Assert.assertEquals(";b", VaadinServlet + .getLastPathParameter("http://myhost.com/a;hello/;b")); + Assert.assertEquals(";b=1", VaadinServlet + .getLastPathParameter("http://myhost.com/a;hello/;b=1")); + Assert.assertEquals(";b=1,c=2", VaadinServlet + .getLastPathParameter("http://myhost.com/a;hello/;b=1,c=2")); + Assert.assertEquals("", VaadinServlet + .getLastPathParameter("http://myhost.com/a;hello/;b=1,c=2/")); + } +} |