diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/SessionHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/SessionHandler.java index 5c0efbf6695..d67c26027d7 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/SessionHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/SessionHandler.java @@ -48,6 +48,7 @@ import javax.servlet.http.HttpSessionListener; import org.eclipse.jetty.http.BadMessageException; import org.eclipse.jetty.http.HttpCookie; +import org.eclipse.jetty.http.Syntax; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.SessionIdManager; @@ -645,7 +646,7 @@ public class SessionHandler extends ScopedHandler HttpCookie cookie = null; cookie = new HttpCookie( - _cookieConfig.getName(), + getSessionCookieName(_cookieConfig), id, _cookieConfig.getDomain(), sessionPath, @@ -1334,6 +1335,13 @@ public class SessionHandler extends ScopedHandler public Session getSession(); } + public static String getSessionCookieName(SessionCookieConfig config) + { + if (config == null || config.getName() == null) + return __DefaultSessionCookie; + return config.getName(); + } + /** * CookieConfig * @@ -1423,6 +1431,10 @@ public class SessionHandler extends ScopedHandler { if (_context != null && _context.getContextHandler().isAvailable()) throw new IllegalStateException("CookieConfig cannot be set after ServletContext is started"); + if ("".equals(name)) + throw new IllegalArgumentException("Blank cookie name"); + if (name != null) + Syntax.requireValidRFC2616Token(name, "Bad Session cookie name"); _sessionCookie = name; } @@ -1596,12 +1608,12 @@ public class SessionHandler extends ScopedHandler Cookie[] cookies = request.getCookies(); if (cookies != null && cookies.length > 0) { - final String sessionCookie = getSessionCookieConfig().getName(); - for (int i = 0; i < cookies.length; i++) + final String sessionCookie = getSessionCookieName(getSessionCookieConfig()); + for (Cookie cookie : cookies) { - if (sessionCookie.equalsIgnoreCase(cookies[i].getName())) + if (sessionCookie.equalsIgnoreCase(cookie.getName())) { - String id = cookies[i].getValue(); + String id = cookie.getValue(); requestedSessionIdFromCookie = true; if (LOG.isDebugEnabled()) LOG.debug("Got Session ID {} from cookie {}", id, sessionCookie); diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/StandardDescriptorProcessor.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/StandardDescriptorProcessor.java index 2e4a24d0c30..08933386287 100644 --- a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/StandardDescriptorProcessor.java +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/StandardDescriptorProcessor.java @@ -38,6 +38,7 @@ import org.eclipse.jetty.http.pathmap.ServletPathSpec; import org.eclipse.jetty.security.ConstraintAware; import org.eclipse.jetty.security.ConstraintMapping; import org.eclipse.jetty.security.authentication.FormAuthenticator; +import org.eclipse.jetty.server.session.SessionHandler; import org.eclipse.jetty.servlet.ErrorPageErrorHandler; import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.FilterMapping; @@ -745,7 +746,7 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor case WebFragment: { //a web-fragment set the value, all web-fragments must have the same value - if (!context.getSessionHandler().getSessionCookieConfig().getName().equals(name)) + if (!name.equals(SessionHandler.getSessionCookieName(context.getSessionHandler().getSessionCookieConfig()))) throw new IllegalStateException("Conflicting cookie-config name " + name + " in " + descriptor.getResource()); break; } @@ -821,7 +822,7 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor case WebFragment: { //a web-fragment set the value, all web-fragments must have the same value - if (!context.getSessionHandler().getSessionCookieConfig().getPath().equals(path)) + if (!path.equals(context.getSessionHandler().getSessionCookieConfig().getPath())) throw new IllegalStateException("Conflicting cookie-config path " + path + " in " + descriptor.getResource()); break; }