org.mortbay.http.handler

Class ProxyHandler

Implemented Interfaces:
HttpHandler, LifeCycle, Serializable

public class ProxyHandler
extends AbstractHttpHandler

Proxy request handler. A HTTP/1.1 Proxy. This implementation uses the JVMs URL implementation to make proxy requests.

The HttpTunnel mechanism is also used to implement the CONNECT method.

Version:
$Id: ProxyHandler.java,v 1.34 2005/10/05 13:32:59 gregwilkins Exp $
Authors:
Greg Wilkins (gregw)
giacof@tiscali.it (chained proxy)

Field Summary

protected StringMap
_DontProxyHeaders
Map of leg by leg headers (not end to end).
protected StringMap
_ProxyAuthHeaders
Map of leg by leg headers (not end to end).
protected StringMap
_ProxySchemes
Map of allows schemes to proxy Should be a set, but more efficient string map is used instead.
protected HashSet
_allowedConnectPorts
Set of allowed CONNECT ports.
protected Set
_proxyHostsBlackList
protected Set
_proxyHostsWhiteList
protected int
_tunnelTimeoutMs

Method Summary

protected void
customizeConnection(String pathInContext, String pathParams, HttpRequest request, Socket socket)
Customize proxy Socket connection for CONNECT.
protected void
customizeConnection(String pathInContext, String pathParams, HttpRequest request, URLConnection connection)
Customize proxy URL connection.
String[]
getProxyHostsBlackList()
Get proxy host black list.
String[]
getProxyHostsWhiteList()
Get proxy host white list.
int
getTunnelTimeoutMs()
void
handle(String pathInContext, String pathParams, HttpRequest request, HttpResponse response)
Handle a request.
void
handleConnect(String pathInContext, String pathParams, HttpRequest request, HttpResponse response)
boolean
isAnonymous()
protected boolean
isForbidden(String scheme, String host, int port, boolean openNonPrivPorts)
Is scheme,host & port Forbidden.
protected boolean
isForbidden(URI uri)
Is URL Forbidden.
protected URL
isProxied(URI uri)
Is URL Proxied.
protected HttpTunnel
newHttpTunnel(HttpRequest request, HttpResponse response, InetAddress iaddr, int port, int timeoutMS)
protected void
sendForbid(HttpRequest request, HttpResponse response, URI uri)
Send Forbidden.
void
setAnonymous(boolean anonymous)
void
setProxyHostsBlackList(String[] hosts)
Set proxy host black list.
void
setProxyHostsWhiteList(String[] hosts)
Set proxy host white list.
void
setTunnelTimeoutMs(int ms)
Tunnel timeout.
void
start()

Methods inherited from class org.mortbay.http.handler.AbstractHttpHandler

getHttpContext, getName, handleTrace, initialize, isStarted, setName, start, stop, toString

Field Details

_DontProxyHeaders

protected StringMap _DontProxyHeaders
Map of leg by leg headers (not end to end). Should be a set, but more efficient string map is used instead.

_ProxyAuthHeaders

protected StringMap _ProxyAuthHeaders
Map of leg by leg headers (not end to end). Should be a set, but more efficient string map is used instead.

_ProxySchemes

protected StringMap _ProxySchemes
Map of allows schemes to proxy Should be a set, but more efficient string map is used instead.

_allowedConnectPorts

protected HashSet _allowedConnectPorts
Set of allowed CONNECT ports.

_proxyHostsBlackList

protected Set _proxyHostsBlackList

_proxyHostsWhiteList

protected Set _proxyHostsWhiteList

_tunnelTimeoutMs

protected int _tunnelTimeoutMs

Method Details

customizeConnection

protected void customizeConnection(String pathInContext,
                                   String pathParams,
                                   HttpRequest request,
                                   Socket socket)
            throws IOException
Customize proxy Socket connection for CONNECT. Method to allow derived handlers to customize the tunnel sockets.

customizeConnection

protected void customizeConnection(String pathInContext,
                                   String pathParams,
                                   HttpRequest request,
                                   URLConnection connection)
            throws IOException
Customize proxy URL connection. Method to allow derived handlers to customize the connection.

getProxyHostsBlackList

public String[] getProxyHostsBlackList()
Get proxy host black list.
Returns:
Array of hostnames and IPs that are NOT proxied.

getProxyHostsWhiteList

public String[] getProxyHostsWhiteList()
Get proxy host white list.
Returns:
Array of hostnames and IPs that are proxied, or an empty array if all hosts are proxied.

getTunnelTimeoutMs

public int getTunnelTimeoutMs()

handle

public void handle(String pathInContext,
                   String pathParams,
                   HttpRequest request,
                   HttpResponse response)
            throws HttpException,
                   IOException
Handle a request. Note that Handlers are tried in order until one has handled the request. i.e. until request.isHandled() returns true. In broad terms this means, either a response has been commited or request.setHandled(true) has been called.
Specified by:
handle in interface HttpHandler
Parameters:
pathInContext - The context path
pathParams - Path parameters such as encoded Session ID
request - The HttpRequest request
response - The HttpResponse response

handleConnect

public void handleConnect(String pathInContext,
                          String pathParams,
                          HttpRequest request,
                          HttpResponse response)
            throws HttpException,
                   IOException

isAnonymous

public boolean isAnonymous()
Returns:
Returns the anonymous.

isForbidden

protected boolean isForbidden(String scheme,
                              String host,
                              int port,
                              boolean openNonPrivPorts)
Is scheme,host & port Forbidden.
Parameters:
scheme - A scheme that mast be in the proxySchemes StringMap.
host - A host that must pass the white and black lists
port - A port that must in the allowedConnectPorts Set
openNonPrivPorts - If true ports greater than 1024 are allowed.
Returns:
True if the request to the scheme,host and port is not forbidden.

isForbidden

protected boolean isForbidden(URI uri)
Is URL Forbidden.
Returns:
True if the URL is not forbidden. Calls isForbidden(scheme,host,port,true);

isProxied

protected URL isProxied(URI uri)
            throws MalformedURLException
Is URL Proxied. Method to allow derived handlers to select which URIs are proxied and to where.
Parameters:
uri - The requested URI, which should include a scheme, host and port.
Returns:
The URL to proxy to, or null if the passed URI should not be proxied. The default implementation returns the passed uri if isForbidden() returns true.

newHttpTunnel

protected HttpTunnel newHttpTunnel(HttpRequest request,
                                   HttpResponse response,
                                   InetAddress iaddr,
                                   int port,
                                   int timeoutMS)
            throws IOException

sendForbid

protected void sendForbid(HttpRequest request,
                          HttpResponse response,
                          URI uri)
            throws IOException
Send Forbidden. Method called to send forbidden response. Default implementation calls sendError(403)

setAnonymous

public void setAnonymous(boolean anonymous)
Parameters:
anonymous - The anonymous to set.

setProxyHostsBlackList

public void setProxyHostsBlackList(String[] hosts)
Set proxy host black list.
Parameters:
hosts - Array of hostnames and IPs that are NOT proxied.

setProxyHostsWhiteList

public void setProxyHostsWhiteList(String[] hosts)
Set proxy host white list.
Parameters:
hosts - Array of hostnames and IPs that are proxied, or null if all hosts are proxied.

setTunnelTimeoutMs

public void setTunnelTimeoutMs(int ms)
Tunnel timeout. IE on win2000 has connections issues with normal timeout handling. This timeout should be set to a low value that will expire to allow IE to see the end of the tunnel connection.

start

public void start()
            throws Exception
Specified by:
start in interface LifeCycle
Overrides:
start in interface AbstractHttpHandler

Copyright © 2004 Mortbay Consulting Pty. Ltd. All Rights Reserved.