Merge branch 'master' of ssh://git.eclipse.org/gitroot/jetty/org.eclipse.jetty.project

This commit is contained in:
Greg Wilkins 2014-10-24 08:46:12 +11:00
commit ac1b111d57
9 changed files with 250 additions and 9 deletions

View File

@ -79,6 +79,16 @@
<artifactId>jetty-proxy</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jaas</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-plus</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>apache-jsp</artifactId>

View File

@ -35,6 +35,7 @@ import org.eclipse.jetty.server.SecureRequestCustomizer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.server.handler.AsyncDelayHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
@ -43,6 +44,7 @@ import org.eclipse.jetty.server.handler.StatisticsHandler;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler;
import org.eclipse.jetty.webapp.Configuration;
public class LikeJettyXml
{
@ -143,7 +145,11 @@ public class LikeJettyXml
deployer.addAppProvider(webapp_provider);
server.addBean(deployer);
// === setup jetty plus ==
Configuration.ClassList.setServerDefault(server)
.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration",
"org.eclipse.jetty.plus.webapp.EnvConfiguration","org.eclipse.jetty.plus.webapp.PlusConfiguration");
// === jetty-stats.xml ===
StatisticsHandler stats = new StatisticsHandler();
@ -183,7 +189,6 @@ public class LikeJettyXml
login.setRefreshInterval(0);
server.addBean(login);
// Start the server
server.start();
server.join();

View File

@ -105,9 +105,6 @@ public abstract class HttpConnection implements Connection
headers.put(getHttpDestination().getHostField());
}
if (request.getAgent() == null)
headers.put(getHttpClient().getUserAgentField());
// Add content headers
if (content != null)
{

View File

@ -94,6 +94,9 @@ public class HttpRequest implements Request
HttpField acceptEncodingField = client.getAcceptEncodingField();
if (acceptEncodingField != null)
headers.put(acceptEncodingField);
HttpField userAgentField = client.getUserAgentField();
if (userAgentField != null)
headers.put(userAgentField);
}
protected HttpConversation getConversation()

View File

