
org.asynchttpclient.netty.request.NettyRequestSender Maven / Gradle / Ivy
/*
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package org.asynchttpclient.netty.request;
import static org.asynchttpclient.util.Assertions.*;
import static org.asynchttpclient.util.AuthenticatorUtils.*;
import static org.asynchttpclient.util.HttpUtils.*;
import static org.asynchttpclient.util.ProxyUtils.getProxyServer;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.util.Timeout;
import io.netty.util.Timer;
import io.netty.util.TimerTask;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.asynchttpclient.AsyncHandler;
import org.asynchttpclient.AsyncHttpClientConfig;
import org.asynchttpclient.ListenableFuture;
import org.asynchttpclient.Realm;
import org.asynchttpclient.Request;
import org.asynchttpclient.Realm.AuthScheme;
import org.asynchttpclient.filter.FilterContext;
import org.asynchttpclient.filter.FilterException;
import org.asynchttpclient.filter.IOExceptionFilter;
import org.asynchttpclient.handler.AsyncHandlerExtensions;
import org.asynchttpclient.handler.TransferCompletionHandler;
import org.asynchttpclient.netty.Callback;
import org.asynchttpclient.netty.NettyResponseFuture;
import org.asynchttpclient.netty.channel.ChannelManager;
import org.asynchttpclient.netty.channel.ChannelState;
import org.asynchttpclient.netty.channel.Channels;
import org.asynchttpclient.netty.channel.NettyConnectListener;
import org.asynchttpclient.netty.timeout.ReadTimeoutTimerTask;
import org.asynchttpclient.netty.timeout.RequestTimeoutTimerTask;
import org.asynchttpclient.netty.timeout.TimeoutsHolder;
import org.asynchttpclient.proxy.ProxyServer;
import org.asynchttpclient.uri.Uri;
import org.asynchttpclient.ws.WebSocketUpgradeHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class NettyRequestSender {
private static final Logger LOGGER = LoggerFactory.getLogger(NettyRequestSender.class);
private final AsyncHttpClientConfig config;
private final ChannelManager channelManager;
private final Timer nettyTimer;
private final AtomicBoolean closed;
private final NettyRequestFactory requestFactory;
public NettyRequestSender(AsyncHttpClientConfig config,//
ChannelManager channelManager,//
Timer nettyTimer,//
AtomicBoolean closed) {
this.config = config;
this.channelManager = channelManager;
this.nettyTimer = nettyTimer;
this.closed = closed;
requestFactory = new NettyRequestFactory(config);
}
public ListenableFuture sendRequest(final Request request,//
final AsyncHandler asyncHandler,//
NettyResponseFuture future,//
boolean reclaimCache) {
if (isClosed())
throw new IllegalStateException("Closed");
validateWebSocketRequest(request, asyncHandler);
ProxyServer proxyServer = getProxyServer(config, request);
boolean connectIsDone = future != null //
&& future.getNettyRequest() != null //
&& future.getNettyRequest().getHttpRequest().getMethod() == HttpMethod.CONNECT //
&& !request.getMethod().equals(HttpMethod.CONNECT.name());
// websockets use connect tunnelling to work with proxies
if (proxyServer != null && (request.getUri().isSecured() || request.getUri().isWebSocket()) && !connectIsDone)
// SSL proxy or websocket, have to handle CONNECT
if (future != null && future.isConnectAllowed())
// CONNECT forced
return sendRequestWithCertainForceConnect(request, asyncHandler, future, reclaimCache, proxyServer, true);
else
return sendRequestThroughSslProxy(request, asyncHandler, future, reclaimCache, proxyServer);
else
return sendRequestWithCertainForceConnect(request, asyncHandler, future, reclaimCache, proxyServer, false);
}
/**
* We know for sure if we have to force to connect or not, so we can build
* the HttpRequest right away This reduces the probability of having a
* pooled channel closed by the server by the time we build the request
*/
private ListenableFuture sendRequestWithCertainForceConnect(//
Request request,//
AsyncHandler asyncHandler,//
NettyResponseFuture future,//
boolean reclaimCache,//
ProxyServer proxyServer,//
boolean forceConnect) {
NettyResponseFuture newFuture = newNettyRequestAndResponseFuture(request, asyncHandler, future, proxyServer, forceConnect);
Channel channel = getCachedChannel(future, request, proxyServer, asyncHandler);
if (Channels.isChannelValid(channel))
return sendRequestWithCachedChannel(request, proxyServer, newFuture, asyncHandler, channel);
else
return sendRequestWithNewChannel(request, proxyServer, newFuture, asyncHandler, reclaimCache);
}
/**
* Using CONNECT depends on wither we can fetch a valid channel or not Loop
* until we get a valid channel from the pool and it's still valid once the
* request is built @
*/
@SuppressWarnings("unused")
private ListenableFuture sendRequestThroughSslProxy(//
Request request,//
AsyncHandler asyncHandler,//
NettyResponseFuture future,//
boolean reclaimCache,//
ProxyServer proxyServer) {
NettyResponseFuture newFuture = null;
for (int i = 0; i < 3; i++) {
Channel channel = getCachedChannel(future, request, proxyServer, asyncHandler);
if (Channels.isChannelValid(channel))
if (newFuture == null)
newFuture = newNettyRequestAndResponseFuture(request, asyncHandler, future, proxyServer, false);
if (Channels.isChannelValid(channel))
// if the channel is still active, we can use it, otherwise try
// gain
return sendRequestWithCachedChannel(request, proxyServer, newFuture, asyncHandler, channel);
else
// pool is empty
break;
}
newFuture = newNettyRequestAndResponseFuture(request, asyncHandler, future, proxyServer, true);
return sendRequestWithNewChannel(request, proxyServer, newFuture, asyncHandler, reclaimCache);
}
private NettyResponseFuture newNettyRequestAndResponseFuture(final Request request, final AsyncHandler asyncHandler, NettyResponseFuture originalFuture,
ProxyServer proxy, boolean forceConnect) {
Realm realm = null;
if (originalFuture != null) {
realm = originalFuture.getRealm();
} else if (config.getRealm() != null ){
realm = config.getRealm();
} else {
realm = request.getRealm();
}
Realm proxyRealm = null;
if (originalFuture != null) {
proxyRealm = originalFuture.getProxyRealm();
} else if (proxy != null){
proxyRealm = proxy.getRealm();
}
NettyRequest nettyRequest = requestFactory.newNettyRequest(request, forceConnect, proxy, realm, proxyRealm);
if (originalFuture == null) {
NettyResponseFuture future = newNettyResponseFuture(request, asyncHandler, nettyRequest, proxy);
future.setRealm(realm);
future.setProxyRealm(proxyRealm);
return future;
} else {
originalFuture.setNettyRequest(nettyRequest);
originalFuture.setCurrentRequest(request);
return originalFuture;
}
}
private Channel getCachedChannel(NettyResponseFuture> future, Request request, ProxyServer proxyServer, AsyncHandler> asyncHandler) {
if (future != null && future.reuseChannel() && Channels.isChannelValid(future.channel()))
return future.channel();
else
return pollAndVerifyCachedChannel(request, proxyServer, asyncHandler);
}
private ListenableFuture sendRequestWithCachedChannel(Request request, ProxyServer proxy, NettyResponseFuture future, AsyncHandler asyncHandler, Channel channel) {
if (asyncHandler instanceof AsyncHandlerExtensions)
AsyncHandlerExtensions.class.cast(asyncHandler).onConnectionPooled(channel);
future.setChannelState(ChannelState.POOLED);
future.attachChannel(channel, false);
LOGGER.debug("Using cached Channel {} for {} '{}'", channel, future.getNettyRequest().getHttpRequest().getMethod(), future.getNettyRequest().getHttpRequest().getUri());
if (Channels.isChannelValid(channel)) {
Channels.setAttribute(channel, future);
writeRequest(future, channel);
} else {
// bad luck, the channel was closed in-between
// there's a very good chance onClose was already notified but the
// future wasn't already registered
handleUnexpectedClosedChannel(channel, future);
}
return future;
}
private ListenableFuture sendRequestWithNewChannel(//
Request request,//
ProxyServer proxy,//
NettyResponseFuture future,//
AsyncHandler asyncHandler,//
boolean reclaimCache) {
// some headers are only set when performing the first request
HttpHeaders headers = future.getNettyRequest().getHttpRequest().headers();
Realm realm = future.getRealm();
Realm proxyRealm = future.getProxyRealm();
requestFactory.addAuthorizationHeader(headers, perConnectionAuthorizationHeader(request, proxy, realm));
requestFactory.setProxyAuthorizationHeader(headers, perConnectionProxyAuthorizationHeader(request, proxyRealm));
future.getInAuth().set(realm != null && realm.isUsePreemptiveAuth() && realm.getScheme() != AuthScheme.NTLM);
future.getInProxyAuth().set(proxyRealm != null && proxyRealm.isUsePreemptiveAuth() && proxyRealm.getScheme() != AuthScheme.NTLM);
// Do not throw an exception when we need an extra connection for a redirect
// FIXME why? This violate the max connection per host handling, right?
Bootstrap bootstrap = channelManager.getBootstrap(request.getUri(), proxy);
boolean channelPreempted = false;
Object partitionKey = future.getPartitionKey();
try {
// Do not throw an exception when we need an extra connection for a
// redirect.
if (!reclaimCache) {
channelManager.preemptChannel(partitionKey);
channelPreempted = true;
}
if (asyncHandler instanceof AsyncHandlerExtensions)
AsyncHandlerExtensions.class.cast(asyncHandler).onConnectionOpen();
new NettyChannelConnector(request, proxy, asyncHandler)
.connect(bootstrap, new NettyConnectListener(future, this, channelManager, channelPreempted, partitionKey));
} catch (Throwable t) {
if (channelPreempted)
channelManager.abortChannelPreemption(partitionKey);
abort(null, future, t.getCause() == null ? t : t.getCause());
}
return future;
}
private NettyResponseFuture newNettyResponseFuture(Request request, AsyncHandler asyncHandler, NettyRequest nettyRequest, ProxyServer proxyServer) {
NettyResponseFuture future = new NettyResponseFuture<>(//
request,//
asyncHandler,//
nettyRequest,//
config.getMaxRequestRetry(),//
request.getConnectionPoolPartitioning(),//
proxyServer);
String expectHeader = request.getHeaders().get(HttpHeaders.Names.EXPECT);
if (expectHeader != null && expectHeader.equalsIgnoreCase(HttpHeaders.Values.CONTINUE))
future.setDontWriteBodyBecauseExpectContinue(true);
return future;
}
public void writeRequest(NettyResponseFuture future, Channel channel) {
NettyRequest nettyRequest = future.getNettyRequest();
HttpRequest httpRequest = nettyRequest.getHttpRequest();
AsyncHandler handler = future.getAsyncHandler();
// if the channel is dead because it was pooled and the remote
// server decided to close it,
// we just let it go and the channelInactive do its work
if (!Channels.isChannelValid(channel))
return;
try {
if (handler instanceof TransferCompletionHandler)
configureTransferAdapter(handler, httpRequest);
if (!future.isHeadersAlreadyWrittenOnContinue()) {
if (future.getAsyncHandler() instanceof AsyncHandlerExtensions)
AsyncHandlerExtensions.class.cast(future.getAsyncHandler()).onRequestSend(nettyRequest);
channel.writeAndFlush(httpRequest, channel.newProgressivePromise()).addListener(new ProgressListener(future.getAsyncHandler(), future, true, 0L));
}
if (!future.isDontWriteBodyBecauseExpectContinue() && httpRequest.getMethod() != HttpMethod.CONNECT && nettyRequest.getBody() != null)
nettyRequest.getBody().write(channel, future);
// don't bother scheduling timeouts if channel became invalid
if (Channels.isChannelValid(channel))
scheduleTimeouts(future);
} catch (Exception e) {
LOGGER.error("Can't write request", e);
abort(channel, future, e);
}
}
private void configureTransferAdapter(AsyncHandler> handler, HttpRequest httpRequest) {
HttpHeaders h = new DefaultHttpHeaders().set(httpRequest.headers());
TransferCompletionHandler.class.cast(handler).headers(h);
}
private void scheduleTimeouts(NettyResponseFuture> nettyResponseFuture) {
nettyResponseFuture.touch();
int requestTimeoutInMs = requestTimeout(config, nettyResponseFuture.getTargetRequest());
TimeoutsHolder timeoutsHolder = new TimeoutsHolder();
if (requestTimeoutInMs != -1) {
Timeout requestTimeout = newTimeout(new RequestTimeoutTimerTask(nettyResponseFuture, this, timeoutsHolder, requestTimeoutInMs), requestTimeoutInMs);
timeoutsHolder.requestTimeout = requestTimeout;
}
int readTimeoutValue = config.getReadTimeout();
if (readTimeoutValue != -1 && readTimeoutValue < requestTimeoutInMs) {
// no need to schedule a readTimeout if the requestTimeout happens first
Timeout readTimeout = newTimeout(new ReadTimeoutTimerTask(nettyResponseFuture, this, timeoutsHolder, requestTimeoutInMs, readTimeoutValue), readTimeoutValue);
timeoutsHolder.readTimeout = readTimeout;
}
nettyResponseFuture.setTimeoutsHolder(timeoutsHolder);
}
public Timeout newTimeout(TimerTask task, long delay) {
return nettyTimer.newTimeout(task, delay, TimeUnit.MILLISECONDS);
}
public void abort(Channel channel, NettyResponseFuture> future, Throwable t) {
if (channel != null)
channelManager.closeChannel(channel);
if (!future.isDone()) {
future.setChannelState(ChannelState.CLOSED);
LOGGER.debug("Aborting Future {}\n", future);
LOGGER.debug(t.getMessage(), t);
future.abort(t);
}
}
public void handleUnexpectedClosedChannel(Channel channel, NettyResponseFuture> future) {
if (future.isDone())
channelManager.closeChannel(channel);
else if (!retry(future))
abort(channel, future, REMOTELY_CLOSED_EXCEPTION);
}
public boolean retry(NettyResponseFuture> future) {
if (isClosed())
return false;
if (future.canBeReplayed()) {
future.setChannelState(ChannelState.RECONNECTED);
future.getAndSetStatusReceived(false);
LOGGER.debug("Trying to recover request {}\n", future.getNettyRequest().getHttpRequest());
if (future.getAsyncHandler() instanceof AsyncHandlerExtensions) {
AsyncHandlerExtensions.class.cast(future.getAsyncHandler()).onRetry();
}
try {
sendNextRequest(future.getCurrentRequest(), future);
return true;
} catch (Exception e) {
abort(future.channel(), future, e);
return false;
}
} else {
LOGGER.debug("Unable to recover future {}\n", future);
return false;
}
}
public boolean applyIoExceptionFiltersAndReplayRequest(NettyResponseFuture> future, IOException e, Channel channel) {
boolean replayed = false;
@SuppressWarnings({ "unchecked", "rawtypes" })
FilterContext> fc = new FilterContext.FilterContextBuilder().asyncHandler(future.getAsyncHandler()).request(future.getCurrentRequest()).ioException(e).build();
for (IOExceptionFilter asyncFilter : config.getIoExceptionFilters()) {
try {
fc = asyncFilter.filter(fc);
assertNotNull(fc, "filterContext");
} catch (FilterException efe) {
abort(channel, future, efe);
}
}
if (fc.replayRequest() && future.canBeReplayed()) {
replayRequest(future, fc, channel);
replayed = true;
}
return replayed;
}
public void sendNextRequest(final Request request, final NettyResponseFuture future) {
sendRequest(request, future.getAsyncHandler(), future, true);
}
private void validateWebSocketRequest(Request request, AsyncHandler> asyncHandler) {
Uri uri = request.getUri();
boolean isWs = uri.isWebSocket();
if (asyncHandler instanceof WebSocketUpgradeHandler) {
if (!isWs)
throw new IllegalArgumentException("WebSocketUpgradeHandler but scheme isn't ws or wss: " + uri.getScheme());
else if (!request.getMethod().equals(HttpMethod.GET.name()))
throw new IllegalArgumentException("WebSocketUpgradeHandler but method isn't GET: " + request.getMethod());
} else if (isWs) {
throw new IllegalArgumentException("No WebSocketUpgradeHandler but scheme is " + uri.getScheme());
}
}
private Channel pollAndVerifyCachedChannel(Request request, ProxyServer proxy, AsyncHandler> asyncHandler) {
if (asyncHandler instanceof AsyncHandlerExtensions)
AsyncHandlerExtensions.class.cast(asyncHandler).onConnectionPool();
Uri uri = request.getUri();
String virtualHost = request.getVirtualHost();
final Channel channel = channelManager.poll(uri, virtualHost, proxy, request.getConnectionPoolPartitioning());
if (channel != null) {
LOGGER.debug("Using cached Channel {}\n for uri {}\n", channel, uri);
try {
channelManager.verifyChannelPipeline(channel.pipeline(), uri, virtualHost);
} catch (Exception ex) {
LOGGER.debug(ex.getMessage(), ex);
}
}
return channel;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public void replayRequest(final NettyResponseFuture> future, FilterContext fc, Channel channel) {
Request newRequest = fc.getRequest();
future.setAsyncHandler(fc.getAsyncHandler());
future.setChannelState(ChannelState.NEW);
future.touch();
LOGGER.debug("\n\nReplaying Request {}\n for Future {}\n", newRequest, future);
if (future.getAsyncHandler() instanceof AsyncHandlerExtensions)
AsyncHandlerExtensions.class.cast(future.getAsyncHandler()).onRetry();
channelManager.drainChannelAndOffer(channel, future);
sendNextRequest(newRequest, future);
}
public boolean isClosed() {
return closed.get();
}
public final Callback newExecuteNextRequestCallback(final NettyResponseFuture> future, final Request nextRequest) {
return new Callback(future) {
@Override
public void call() {
sendNextRequest(nextRequest, future);
}
};
}
public void drainChannelAndExecuteNextRequest(final Channel channel, final NettyResponseFuture> future, Request nextRequest) {
Channels.setAttribute(channel, newExecuteNextRequestCallback(future, nextRequest));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy