JETTY-1049 Improved transparent proxy usability

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@440 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2009-06-27 03:43:47 +00:00
parent 87d35a5dfd
commit 876369b567
4 changed files with 78 additions and 11 deletions

View File

@ -2,6 +2,7 @@ jetty-7.0.0.M4-SNAPSHOT
+ 281059 NPE in QTP with debug on
+ JETTY-1042 Prevent cookie leak between shared connection
+ JETTY-1048 Fix for large partially filtered static content
+ JETTY-1049 Improved transparent proxy usability
jetty-7.0.0.M3 20 June 2009
+ fixed race with expired async listeners

View File

@ -29,6 +29,7 @@ import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -46,11 +47,14 @@ import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.log.Log;
/**
* EXPERIMENTAL Proxy servlet.
* Asynchronous Proxy Servlet.
*
*
* Forward requests to another server either as a standard web proxy (as defined by
* RFC2616) or as a transparent proxy.
*
* This servlet needs the jetty-util and jetty-client classes to be available to
* the web application.
*/
public class ProxyServlet implements Servlet
{
@ -118,7 +122,7 @@ public class ProxyServlet implements Servlet
final InputStream in=request.getInputStream();
final OutputStream out=response.getOutputStream();
final Continuation continuation = ContinuationSupport.getContinuation(request,response);
final Continuation continuation = ContinuationSupport.getContinuation(request);
if (!continuation.isInitial())
response.sendError(HttpServletResponse.SC_GATEWAY_TIMEOUT); // Need better test that isInitial
@ -138,7 +142,6 @@ public class ProxyServlet implements Servlet
return;
}
HttpExchange exchange = new HttpExchange()
{
protected void onRequestCommitted() throws IOException
@ -286,8 +289,6 @@ public class ProxyServlet implements Servlet
{
String uri = request.getRequestURI();
context.log("CONNECT: "+uri);
String port = "";
String host = "";
@ -315,16 +316,13 @@ public class ProxyServlet implements Servlet
OutputStream out=response.getOutputStream();
Socket socket = new Socket(inetAddress.getAddress(),inetAddress.getPort());
context.log("Socket: "+socket);
response.setStatus(200);
response.setHeader("Connection","close");
response.flushBuffer();
// TODO prevent real close!
context.log("out<-in");
IO.copyThread(socket.getInputStream(),out);
context.log("in->out");
IO.copy(in,socket.getOutputStream());
}
}
@ -352,5 +350,55 @@ public class ProxyServlet implements Servlet
{
}
/**
* Transparent Proxy.
*
* This convenience extension to AsyncProxyServlet configures the servlet
* as a transparent proxy. The servlet is configured with init parameter:<ul>
* <li> ProxyTo - a URI like http://host:80/context to which the request is proxied.
* <li> Prefix - a URI prefix that is striped from the start of the forwarded URI.
* </ul>
* For example, if a request was received at /foo/bar and the ProxyTo was http://host:80/context
* and the Prefix was /foo, then the request would be proxied to http://host:80/context/bar
*
*/
public static class Transparent extends ProxyServlet
{
String _prefix;
String _proxyTo;
public Transparent()
{
}
public Transparent(String prefix,String server, int port)
{
_prefix=prefix;
_proxyTo="http://"+server+":"+port;
}
public void init(ServletConfig config) throws ServletException
{
if (config.getInitParameter("ProxyTo")!=null)
_proxyTo=config.getInitParameter("ProxyTo");
if (config.getInitParameter("Prefix")!=null)
_prefix=config.getInitParameter("Prefix");
if (_proxyTo==null)
throw new UnavailableException("No ProxyTo");
super.init(config);
config.getServletContext().log("Transparent AsyncProxyServlet @ "+(_prefix==null?"-":_prefix)+ " to "+_proxyTo);
}
protected HttpURI proxyHttpURI(final String scheme, final String serverName, int serverPort, final String uri) throws MalformedURLException
{
if (_prefix!=null && !uri.startsWith(_prefix))
return null;
if (_prefix!=null)
return new HttpURI(_proxyTo+uri.substring(_prefix.length()));
return new HttpURI(_proxyTo+uri);
}
}
}

View File

@ -173,6 +173,23 @@
<url-pattern>/chat/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>TransparentProxy</servlet-name>
<servlet-class>org.eclipse.jetty.servlets.ProxyServlet$Transparent</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>ProxyTo</param-name><param-value>http://jetty.mortbay.org/test</param-value>
</init-param>
<init-param>
<param-name>Prefix</param-name><param-value>/jetty</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>TransparentProxy</servlet-name>
<url-pattern>/jetty/*</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/error404.html</location>

View File

@ -29,7 +29,8 @@ This is a test context that serves:
<li>a <a href="session/">Session Dump Servlet</a></li>
<li>a <a href="cookie/">Cookie Dump Servlet</a></li>
<li>a <a href="dispatch">Dispatcher Servlet</a></li>
<li>a <a href="cgi-bin/hello.sh">CGI script</a>(unix only)</li>
<li>a <a href="jetty/">Transparent Proxy</a> (to jetty.mortbay.org)</li>
<li>a <a href="cgi-bin/hello.sh">CGI script</a> (unix only)</li>
</ul>
<p/>