jetty-9 ported rewrite
This commit is contained in:
parent
fac580c374
commit
9d0828614f
|
@ -70,8 +70,8 @@
|
|||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<groupId>org.eclipse.jetty.toolchain</groupId>
|
||||
<artifactId>jetty-test-helper</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -88,11 +88,5 @@
|
|||
<groupId>org.eclipse.jetty.orbit</groupId>
|
||||
<artifactId>javax.servlet</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>test-jetty-servlet</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -70,6 +70,7 @@ public class CookiePatternRule extends PatternRule
|
|||
* (non-Javadoc)
|
||||
* @see org.eclipse.jetty.server.server.handler.rules.RuleBase#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
|
||||
*/
|
||||
@Override
|
||||
public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
|
||||
{
|
||||
response.addCookie(new Cookie(_name, _value));
|
||||
|
@ -80,6 +81,7 @@ public class CookiePatternRule extends PatternRule
|
|||
/**
|
||||
* Returns the cookie contents.
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return super.toString()+"["+_name+","+_value + "]";
|
||||
|
|
|
@ -50,6 +50,7 @@ public class ForwardedSchemeHeaderRule extends HeaderRule {
|
|||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
protected String apply(String target, String value, HttpServletRequest request, HttpServletResponse response)
|
||||
{
|
||||
((Request) request).setScheme(_scheme);
|
||||
|
|
|
@ -80,6 +80,7 @@ public class HeaderPatternRule extends PatternRule
|
|||
*
|
||||
*@see org.eclipse.jetty.rewrite.handler.Rule#matchAndApply(String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
|
||||
*/
|
||||
@Override
|
||||
public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
|
||||
{
|
||||
// process header
|
||||
|
@ -125,6 +126,7 @@ public class HeaderPatternRule extends PatternRule
|
|||
/**
|
||||
* Returns the header contents.
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return super.toString()+"["+_name+","+_value+"]";
|
||||
|
|
|
@ -99,6 +99,7 @@ public abstract class HeaderRule extends Rule
|
|||
protected abstract String apply(String target, String value, HttpServletRequest request, HttpServletResponse response) throws IOException;
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return super.toString() + "[" + _header + ":" + _headerValue + "]";
|
||||
|
|
|
@ -23,8 +23,8 @@ import java.io.IOException;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.eclipse.jetty.http.HttpHeaderValues;
|
||||
import org.eclipse.jetty.http.HttpHeaders;
|
||||
import org.eclipse.jetty.http.HttpHeader;
|
||||
import org.eclipse.jetty.http.HttpHeaderValue;
|
||||
import org.eclipse.jetty.util.StringMap;
|
||||
|
||||
/**
|
||||
|
@ -55,11 +55,12 @@ public class MsieSslRule extends Rule
|
|||
_terminating = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
|
||||
{
|
||||
if (request.isSecure())
|
||||
{
|
||||
String user_agent = request.getHeader(HttpHeaders.USER_AGENT);
|
||||
String user_agent = request.getHeader(HttpHeader.USER_AGENT.asString());
|
||||
|
||||
if (user_agent!=null)
|
||||
{
|
||||
|
@ -71,7 +72,7 @@ public class MsieSslRule extends Rule
|
|||
|
||||
if ( ieVersion<=IEv5)
|
||||
{
|
||||
response.setHeader(HttpHeaders.CONNECTION, HttpHeaderValues.CLOSE);
|
||||
response.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString());
|
||||
return target;
|
||||
}
|
||||
|
||||
|
@ -81,9 +82,9 @@ public class MsieSslRule extends Rule
|
|||
if (windows>0)
|
||||
{
|
||||
int end=user_agent.indexOf(')',windows+8);
|
||||
if(end<0 || __IE6_BadOS.getEntry(user_agent,windows+8,end-windows-8)!=null)
|
||||
if(end<0 || __IE6_BadOS.get(user_agent,windows+8,end-windows-8)!=null)
|
||||
{
|
||||
response.setHeader(HttpHeaders.CONNECTION, HttpHeaderValues.CLOSE);
|
||||
response.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString());
|
||||
return target;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -55,6 +55,7 @@ public abstract class PatternRule extends Rule
|
|||
/* (non-Javadoc)
|
||||
* @see org.eclipse.jetty.server.server.handler.rules.RuleBase#matchAndApply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
|
||||
*/
|
||||
@Override
|
||||
public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
|
||||
{
|
||||
if (PathMap.match(_pattern, target))
|
||||
|
@ -77,6 +78,7 @@ public abstract class PatternRule extends Rule
|
|||
/**
|
||||
* Returns the rule pattern.
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return super.toString()+"["+_pattern+"]";
|
||||
|
|
|
@ -1,502 +0,0 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// 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.rewrite.handler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashSet;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.eclipse.jetty.client.HttpClient;
|
||||
import org.eclipse.jetty.client.HttpExchange;
|
||||
import org.eclipse.jetty.http.HttpHeaderValues;
|
||||
import org.eclipse.jetty.http.HttpHeaders;
|
||||
import org.eclipse.jetty.http.HttpURI;
|
||||
import org.eclipse.jetty.http.PathMap;
|
||||
import org.eclipse.jetty.io.Buffer;
|
||||
import org.eclipse.jetty.io.EofException;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
import org.eclipse.jetty.util.thread.QueuedThreadPool;
|
||||
|
||||
/**
|
||||
* This rule allows the user to configure a particular rewrite rule that will proxy out
|
||||
* to a configured location. This rule uses the jetty http client.
|
||||
*
|
||||
* Rule rule = new ProxyRule();
|
||||
* rule.setPattern("/foo/*");
|
||||
* rule.setProxyTo("http://url.com");
|
||||
*
|
||||
* see api for other configuration options which influence the configuration of the jetty
|
||||
* client instance
|
||||
*
|
||||
*/
|
||||
public class ProxyRule extends PatternRule
|
||||
{
|
||||
private static final Logger _log = Log.getLogger(ProxyRule.class);
|
||||
|
||||
private HttpClient _client;
|
||||
private String _hostHeader;
|
||||
private String _proxyTo;
|
||||
|
||||
private int _connectorType = HttpClient.CONNECTOR_SELECT_CHANNEL;
|
||||
private String _maxThreads;
|
||||
private String _maxConnections;
|
||||
private String _timeout;
|
||||
private String _idleTimeout;
|
||||
private String _requestHeaderSize;
|
||||
private String _requestBufferSize;
|
||||
private String _responseHeaderSize;
|
||||
private String _responseBufferSize;
|
||||
|
||||
private HashSet<String> _DontProxyHeaders = new HashSet<String>();
|
||||
{
|
||||
_DontProxyHeaders.add("proxy-connection");
|
||||
_DontProxyHeaders.add("connection");
|
||||
_DontProxyHeaders.add("keep-alive");
|
||||
_DontProxyHeaders.add("transfer-encoding");
|
||||
_DontProxyHeaders.add("te");
|
||||
_DontProxyHeaders.add("trailer");
|
||||
_DontProxyHeaders.add("proxy-authorization");
|
||||
_DontProxyHeaders.add("proxy-authenticate");
|
||||
_DontProxyHeaders.add("upgrade");
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public ProxyRule()
|
||||
{
|
||||
_handling = true;
|
||||
_terminating = true;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
private void initializeClient() throws Exception
|
||||
{
|
||||
_client = new HttpClient();
|
||||
_client.setConnectorType(_connectorType);
|
||||
|
||||
if ( _maxThreads != null )
|
||||
{
|
||||
_client.setThreadPool(new QueuedThreadPool(Integer.parseInt(_maxThreads)));
|
||||
}
|
||||
else
|
||||
{
|
||||
_client.setThreadPool(new QueuedThreadPool());
|
||||
}
|
||||
|
||||
if ( _maxConnections != null )
|
||||
{
|
||||
_client.setMaxConnectionsPerAddress(Integer.parseInt(_maxConnections));
|
||||
}
|
||||
|
||||
if ( _timeout != null )
|
||||
{
|
||||
_client.setTimeout(Long.parseLong(_timeout));
|
||||
}
|
||||
|
||||
if ( _idleTimeout != null )
|
||||
{
|
||||
_client.setIdleTimeout(Long.parseLong(_idleTimeout));
|
||||
}
|
||||
|
||||
if ( _requestBufferSize != null )
|
||||
{
|
||||
_client.setRequestBufferSize(Integer.parseInt(_requestBufferSize));
|
||||
}
|
||||
|
||||
if ( _requestHeaderSize != null )
|
||||
{
|
||||
_client.setRequestHeaderSize(Integer.parseInt(_requestHeaderSize));
|
||||
}
|
||||
|
||||
if ( _responseBufferSize != null )
|
||||
{
|
||||
_client.setResponseBufferSize(Integer.parseInt(_responseBufferSize));
|
||||
}
|
||||
|
||||
if ( _responseHeaderSize != null )
|
||||
{
|
||||
_client.setResponseHeaderSize(Integer.parseInt(_responseHeaderSize));
|
||||
}
|
||||
|
||||
_client.start();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
private HttpURI proxyHttpURI(String uri) throws MalformedURLException
|
||||
{
|
||||
return new HttpURI(_proxyTo + uri);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
protected String apply(String target, HttpServletRequest request, final HttpServletResponse response) throws IOException
|
||||
{
|
||||
synchronized (this)
|
||||
{
|
||||
if (_client == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
initializeClient();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new IOException("Unable to proxy: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final int debug = _log.isDebugEnabled()?request.hashCode():0;
|
||||
|
||||
final InputStream in = request.getInputStream();
|
||||
final OutputStream out = response.getOutputStream();
|
||||
|
||||
HttpURI url = createUrl(request,debug);
|
||||
|
||||
if (url == null)
|
||||
{
|
||||
response.sendError(HttpServletResponse.SC_FORBIDDEN);
|
||||
return target;
|
||||
}
|
||||
|
||||
HttpExchange exchange = new HttpExchange()
|
||||
{
|
||||
@Override
|
||||
protected void onRequestCommitted() throws IOException
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onRequestComplete() throws IOException
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResponseComplete() throws IOException
|
||||
{
|
||||
if (debug != 0)
|
||||
_log.debug(debug + " complete");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResponseContent(Buffer content) throws IOException
|
||||
{
|
||||
if (debug != 0)
|
||||
_log.debug(debug + " content" + content.length());
|
||||
content.writeTo(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResponseHeaderComplete() throws IOException
|
||||
{
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
protected void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException
|
||||
{
|
||||
if (debug != 0)
|
||||
_log.debug(debug + " " + version + " " + status + " " + reason);
|
||||
|
||||
if (reason != null && reason.length() > 0)
|
||||
response.setStatus(status,reason.toString());
|
||||
else
|
||||
response.setStatus(status);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResponseHeader(Buffer name, Buffer value) throws IOException
|
||||
{
|
||||
String s = name.toString().toLowerCase();
|
||||
if (!_DontProxyHeaders.contains(s) || (HttpHeaders.CONNECTION_BUFFER.equals(name) && HttpHeaderValues.CLOSE_BUFFER.equals(value)))
|
||||
{
|
||||
if (debug != 0)
|
||||
_log.debug(debug + " " + name + ": " + value);
|
||||
|
||||
response.addHeader(name.toString(),value.toString());
|
||||
}
|
||||
else if (debug != 0)
|
||||
_log.debug(debug + " " + name + "! " + value);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onConnectionFailed(Throwable ex)
|
||||
{
|
||||
_log.warn(ex.toString());
|
||||
_log.debug(ex);
|
||||
if (!response.isCommitted())
|
||||
{
|
||||
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onException(Throwable ex)
|
||||
{
|
||||
if (ex instanceof EofException)
|
||||
{
|
||||
_log.ignore(ex);
|
||||
return;
|
||||
}
|
||||
_log.warn(ex.toString());
|
||||
_log.debug(ex);
|
||||
if (!response.isCommitted())
|
||||
{
|
||||
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onExpire()
|
||||
{
|
||||
if (!response.isCommitted())
|
||||
{
|
||||
response.setStatus(HttpServletResponse.SC_GATEWAY_TIMEOUT);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
exchange.setMethod(request.getMethod());
|
||||
exchange.setURL(url.toString());
|
||||
exchange.setVersion(request.getProtocol());
|
||||
|
||||
if (debug != 0)
|
||||
{
|
||||
_log.debug("{} {} {} {}", debug ,request.getMethod(), url, request.getProtocol());
|
||||
}
|
||||
|
||||
boolean hasContent = createHeaders(request,debug,exchange);
|
||||
|
||||
if (hasContent)
|
||||
{
|
||||
exchange.setRequestContentSource(in);
|
||||
}
|
||||
|
||||
/*
|
||||
* we need to set the timeout on the exchange to take into account the timeout of the HttpClient and the HttpExchange
|
||||
*/
|
||||
long ctimeout = (_client.getTimeout() > exchange.getTimeout())?_client.getTimeout():exchange.getTimeout();
|
||||
exchange.setTimeout(ctimeout);
|
||||
|
||||
_client.send(exchange);
|
||||
|
||||
try
|
||||
{
|
||||
exchange.waitForDone();
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
_log.info("Exception while waiting for response on proxied request", e);
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
private HttpURI createUrl(HttpServletRequest request, final int debug) throws MalformedURLException
|
||||
{
|
||||
String uri = request.getRequestURI();
|
||||
|
||||
if (request.getQueryString() != null)
|
||||
{
|
||||
uri += "?" + request.getQueryString();
|
||||
}
|
||||
|
||||
uri = PathMap.pathInfo(_pattern,uri);
|
||||
|
||||
if(uri==null)
|
||||
{
|
||||
uri = "/";
|
||||
}
|
||||
|
||||
HttpURI url = proxyHttpURI(uri);
|
||||
|
||||
if (debug != 0)
|
||||
{
|
||||
_log.debug(debug + " proxy " + uri + "-->" + url);
|
||||
}
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
private boolean createHeaders(final HttpServletRequest request, final int debug, HttpExchange exchange)
|
||||
{
|
||||
// check connection header
|
||||
String connectionHdr = request.getHeader("Connection");
|
||||
if (connectionHdr != null)
|
||||
{
|
||||
connectionHdr = connectionHdr.toLowerCase();
|
||||
if (connectionHdr.indexOf("keep-alive") < 0 && connectionHdr.indexOf("close") < 0)
|
||||
{
|
||||
connectionHdr = null;
|
||||
}
|
||||
}
|
||||
|
||||
// force host
|
||||
if (_hostHeader != null)
|
||||
{
|
||||
exchange.setRequestHeader("Host",_hostHeader);
|
||||
}
|
||||
|
||||
// copy headers
|
||||
boolean xForwardedFor = false;
|
||||
boolean hasContent = false;
|
||||
long contentLength = -1;
|
||||
Enumeration<?> enm = request.getHeaderNames();
|
||||
while (enm.hasMoreElements())
|
||||
{
|
||||
// TODO could be better than this!
|
||||
String hdr = (String)enm.nextElement();
|
||||
String lhdr = hdr.toLowerCase();
|
||||
|
||||
if (_DontProxyHeaders.contains(lhdr))
|
||||
continue;
|
||||
if (connectionHdr != null && connectionHdr.indexOf(lhdr) >= 0)
|
||||
continue;
|
||||
if (_hostHeader != null && "host".equals(lhdr))
|
||||
continue;
|
||||
|
||||
if ("content-type".equals(lhdr))
|
||||
hasContent = true;
|
||||
else if ("content-length".equals(lhdr))
|
||||
{
|
||||
contentLength = request.getContentLength();
|
||||
exchange.setRequestHeader(HttpHeaders.CONTENT_LENGTH,Long.toString(contentLength));
|
||||
if (contentLength > 0)
|
||||
hasContent = true;
|
||||
}
|
||||
else if ("x-forwarded-for".equals(lhdr))
|
||||
xForwardedFor = true;
|
||||
|
||||
Enumeration<?> vals = request.getHeaders(hdr);
|
||||
while (vals.hasMoreElements())
|
||||
{
|
||||
String val = (String)vals.nextElement();
|
||||
if (val != null)
|
||||
{
|
||||
if (debug != 0)
|
||||
_log.debug("{} {} {}",debug,hdr,val);
|
||||
|
||||
exchange.setRequestHeader(hdr,val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Proxy headers
|
||||
exchange.setRequestHeader("Via","1.1 (jetty)");
|
||||
if (!xForwardedFor)
|
||||
{
|
||||
exchange.addRequestHeader("X-Forwarded-For",request.getRemoteAddr());
|
||||
exchange.addRequestHeader("X-Forwarded-Proto",request.getScheme());
|
||||
exchange.addRequestHeader("X-Forwarded-Host",request.getServerName());
|
||||
exchange.addRequestHeader("X-Forwarded-Server",request.getLocalName());
|
||||
}
|
||||
return hasContent;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public void setProxyTo(String proxyTo)
|
||||
{
|
||||
this._proxyTo = proxyTo;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public void setMaxThreads(String maxThreads)
|
||||
{
|
||||
this._maxThreads = maxThreads;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public void setMaxConnections(String maxConnections)
|
||||
{
|
||||
_maxConnections = maxConnections;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public void setTimeout(String timeout)
|
||||
{
|
||||
_timeout = timeout;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public void setIdleTimeout(String idleTimeout)
|
||||
{
|
||||
_idleTimeout = idleTimeout;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public void setRequestHeaderSize(String requestHeaderSize)
|
||||
{
|
||||
_requestHeaderSize = requestHeaderSize;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public void setRequestBufferSize(String requestBufferSize)
|
||||
{
|
||||
_requestBufferSize = requestBufferSize;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public void setResponseHeaderSize(String responseHeaderSize)
|
||||
{
|
||||
_responseHeaderSize = responseHeaderSize;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public void setResponseBufferSize(String responseBufferSize)
|
||||
{
|
||||
_responseBufferSize = responseBufferSize;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public void addDontProxyHeaders(String dontProxyHeader)
|
||||
{
|
||||
_DontProxyHeaders.add(dontProxyHeader);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* CONNECTOR_SOCKET = 0;
|
||||
* CONNECTOR_SELECT_CHANNEL = 2; (default)
|
||||
*
|
||||
* @param connectorType
|
||||
*/
|
||||
public void setConnectorType( int connectorType )
|
||||
{
|
||||
_connectorType = connectorType;
|
||||
}
|
||||
|
||||
public String getHostHeader()
|
||||
{
|
||||
return _hostHeader;
|
||||
}
|
||||
|
||||
public void setHostHeader(String hostHeader)
|
||||
{
|
||||
_hostHeader = hostHeader;
|
||||
}
|
||||
|
||||
}
|
|
@ -53,6 +53,7 @@ public class RedirectPatternRule extends PatternRule
|
|||
* (non-Javadoc)
|
||||
* @see org.eclipse.jetty.server.server.handler.rules.RuleBase#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
|
||||
*/
|
||||
@Override
|
||||
public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
|
||||
{
|
||||
response.sendRedirect(response.encodeRedirectURL(_location));
|
||||
|
@ -63,6 +64,7 @@ public class RedirectPatternRule extends PatternRule
|
|||
/**
|
||||
* Returns the redirect location.
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return super.toString()+"["+_location+"]";
|
||||
|
|
|
@ -55,6 +55,7 @@ public abstract class RegexRule extends Rule
|
|||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
|
||||
{
|
||||
Matcher matcher=_regex.matcher(target);
|
||||
|
@ -82,6 +83,7 @@ public abstract class RegexRule extends Rule
|
|||
/**
|
||||
* Returns the regular expression string.
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return super.toString()+"["+_regex+"]";
|
||||
|
|
|
@ -65,6 +65,7 @@ public class ResponsePatternRule extends PatternRule
|
|||
* (non-Javadoc)
|
||||
* @see org.eclipse.jetty.server.server.handler.rules.RuleBase#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
|
||||
*/
|
||||
@Override
|
||||
public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
|
||||
{
|
||||
int code = Integer.parseInt(_code);
|
||||
|
@ -85,6 +86,7 @@ public class ResponsePatternRule extends PatternRule
|
|||
/**
|
||||
* Returns the code and reason string.
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return super.toString()+"["+_code+","+_reason+"]";
|
||||
|
|
|
@ -57,6 +57,7 @@ public class RewritePatternRule extends PatternRule implements Rule.ApplyURI
|
|||
* (non-Javadoc)
|
||||
* @see org.eclipse.jetty.server.handler.rules.RuleBase#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
|
||||
*/
|
||||
@Override
|
||||
public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
|
||||
{
|
||||
target = URIUtil.addPaths(_replacement, PathMap.pathInfo(_pattern,target));
|
||||
|
@ -64,6 +65,7 @@ public class RewritePatternRule extends PatternRule implements Rule.ApplyURI
|
|||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public void applyURI(Request request, String oldTarget, String newTarget) throws IOException
|
||||
{
|
||||
String uri = URIUtil.addPaths(_replacement, PathMap.pathInfo(_pattern,request.getRequestURI()));
|
||||
|
@ -74,6 +76,7 @@ public class RewritePatternRule extends PatternRule implements Rule.ApplyURI
|
|||
/**
|
||||
* Returns the replacement string.
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return super.toString()+"["+_replacement+"]";
|
||||
|
|
|
@ -67,6 +67,7 @@ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI
|
|||
/* (non-Javadoc)
|
||||
* @see org.eclipse.jetty.server.handler.rules.RegexRule#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.util.regex.Matcher)
|
||||
*/
|
||||
@Override
|
||||
public String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException
|
||||
{
|
||||
target=_replacement;
|
||||
|
@ -94,6 +95,7 @@ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI
|
|||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public void applyURI(Request request, String oldTarget, String newTarget) throws IOException
|
||||
{
|
||||
if (_query==null)
|
||||
|
@ -117,6 +119,7 @@ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI
|
|||
/**
|
||||
* Returns the replacement string.
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return super.toString()+"["+_replacement+"]";
|
||||
|
|
|
@ -97,6 +97,7 @@ public abstract class Rule
|
|||
/**
|
||||
* Returns the handling and terminating flag values.
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return this.getClass().getName()+(_handling?"[H":"[h")+(_terminating?"T]":"t]");
|
||||
|
|
|
@ -23,9 +23,9 @@ import java.io.IOException;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.eclipse.jetty.server.AbstractHttpConnection;
|
||||
import org.eclipse.jetty.server.HttpChannel;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.util.LazyList;
|
||||
import org.eclipse.jetty.util.ArrayUtil;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
|
||||
|
@ -110,7 +110,7 @@ public class RuleContainer extends Rule
|
|||
*/
|
||||
public void addRule(Rule rule)
|
||||
{
|
||||
_rules = (Rule[])LazyList.addToArray(_rules,rule,Rule.class);
|
||||
_rules = ArrayUtil.addToArray(_rules,rule,Rule.class);
|
||||
}
|
||||
|
||||
|
||||
|
@ -225,7 +225,7 @@ public class RuleContainer extends Rule
|
|||
if (rule.isHandling())
|
||||
{
|
||||
LOG.debug("handling {}",rule);
|
||||
(request instanceof Request?(Request)request:AbstractHttpConnection.getCurrentConnection().getRequest()).setHandled(true);
|
||||
(request instanceof Request?(Request)request:HttpChannel.getCurrentHttpChannel().getRequest()).setHandled(true);
|
||||
}
|
||||
|
||||
if (rule.isTerminating())
|
||||
|
|
|
@ -23,7 +23,7 @@ import java.io.IOException;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.eclipse.jetty.util.LazyList;
|
||||
import org.eclipse.jetty.util.ArrayUtil;
|
||||
|
||||
/**
|
||||
* Groups rules that apply only to a specific virtual host
|
||||
|
@ -72,7 +72,7 @@ public class VirtualHostRuleContainer extends RuleContainer
|
|||
*/
|
||||
public void addVirtualHost(String virtualHost)
|
||||
{
|
||||
_virtualHosts = (String[])LazyList.addToArray(_virtualHosts,virtualHost,String.class);
|
||||
_virtualHosts = ArrayUtil.addToArray(_virtualHosts,virtualHost,String.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,24 +18,30 @@
|
|||
|
||||
package org.eclipse.jetty.rewrite.handler;
|
||||
|
||||
import org.eclipse.jetty.io.bio.StringEndPoint;
|
||||
import org.eclipse.jetty.server.BlockingHttpConnection;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.eclipse.jetty.server.Connector;
|
||||
import org.eclipse.jetty.server.AbstractHttpConnection;
|
||||
import org.eclipse.jetty.server.HttpChannelConfig;
|
||||
import org.eclipse.jetty.server.HttpConnectionFactory;
|
||||
import org.eclipse.jetty.server.LocalConnector;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.server.Response;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.handler.AbstractHandler;
|
||||
import org.junit.After;
|
||||
|
||||
public abstract class AbstractRuleTestCase
|
||||
{
|
||||
protected Server _server = new Server();
|
||||
protected LocalConnector _connector;
|
||||
protected StringEndPoint _endpoint = new StringEndPoint();
|
||||
protected AbstractHttpConnection _connection;
|
||||
protected Request _request;
|
||||
protected Response _response;
|
||||
protected volatile Request _request;
|
||||
protected volatile Response _response;
|
||||
protected volatile CountDownLatch _latch;
|
||||
protected boolean _isSecure = false;
|
||||
|
||||
@After
|
||||
|
@ -46,31 +52,65 @@ public abstract class AbstractRuleTestCase
|
|||
|
||||
protected void start(final boolean isSecure) throws Exception
|
||||
{
|
||||
_connector = new LocalConnector()
|
||||
_connector = new LocalConnector(_server);
|
||||
_connector.getConnectionFactory(HttpConnectionFactory.class).getHttpChannelConfig().addCustomizer(new HttpChannelConfig.Customizer()
|
||||
{
|
||||
public boolean isConfidential(Request request)
|
||||
@Override
|
||||
public void customize(Connector connector, HttpChannelConfig channelConfig, Request request)
|
||||
{
|
||||
return isSecure;
|
||||
request.setSecure(isSecure);
|
||||
}
|
||||
};
|
||||
});
|
||||
_server.setConnectors(new Connector[]{_connector});
|
||||
|
||||
|
||||
_server.setHandler(new AbstractHandler()
|
||||
{
|
||||
@Override
|
||||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
||||
{
|
||||
_request=baseRequest;
|
||||
_response=_request.getResponse();
|
||||
try
|
||||
{
|
||||
_latch.await();
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
throw new ServletException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
_server.start();
|
||||
reset();
|
||||
|
||||
_latch=new CountDownLatch(1);
|
||||
_connector.executeRequest("GET / HTTP/1.0\n\n");
|
||||
|
||||
while (_response==null)
|
||||
Thread.sleep(1);
|
||||
}
|
||||
|
||||
protected void reset()
|
||||
{
|
||||
if (_latch!=null)
|
||||
_latch.countDown();
|
||||
_request = null;
|
||||
_response = null;
|
||||
_latch=new CountDownLatch(1);
|
||||
_connector.executeRequest("GET / HTTP/1.0\n\n");
|
||||
|
||||
while (_response==null)
|
||||
Thread.yield();
|
||||
}
|
||||
|
||||
protected void stop() throws Exception
|
||||
{
|
||||
_latch.countDown();
|
||||
_server.stop();
|
||||
_server.join();
|
||||
_request = null;
|
||||
_response = null;
|
||||
}
|
||||
|
||||
protected void reset()
|
||||
{
|
||||
_connection = new BlockingHttpConnection(_connector, _endpoint, _server);
|
||||
_request = new Request(_connection);
|
||||
_response = new Response(_connection);
|
||||
_request.setRequestURI("/test/");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ import java.io.IOException;
|
|||
import java.util.Enumeration;
|
||||
|
||||
import org.eclipse.jetty.http.HttpFields;
|
||||
import org.eclipse.jetty.http.HttpHeaders;
|
||||
import org.eclipse.jetty.http.HttpHeader;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -73,7 +73,7 @@ public class CookiePatternRuleTest extends AbstractRuleTestCase
|
|||
|
||||
// verify
|
||||
HttpFields httpFields = _response.getHttpFields();
|
||||
Enumeration e = httpFields.getValues(HttpHeaders.SET_COOKIE_BUFFER);
|
||||
Enumeration e = httpFields.getValues(HttpHeader.SET_COOKIE.asString());
|
||||
int index = 0;
|
||||
while (e.hasMoreElements())
|
||||
{
|
||||
|
|
|
@ -34,7 +34,7 @@ public class ForwardedSchemeHeaderRuleTest extends AbstractRuleTestCase
|
|||
{
|
||||
start(false);
|
||||
_rule = new ForwardedSchemeHeaderRule();
|
||||
_requestHeaderFields = _connection.getRequestFields();
|
||||
_requestHeaderFields = _request.getHttpFields();
|
||||
_request.setScheme(null);
|
||||
}
|
||||
|
||||
|
|
|
@ -19,8 +19,8 @@
|
|||
package org.eclipse.jetty.rewrite.handler;
|
||||
|
||||
import org.eclipse.jetty.http.HttpFields;
|
||||
import org.eclipse.jetty.http.HttpHeaderValues;
|
||||
import org.eclipse.jetty.http.HttpHeaders;
|
||||
import org.eclipse.jetty.http.HttpHeader;
|
||||
import org.eclipse.jetty.http.HttpHeaderValue;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -41,186 +41,186 @@ public class MsieSslRuleTest extends AbstractRuleTestCase
|
|||
@Test
|
||||
public void testWin2kWithIE5() throws Exception
|
||||
{
|
||||
HttpFields fields = _connection.getRequestFields();
|
||||
HttpFields fields = _request.getHttpFields();
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.0)");
|
||||
|
||||
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
|
||||
assertEquals(_request.getRequestURI(), result);
|
||||
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(HttpHeaderValue.CLOSE.asString(), _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
|
||||
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
|
||||
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
assertEquals(_request.getRequestURI(), result);
|
||||
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(HttpHeaderValue.CLOSE.asString(), _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)");
|
||||
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
assertEquals(_request.getRequestURI(), result);
|
||||
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(HttpHeaderValue.CLOSE.asString(), _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWin2kWithIE6() throws Exception
|
||||
{
|
||||
HttpFields fields = _connection.getRequestFields();
|
||||
HttpFields fields = _request.getHttpFields();
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
|
||||
|
||||
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
|
||||
assertEquals(_request.getRequestURI(), result);
|
||||
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(HttpHeaderValue.CLOSE.asString(), _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWin2kWithIE7() throws Exception
|
||||
{
|
||||
HttpFields fields = _connection.getRequestFields();
|
||||
HttpFields fields = _request.getHttpFields();
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.0)");
|
||||
|
||||
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
|
||||
assertEquals(null, result);
|
||||
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(null, _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWin2kSP1WithIE5() throws Exception
|
||||
{
|
||||
HttpFields fields = _connection.getRequestFields();
|
||||
HttpFields fields = _request.getHttpFields();
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.01)");
|
||||
|
||||
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
|
||||
assertEquals(_request.getRequestURI(), result);
|
||||
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(HttpHeaderValue.CLOSE.asString(), _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.01)");
|
||||
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
assertEquals(_request.getRequestURI(), result);
|
||||
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(HttpHeaderValue.CLOSE.asString(), _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.01)");
|
||||
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
assertEquals(_request.getRequestURI(), result);
|
||||
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(HttpHeaderValue.CLOSE.asString(), _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWin2kSP1WithIE6() throws Exception
|
||||
{
|
||||
HttpFields fields = _connection.getRequestFields();
|
||||
HttpFields fields = _request.getHttpFields();
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.01)");
|
||||
|
||||
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
|
||||
assertEquals(_request.getRequestURI(), result);
|
||||
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(HttpHeaderValue.CLOSE.asString(), _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWin2kSP1WithIE7() throws Exception
|
||||
{
|
||||
HttpFields fields = _connection.getRequestFields();
|
||||
HttpFields fields = _request.getHttpFields();
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.01)");
|
||||
|
||||
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
|
||||
assertEquals(null, result);
|
||||
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(null, _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWinXpWithIE5() throws Exception
|
||||
{
|
||||
HttpFields fields = _connection.getRequestFields();
|
||||
HttpFields fields = _request.getHttpFields();
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.1)");
|
||||
|
||||
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
|
||||
assertEquals(_request.getRequestURI(), result);
|
||||
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(HttpHeaderValue.CLOSE.asString(), _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.1)");
|
||||
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
assertEquals(_request.getRequestURI(), result);
|
||||
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(HttpHeaderValue.CLOSE.asString(), _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.1)");
|
||||
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
assertEquals(_request.getRequestURI(), result);
|
||||
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(HttpHeaderValue.CLOSE.asString(), _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWinXpWithIE6() throws Exception
|
||||
{
|
||||
HttpFields fields = _connection.getRequestFields();
|
||||
HttpFields fields = _request.getHttpFields();
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
|
||||
|
||||
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
|
||||
assertEquals(null, result);
|
||||
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(null, _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWinXpWithIE7() throws Exception
|
||||
{
|
||||
HttpFields fields = _connection.getRequestFields();
|
||||
HttpFields fields = _request.getHttpFields();
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
|
||||
|
||||
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
|
||||
assertEquals(null, result);
|
||||
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(null, _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWinVistaWithIE5() throws Exception
|
||||
{
|
||||
HttpFields fields = _connection.getRequestFields();
|
||||
HttpFields fields = _request.getHttpFields();
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 6.0)");
|
||||
|
||||
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
|
||||
assertEquals(_request.getRequestURI(), result);
|
||||
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(HttpHeaderValue.CLOSE.asString(), _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 6.0)");
|
||||
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
assertEquals(_request.getRequestURI(), result);
|
||||
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(HttpHeaderValue.CLOSE.asString(), _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 6.0)");
|
||||
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
assertEquals(_request.getRequestURI(), result);
|
||||
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(HttpHeaderValue.CLOSE.asString(), _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWinVistaWithIE6() throws Exception
|
||||
{
|
||||
HttpFields fields = _connection.getRequestFields();
|
||||
HttpFields fields = _request.getHttpFields();
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 6.0)");
|
||||
|
||||
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
|
||||
assertEquals(null, result);
|
||||
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(null, _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWinVistaWithIE7() throws Exception
|
||||
{
|
||||
HttpFields fields = _connection.getRequestFields();
|
||||
HttpFields fields = _request.getHttpFields();
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");
|
||||
|
||||
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
|
||||
assertEquals(null, result);
|
||||
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(null, _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -230,12 +230,12 @@ public class MsieSslRuleTest extends AbstractRuleTestCase
|
|||
super.stop();
|
||||
super.start(false);
|
||||
|
||||
HttpFields fields = _connection.getRequestFields();
|
||||
HttpFields fields = _request.getHttpFields();
|
||||
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.0)");
|
||||
|
||||
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
|
||||
|
||||
assertEquals(null, result);
|
||||
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
|
||||
assertEquals(null, _response.getHeader(HttpHeader.CONNECTION.asString()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -132,8 +132,9 @@ public class PatternRuleTest
|
|||
{
|
||||
_rule.setPattern(matchCase[0]);
|
||||
final String uri=matchCase[1];
|
||||
|
||||
String result = _rule.matchAndApply(uri,
|
||||
new Request()
|
||||
new Request(null,null)
|
||||
{
|
||||
{
|
||||
setRequestURI(uri);
|
||||
|
|
|
@ -1,136 +0,0 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// 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.rewrite.handler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.eclipse.jetty.client.ContentExchange;
|
||||
import org.eclipse.jetty.client.HttpClient;
|
||||
import org.eclipse.jetty.client.HttpExchange;
|
||||
import org.eclipse.jetty.http.HttpMethods;
|
||||
import org.eclipse.jetty.server.Connector;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.handler.AbstractHandler;
|
||||
import org.eclipse.jetty.server.nio.SelectChannelConnector;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ProxyRuleTest
|
||||
{
|
||||
private static ProxyRule _rule;
|
||||
private static RewriteHandler _handler;
|
||||
private static Server _proxyServer = new Server();
|
||||
private static Connector _proxyServerConnector = new SelectChannelConnector();
|
||||
private static Server _targetServer = new Server();
|
||||
private static Connector _targetServerConnector = new SelectChannelConnector();
|
||||
private static HttpClient _httpClient = new HttpClient();
|
||||
|
||||
@BeforeClass
|
||||
public static void setupOnce() throws Exception
|
||||
{
|
||||
_targetServer.addConnector(_targetServerConnector);
|
||||
_targetServer.setHandler(new AbstractHandler()
|
||||
{
|
||||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
||||
{
|
||||
baseRequest.setHandled(true);
|
||||
String responseString = "uri: " + request.getRequestURI() + " some content";
|
||||
response.getOutputStream().write(responseString.getBytes());
|
||||
response.setStatus(201);
|
||||
}
|
||||
});
|
||||
_targetServer.start();
|
||||
|
||||
_rule = new ProxyRule();
|
||||
_rule.setPattern("/foo/*");
|
||||
_rule.setProxyTo("http://localhost:" + _targetServerConnector.getLocalPort());
|
||||
_handler = new RewriteHandler();
|
||||
_handler.setRewriteRequestURI(true);
|
||||
_handler.setRules(new Rule[] { _rule });
|
||||
|
||||
_proxyServer.addConnector(_proxyServerConnector);
|
||||
_proxyServer.setHandler(_handler);
|
||||
_proxyServer.start();
|
||||
|
||||
_httpClient.start();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void destroy() throws Exception
|
||||
{
|
||||
_httpClient.stop();
|
||||
_proxyServer.stop();
|
||||
_targetServer.stop();
|
||||
_rule = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProxy() throws Exception
|
||||
{
|
||||
|
||||
ContentExchange exchange = new ContentExchange(true);
|
||||
exchange.setMethod(HttpMethods.GET);
|
||||
String body = "BODY";
|
||||
String url = "http://localhost:" + _proxyServerConnector.getLocalPort() + "/foo?body=" + URLEncoder.encode(body,"UTF-8");
|
||||
exchange.setURL(url);
|
||||
|
||||
_httpClient.send(exchange);
|
||||
assertEquals(HttpExchange.STATUS_COMPLETED,exchange.waitForDone());
|
||||
assertEquals("uri: / some content",exchange.getResponseContent());
|
||||
assertEquals(201,exchange.getResponseStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProxyWithDeeperPath() throws Exception
|
||||
{
|
||||
|
||||
ContentExchange exchange = new ContentExchange(true);
|
||||
exchange.setMethod(HttpMethods.GET);
|
||||
String body = "BODY";
|
||||
String url = "http://localhost:" + _proxyServerConnector.getLocalPort() + "/foo/bar/foobar?body=" + URLEncoder.encode(body,"UTF-8");
|
||||
exchange.setURL(url);
|
||||
|
||||
_httpClient.send(exchange);
|
||||
assertEquals(HttpExchange.STATUS_COMPLETED,exchange.waitForDone());
|
||||
assertEquals("uri: /bar/foobar some content",exchange.getResponseContent());
|
||||
assertEquals(201,exchange.getResponseStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProxyNoMatch() throws Exception
|
||||
{
|
||||
ContentExchange exchange = new ContentExchange(true);
|
||||
exchange.setMethod(HttpMethods.GET);
|
||||
String body = "BODY";
|
||||
String url = "http://localhost:" + _proxyServerConnector.getLocalPort() + "/foobar?body=" + URLEncoder.encode(body,"UTF-8");
|
||||
exchange.setURL(url);
|
||||
|
||||
_httpClient.send(exchange);
|
||||
assertEquals(HttpExchange.STATUS_COMPLETED,exchange.waitForDone());
|
||||
assertEquals(404,exchange.getResponseStatus());
|
||||
}
|
||||
}
|
|
@ -20,7 +20,7 @@ package org.eclipse.jetty.rewrite.handler;
|
|||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.eclipse.jetty.http.HttpHeaders;
|
||||
import org.eclipse.jetty.http.HttpHeader;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -51,6 +51,6 @@ public class RedirectPatternRuleTest extends AbstractRuleTestCase
|
|||
String location = "http://eclipse.com";
|
||||
_rule.setLocation(location);
|
||||
_rule.apply(null, _request, _response);
|
||||
assertEquals(location, _response.getHeader(HttpHeaders.LOCATION));
|
||||
assertEquals(location, _response.getHeader(HttpHeader.LOCATION.asString()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ package org.eclipse.jetty.rewrite.handler;
|
|||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.eclipse.jetty.http.HttpHeaders;
|
||||
import org.eclipse.jetty.http.HttpHeader;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -52,7 +52,7 @@ public class RedirectRegexRuleTest extends AbstractRuleTestCase
|
|||
|
||||
// Resource is dir
|
||||
_rule.matchAndApply("/my/dir/file/", _request, _response);
|
||||
assertEquals("http://www.mortbay.org/", _response.getHeader(HttpHeaders.LOCATION));
|
||||
assertEquals("http://www.mortbay.org/", _response.getHeader(HttpHeader.LOCATION.asString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -63,7 +63,7 @@ public class RedirectRegexRuleTest extends AbstractRuleTestCase
|
|||
|
||||
// Resource is an image
|
||||
_rule.matchAndApply("/my/dir/file/image.png", _request, _response);
|
||||
assertEquals("http://www.mortbay.org/image.png", _response.getHeader(HttpHeaders.LOCATION));
|
||||
assertEquals("http://www.mortbay.org/image.png", _response.getHeader(HttpHeader.LOCATION.asString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -74,6 +74,6 @@ public class RedirectRegexRuleTest extends AbstractRuleTestCase
|
|||
|
||||
// Resource is api with parameters
|
||||
_rule.matchAndApply("/my/dir/file/api/rest/foo?id=100&sort=date", _request, _response);
|
||||
assertEquals("http://www.mortbay.org/api/rest/foo?id=100&sort=date", _response.getHeader(HttpHeaders.LOCATION));
|
||||
assertEquals("http://www.mortbay.org/api/rest/foo?id=100&sort=date", _response.getHeader(HttpHeader.LOCATION.asString()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -96,8 +96,9 @@ public class RegexRuleTest
|
|||
_rule.setRegex(matchCase[0]);
|
||||
final String uri=matchCase[1];
|
||||
String result = _rule.matchAndApply(uri,
|
||||
new Request()
|
||||
new Request(null,null)
|
||||
{
|
||||
@Override
|
||||
public String getRequestURI()
|
||||
{
|
||||
return uri;
|
||||
|
|
|
@ -43,9 +43,10 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
|
|||
public void init() throws Exception
|
||||
{
|
||||
_handler=new RewriteHandler();
|
||||
_server.setHandler(_handler);
|
||||
_handler.setHandler(new AbstractHandler(){
|
||||
|
||||
_handler.setServer(_server);
|
||||
_handler.setHandler(new AbstractHandler()
|
||||
{
|
||||
@Override
|
||||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
||||
{
|
||||
response.setStatus(201);
|
||||
|
@ -55,6 +56,7 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
|
|||
}
|
||||
|
||||
});
|
||||
_handler.start();
|
||||
|
||||
_rule1 = new RewritePatternRule();
|
||||
_rule1.setPattern("/aaa/*");
|
||||
|
@ -84,6 +86,7 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
|
|||
_handler.setRewritePathInfo(false);
|
||||
_request.setRequestURI("/foo/bar");
|
||||
_request.setPathInfo("/foo/bar");
|
||||
|
||||
_handler.handle("/foo/bar",_request,_request, _response);
|
||||
assertEquals(201,_response.getStatus());
|
||||
assertEquals("/foo/bar",_request.getAttribute("target"));
|
||||
|
|
|
@ -48,10 +48,11 @@ public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
|
|||
_fooContainerRule.setVirtualHosts(new String[] {"foo.com"});
|
||||
_fooContainerRule.setRules(new Rule[] { _fooRule });
|
||||
|
||||
_server.setHandler(_handler);
|
||||
|
||||
start(false);
|
||||
_request.setRequestURI("/cheese/bar");
|
||||
|
||||
_handler.setServer(_server);
|
||||
_handler.start();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -193,6 +194,6 @@ public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
|
|||
|
||||
private void handleRequest() throws Exception
|
||||
{
|
||||
_server.handle("/cheese/bar", _request, _request, _response);
|
||||
_handler.handle("/cheese/bar", _request, _request, _response);
|
||||
}
|
||||
}
|
||||
|
|
8
pom.xml
8
pom.xml
|
@ -405,20 +405,20 @@
|
|||
<module>jetty-jndi</module>
|
||||
<module>jetty-jsp</module>
|
||||
<module>jetty-distribution</module>
|
||||
|
||||
<module>jetty-spring</module>
|
||||
<module>jetty-client</module>
|
||||
<module>jetty-osgi</module>
|
||||
|
||||
<module>example-async-rest</module>
|
||||
<module>jetty-rewrite</module>
|
||||
|
||||
<!-- modules that need fixed and added back, or simply dropped and not maintained
|
||||
<module>tests</module>
|
||||
|
||||
<module>jetty-runner</module>
|
||||
<module>jetty-rhttp</module>
|
||||
<module>jetty-jaspi</module>
|
||||
<module>jetty-client</module>
|
||||
<module>jetty-proxy</module>
|
||||
<module>jetty-rewrite</module>
|
||||
<module>jetty-policy</module>
|
||||
<module>jetty-monitor</module>
|
||||
<module>jetty-nested</module>
|
||||
|
@ -426,8 +426,6 @@
|
|||
<module>jetty-nosql</module>
|
||||
<module>jetty-http-spi</module>
|
||||
<module>test-jetty-nested</module>
|
||||
<module>test-jetty-servlet</module>
|
||||
<module>tests</module>
|
||||
-->
|
||||
</modules>
|
||||
<dependencyManagement>
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<parent>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-project</artifactId>
|
||||
<version>9.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>test-jetty-servlet</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>Test :: Jetty Servlet Tester</name>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-webapp</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty.toolchain</groupId>
|
||||
<artifactId>jetty-test-helper</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
Loading…
Reference in New Issue