diff options
author | Jan Bartel | 2012-08-30 06:16:44 +0000 |
---|---|---|
committer | Jan Bartel | 2012-08-30 06:16:44 +0000 |
commit | c7b9081a5234eaa1a5b4403a6fdfc523e91bd4f0 (patch) | |
tree | b199c7e524e9a3681dc490898299741563e7c419 | |
parent | ef17e69bd5f65116cce6b3b6914f02f2c18593c1 (diff) | |
parent | 2ed3a1688dc9720ffa46252db87472bed8cc43a9 (diff) | |
download | org.eclipse.jetty.project-c7b9081a5234eaa1a5b4403a6fdfc523e91bd4f0.tar.gz org.eclipse.jetty.project-c7b9081a5234eaa1a5b4403a6fdfc523e91bd4f0.tar.xz org.eclipse.jetty.project-c7b9081a5234eaa1a5b4403a6fdfc523e91bd4f0.zip |
Merge remote-tracking branch 'origin/master' into jetty-8
11 files changed, 385 insertions, 106 deletions
diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/AbstractHttpConnection.java b/jetty-client/src/main/java/org/eclipse/jetty/client/AbstractHttpConnection.java index 0ff501e864..3cb1a90656 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/AbstractHttpConnection.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/AbstractHttpConnection.java @@ -172,6 +172,9 @@ public abstract class AbstractHttpConnection extends AbstractConnection implemen { } + /** + * @throws IOException + */ protected void commitRequest() throws IOException { synchronized (this) @@ -223,6 +226,7 @@ public abstract class AbstractHttpConnection extends AbstractConnection implemen requestHeaders.putLongField(HttpHeaders.CONTENT_LENGTH, requestContent.length()); _generator.completeHeader(requestHeaders,false); _generator.addContent(new View(requestContent),true); + _exchange.setStatus(HttpExchange.STATUS_WAITING_FOR_RESPONSE); } else { @@ -230,24 +234,14 @@ public abstract class AbstractHttpConnection extends AbstractConnection implemen if (requestContentStream != null) { _generator.completeHeader(requestHeaders, false); - int available = requestContentStream.available(); - if (available > 0) - { - // TODO deal with any known content length - // TODO reuse this buffer! - byte[] buf = new byte[available]; - int length = requestContentStream.read(buf); - _generator.addContent(new ByteArrayBuffer(buf, 0, length), false); - } } else { requestHeaders.remove(HttpHeaders.CONTENT_LENGTH); _generator.completeHeader(requestHeaders, true); + _exchange.setStatus(HttpExchange.STATUS_WAITING_FOR_RESPONSE); } } - - _exchange.setStatus(HttpExchange.STATUS_WAITING_FOR_RESPONSE); } } diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/AsyncHttpConnection.java b/jetty-client/src/main/java/org/eclipse/jetty/client/AsyncHttpConnection.java index 1ea6b06721..dbb75d897c 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/AsyncHttpConnection.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/AsyncHttpConnection.java @@ -114,6 +114,8 @@ public class AsyncHttpConnection extends AbstractHttpConnection implements Async Buffer chunk=_requestContentChunk; _requestContentChunk=exchange.getRequestContentChunk(null); _generator.addContent(chunk,_requestContentChunk==null); + if (_requestContentChunk==null) + exchange.setStatus(HttpExchange.STATUS_WAITING_FOR_RESPONSE); } } } diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/BlockingHttpConnection.java b/jetty-client/src/main/java/org/eclipse/jetty/client/BlockingHttpConnection.java index c10319fdeb..454cee300e 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/BlockingHttpConnection.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/BlockingHttpConnection.java @@ -122,6 +122,8 @@ public class BlockingHttpConnection extends AbstractHttpConnection Buffer chunk=_requestContentChunk; _requestContentChunk=exchange.getRequestContentChunk(null); _generator.addContent(chunk,_requestContentChunk==null); + if (_requestContentChunk==null) + exchange.setStatus(HttpExchange.STATUS_WAITING_FOR_RESPONSE); } } } diff --git a/jetty-osgi/jetty-osgi-boot-jsp/src/main/java/org/eclipse/jetty/osgi/boot/jasper/WebappRegistrationCustomizerImpl.java b/jetty-osgi/jetty-osgi-boot-jsp/src/main/java/org/eclipse/jetty/osgi/boot/jasper/WebappRegistrationCustomizerImpl.java index d6a49fe607..2d1cecd2bb 100644 --- a/jetty-osgi/jetty-osgi-boot-jsp/src/main/java/org/eclipse/jetty/osgi/boot/jasper/WebappRegistrationCustomizerImpl.java +++ b/jetty-osgi/jetty-osgi-boot-jsp/src/main/java/org/eclipse/jetty/osgi/boot/jasper/WebappRegistrationCustomizerImpl.java @@ -139,17 +139,23 @@ public class WebappRegistrationCustomizerImpl implements WebappRegistrationCusto public URL[] getJarsWithTlds(DeploymentManager deployer, BundleFileLocatorHelper locatorHelper) throws Exception { - HashSet<Class<?>> classesToAddToTheTldBundles = new HashSet<Class<?>>(); + ArrayList<URL> urls = new ArrayList<URL>(); + HashSet<Class<?>> classesToAddToTheTldBundles = new HashSet<Class<?>>(); // Look for the jstl bundle // We assume the jstl's tlds are defined there. // We assume that the jstl bundle is imported by this bundle // So we can look for this class using this bundle's classloader: - Class<?> jstlClass = WebappRegistrationCustomizerImpl.class.getClassLoader().loadClass(DEFAULT_JSTL_BUNDLE_CLASS); - - classesToAddToTheTldBundles.add(jstlClass); + try + { + Class<?> jstlClass = WebappRegistrationCustomizerImpl.class.getClassLoader().loadClass(DEFAULT_JSTL_BUNDLE_CLASS); - ArrayList<URL> urls = new ArrayList<URL>(); + classesToAddToTheTldBundles.add(jstlClass); + } + catch (ClassNotFoundException e) + { + LOG.info("jstl not on classpath", e); + } for (Class<?> cl : classesToAddToTheTldBundles) { Bundle tldBundle = FrameworkUtil.getBundle(cl); diff --git a/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/SessionAuthentication.java b/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/SessionAuthentication.java index 74f143b05f..260d5ddd3f 100644 --- a/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/SessionAuthentication.java +++ b/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/SessionAuthentication.java @@ -119,16 +119,23 @@ public class SessionAuthentication implements Authentication.User, Serializable, public void sessionWillPassivate(HttpSessionEvent se) { + } public void sessionDidActivate(HttpSessionEvent se) { if (_session==null) + { _session=se.getSession(); + } } public void valueBound(HttpSessionBindingEvent event) { + if (_session==null) + { + _session=event.getSession(); + } } public void valueUnbound(HttpSessionBindingEvent event) 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 1ce7d1d6c6..465b59dbc7 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 @@ -39,7 +39,8 @@ import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; /* ------------------------------------------------------------ */ -/** SessionHandler. +/** + * SessionHandler. */ public class SessionHandler extends ScopedHandler { @@ -47,15 +48,12 @@ public class SessionHandler extends ScopedHandler public final static EnumSet<SessionTrackingMode> DEFAULT_TRACKING = EnumSet.of(SessionTrackingMode.COOKIE,SessionTrackingMode.URL); - - /* -------------------------------------------------------------- */ private SessionManager _sessionManager; /* ------------------------------------------------------------ */ - /** Constructor. - * Construct a SessionHandler witha a HashSessionManager with a standard - * java.util.Random generator is created. + /** + * Constructor. Construct a SessionHandler witha a HashSessionManager with a standard java.util.Random generator is created. */ public SessionHandler() { @@ -64,7 +62,8 @@ public class SessionHandler extends ScopedHandler /* ------------------------------------------------------------ */ /** - * @param manager The session manager + * @param manager + * The session manager */ public SessionHandler(SessionManager manager) { @@ -82,7 +81,8 @@ public class SessionHandler extends ScopedHandler /* ------------------------------------------------------------ */ /** - * @param sessionManager The sessionManager to set. + * @param sessionManager + * The sessionManager to set. */ public void setSessionManager(SessionManager sessionManager) { @@ -90,32 +90,30 @@ public class SessionHandler extends ScopedHandler throw new IllegalStateException(); SessionManager old_session_manager = _sessionManager; - if (getServer()!=null) - getServer().getContainer().update(this, old_session_manager, sessionManager, "sessionManager",true); + if (getServer() != null) + getServer().getContainer().update(this,old_session_manager,sessionManager,"sessionManager",true); - if (sessionManager!=null) + if (sessionManager != null) sessionManager.setSessionHandler(this); _sessionManager = sessionManager; - if (old_session_manager!=null) + if (old_session_manager != null) old_session_manager.setSessionHandler(null); } - /* ------------------------------------------------------------ */ @Override public void setServer(Server server) { - Server old_server=getServer(); - if (old_server!=null && old_server!=server) - old_server.getContainer().update(this, _sessionManager, null, "sessionManager",true); + Server old_server = getServer(); + if (old_server != null && old_server != server) + old_server.getContainer().update(this,_sessionManager,null,"sessionManager",true); super.setServer(server); - if (server!=null && server!=old_server) - server.getContainer().update(this, null,_sessionManager, "sessionManager",true); + if (server != null && server != old_server) + server.getContainer().update(this,null,_sessionManager,"sessionManager",true); } - /* ------------------------------------------------------------ */ /* * @see org.eclipse.thread.AbstractLifeCycle#doStart() @@ -126,6 +124,7 @@ public class SessionHandler extends ScopedHandler _sessionManager.start(); super.doStart(); } + /* ------------------------------------------------------------ */ /* * @see org.eclipse.thread.AbstractLifeCycle#doStop() @@ -138,18 +137,16 @@ public class SessionHandler extends ScopedHandler super.doStop(); } - /* ------------------------------------------------------------ */ /* * @see org.eclipse.jetty.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) */ @Override - public void doScope(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) - throws IOException, ServletException + public void doScope(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - SessionManager old_session_manager=null; - HttpSession old_session=null; - HttpSession access=null; + SessionManager old_session_manager = null; + HttpSession old_session = null; + HttpSession access = null; try { old_session_manager = baseRequest.getSessionManager(); @@ -164,54 +161,54 @@ public class SessionHandler extends ScopedHandler } // access any existing session - HttpSession session=null; - if (_sessionManager!=null) + HttpSession session = null; + if (_sessionManager != null) { - session=baseRequest.getSession(false); - if (session!=null) + session = baseRequest.getSession(false); + if (session != null) { - if(session!=old_session) + if (session != old_session) { - access=session; + access = session; HttpCookie cookie = _sessionManager.access(session,request.isSecure()); - if (cookie!=null ) // Handle changed ID or max-age refresh + if (cookie != null) // Handle changed ID or max-age refresh baseRequest.getResponse().addCookie(cookie); } } else { - session=baseRequest.recoverNewSession(_sessionManager); - if (session!=null) + session = baseRequest.recoverNewSession(_sessionManager); + if (session != null) baseRequest.setSession(session); } } - if(LOG.isDebugEnabled()) + if (LOG.isDebugEnabled()) { - LOG.debug("sessionManager="+_sessionManager); - LOG.debug("session="+session); + LOG.debug("sessionManager=" + _sessionManager); + LOG.debug("session=" + session); } // start manual inline of nextScope(target,baseRequest,request,response); - if (_nextScope!=null) - _nextScope.doScope(target,baseRequest,request, response); - else if (_outerScope!=null) - _outerScope.doHandle(target,baseRequest,request, response); - else - doHandle(target,baseRequest,request, response); + if (_nextScope != null) + _nextScope.doScope(target,baseRequest,request,response); + else if (_outerScope != null) + _outerScope.doHandle(target,baseRequest,request,response); + else + doHandle(target,baseRequest,request,response); // end manual inline (pathentic attempt to reduce stack depth) - + } finally { - if (access!=null) + if (access != null) _sessionManager.complete(access); HttpSession session = baseRequest.getSession(false); - if (session!=null && old_session==null && session!=access) + if (session != null && old_session == null && session != access) _sessionManager.complete(session); - if (old_session_manager!=null && old_session_manager != _sessionManager) + if (old_session_manager != null && old_session_manager != _sessionManager) { baseRequest.setSessionManager(old_session_manager); baseRequest.setSession(old_session); @@ -224,100 +221,111 @@ public class SessionHandler extends ScopedHandler * @see org.eclipse.jetty.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) */ @Override - public void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) - throws IOException, ServletException + public void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // start manual inline of nextHandle(target,baseRequest,request,response); if (never()) nextHandle(target,baseRequest,request,response); - else if (_nextScope!=null && _nextScope==_handler) - _nextScope.doHandle(target,baseRequest,request, response); - else if (_handler!=null) - _handler.handle(target,baseRequest, request, response); + else if (_nextScope != null && _nextScope == _handler) + _nextScope.doHandle(target,baseRequest,request,response); + else if (_handler != null) + _handler.handle(target,baseRequest,request,response); // end manual inline } /* ------------------------------------------------------------ */ - /** Look for a requested session ID in cookies and URI parameters + /** + * Look for a requested session ID in cookies and URI parameters + * * @param baseRequest * @param request */ protected void checkRequestedSessionId(Request baseRequest, HttpServletRequest request) { - String requested_session_id=request.getRequestedSessionId(); - + String requested_session_id = request.getRequestedSessionId(); + SessionManager sessionManager = getSessionManager(); - - if (requested_session_id!=null && sessionManager!=null) + + if (requested_session_id != null && sessionManager != null) { - HttpSession session=sessionManager.getHttpSession(requested_session_id); - if (session!=null && sessionManager.isValid(session)) + HttpSession session = sessionManager.getHttpSession(requested_session_id); + if (session != null && sessionManager.isValid(session)) baseRequest.setSession(session); return; } else if (!DispatcherType.REQUEST.equals(baseRequest.getDispatcherType())) return; - boolean requested_session_id_from_cookie=false; - HttpSession session=null; + boolean requested_session_id_from_cookie = false; + HttpSession session = null; // Look for session id cookie if (_sessionManager.isUsingCookies()) { - Cookie[] cookies=request.getCookies(); - if (cookies!=null && cookies.length>0) + Cookie[] cookies = request.getCookies(); + if (cookies != null && cookies.length > 0) { final String sessionCookie=sessionManager.getSessionCookieConfig().getName(); - for (int i=0;i<cookies.length;i++) + for (int i = 0; i < cookies.length; i++) { if (sessionCookie.equalsIgnoreCase(cookies[i].getName())) { - requested_session_id=cookies[i].getValue(); + requested_session_id = cookies[i].getValue(); requested_session_id_from_cookie = true; - if(LOG.isDebugEnabled()) - LOG.debug("Got Session ID {} from cookie",requested_session_id); - - session=sessionManager.getHttpSession(requested_session_id); - if (session!=null && sessionManager.isValid(session)) - break; + + LOG.info("Got Session ID {} from cookie",requested_session_id); + + if (requested_session_id != null) + { + session = sessionManager.getHttpSession(requested_session_id); + + if (session != null && sessionManager.isValid(session)) + { + break; + } + } + else + { + LOG.warn("null session id from cookie"); + } } } } } - if (requested_session_id==null || session==null) + if (requested_session_id == null || session == null) { String uri = request.getRequestURI(); - String prefix=sessionManager.getSessionIdPathParameterNamePrefix(); - if (prefix!=null) + String prefix = sessionManager.getSessionIdPathParameterNamePrefix(); + if (prefix != null) { int s = uri.indexOf(prefix); - if (s>=0) - { - s+=prefix.length(); - int i=s; - while (i<uri.length()) + if (s >= 0) + { + s += prefix.length(); + int i = s; + while (i < uri.length()) { - char c=uri.charAt(i); - if (c==';'||c=='#'||c=='?'||c=='/') + char c = uri.charAt(i); + if (c == ';' || c == '#' || c == '?' || c == '/') break; i++; } requested_session_id = uri.substring(s,i); requested_session_id_from_cookie = false; - session=sessionManager.getHttpSession(requested_session_id); - if(LOG.isDebugEnabled()) + session = sessionManager.getHttpSession(requested_session_id); + if (LOG.isDebugEnabled()) LOG.debug("Got Session ID {} from URL",requested_session_id); } } } baseRequest.setRequestedSessionId(requested_session_id); - baseRequest.setRequestedSessionIdFromCookie(requested_session_id!=null && requested_session_id_from_cookie); - if (session!=null && sessionManager.isValid(session)) - baseRequest.setSession(session); + baseRequest.setRequestedSessionIdFromCookie(requested_session_id != null && requested_session_id_from_cookie); + if (session != null && sessionManager.isValid(session)) + baseRequest.setSession(session); } /* ------------------------------------------------------------ */ @@ -326,14 +334,14 @@ public class SessionHandler extends ScopedHandler */ public void addEventListener(EventListener listener) { - if(_sessionManager!=null) + if (_sessionManager != null) _sessionManager.addEventListener(listener); } /* ------------------------------------------------------------ */ public void clearEventListeners() { - if(_sessionManager!=null) + if (_sessionManager != null) _sessionManager.clearEventListeners(); } } diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/TagLibConfiguration.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/TagLibConfiguration.java index 89b2218b52..580f363f81 100644 --- a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/TagLibConfiguration.java +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/TagLibConfiguration.java @@ -111,7 +111,13 @@ public class TagLibConfiguration extends AbstractConfiguration //Get the system classpath tlds and tell jasper about them, if jasper is on the classpath try { - Class<?> clazz = getClass().getClassLoader().loadClass("org.apache.jasper.compiler.TldLocationsCache"); + + ClassLoader loader = _context.getClassLoader(); + if (loader == null || loader.getParent() == null) + loader = getClass().getClassLoader(); + else + loader = loader.getParent(); + Class<?> clazz = loader.loadClass("org.apache.jasper.compiler.TldLocationsCache"); assert clazz!=null; Collection<Resource> tld_resources = (Collection<Resource>)_context.getAttribute(TLD_RESOURCES); diff --git a/tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/ImmortalSessionTest.java b/tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/ImmortalSessionTest.java new file mode 100644 index 0000000000..b2056fc130 --- /dev/null +++ b/tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/ImmortalSessionTest.java @@ -0,0 +1,30 @@ +// +// ======================================================================== +// Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.server.session; + +public class ImmortalSessionTest extends AbstractImmortalSessionTest +{ + + @Override + public AbstractTestServer createServer(int port, int max, int scavenge) + { + return new HashTestServer(port,max,scavenge); + } + +} diff --git a/tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/SessionCookieTest.java b/tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/SessionCookieTest.java new file mode 100644 index 0000000000..3319bbb841 --- /dev/null +++ b/tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/SessionCookieTest.java @@ -0,0 +1,30 @@ +// +// ======================================================================== +// Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.server.session; + +public class SessionCookieTest extends AbstractSessionCookieTest +{ + + @Override + public AbstractTestServer createServer(int port, int max, int scavenge) + { + return new HashTestServer(port, max, scavenge); + } + +} diff --git a/tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/SessionValueSharedSaving.java b/tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/SessionValueSharedSaving.java new file mode 100644 index 0000000000..14626095b3 --- /dev/null +++ b/tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/SessionValueSharedSaving.java @@ -0,0 +1,30 @@ +// +// ======================================================================== +// Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.server.session; + +public class SessionValueSharedSaving extends AbstractSessionValueSavingTest +{ + + @Override + public AbstractTestServer createServer(int port, int max, int scavenge) + { + return new HashTestServer(port,max,scavenge); + } + +} diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionCookieTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionCookieTest.java new file mode 100644 index 0000000000..5246186f5a --- /dev/null +++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionCookieTest.java @@ -0,0 +1,164 @@ +// +// ======================================================================== +// Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.server.session; + +import java.io.IOException; +import java.util.Random; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import junit.framework.Assert; + +import org.eclipse.jetty.client.Address; +import org.eclipse.jetty.client.ContentExchange; +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.client.HttpDestination; +import org.eclipse.jetty.http.HttpCookie; +import org.eclipse.jetty.http.HttpMethods; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.util.log.Log; +import org.junit.Ignore; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * AbstractNewSessionTest + */ +public abstract class AbstractSessionCookieTest +{ + public abstract AbstractTestServer createServer(int port, int max, int scavenge); + + public void pause(int scavenge) + { + try + { + Thread.sleep(scavenge * 2500L); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + } + + @Test + @Ignore("failing because an http cookie with null value is coming over as \"null\"") + public void testSessionCookie() throws Exception + { + String contextPath = ""; + String servletMapping = "/server"; + int scavengePeriod = 3; + AbstractTestServer server = createServer(0, 1, scavengePeriod); + ServletContextHandler context = server.addContext(contextPath); + context.addServlet(TestServlet.class, servletMapping); + server.start(); + int port=server.getPort(); + try + { + HttpClient client = new HttpClient(); + client.setConnectorType(HttpClient.CONNECTOR_SOCKET); + client.start(); + try + { + ContentExchange exchange = new ContentExchange(true); + exchange.setMethod(HttpMethods.GET); + exchange.setURL("http://localhost:" + port + contextPath + servletMapping + "?action=create"); + client.send(exchange); + exchange.waitForDone(); + assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus()); + String sessionCookie = exchange.getResponseFields().getStringField("Set-Cookie"); + assertTrue(sessionCookie != null); + // Mangle the cookie, replacing Path with $Path, etc. + //sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path="); + + // Let's wait for the scavenger to run, waiting 2.5 times the scavenger period + //pause(scavengePeriod); + + exchange = new ContentExchange(true); + exchange.setMethod(HttpMethods.GET); + exchange.setURL("http://localhost:" + port + contextPath + servletMapping + "?action=check-cookie"); + exchange.getRequestFields().add("Cookie", sessionCookie); + client.send(exchange); + exchange.waitForDone(); + assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus()); + + exchange = new ContentExchange(true); + exchange.setMethod(HttpMethods.GET); + exchange.setURL("http://localhost:" + port + contextPath + servletMapping + "?action=null-cookie"); + //exchange.getRequestFields().add("Cookie", "null"); + HttpDestination dest = client.getDestination(new Address("localhost",port),false); + + dest.addCookie(new HttpCookie("Cookie",null)); + + client.send(exchange); + exchange.waitForDone(); + assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus()); + } + finally + { + client.stop(); + } + } + finally + { + server.stop(); + } + + } + public static class TestServlet extends HttpServlet + { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + { + String action = request.getParameter("action"); + if ("create".equals(action)) + { + HttpSession session = request.getSession(true); + assertTrue(session.isNew()); + } + else if ("check-cookie".equals(action)) + { + HttpSession session = request.getSession(false); + + assertTrue(session != null); + + //request.getSession(true); + } + else if ("null-cookie".equals(action)) + { + HttpSession session = request.getSession(false); + + assertEquals(1, request.getCookies().length); + + Assert.assertFalse("null".equals(request.getCookies()[0].getValue())); + + assertTrue(session == null); + + } + else + { + assertTrue(false); + } + } + } +} |