Merge branch 'master' into sslClose

This commit is contained in:
Greg Wilkins 2011-10-03 10:50:11 +11:00
commit b30ac39868
6 changed files with 341 additions and 2 deletions

View File

@ -191,7 +191,7 @@ public class DigestAuthenticator extends LoginAuthenticator
+ domain
+ "\", nonce=\""
+ newNonce((Request)request)
+ "\", algorithm=MD5, qop=\"auth\""
+ "\", algorithm=MD5, qop=\"auth\","
+ " stale=" + stale);
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);

View File

@ -107,5 +107,11 @@
<version>${project.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.8.5</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,136 @@
// ========================================================================
// Copyright (c) 2009-2009 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.handler;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/* ------------------------------------------------------------ */
/**
* A handler that shuts the server down on a valid request. Used to do "soft" restarts from Java. If _exitJvm ist set to true a hard System.exit() call is being
* made.
*
* This handler is a contribution from Johannes Brodwall: https://bugs.eclipse.org/bugs/show_bug.cgi?id=357687
*
* Usage:
*
* <pre>
* Server server = new Server(8080);
* HandlerList handlers = new HandlerList();
* handlers.setHandlers(new Handler[]
* { someOtherHandler, new ShutdownHandler(server,&quot;secret password&quot;) });
* server.setHandler(handlers);
* server.start();
* </pre>
*/
public class ShutdownHandler extends AbstractHandler
{
private static final Logger LOG = Log.getLogger(ShutdownHandler.class);
private final String _shutdownToken;
private final Server _server;
private boolean _exitJvm = false;
/**
* Creates a listener that lets the server be shut down remotely (but only from localhost).
*
* @param server
* the Jetty instance that should be shut down
* @param shutdownToken
* a secret password to avoid unauthorized shutdown attempts
*/
public ShutdownHandler(Server server, String shutdownToken)
{
this._server = server;
this._shutdownToken = shutdownToken;
}
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
if (!target.equals("/shutdown"))
{
return;
}
if (!request.getMethod().equals("POST"))
{
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
if (!hasCorrectSecurityToken(request))
{
LOG.warn("Unauthorized shutdown attempt from " + getRemoteAddr(request));
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
return;
}
if (!requestFromLocalhost(request))
{
LOG.warn("Unauthorized shutdown attempt from " + getRemoteAddr(request));
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
return;
}
LOG.info("Shutting down by request from " + getRemoteAddr(request));
try
{
shutdownServer();
}
catch (Exception e)
{
throw new RuntimeException("Shutting down server",e);
}
}
private boolean requestFromLocalhost(HttpServletRequest request)
{
return "127.0.0.1".equals(getRemoteAddr(request));
}
protected String getRemoteAddr(HttpServletRequest request)
{
return request.getRemoteAddr();
}
private boolean hasCorrectSecurityToken(HttpServletRequest request)
{
return _shutdownToken.equals(request.getParameter("token"));
}
private void shutdownServer() throws Exception
{
_server.stop();
if (_exitJvm)
{
System.exit(0);
}
}
public void setExitJvm(boolean exitJvm)
{
this._exitJvm = exitJvm;
}
}

View File

@ -0,0 +1,80 @@
package org.eclipse.jetty.server.handler;
//========================================================================
//Copyright (c) 2009-2009 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.
//========================================================================
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Server;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
public class ShutdownHandlerTest
{
@Mock private HttpServletRequest request;
@Mock private HttpServletResponse response;
private Server server = new Server(0);
private String shutdownToken = "asdlnsldgnklns";
// class under test
private ShutdownHandler shutdownHandler;
@Before
public void startServer() throws Exception
{
MockitoAnnotations.initMocks(this);
server.start();
shutdownHandler = new ShutdownHandler(server,shutdownToken);
}
@Test
public void shutdownServerWithCorrectTokenAndIPTest() throws Exception
{
setDefaultExpectations();
shutdownHandler.handle("/shutdown",null,request,response);
assertEquals("Server should be stopped","STOPPED",server.getState());
}
@Test
public void wrongTokenTest() throws Exception
{
setDefaultExpectations();
when(request.getParameter("token")).thenReturn("anothertoken");
shutdownHandler.handle("/shutdown",null,request,response);
assertEquals("Server should be running","STARTED",server.getState());
}
@Test
public void shutdownRequestNotFromLocalhostTest() throws Exception
{
setDefaultExpectations();
when(request.getRemoteAddr()).thenReturn("192.168.3.3");
shutdownHandler.handle("/shutdown",null,request,response);
assertEquals("Server should be running","STARTED",server.getState());
}
private void setDefaultExpectations()
{
when(request.getMethod()).thenReturn("POST");
when(request.getParameter("token")).thenReturn(shutdownToken);
when(request.getRemoteAddr()).thenReturn("127.0.0.1");
}
}

View File

@ -15,12 +15,16 @@ package org.eclipse.jetty.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpRetryException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.GenericServlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
@ -213,7 +217,6 @@ public class DispatcherTest
// from inside the context.txt file
Assert.assertNotNull(responses);
System.out.println(responses);
assertTrue(responses.contains("content goes here"));
}
@ -250,6 +253,26 @@ public class DispatcherTest
assertTrue(responses.contains("content goes here"));
}
@Test
public void testForwardFilterToRogerServlet() throws Exception
{
_contextHandler.addServlet(RogerThatServlet.class, "/*");
_contextHandler.addServlet(ReserveEchoServlet.class,"/recho/*");
_contextHandler.addServlet(EchoServlet.class, "/echo/*");
_contextHandler.addFilter(ForwardFilter.class, "/*", FilterMapping.REQUEST);
String rogerResponse = _connector.getResponses("GET /context/ HTTP/1.0\n" + "Host: localhost\n\n");
String echoResponse = _connector.getResponses("GET /context/foo?echo=echoText HTTP/1.0\n" + "Host: localhost\n\n");
String rechoResponse = _connector.getResponses("GET /context/?echo=echoText HTTP/1.0\n" + "Host: localhost\n\n");
assertTrue(rogerResponse.contains("Roger That!"));
assertTrue(echoResponse.contains("echoText"));
assertTrue(rechoResponse.contains("txeTohce"));
}
public static class ForwardServlet extends HttpServlet implements Servlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
@ -266,6 +289,59 @@ public class DispatcherTest
}
}
/*
* Forward filter works with roger, echo and reverse echo servlets to test various
* forwarding bits using filters.
*
* when there is an echo parameter and the path info is / it forwards to the reverse echo
* anything else in the pathInfo and it sends straight to the echo servlet...otherwise its
* all roger servlet
*/
public static class ForwardFilter implements Filter
{
ServletContext servletContext;
public void init(FilterConfig filterConfig) throws ServletException
{
servletContext = filterConfig.getServletContext().getContext("/context");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
if ( servletContext == null || !(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse))
{
chain.doFilter(request,response);
return;
}
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse resp = (HttpServletResponse)response;
if ( req.getParameter("echo") != null && "/".equals(req.getPathInfo()))
{
RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/recho");
dispatcher.forward(request,response);
}
else if ( req.getParameter("echo") != null )
{
RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/echo");
dispatcher.forward(request,response);
}
else
{
chain.doFilter(request,response);
return;
}
}
public void destroy()
{
}
}
public static class DispatchServletServlet extends HttpServlet implements Servlet
{
@Override
@ -309,6 +385,42 @@ public class DispatcherTest
}
}
public static class EchoServlet extends GenericServlet
{
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
{
String echoText = req.getParameter("echo");
if ( echoText == null )
{
throw new ServletException("echo is a required parameter");
}
else
{
res.getWriter().print(echoText);
}
}
}
public static class ReserveEchoServlet extends GenericServlet
{
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
{
String echoText = req.getParameter("echo");
if ( echoText == null )
{
throw new ServletException("echo is a required parameter");
}
else
{
res.getWriter().print(new StringBuffer(echoText).reverse().toString());
}
}
}
public static class DispatchToResourceServlet extends HttpServlet implements Servlet
{
@Override

View File

@ -407,6 +407,11 @@
<artifactId>junit</artifactId>
<version>${junit-version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.8.5</version>
</dependency>
</dependencies>
</dependencyManagement>
<!--