@ -151,10 +151,15 @@ public interface Request
* @param name the name of the header
* @param value the value of the header
* @return this request object
* @see #header(HttpHeader, String)
*/
Request header(String name, String value);
/**
* <p>Adds the given {@code value} to the specified {@code header}.</p>
* <p>Multiple calls with the same parameters will add multiple values;
* use the value {@code null} to remove the header completely.</p>
*
* @param header the header name
* @param value the value of the header
* @return this request object

View File

@ -847,6 +847,7 @@ public class HttpClientTest extends AbstractHttpClientServerTest
response = client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.header(HttpHeader.USER_AGENT, null)
.header(HttpHeader.USER_AGENT, userAgent)
.timeout(5, TimeUnit.SECONDS)
.send();
@ -854,6 +855,51 @@ public class HttpClientTest extends AbstractHttpClientServerTest
Assert.assertEquals(200, response.getStatus());
}
@Test
public void testUserAgentCanBeRemoved() throws Exception
{
start(new AbstractHandler()
{
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
baseRequest.setHandled(true);
ArrayList<String> userAgents = Collections.list(request.getHeaders("User-Agent"));
if ("/ua".equals(target))
Assert.assertEquals(1, userAgents.size());
else
Assert.assertEquals(0, userAgents.size());
}
});
// User agent not specified, use default.
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.path("/ua")
.timeout(5, TimeUnit.SECONDS)
.send();
Assert.assertEquals(200, response.getStatus());
// User agent explicitly removed.
response = client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.agent(null)
.timeout(5, TimeUnit.SECONDS)
.send();
Assert.assertEquals(200, response.getStatus());
// User agent explicitly removed.
response = client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.header(HttpHeader.USER_AGENT, null)
.timeout(5, TimeUnit.SECONDS)
.send();
Assert.assertEquals(200, response.getStatus());
}
@Test
public void testRequestListenerForMultipleEventsIsInvokedOncePerEvent() throws Exception
{

View File

@ -0,0 +1,153 @@
//
// ========================================================================
// Copyright (c) 1995-2014 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.AsyncContext;
import javax.servlet.DispatcherType;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
/* ------------------------------------------------------------ */
/** A handler wrapper that provides the framework to asynchronously
* delay the handling of a request. While it uses standard servlet
* API for asynchronous servlets, it adjusts the dispatch type of the
* request so that it does not appear to be asynchronous during the
* delayed dispatch.
*
*/
public class AsyncDelayHandler extends HandlerWrapper
{
public final static String AHW_ATTR = "o.e.j.s.h.AsyncHandlerWrapper";
/* ------------------------------------------------------------ */
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
if (!isStarted() || _handler==null)
return;
// Get the dispatcher types
DispatcherType ctype = baseRequest.getDispatcherType();
DispatcherType dtype = (DispatcherType)baseRequest.getAttribute(AHW_ATTR);
Object async_context_path=null;
Object async_path_info=null;
Object async_query_string=null;
Object async_request_uri=null;
Object async_servlet_path=null;
// Is this request a restarted one?
boolean restart=false;
if (dtype!=null)
{
// fake the dispatch type to the original
baseRequest.setAttribute(AHW_ATTR,null);
baseRequest.setDispatcherType(dtype);
restart=true;
async_context_path=baseRequest.getAttribute(AsyncContext.ASYNC_CONTEXT_PATH);
baseRequest.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,null);
async_path_info=baseRequest.getAttribute(AsyncContext.ASYNC_PATH_INFO);
baseRequest.setAttribute(AsyncContext.ASYNC_PATH_INFO,null);
async_query_string=baseRequest.getAttribute(AsyncContext.ASYNC_QUERY_STRING);
baseRequest.setAttribute(AsyncContext.ASYNC_QUERY_STRING,null);
async_request_uri=baseRequest.getAttribute(AsyncContext.ASYNC_REQUEST_URI);
baseRequest.setAttribute(AsyncContext.ASYNC_REQUEST_URI,null);
async_servlet_path=baseRequest.getAttribute(AsyncContext.ASYNC_SERVLET_PATH);
baseRequest.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,null);
}
// Should we handle this request now?
if (!startHandling(baseRequest,restart))
{
// No, so go async and remember dispatch type
AsyncContext context = baseRequest.startAsync();
baseRequest.setAttribute(AHW_ATTR,ctype);
delayHandling(baseRequest, context);
return;
}
// Handle the request
try
{
_handler.handle(target,baseRequest, request, response);
}
finally
{
if(restart)
{
// reset the request
baseRequest.setDispatcherType(ctype);
baseRequest.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,async_context_path);
baseRequest.setAttribute(AsyncContext.ASYNC_PATH_INFO,async_path_info);
baseRequest.setAttribute(AsyncContext.ASYNC_QUERY_STRING,async_query_string);
baseRequest.setAttribute(AsyncContext.ASYNC_REQUEST_URI,async_request_uri);
baseRequest.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,async_servlet_path);
}
// signal the request is leaving the handler
endHandling(baseRequest);
}
}
/* ------------------------------------------------------------ */
/** Called to indicate that a request has been presented for handling
* @param request The request to handle
* @param restart True if this request is being restarted after a delay
* @return True if the request should be handled now
*/
protected boolean startHandling(Request request, boolean restart)
{
return true;
}
/* ------------------------------------------------------------ */
/** Called to indicate that a requests handling is being delayed/
* The implementation should arrange for context.dispatch() to be
* called when the request should be handled. It may also set
* timeouts on the context.
*
* @param request The request to be delayed
* @param context The AsyncContext of the delayed request
*/
protected void delayHandling(Request request,AsyncContext context)
{
context.dispatch();
}
/* ------------------------------------------------------------ */
/** Called to indicated the handling of the request is ending.
* This is only the end of the current dispatch of the request and
* if the request is asynchronous, it may be handled again.
* @param request The request
*/
protected void endHandling(Request request)
{
}
}

View File

@ -579,6 +579,8 @@ public class ServletHandler extends ScopedHandler
res = ((ServletResponseHttpWrapper)res).getResponse();
// Do the filter/handling thang
servlet_holder.prepare(baseRequest, req, res);
if (chain!=null)
chain.doFilter(req, res);
else

View File

@ -732,9 +732,33 @@ public class ServletHolder extends Holder<Servlet> implements UserIdentity.Scope
{
_runAsRole = role;
}
/* ------------------------------------------------------------ */
/**
* Prepare to service a request.
*
* @param baseRequest
* @param request
* @param response
* @throws ServletException
* @throws UnavailableException
*/
protected void prepare (Request baseRequest, ServletRequest request, ServletResponse response)
throws ServletException, UnavailableException
{
MultipartConfigElement mpce = ((Registration)getRegistration()).getMultipartConfig();
if (mpce != null)
baseRequest.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, mpce);
}
/* ------------------------------------------------------------ */
/** Service a request with this servlet.
* @param baseRequest
* @param request
* @param response
* @throws ServletException
* @throws UnavailableException
* @throws IOException
*/
public void handle(Request baseRequest,
ServletRequest request,
@ -774,10 +798,6 @@ public class ServletHolder extends Holder<Servlet> implements UserIdentity.Scope
if (!isAsyncSupported())
baseRequest.setAsyncSupported(false);
MultipartConfigElement mpce = ((Registration)getRegistration()).getMultipartConfig();
if (mpce != null)
request.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, mpce);
servlet.service(request,response);
servlet_error=false;
}