Basic functionality for ProxyRule implemented
This commit is contained in:
parent
88ce9aceb2
commit
0d8bd99d6a
|
@ -4,11 +4,11 @@
|
|||
// 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
|
||||
// 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.
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
|
||||
package org.eclipse.jetty.http;
|
||||
|
@ -34,7 +34,7 @@ import org.eclipse.jetty.util.URIUtil;
|
|||
* /foo/bar - an exact path specification.
|
||||
* /foo/* - a prefix path specification (must end '/*').
|
||||
* *.ext - a suffix path specification.
|
||||
* / - the default path specification.
|
||||
* / - the default path specification.
|
||||
* </PRE>
|
||||
* Matching is performed in the following order <NL>
|
||||
* <LI>Exact match.
|
||||
|
@ -43,24 +43,24 @@ import org.eclipse.jetty.util.URIUtil;
|
|||
* <LI>default.
|
||||
* </NL>
|
||||
* Multiple path specifications can be mapped by providing a list of
|
||||
* specifications. By default this class uses characters ":," as path
|
||||
* separators, unless configured differently by calling the static
|
||||
* method @see PathMap#setPathSpecSeparators(String)
|
||||
* specifications. By default this class uses characters ":," as path
|
||||
* separators, unless configured differently by calling the static
|
||||
* method @see PathMap#setPathSpecSeparators(String)
|
||||
* <P>
|
||||
* Special characters within paths such as '?<EFBFBD> and ';' are not treated specially
|
||||
* as it is assumed they would have been either encoded in the original URL or
|
||||
* as it is assumed they would have been either encoded in the original URL or
|
||||
* stripped from the path.
|
||||
* <P>
|
||||
* This class is not synchronized. If concurrent modifications are
|
||||
* possible then it should be synchronized at a higher level.
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class PathMap extends HashMap implements Externalizable
|
||||
{
|
||||
/* ------------------------------------------------------------ */
|
||||
private static String __pathSpecSeparators = ":,";
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Set the path spec separator.
|
||||
* Multiple path specification may be included in a single string
|
||||
|
@ -72,7 +72,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
{
|
||||
__pathSpecSeparators=s;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
final StringMap _prefixMap=new StringMap();
|
||||
final StringMap _suffixMap=new StringMap();
|
||||
|
@ -83,7 +83,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
Entry _default=null;
|
||||
final Set _entrySet;
|
||||
boolean _nodefault=false;
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
/** Construct empty PathMap.
|
||||
*/
|
||||
|
@ -102,7 +102,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
_entrySet=entrySet();
|
||||
_nodefault=nodefault;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
/** Construct empty PathMap.
|
||||
*/
|
||||
|
@ -111,7 +111,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
super (capacity);
|
||||
_entrySet=entrySet();
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
/** Construct from dictionary PathMap.
|
||||
*/
|
||||
|
@ -120,7 +120,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
putAll(m);
|
||||
_entrySet=entrySet();
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public void writeExternal(java.io.ObjectOutput out)
|
||||
throws java.io.IOException
|
||||
|
@ -128,7 +128,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
HashMap map = new HashMap(this);
|
||||
out.writeObject(map);
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public void readExternal(java.io.ObjectInput in)
|
||||
throws java.io.IOException, ClassNotFoundException
|
||||
|
@ -136,7 +136,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
HashMap map = (HashMap)in.readObject();
|
||||
this.putAll(map);
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
/** Add a single path match to the PathMap.
|
||||
* @param pathSpec The path specification, or comma separated list of
|
||||
|
@ -148,16 +148,16 @@ public class PathMap extends HashMap implements Externalizable
|
|||
{
|
||||
StringTokenizer tok = new StringTokenizer(pathSpec.toString(),__pathSpecSeparators);
|
||||
Object old =null;
|
||||
|
||||
|
||||
while (tok.hasMoreTokens())
|
||||
{
|
||||
String spec=tok.nextToken();
|
||||
|
||||
|
||||
if (!spec.startsWith("/") && !spec.startsWith("*."))
|
||||
throw new IllegalArgumentException("PathSpec "+spec+". must start with '/' or '*.'");
|
||||
|
||||
|
||||
old = super.put(spec,object);
|
||||
|
||||
|
||||
// Make entry that was just created.
|
||||
Entry entry = new Entry(spec,object);
|
||||
|
||||
|
@ -176,7 +176,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
else if (spec.startsWith("*."))
|
||||
_suffixMap.put(spec.substring(2),entry);
|
||||
else if (spec.equals(URIUtil.SLASH))
|
||||
{
|
||||
{
|
||||
if (_nodefault)
|
||||
_exactMap.put(spec,entry);
|
||||
else
|
||||
|
@ -193,7 +193,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return old;
|
||||
}
|
||||
|
||||
|
@ -209,8 +209,8 @@ public class PathMap extends HashMap implements Externalizable
|
|||
return entry.getValue();
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
/** Get the entry mapped by the best specification.
|
||||
* @param path the path.
|
||||
|
@ -222,14 +222,14 @@ public class PathMap extends HashMap implements Externalizable
|
|||
|
||||
if (path==null)
|
||||
return null;
|
||||
|
||||
int l=path.length();
|
||||
|
||||
int l=path.length();
|
||||
|
||||
// try exact match
|
||||
entry=_exactMap.getEntry(path,0,l);
|
||||
if (entry!=null)
|
||||
return (Entry) entry.getValue();
|
||||
|
||||
|
||||
// prefix search
|
||||
int i=l;
|
||||
while((i=path.lastIndexOf('/',i-1))>=0)
|
||||
|
@ -238,11 +238,11 @@ public class PathMap extends HashMap implements Externalizable
|
|||
if (entry!=null)
|
||||
return (Entry) entry.getValue();
|
||||
}
|
||||
|
||||
|
||||
// Prefix Default
|
||||
if (_prefixDefault!=null)
|
||||
return _prefixDefault;
|
||||
|
||||
|
||||
// Extension search
|
||||
i=0;
|
||||
while ((i=path.indexOf('.',i+1))>0)
|
||||
|
@ -250,12 +250,12 @@ public class PathMap extends HashMap implements Externalizable
|
|||
entry=_suffixMap.getEntry(path,i+1,l-i-1);
|
||||
if (entry!=null)
|
||||
return (Entry) entry.getValue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Default
|
||||
return _default;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
/** Get all entries matched by the path.
|
||||
* Best match first.
|
||||
|
@ -263,20 +263,20 @@ public class PathMap extends HashMap implements Externalizable
|
|||
* @return LazyList of Map.Entry instances key=pathSpec
|
||||
*/
|
||||
public Object getLazyMatches(String path)
|
||||
{
|
||||
{
|
||||
Map.Entry entry;
|
||||
Object entries=null;
|
||||
|
||||
if (path==null)
|
||||
return LazyList.getList(entries);
|
||||
|
||||
|
||||
int l=path.length();
|
||||
|
||||
// try exact match
|
||||
entry=_exactMap.getEntry(path,0,l);
|
||||
if (entry!=null)
|
||||
entries=LazyList.add(entries,entry.getValue());
|
||||
|
||||
|
||||
// prefix search
|
||||
int i=l-1;
|
||||
while((i=path.lastIndexOf('/',i-1))>=0)
|
||||
|
@ -285,11 +285,11 @@ public class PathMap extends HashMap implements Externalizable
|
|||
if (entry!=null)
|
||||
entries=LazyList.add(entries,entry.getValue());
|
||||
}
|
||||
|
||||
|
||||
// Prefix Default
|
||||
if (_prefixDefault!=null)
|
||||
entries=LazyList.add(entries,_prefixDefault);
|
||||
|
||||
|
||||
// Extension search
|
||||
i=0;
|
||||
while ((i=path.indexOf('.',i+1))>0)
|
||||
|
@ -305,13 +305,13 @@ public class PathMap extends HashMap implements Externalizable
|
|||
// Optimization for just the default
|
||||
if (entries==null)
|
||||
return _defaultSingletonList;
|
||||
|
||||
|
||||
entries=LazyList.add(entries,_default);
|
||||
}
|
||||
|
||||
|
||||
return entries;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
/** Get all entries matched by the path.
|
||||
* Best match first.
|
||||
|
@ -319,7 +319,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
* @return List of Map.Entry instances key=pathSpec
|
||||
*/
|
||||
public List getMatches(String path)
|
||||
{
|
||||
{
|
||||
return LazyList.getList(getLazyMatches(path));
|
||||
}
|
||||
|
||||
|
@ -330,12 +330,12 @@ public class PathMap extends HashMap implements Externalizable
|
|||
* @return Whether the PathMap contains any entries that match this
|
||||
*/
|
||||
public boolean containsMatch(String path)
|
||||
{
|
||||
{
|
||||
Entry match = getMatch(path);
|
||||
return match!=null && !match.equals(_default);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
/* --------------------------------------------------------------- */
|
||||
@Override
|
||||
public Object remove(Object pathSpec)
|
||||
{
|
||||
|
@ -362,7 +362,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
}
|
||||
return super.remove(pathSpec);
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
@Override
|
||||
public void clear()
|
||||
|
@ -374,7 +374,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
_defaultSingletonList=null;
|
||||
super.clear();
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
/**
|
||||
* @return true if match.
|
||||
|
@ -397,7 +397,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
{
|
||||
if (!noDefault && pathSpec.length()==1 || pathSpec.equals(path))
|
||||
return true;
|
||||
|
||||
|
||||
if(isPathWildcardMatch(pathSpec, path))
|
||||
return true;
|
||||
}
|
||||
|
@ -419,24 +419,24 @@ public class PathMap extends HashMap implements Externalizable
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
/** Return the portion of a path that matches a path spec.
|
||||
* @return null if no match at all.
|
||||
*/
|
||||
public static String pathMatch(String pathSpec, String path)
|
||||
{
|
||||
{
|
||||
char c = pathSpec.charAt(0);
|
||||
|
||||
|
||||
if (c=='/')
|
||||
{
|
||||
if (pathSpec.length()==1)
|
||||
return path;
|
||||
|
||||
|
||||
if (pathSpec.equals(path))
|
||||
return path;
|
||||
|
||||
|
||||
if (isPathWildcardMatch(pathSpec, path))
|
||||
return path.substring(0,pathSpec.length()-2);
|
||||
}
|
||||
|
@ -448,7 +448,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
/** Return the portion of a path that is after a path spec.
|
||||
* @return The path info string
|
||||
|
@ -456,12 +456,12 @@ public class PathMap extends HashMap implements Externalizable
|
|||
public static String pathInfo(String pathSpec, String path)
|
||||
{
|
||||
char c = pathSpec.charAt(0);
|
||||
|
||||
|
||||
if (c=='/')
|
||||
{
|
||||
if (pathSpec.length()==1)
|
||||
return null;
|
||||
|
||||
|
||||
boolean wildcard = isPathWildcardMatch(pathSpec, path);
|
||||
|
||||
// handle the case where pathSpec uses a wildcard and path info is "/*"
|
||||
|
@ -474,7 +474,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
return null;
|
||||
return path.substring(pathSpec.length()-2);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -484,7 +484,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
* @param base The base the path is relative to.
|
||||
* @param pathSpec The spec of the path segment to ignore.
|
||||
* @param path the additional path
|
||||
* @return base plus path with pathspec removed
|
||||
* @return base plus path with pathspec removed
|
||||
*/
|
||||
public static String relativePath(String base,
|
||||
String pathSpec,
|
||||
|
@ -508,7 +508,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
path = base + URIUtil.SLASH + info;
|
||||
return path;
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -516,7 +516,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
{
|
||||
private final Object key;
|
||||
private final Object value;
|
||||
private String mapped;
|
||||
private String mapped;
|
||||
private transient String string;
|
||||
|
||||
Entry(Object key, Object value)
|
||||
|
@ -529,7 +529,7 @@ public class PathMap extends HashMap implements Externalizable
|
|||
{
|
||||
return key;
|
||||
}
|
||||
|
||||
|
||||
public Object getValue()
|
||||
{
|
||||
return value;
|
||||
|
|
|
@ -74,6 +74,11 @@
|
|||
<artifactId>jetty-server</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-client</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>servlet-api</artifactId>
|
||||
|
|
|
@ -14,22 +14,23 @@ 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.HttpSchemes;
|
||||
import org.eclipse.jetty.http.HttpURI;
|
||||
import org.eclipse.jetty.io.Buffer;
|
||||
import org.eclipse.jetty.io.EofException;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
import org.eclipse.jetty.util.thread.QueuedThreadPool;
|
||||
|
||||
public class ProxyRule extends Rule
|
||||
public class ProxyRule extends PatternRule implements Rule.ApplyURI
|
||||
{
|
||||
private static final Logger _log = Log.getLogger(ProxyRule.class);
|
||||
|
||||
protected HttpClient _client;
|
||||
protected String _hostHeader;
|
||||
private HttpClient _client;
|
||||
private String _hostHeader;
|
||||
private String _proxyTo;
|
||||
|
||||
protected HashSet<String> _DontProxyHeaders = new HashSet<String>();
|
||||
private HashSet<String> _DontProxyHeaders = new HashSet<String>();
|
||||
{
|
||||
_DontProxyHeaders.add("proxy-connection");
|
||||
_DontProxyHeaders.add("connection");
|
||||
|
@ -58,13 +59,18 @@ public class ProxyRule extends Rule
|
|||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
protected HttpURI proxyHttpURI(String scheme, String serverName, int serverPort, String uri) throws MalformedURLException
|
||||
private HttpURI proxyHttpURI(String uri) throws MalformedURLException
|
||||
{
|
||||
return new HttpURI(scheme + "://" + serverName + ":" + serverPort + uri);
|
||||
return new HttpURI(_proxyTo + uri);
|
||||
}
|
||||
|
||||
public void applyURI(Request request, String oldTarget, String newTarget) throws IOException
|
||||
{
|
||||
System.out.println("applyURI called");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String matchAndApply(String target, HttpServletRequest req, HttpServletResponse res) throws IOException
|
||||
protected String apply(String target, HttpServletRequest request, final HttpServletResponse response) throws IOException
|
||||
{
|
||||
synchronized (this)
|
||||
{
|
||||
|
@ -81,9 +87,6 @@ public class ProxyRule extends Rule
|
|||
}
|
||||
}
|
||||
|
||||
final HttpServletRequest request = (HttpServletRequest)req;
|
||||
final HttpServletResponse response = (HttpServletResponse)res;
|
||||
|
||||
final int debug = _log.isDebugEnabled()?request.hashCode():0;
|
||||
|
||||
final InputStream in = request.getInputStream();
|
||||
|
@ -93,7 +96,7 @@ public class ProxyRule extends Rule
|
|||
if (request.getQueryString() != null)
|
||||
uri += "?" + request.getQueryString();
|
||||
|
||||
HttpURI url = proxyHttpURI(request.getScheme(),request.getServerName(),request.getServerPort(),uri);
|
||||
HttpURI url = proxyHttpURI(uri);
|
||||
|
||||
if (debug != 0)
|
||||
_log.debug(debug + " proxy " + uri + "-->" + url);
|
||||
|
@ -106,20 +109,24 @@ public class ProxyRule extends Rule
|
|||
|
||||
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)
|
||||
|
@ -127,10 +134,13 @@ public class ProxyRule extends Rule
|
|||
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)
|
||||
|
@ -142,6 +152,7 @@ public class ProxyRule extends Rule
|
|||
response.setStatus(status);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResponseHeader(Buffer name, Buffer value) throws IOException
|
||||
{
|
||||
String s = name.toString().toLowerCase();
|
||||
|
@ -156,6 +167,7 @@ public class ProxyRule extends Rule
|
|||
_log.debug(debug + " " + name + "! " + value);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onConnectionFailed(Throwable ex)
|
||||
{
|
||||
_log.warn(ex.toString());
|
||||
|
@ -166,6 +178,7 @@ public class ProxyRule extends Rule
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onException(Throwable ex)
|
||||
{
|
||||
if (ex instanceof EofException)
|
||||
|
@ -181,6 +194,7 @@ public class ProxyRule extends Rule
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onExpire()
|
||||
{
|
||||
if (!response.isCommitted())
|
||||
|
@ -191,7 +205,6 @@ public class ProxyRule extends Rule
|
|||
|
||||
};
|
||||
|
||||
exchange.setScheme(HttpSchemes.HTTPS.equals(request.getScheme())?HttpSchemes.HTTPS_BUFFER:HttpSchemes.HTTP_BUFFER);
|
||||
exchange.setMethod(request.getMethod());
|
||||
exchange.setURL(url.toString());
|
||||
exchange.setVersion(request.getProtocol());
|
||||
|
@ -199,6 +212,30 @@ public class ProxyRule extends Rule
|
|||
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 continuation to take into account the timeout of the HttpClient and the HttpExchange
|
||||
*/
|
||||
long ctimeout = (_client.getTimeout() > exchange.getTimeout())?_client.getTimeout():exchange.getTimeout();
|
||||
|
||||
_client.send(exchange);
|
||||
try
|
||||
{
|
||||
exchange.waitForDone();
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
_log.info(e);
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
private boolean createHeaders(final HttpServletRequest request, final int debug, HttpExchange exchange)
|
||||
{
|
||||
// check connection header
|
||||
String connectionHdr = request.getHeader("Connection");
|
||||
if (connectionHdr != null)
|
||||
|
@ -265,18 +302,11 @@ public class ProxyRule extends Rule
|
|||
exchange.addRequestHeader("X-Forwarded-Host",request.getServerName());
|
||||
exchange.addRequestHeader("X-Forwarded-Server",request.getLocalName());
|
||||
}
|
||||
|
||||
if (hasContent)
|
||||
exchange.setRequestContentSource(in);
|
||||
|
||||
/*
|
||||
* we need to set the timeout on the continuation to take into account the timeout of the HttpClient and the HttpExchange
|
||||
*/
|
||||
long ctimeout = (_client.getTimeout() > exchange.getTimeout())?_client.getTimeout():exchange.getTimeout();
|
||||
|
||||
_client.send(exchange);
|
||||
|
||||
return target;
|
||||
return hasContent;
|
||||
}
|
||||
|
||||
public void setProxyTo(String proxyTo)
|
||||
{
|
||||
this._proxyTo = proxyTo;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,40 +12,66 @@
|
|||
// ========================================================================
|
||||
package org.eclipse.jetty.rewrite.handler;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
import org.eclipse.jetty.client.Address;
|
||||
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.HttpHeaders;
|
||||
import org.eclipse.jetty.http.HttpMethods;
|
||||
import org.eclipse.jetty.toolchain.test.SimpleRequest;
|
||||
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.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ProxyRuleTest extends AbstractRuleTestCase
|
||||
public class ProxyRuleTest
|
||||
{
|
||||
private ProxyRule _rule;
|
||||
private RewriteHandler _handler;
|
||||
private Server _proxyServer = new Server();
|
||||
private Connector _proxyServerConnector = new SelectChannelConnector();
|
||||
private Server _targetServer = new Server();
|
||||
private Connector _targetServerConnector= new SelectChannelConnector();
|
||||
private HttpClient httpClient = new HttpClient();
|
||||
|
||||
@Before
|
||||
public void init() throws Exception
|
||||
{
|
||||
start(false);
|
||||
_rule = new ProxyRule();
|
||||
|
||||
_targetServer.addConnector(_targetServerConnector);
|
||||
_targetServer.setHandler(new AbstractHandler()
|
||||
{
|
||||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException,
|
||||
ServletException
|
||||
{
|
||||
baseRequest.setHandled(true);
|
||||
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 });
|
||||
|
||||
_handler.setRules(new Rule[] { _rule } );
|
||||
_proxyServer.addConnector(_proxyServerConnector);
|
||||
_proxyServer.setHandler(_handler);
|
||||
_proxyServer.start();
|
||||
|
||||
_server.setHandler(_handler);
|
||||
httpClient.start();
|
||||
}
|
||||
|
||||
@After
|
||||
|
@ -57,21 +83,30 @@ public class ProxyRuleTest extends AbstractRuleTestCase
|
|||
@Test
|
||||
public void testProxy() throws Exception
|
||||
{
|
||||
// HttpClient httpClient = new HttpClient();
|
||||
// httpClient.setProxy(new Address("localhost", proxyPort()));
|
||||
// httpClient.start();
|
||||
//
|
||||
// try
|
||||
// {
|
||||
// ContentExchange exchange = new ContentExchange(true);
|
||||
// exchange.setMethod(HttpMethods.GET);
|
||||
// String body = "BODY";
|
||||
// exchange.setURL("https://localhost:" + serverConnector.getLocalPort() + "/echo?body=" + URLEncoder.encode(body, "UTF-8"));
|
||||
//
|
||||
// httpClient.send(exchange);
|
||||
// assertEquals(HttpExchange.STATUS_COMPLETED, exchange.waitForDone());
|
||||
// //_rule.matchAndApply(null, _request, _response);
|
||||
//
|
||||
// //assertEquals(location, _response.getHeader(HttpHeaders.LOCATION));
|
||||
|
||||
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(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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue