@ChannelHandler.Sharable public class ProxyToServerConnection extends HttpProxyConnection<io.netty.handler.codec.http.HttpResponse>
Represents a connection from our proxy to a server on the web. ProxyConnections are reused fairly liberally, and can go from disconnected to connected, back to disconnected and so on.
Connecting a ProxyToServerConnection can involve more than just connecting the underlying Channel. In
particular, the connection may use encryption (i.e. TLS) and it may also establish an HTTP CONNECT tunnel. The
various steps involved in fully establishing a connection are encapsulated in the property connectionFlow,
which is initialized in initializeConnectionFlow().
| 限定符和类型 | 类和说明 |
|---|---|
private class |
ProxyToServerConnection.HeadAwareHttpResponseDecoder
Responses to HEAD requests aren't supposed to have content, but Netty doesn't know that any given response is to
a HEAD request, so it needs to be told that there's no content so that it doesn't hang waiting for it.
|
HttpProxyConnection.RequestReadMonitor, HttpProxyConnection.RequestWrittenMonitor, HttpProxyConnection.ResponseReadMonitor, HttpProxyConnection.ResponseWrittenMonitorChannelHandler.Sharable| 限定符和类型 | 字段和说明 |
|---|---|
private ClientToProxyConnection |
clientConnection |
private ConnectionFlowStep |
ConnectChannel |
private ConnectionFlow |
connectionFlow |
private Object |
connectLock |
private HttpFilters |
currentFilters |
private HttpRequestWrapper |
currentHttpRequest |
private io.netty.handler.codec.http.HttpResponse |
currentHttpResponse |
private FullFlowContext |
flowContext |
private InetSocketAddress |
localAddress |
private ProxyConfigOptions |
proxyConfigOptions |
private InetSocketAddress |
remoteAddress |
private HttpProxyConnection.RequestWrittenMonitor |
requestWrittenMonitor |
private HttpProxyConnection.ResponseReadMonitor |
responseReadMonitor
Activity Tracking/Statistics
We track statistics on bytes, requests and responses by adding handlers at the appropriate parts of the pipeline
(see initChannelPipeline()).
|
private io.netty.handler.traffic.GlobalTrafficShapingHandler |
trafficHandler |
private TransportProtocol |
transportProtocol |
log, proxyManagerchannel, ctx, lastReadTime, logger, sslEngine| 限定符 | 构造器和说明 |
|---|---|
private |
ProxyToServerConnection(ProxyConfigOptions proxyConfigOptions,
HttpProxyManager httpProxyManager,
String applicationName,
ClientToProxyConnection clientConnection,
HttpFilters initialFilters,
io.netty.handler.traffic.GlobalTrafficShapingHandler globalTrafficShapingHandler,
InetSocketAddress remoteAddress) |
| 限定符和类型 | 方法和说明 |
|---|---|
protected void |
becameSaturated()
Callback that's invoked if this connection becomes saturated.
|
protected void |
becameWritable()
Callback that's invoked when this connection becomes writeable again.
|
protected void |
become(ConnectionState newState)
Lifecycle
|
private ChannelFuture |
connect() |
private void |
connectAndWrite(HttpRequestWrapper currentHttpRequest) |
protected boolean |
connectionFailed(Throwable cause)
Called when the connection to the server or upstream chained proxy fails.
|
(专用程序包) void |
connectionSucceeded(boolean shouldForwardInitialRequest)
Do all the stuff that needs to be done after our
ConnectionFlow has succeeded. |
(专用程序包) static ProxyToServerConnection |
create(ProxyConfigOptions proxyConfigOptions,
HttpProxyManager httpProxyManager,
String applicationName,
ClientToProxyConnection clientConnection,
HttpFilters initialFilters,
InetSocketAddress remoteAddress,
io.netty.handler.traffic.GlobalTrafficShapingHandler globalTrafficShapingHandler) |
protected void |
disconnected()
This method is called as soon as the underlying
Channel becomes disconnected. |
protected void |
exceptionCaught(Throwable cause)
Override this to handle exceptions that occurred during asynchronous processing on the
Channel. |
HttpRequestWrapper |
getCurrentHttpRequest() |
protected HttpFilters |
getHttpFiltersFromProxyServer(io.netty.handler.codec.http.HttpRequest httpRequest)
Request the ProxyServer for Filters.
|
InetSocketAddress |
getRemoteAddress() |
TransportProtocol |
getTransportProtocol()
Get transport protocol.
|
private String |
hostToString(InetSocketAddress address) |
private void |
initChannelPipeline(ChannelPipeline pipeline)
Initialize our
ChannelPipeline to connect the upstream server. |
private void |
initializeConnectionFlow() |
protected void |
readHTTPChunk(io.netty.handler.codec.http.HttpContent chunk)
Implement this to handle reading a chunk in a chunked transfer.
|
protected ConnectionState |
readHTTPInitial(io.netty.handler.codec.http.HttpResponse httpResponse)
Implement this to handle reading the initial object (e.g.
|
protected void |
readIncoming(ChannelHandlerContext ctx,
Object msg)
Reading
|
private void |
rememberCurrentResponse(io.netty.handler.codec.http.HttpResponse response)
Keeps track of the current HttpResponse so that we can associate its headers with future related chunks for this
same transfer.
|
private void |
replaceProxyHostForRemoteConnenct(io.netty.handler.codec.http.HttpRequest httpRequest,
String host) |
private void |
respondWith(io.netty.handler.codec.http.HttpObject httpObject) |
void |
setCurrentHttpRequest(HttpRequestWrapper currentHttpRequest) |
private void |
setupConnectionParameters()
Set up our connection parameters based on server address and chained proxies.
|
protected void |
timedOut()
This method is called when the underlying
Channel times out due to an idle timeout. |
(专用程序包) void |
write(HttpRequestWrapper httpRequest,
HttpFilters filters)
Like
write(Object) and also sets the current filters to the given value. |
(专用程序包) void |
write(Object msg)
This method is called by users of the ProxyConnection to send stuff out over the socket.
|
protected void |
writeHttp(io.netty.handler.codec.http.HttpObject httpObject)
Writes HttpObjects to the connection asynchronously.
|
connected, disconnect, doWrite, encrypt, encrypt, getLOG, getSslEngine, isSaturated, readProxyIncoming, resumeReading, stopReading, writeRaw, writeToChannel, writeToChannelNoFlushchannelActive, channelInactive, channelRead0, channelRegistered, channelRegistered0, channelWritabilityChanged, exceptionCaught, getApplicationName, getChannel, getCurrentState, is, isConnecting, userEventTriggeredacceptInboundMessage, channelReadchannelReadComplete, channelUnregisteredensureNotSharable, handlerAdded, handlerRemoved, isSharableclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waithandlerAdded, handlerRemovedprivate final ClientToProxyConnection clientConnection
private volatile TransportProtocol transportProtocol
private volatile InetSocketAddress remoteAddress
private volatile InetSocketAddress localAddress
private volatile HttpFilters currentFilters
private volatile ConnectionFlow connectionFlow
private final Object connectLock
private volatile HttpRequestWrapper currentHttpRequest
private volatile io.netty.handler.codec.http.HttpResponse currentHttpResponse
private volatile io.netty.handler.traffic.GlobalTrafficShapingHandler trafficHandler
private volatile FullFlowContext flowContext
private final ProxyConfigOptions proxyConfigOptions
private ConnectionFlowStep ConnectChannel
private HttpProxyConnection.ResponseReadMonitor responseReadMonitor
private HttpProxyConnection.RequestWrittenMonitor requestWrittenMonitor
private ProxyToServerConnection(ProxyConfigOptions proxyConfigOptions, HttpProxyManager httpProxyManager, String applicationName, ClientToProxyConnection clientConnection, HttpFilters initialFilters, io.netty.handler.traffic.GlobalTrafficShapingHandler globalTrafficShapingHandler, InetSocketAddress remoteAddress) throws UnknownHostException
static ProxyToServerConnection create(ProxyConfigOptions proxyConfigOptions, HttpProxyManager httpProxyManager, String applicationName, ClientToProxyConnection clientConnection, HttpFilters initialFilters, InetSocketAddress remoteAddress, io.netty.handler.traffic.GlobalTrafficShapingHandler globalTrafficShapingHandler) throws UnknownHostException
protected void readIncoming(ChannelHandlerContext ctx, Object msg)
readIncoming 在类中 ProxyConnection<Object>ctx - handler contextmsg - messageprotected ConnectionState readHTTPInitial(io.netty.handler.codec.http.HttpResponse httpResponse)
HttpProxyConnectionHttpRequest or HttpResponse).readHTTPInitial 在类中 HttpProxyConnection<io.netty.handler.codec.http.HttpResponse>httpResponse - http objectprotected void readHTTPChunk(io.netty.handler.codec.http.HttpContent chunk)
HttpProxyConnectionreadHTTPChunk 在类中 HttpProxyConnection<io.netty.handler.codec.http.HttpResponse>chunk - http contentvoid write(HttpRequestWrapper httpRequest, HttpFilters filters)
write(Object) and also sets the current filters to the given value.httpRequest - requestfilters - filter collectionvoid write(Object msg)
HttpProxyConnectionwrite 在类中 HttpProxyConnection<io.netty.handler.codec.http.HttpResponse>msg - messageprotected void writeHttp(io.netty.handler.codec.http.HttpObject httpObject)
HttpProxyConnectionwriteHttp 在类中 HttpProxyConnection<io.netty.handler.codec.http.HttpResponse>httpObject - http bjectprivate void replaceProxyHostForRemoteConnenct(io.netty.handler.codec.http.HttpRequest httpRequest,
String host)
private String hostToString(InetSocketAddress address)
protected void become(ConnectionState newState)
become 在类中 ProxyConnection<Object>newState - connection stateprotected void becameSaturated()
HttpProxyConnectionbecameSaturated 在类中 HttpProxyConnection<io.netty.handler.codec.http.HttpResponse>protected void becameWritable()
HttpProxyConnectionbecameWritable 在类中 HttpProxyConnection<io.netty.handler.codec.http.HttpResponse>protected void timedOut()
HttpProxyConnectionChannel times out due to an idle timeout.timedOut 在类中 HttpProxyConnection<io.netty.handler.codec.http.HttpResponse>protected void disconnected()
HttpProxyConnectionChannel becomes disconnected.disconnected 在类中 HttpProxyConnection<io.netty.handler.codec.http.HttpResponse>protected void exceptionCaught(Throwable cause)
HttpProxyConnectionChannel.exceptionCaught 在类中 HttpProxyConnection<io.netty.handler.codec.http.HttpResponse>cause - raised errorpublic TransportProtocol getTransportProtocol()
public InetSocketAddress getRemoteAddress()
protected HttpFilters getHttpFiltersFromProxyServer(io.netty.handler.codec.http.HttpRequest httpRequest)
HttpProxyConnectiongetHttpFiltersFromProxyServer 在类中 HttpProxyConnection<io.netty.handler.codec.http.HttpResponse>httpRequest - Filter attached to the give HttpRequest (if any)private void rememberCurrentResponse(io.netty.handler.codec.http.HttpResponse response)
response - http responseprivate void respondWith(io.netty.handler.codec.http.HttpObject httpObject)
private void connectAndWrite(HttpRequestWrapper currentHttpRequest)
private void initializeConnectionFlow()
private ChannelFuture connect()
protected boolean connectionFailed(Throwable cause) throws UnknownHostException
cause - the reason that our attempt to connect failed (can be null)UnknownHostException - when unable to resolve the hostname to an IP addressprivate void setupConnectionParameters()
throws UnknownHostException
UnknownHostException - when unable to resolve the hostname to an IP addressprivate void initChannelPipeline(ChannelPipeline pipeline)
ChannelPipeline to connect the upstream server. Proxy acts as a client here.
A ChannelPipeline invokes the read (Inbound) handlers in ascending ordering of the list and then the
write (Outbound) handlers in descending ordering.
Regarding the Javadoc of HttpObjectAggregator it's needed to have the HttpResponseEncoder or
HttpRequestEncoder before the HttpObjectAggregator in the ChannelPipeline.pipeline - channel pipelinevoid connectionSucceeded(boolean shouldForwardInitialRequest)
Do all the stuff that needs to be done after our ConnectionFlow has succeeded.
shouldForwardInitialRequest - whether or not we should forward the initial HttpRequest to the server after the connection has been
established.public HttpRequestWrapper getCurrentHttpRequest()
public void setCurrentHttpRequest(HttpRequestWrapper currentHttpRequest)
Copyright © 2021 Zodiac. All rights reserved.