Merged branch 'master' into 'jetty-9-alpn'.

This commit is contained in:
Simone Bordet 2014-03-20 12:19:03 +01:00
commit 3f02e56514
12 changed files with 289 additions and 35 deletions

View File

@ -109,6 +109,7 @@ ppm=image/x-portable-pixmap
pps=application/vnd.ms-powerpoint
ppt=application/vnd.ms-powerpoint
ps=application/postscript
qml=text/x-qml
qt=video/quicktime
ra=audio/x-pn-realaudio
ram=audio/x-pn-realaudio

View File

@ -38,7 +38,7 @@ public class CompactPathRule extends Rule implements Rule.ApplyURI
}
@Override
public void applyURI(Request request, String oldTarget, String newTarget) throws IOException
public void applyURI(Request request, String oldURI, String newURI) throws IOException
{
String uri = request.getRequestURI();
if (uri.startsWith("/"))

View File

@ -71,7 +71,6 @@ public class RewritePatternRule extends PatternRule implements Rule.ApplyURI
}
/* ------------------------------------------------------------ */
/**
* This method will add _query to the requests's queryString and also combine it with existing queryStrings in
* the request. However it won't take care for duplicate. E.g. if request.getQueryString contains a parameter
@ -80,16 +79,16 @@ public class RewritePatternRule extends PatternRule implements Rule.ApplyURI
* cases.
*
* @param request
* @param oldTarget
* @param newTarget
* @param oldURI
* @param newURI
* @throws IOException
*/
@Override
public void applyURI(Request request, String oldTarget, String newTarget) throws IOException
public void applyURI(Request request, String oldURI, String newURI) throws IOException
{
if (_query == null)
{
request.setRequestURI(newTarget);
request.setRequestURI(newURI);
}
else
{
@ -98,9 +97,9 @@ public class RewritePatternRule extends PatternRule implements Rule.ApplyURI
queryString = queryString + "&" + _query;
else
queryString = _query;
HttpURI uri = new HttpURI(newTarget + "?" + queryString);
HttpURI uri = new HttpURI(newURI + "?" + queryString);
request.setUri(uri);
request.setRequestURI(newTarget);
request.setRequestURI(newURI);
request.setQueryString(queryString);
}
}

View File

@ -96,11 +96,11 @@ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI
/* ------------------------------------------------------------ */
@Override
public void applyURI(Request request, String oldTarget, String newTarget) throws IOException
public void applyURI(Request request, String oldURI, String newURI) throws IOException
{
if (_query==null)
{
request.setRequestURI(newTarget);
request.setRequestURI(newURI);
}
else
{
@ -108,9 +108,9 @@ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI
if (!_queryGroup && request.getQueryString()!=null)
query=request.getQueryString()+"&"+query;
HttpURI uri=new HttpURI(newTarget+"?"+query);
HttpURI uri=new HttpURI(newURI+"?"+query);
request.setUri(uri);
request.setRequestURI(newTarget);
request.setRequestURI(newURI);
request.setQueryString(query);
}
}

View File

@ -35,7 +35,7 @@ public abstract class Rule
*/
public interface ApplyURI
{
void applyURI(Request request, String oldTarget, String newTarget) throws IOException;
void applyURI(Request request, String oldURI, String newURI) throws IOException;
}
protected boolean _terminating;

View File

@ -26,6 +26,8 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.HttpChannel;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.util.ArrayUtil;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.UrlEncoded;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@ -211,10 +213,11 @@ public class RuleContainer extends Rule
if (_rewriteRequestURI)
{
String encoded=URIUtil.encodePath(applied);
if (rule instanceof Rule.ApplyURI)
((Rule.ApplyURI)rule).applyURI((Request)request, target, applied);
((Rule.ApplyURI)rule).applyURI((Request)request,((Request)request).getRequestURI(), encoded);
else
((Request)request).setRequestURI(applied);
((Request)request).setRequestURI(encoded);
}
if (_rewritePathInfo)

View File

@ -166,6 +166,7 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
}
@Test
public void testEncodedPattern() throws Exception
{
_response.setStatus(200);
@ -173,15 +174,18 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
_handler.setOriginalPathAttribute("/before");
_handler.setRewriteRequestURI(true);
_handler.setRewritePathInfo(false);
_request.setRequestURI("/ccc/x%2Fy");
_request.setPathInfo("/ccc/x/y");
_handler.handle("/ccc/x/y",_request,_request, _response);
_request.setRequestURI("/ccc/x%20y");
_request.setPathInfo("/ccc/x y");
_handler.handle("/ccc/x y",_request,_request, _response);
assertEquals(201,_response.getStatus());
assertEquals("/ddd/x/y",_request.getAttribute("target"));
assertEquals("/ddd/x%2Fy",_request.getAttribute("URI"));
assertEquals("/ccc/x/y",_request.getAttribute("info"));
assertEquals("/ddd/x y",_request.getAttribute("target"));
assertEquals("/ddd/x%20y",_request.getAttribute("URI"));
assertEquals("/ccc/x y",_request.getAttribute("info"));
}
@Test
public void testEncodedRegex() throws Exception
{
_response.setStatus(200);
@ -189,13 +193,13 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
_handler.setOriginalPathAttribute("/before");
_handler.setRewriteRequestURI(true);
_handler.setRewritePathInfo(false);
_request.setRequestURI("/xxx/x%2Fy");
_request.setPathInfo("/xxx/x/y");
_handler.handle("/xxx/x/y",_request,_request, _response);
_request.setRequestURI("/xxx/x%20y");
_request.setPathInfo("/xxx/x y");
_handler.handle("/xxx/x y",_request,_request, _response);
assertEquals(201,_response.getStatus());
assertEquals("/x/y/zzz",_request.getAttribute("target"));
assertEquals("/x%2Fy/zzz",_request.getAttribute("URI"));
assertEquals("/xxx/x/y",_request.getAttribute("info"));
assertEquals("/x y/zzz",_request.getAttribute("target"));
assertEquals("/x%20y/zzz",_request.getAttribute("URI"));
assertEquals("/xxx/x y",_request.getAttribute("info"));
}
}

View File

@ -36,6 +36,7 @@ public class RewritePatternRuleTest extends AbstractRuleTestCase
{"/foo/bar", "/foo/*", "/replace/bar"},
{"/foo/bar", "/foo/bar", "/replace"},
{"/foo/bar.txt", "*.txt", "/replace"},
{"/foo/bar/%20x", "/foo/*", "/replace/bar/%20x"},
};
private RewritePatternRule _rule;

View File

@ -25,6 +25,7 @@ import java.nio.charset.StandardCharsets;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.util.MultiMap;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.UrlEncoded;
import org.junit.Before;
import org.junit.Test;
@ -37,6 +38,7 @@ public class RewriteRegexRuleTest extends AbstractRuleTestCase
{"/foo1/bar","n=v",".*","/replace","/replace","n=v"},
{"/foo2/bar",null,"/xxx.*","/replace",null,null},
{"/foo3/bar",null,"/(.*)/(.*)","/$2/$1/xxx","/bar/foo3/xxx",null},
{"/f%20o3/bar",null,"/(.*)/(.*)","/$2/$1/xxx","/bar/f%20o3/xxx",null},
{"/foo4/bar",null,"/(.*)/(.*)","/test?p2=$2&p1=$1","/test","p2=bar&p1=foo4"},
{"/foo5/bar","n=v","/(.*)/(.*)","/test?p2=$2&p1=$1","/test","n=v&p2=bar&p1=foo5"},
{"/foo6/bar",null,"/(.*)/(.*)","/foo6/bar?p2=$2&p1=$1","/foo6/bar","p2=bar&p1=foo6"},
@ -112,8 +114,8 @@ public class RewriteRegexRuleTest extends AbstractRuleTestCase
_request.setQueryString(test[1]);
_request.getAttributes().clearAttributes();
String result = container.apply(test[0],_request,_response);
assertEquals(t,test[4]==null?test[0]:test[4], result);
String result = container.apply(URIUtil.decodePath(test[0]),_request,_response);
assertEquals(t,URIUtil.decodePath(test[4]==null?test[0]:test[4]), result);
assertEquals(t,test[4]==null?test[0]:test[4], _request.getRequestURI());
assertEquals(t,test[5], _request.getQueryString());
}

