Class ProxyHandler
- All Implemented Interfaces:
Handler
,Request.Handler
,Container
,Destroyable
,Dumpable
,Dumpable.DumpableContainer
,LifeCycle
,Invocable
- Direct Known Subclasses:
ProxyHandler.Forward
,ProxyHandler.Reverse
A Handler
that can be used to implement a forward
proxy ("proxy")
or a reverse proxy ("gateway")
as defined by
RFC 7230.
This class uses HttpClient
to send requests from the proxy to the server.
The HttpClient
instance is either
set explicitly
, or created implicitly.
To customize the implicit HttpClient
instance, applications can
override newHttpClient()
and configureHttpClient(HttpClient)
.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
AProxyHandler
that can be used to implement a forward proxy server.protected static class
protected class
static class
AProxyHandler
that can be used to implement a reverse proxy.Nested classes/interfaces inherited from class org.eclipse.jetty.server.Handler.Abstract
Handler.Abstract.NonBlocking
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener, AbstractLifeCycle.StopException
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Container
Container.InheritedListener, Container.Listener
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Dumpable
Dumpable.DumpableContainer
Nested classes/interfaces inherited from interface org.eclipse.jetty.server.Handler
Handler.Abstract, Handler.AbstractContainer, Handler.Collection, Handler.Container, Handler.Sequence, Handler.Singleton, Handler.Wrapper
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.thread.Invocable
Invocable.Callable, Invocable.InvocationType, Invocable.ReadyTask, Invocable.Task
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle
LifeCycle.Listener
Nested classes/interfaces inherited from interface org.eclipse.jetty.server.Request.Handler
Request.Handler.AbortException
-
Field Summary
Fields inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
FAILED, STARTED, STARTING, STOPPED, STOPPING
Fields inherited from interface org.eclipse.jetty.util.thread.Invocable
__nonBlocking, NOOP
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
addForwardedHeader
(Request clientToProxyRequest, Request proxyToServerRequest) protected void
addProxyHeaders
(Request clientToProxyRequest, Request proxyToServerRequest) protected void
addViaHeader
(Request clientToProxyRequest, Request proxyToServerRequest) protected void
configureHttpClient
(HttpClient httpClient) Configures theHttpClient
instance before it is started.protected void
copyRequestHeaders
(Request clientToProxyRequest, Request proxyToServerRequest) protected void
doStart()
Starts the managed lifecycle beans in the order they were added.protected HttpField
filterServerToProxyResponseField
(HttpField serverToProxyResponseField) Get the proxy-to-serverHost
header value.Get the value to use for theVia
header.boolean
handle
(Request clientToProxyRequest, Response proxyToClientResponse, Callback proxyToClientCallback) Invoked to handle the passed HTTP request and response.protected HttpClient
Creates a newHttpClient
instance, by default with a thread pool namedproxy-client
and with thedynamic transport
configured only with HTTP/1.1.protected Request
newProxyToServerRequest
(Request clientToProxyRequest, HttpURI newHttpURI) protected Request.Content
newProxyToServerRequestContent
(Request clientToProxyRequest, Response proxyToClientResponse, Request proxyToServerRequest) protected Response.CompleteListener
newServerToProxyResponseListener
(Request clientToProxyRequest, Request proxyToServerRequest, Response proxyToClientResponse, Callback proxyToClientCallback) protected void
onProxyToClientResponseComplete
(Request clientToProxyRequest, Request proxyToServerRequest, Response serverToProxyResponse, Response proxyToClientResponse, Callback proxyToClientCallback) protected void
onProxyToClientResponseFailure
(Request clientToProxyRequest, Request proxyToServerRequest, Response serverToProxyResponse, Response proxyToClientResponse, Callback proxyToClientCallback, Throwable failure) protected Runnable
onServerToProxyResponse100Continue
(Request clientToProxyRequest, Request proxyToServerRequest) protected void
onServerToProxyResponse102Processing
(Request clientToProxyRequest, Request proxyToServerRequest, HttpFields serverToProxyResponseHeaders, Response proxyToClientResponse) protected void
onServerToProxyResponse103EarlyHints
(Request clientToProxyRequest, Request proxyToServerRequest, HttpFields serverToProxyResponseHeaders, Response proxyToClientResponse) protected void
onServerToProxyResponseFailure
(Request clientToProxyRequest, Request proxyToServerRequest, Response serverToProxyResponse, Response proxyToClientResponse, Callback proxyToClientCallback, Throwable failure) protected static String
protected abstract HttpURI
rewriteHttpURI
(Request clientToProxyRequest) Rewrites the client-to-proxy request URI to the proxy-to-server request URI.protected void
sendProxyToServerRequest
(Request clientToProxyRequest, Request proxyToServerRequest, Response proxyToClientResponse, Callback proxyToClientCallback) void
setHttpClient
(HttpClient httpClient) void
setProxyToServerHost
(String host) Sets the value to use for theHost
header in proxy-to-server requests.void
setViaHost
(String viaHost) Sets the value to use for theVia
header in proxy-to-server requests.Methods inherited from class org.eclipse.jetty.server.Handler.Abstract
destroy, doStop, getInvocationType, getServer, setServer
Methods inherited from class org.eclipse.jetty.util.component.ContainerLifeCycle
addBean, addBean, addEventListener, addManaged, contains, dump, dump, dump, dumpObjects, dumpStdErr, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, installBean, installBean, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, start, stop, unmanage, updateBean, updateBean, updateBeans, updateBeans
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
getEventListeners, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, setEventListeners, start, stop, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.eclipse.jetty.util.component.Container
getCachedBeans, getEventListeners
Methods inherited from interface org.eclipse.jetty.util.component.Dumpable.DumpableContainer
isDumpable
Methods inherited from interface org.eclipse.jetty.util.component.LifeCycle
addEventListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeEventListener, start, stop
-
Constructor Details
-
ProxyHandler
public ProxyHandler()
-
-
Method Details
-
getHttpClient
-
setHttpClient
-
getProxyToServerHost
Get the proxy-to-serverHost
header value.- Returns:
- the proxy-to-server
Host
header value
-
setProxyToServerHost
Sets the value to use for the
Host
header in proxy-to-server requests.If
null
, the client-to-proxy value is used.- Parameters:
host
- the proxy-to-serverHost
header value
-
getViaHost
Get the value to use for theVia
header.- Returns:
- the value to use for the
Via
header
-
setViaHost
Sets the value to use for the
Via
header in proxy-to-server requests.If
null
, the local host name is used.- Parameters:
viaHost
- the value to use for theVia
header
-
doStart
Description copied from class:ContainerLifeCycle
Starts the managed lifecycle beans in the order they were added.- Overrides:
doStart
in classHandler.Abstract
- Throws:
AbstractLifeCycle.StopException
- If thrown, the lifecycle will immediately be stopped.Exception
- If there was a problem starting. Will cause a transition to FAILED state
-
newHttpClient
Creates a new
HttpClient
instance, by default with a thread pool namedproxy-client
and with thedynamic transport
configured only with HTTP/1.1.- Returns:
- a new
HttpClient
instance
-
configureHttpClient
Configures the
HttpClient
instance before it is started.- Parameters:
httpClient
- theHttpClient
instance to configure
-
requestId
-
handle
public boolean handle(Request clientToProxyRequest, Response proxyToClientResponse, Callback proxyToClientCallback) Description copied from interface:Request.Handler
Invoked to handle the passed HTTP request and response.
The request is accepted by returning true, then handling must be concluded by completing the passed callback. The handling may be asynchronous, i.e. this method may return true and complete the given callback later, possibly from a different thread. If this method returns false, then the callback must not be invoked and any mutation on the response reversed.
Exceptions thrown by this method may be subsequently handled by an error
Request.Handler
, if present, otherwise a default HTTP 500 error is generated and the callback completed while writing the error response.The simplest implementation is:
public boolean handle(Request request, Response response, Callback callback) { callback.succeeded(); return true; }
A HelloWorld implementation is:
public boolean handle(Request request, Response response, Callback callback) { response.write(true, ByteBuffer.wrap("Hello World\n".getBytes(StandardCharsets.UTF_8)), callback); return true; }
- Parameters:
clientToProxyRequest
- the HTTP request to handleproxyToClientResponse
- the HTTP response to handleproxyToClientCallback
- the callback to complete when the handling is complete- Returns:
- True if and only if the request will be handled, a response generated and the callback eventually called. This may occur within the scope of the call to this method, or asynchronously some time later. If false is returned, then this method must not generate a response, nor complete the callback.
- See Also:
-
rewriteHttpURI
Rewrites the client-to-proxy request URI to the proxy-to-server request URI.
- Parameters:
clientToProxyRequest
- the client-to-proxy request- Returns:
- an
HttpURI
for the proxy-to-server request
-
newProxyToServerRequest
-
copyRequestHeaders
-
addProxyHeaders
-
addViaHeader
-
addForwardedHeader
-
newProxyToServerRequestContent
protected Request.Content newProxyToServerRequestContent(Request clientToProxyRequest, Response proxyToClientResponse, Request proxyToServerRequest) -
sendProxyToServerRequest
-
newServerToProxyResponseListener
protected Response.CompleteListener newServerToProxyResponseListener(Request clientToProxyRequest, Request proxyToServerRequest, Response proxyToClientResponse, Callback proxyToClientCallback) -
filterServerToProxyResponseField
-
onServerToProxyResponseFailure
-
onServerToProxyResponse100Continue
-
onServerToProxyResponse102Processing
protected void onServerToProxyResponse102Processing(Request clientToProxyRequest, Request proxyToServerRequest, HttpFields serverToProxyResponseHeaders, Response proxyToClientResponse) -
onServerToProxyResponse103EarlyHints
protected void onServerToProxyResponse103EarlyHints(Request clientToProxyRequest, Request proxyToServerRequest, HttpFields serverToProxyResponseHeaders, Response proxyToClientResponse) -
onProxyToClientResponseComplete
-
onProxyToClientResponseFailure
-