View File

@ -274,7 +274,6 @@ public class HttpChannel<T> implements HttpParser.RequestHandler<T>, Runnable
case REQUEST_DISPATCH:
_request.setHandled(false);
_response.getHttpOutput().reopen();
_request.setTimeStamp(System.currentTimeMillis());
_request.setDispatcherType(DispatcherType.REQUEST);
for (HttpConfiguration.Customizer customizer : _configuration.getCustomizers())
@ -486,8 +485,7 @@ public class HttpChannel<T> implements HttpParser.RequestHandler<T>, Runnable
_expect100Continue = false;
_expect102Processing = false;
if (_request.getTimeStamp() == 0)
_request.setTimeStamp(System.currentTimeMillis());
_request.setTimeStamp(System.currentTimeMillis());
_request.setMethod(httpMethod, method);
if (httpMethod == HttpMethod.CONNECT)

View File

@ -0,0 +1,238 @@
//
// ========================================================================
// 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.servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.hamcrest.Matchers;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class RequestURITest
{
@Parameters(name = "rawpath: \"{0}\"")
public static List<String[]> data()
{
List<String[]> ret = new ArrayList<>();
ret.add(new String[] { "/hello", "/hello", null });
ret.add(new String[] { "/hello%20world", "/hello%20world", null });
ret.add(new String[] { "/hello;world", "/hello;world", null });
ret.add(new String[] { "/hello:world", "/hello:world", null });
ret.add(new String[] { "/hello!world", "/hello!world", null });
ret.add(new String[] { "/hello?world", "/hello", "world" });
ret.add(new String[] { "/hello?type=world", "/hello", "type=world" });
ret.add(new String[] { "/hello?type=wo&rld", "/hello", "type=wo&rld" });
ret.add(new String[] { "/hello?type=wo%20rld", "/hello", "type=wo%20rld" });
ret.add(new String[] { "/hello?type=wo+rld", "/hello", "type=wo+rld" });
ret.add(new String[] { "/It%27s%20me%21", "/It%27s%20me%21", null });
// try some slash encoding (with case preservation tests)
ret.add(new String[] { "/hello%2fworld", "/hello%2fworld", null });
ret.add(new String[] { "/hello%2Fworld", "/hello%2Fworld", null });
ret.add(new String[] { "/%2f%2Fhello%2Fworld", "/%2f%2Fhello%2Fworld", null });
// try some "?" encoding (should not see as query string)
ret.add(new String[] { "/hello%3Fworld", "/hello%3Fworld", null });
// try some strange encodings (should preserve them)
ret.add(new String[] { "/hello%252Fworld", "/hello%252Fworld", null });
ret.add(new String[] { "/hello%u0025world", "/hello%u0025world", null });
ret.add(new String[] { "/hello-euro-%E2%82%AC", "/hello-euro-%E2%82%AC", null });
ret.add(new String[] { "/hello-euro?%E2%82%AC", "/hello-euro","%E2%82%AC" });
// test the ascii control characters (just for completeness)
for (int i = 0x0; i < 0x1f; i++)
{
String raw = String.format("/hello%%%02Xworld",i);
ret.add(new String[] { raw, raw, null });
}
return ret;
}
@SuppressWarnings("serial")
public static class RequestUriServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
out.println("RequestURI: " + req.getRequestURI());
out.println("QueryString: " + req.getQueryString());
out.print("FullURI: " + req.getRequestURI());
if (req.getQueryString() != null)
{
out.print('?');
out.print(req.getQueryString());
}
out.println();
}
}
private static Server server;
private static URI serverURI;
@Parameter(value = 0)
public String rawpath;
@Parameter(value = 1)
public String expectedReqUri;
@Parameter(value = 2)
public String expectedQuery;
@BeforeClass
public static void startServer() throws Exception
{
server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
server.addConnector(connector);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
server.setHandler(context);
context.addServlet(RequestUriServlet.class,"/*");
server.start();
String host = connector.getHost();
if (host == null)
{
host = "localhost";
}
int port = connector.getLocalPort();
serverURI = new URI(String.format("http://%s:%d/",host,port));
}
@AfterClass
public static void stopServer()
{
try
{
server.stop();
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
}
protected Socket newSocket(String host, int port) throws Exception
{
Socket socket = new Socket(host,port);
socket.setSoTimeout(10000);
socket.setTcpNoDelay(true);
socket.setSoLinger(false,0);
return socket;
}
/**
* Read entire response from the client. Close the output.
*
* @param client
* Open client socket.
* @return The response string.
* @throws IOException
* in case of I/O problems
*/
protected static String readResponse(Socket client) throws IOException
{
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream())))
{
String line;
while ((line = br.readLine()) != null)
{
sb.append(line);
sb.append('\n');
}
return sb.toString();
}
catch (IOException e)
{
System.err.println(e + " while reading '" + sb + "'");
throw e;
}
}
@Test
public void testGetRequestURI_HTTP10() throws Exception
{
try (Socket client = newSocket(serverURI.getHost(),serverURI.getPort()))
{
OutputStream os = client.getOutputStream();
String request = String.format("GET %s HTTP/1.0\r\n\r\n",rawpath);
os.write(request.getBytes(StandardCharsets.ISO_8859_1));
os.flush();
// Read the response.
String response = readResponse(client);
// TODO: is HTTP/1.1 response appropriate for a HTTP/1.0 request?
Assert.assertThat(response,Matchers.containsString("HTTP/1.1 200 OK"));
Assert.assertThat(response,Matchers.containsString("RequestURI: " + expectedReqUri));
Assert.assertThat(response,Matchers.containsString("QueryString: " + expectedQuery));
}
}
@Test
public void testGetRequestURI_HTTP11() throws Exception
{
try (Socket client = newSocket(serverURI.getHost(),serverURI.getPort()))
{
OutputStream os = client.getOutputStream();
String request = String.format("GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n",rawpath,serverURI.getHost());
os.write(request.getBytes(StandardCharsets.ISO_8859_1));
os.flush();
// Read the response.
String response = readResponse(client);
Assert.assertThat(response,Matchers.containsString("HTTP/1.1 200 OK"));
Assert.assertThat(response,Matchers.containsString("RequestURI: " + expectedReqUri));
Assert.assertThat(response,Matchers.containsString("QueryString: " + expectedQuery));
}
}
}

View File

@ -36,9 +36,17 @@ import org.eclipse.jetty.util.log.Logger;
* Beans can be added the ContainerLifeCycle either as managed beans or as unmanaged beans. A managed bean is started, stopped and destroyed with the aggregate.
* An unmanaged bean is associated with the aggregate for the purposes of {@link #dump()}, but it's lifecycle must be managed externally.
* <p>
* When a {@link LifeCycle} bean is added with out a managed state being specified, if it is already started, then it is assumed to be an unmanaged bean.
* If it is not started then it is added in and auto managed state, which means that when this bean is itself started, it if must also start the added bean, then it
* is switched from Auto to be a managed bean. Otherwise it becomes an unmanaged bean. Simply put an Auto bean will be stopped by this aggregate only if it
* When a {@link LifeCycle} bean is added without a managed state being specified the state is determined heuristically:
* <ul>
* <li>If when added, a bean is already started, then it is assumed to be an unmanaged bean.
* <li>If when added, a bean is not started, then it is added in Auto state.
* </ul>
* When the container is started, then all contained managed beans will also be started. Any contained Auto beans
* will be check for their status and if already started will be switched unmanaged beans, else they will be
* started and switched to managed beans. Beans added after a container is started are not started and their state needs to
* be explicitly managed.
* <p>
* When stopping the container, a contained bean will be stopped by this aggregate only if it
* is started by this aggregate.
* <p>
* The methods {@link #addBean(Object, boolean)}, {@link #manage(Object)} and {@link #unmanage(Object)} can be used to