Skip to content

Commit 4865e37

Browse files
committed
Minor cleanup to the grizzly provider.
1 parent d2c3551 commit 4865e37

File tree

1 file changed

+47
-52
lines changed

1 file changed

+47
-52
lines changed

providers/grizzly/src/main/java/com/ning/http/client/providers/grizzly/GrizzlyAsyncHttpProvider.java

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
import com.ning.http.util.SslUtils;
5555

5656
import org.glassfish.grizzly.Buffer;
57+
import org.glassfish.grizzly.CloseListener;
58+
import org.glassfish.grizzly.CloseType;
59+
import org.glassfish.grizzly.Closeable;
5760
import org.glassfish.grizzly.CompletionHandler;
5861
import org.glassfish.grizzly.Connection;
5962
import org.glassfish.grizzly.EmptyCompletionHandler;
@@ -219,7 +222,7 @@ public void completed(final Connection c) {
219222
try {
220223
execute(c, request, handler, future);
221224
} catch (Exception e) {
222-
if (e instanceof RuntimeException || e instanceof IOException) {
225+
if (e instanceof IOException) {
223226
failed(e);
224227
}
225228
if (LOGGER.isWarnEnabled()) {
@@ -299,9 +302,7 @@ protected <T> ListenableFuture<T> execute(final Connection c,
299302
}
300303
c.write(request, createWriteCompletionHandler(future));
301304
} catch (Exception e) {
302-
if (e instanceof RuntimeException) {
303-
throw (RuntimeException) e;
304-
} else if (e instanceof IOException) {
305+
if (e instanceof IOException) {
305306
throw (IOException) e;
306307
}
307308
if (LOGGER.isWarnEnabled()) {
@@ -740,7 +741,6 @@ public void failed(Throwable throwable) {
740741
if (throwable instanceof EOFException) {
741742
context.abort(new IOException("Remotely Closed"));
742743
}
743-
context.abort(throwable);
744744
}
745745

746746
@Override
@@ -808,23 +808,6 @@ public NextAction handleEvent(final FilterChainContext ctx,
808808

809809
}
810810

811-
// @Override
812-
// public NextAction handleRead(FilterChainContext ctx) throws IOException {
813-
// Object message = ctx.getMessage();
814-
// if (HttpPacket.isHttp(message)) {
815-
// final HttpPacket packet = (HttpPacket) message;
816-
// HttpResponsePacket responsePacket;
817-
// if (HttpContent.isContent(packet)) {
818-
// responsePacket = (HttpResponsePacket) ((HttpContent) packet).getHttpHeader();
819-
// } else {
820-
// responsePacket = (HttpResponsePacket) packet;
821-
// }
822-
// if (HttpStatus.SWITCHING_PROTOCOLS_101.statusMatches(responsePacket.getStatus())) {
823-
// return ctx.getStopAction();
824-
// }
825-
// }
826-
// return super.handleRead(ctx);
827-
// }
828811

829812
// ----------------------------------------------------- Private Methods
830813

@@ -841,19 +824,10 @@ private boolean sendAsGrizzlyRequest(final Request request,
841824
final URI uri = httpCtx.request.getURI();
842825
final HttpRequestPacket.Builder builder = HttpRequestPacket.builder();
843826
final String scheme = uri.getScheme();
844-
boolean secure = "https".equals(scheme) || "wss".equals(scheme);
827+
boolean secure = isSecure(scheme);
845828
builder.method(request.getMethod());
846829
builder.protocol(Protocol.HTTP_1_1);
847-
String host = request.getVirtualHost();
848-
if (host != null) {
849-
builder.header(Header.Host, host);
850-
} else {
851-
if (uri.getPort() == -1) {
852-
builder.header(Header.Host, uri.getHost());
853-
} else {
854-
builder.header(Header.Host, uri.getHost() + ':' + uri.getPort());
855-
}
856-
}
830+
addHostHeader(request, uri, builder);
857831
final ProxyServer proxy = ProxyUtils.getProxyServer(config, request);
858832
final boolean useProxy = proxy != null;
859833
if (useProxy) {
@@ -902,22 +876,9 @@ private boolean sendAsGrizzlyRequest(final Request request,
902876
if (!useProxy && !httpCtx.isWSRequest) {
903877
addQueryString(request, requestPacket);
904878
}
905-
addHeaders(request, requestPacket);
879+
addHeaders(request, requestPacket, proxy);
906880
addCookies(request, requestPacket);
907881

908-
if (useProxy) {
909-
boolean avoidProxy = ProxyUtils.avoidProxy(proxy, request);
910-
if (!avoidProxy) {
911-
if (!requestPacket.getHeaders().contains(Header.ProxyConnection)) {
912-
requestPacket.setHeader(Header.ProxyConnection, "keep-alive");
913-
}
914-
915-
if (proxy.getPrincipal() != null && proxy.isBasic()) {
916-
requestPacket.setHeader(Header.ProxyAuthorization, AuthenticatorUtils.computeBasicAuthentication(proxy));
917-
}
918-
919-
}
920-
}
921882
final AsyncHandler h = httpCtx.handler;
922883
if (h != null) {
923884
if (TransferCompletionHandler.class.isAssignableFrom(h.getClass())) {
@@ -930,6 +891,25 @@ private boolean sendAsGrizzlyRequest(final Request request,
930891

931892
}
932893

894+
private boolean isSecure(String scheme) {
895+
return "https".equals(scheme) || "wss".equals(scheme);
896+
}
897+
898+
private void addHostHeader(final Request request,
899+
final URI uri,
900+
final HttpRequestPacket.Builder builder) {
901+
String host = request.getVirtualHost();
902+
if (host != null) {
903+
builder.header(Header.Host, host);
904+
} else {
905+
if (uri.getPort() == -1) {
906+
builder.header(Header.Host, uri.getHost());
907+
} else {
908+
builder.header(Header.Host, uri.getHost() + ':' + uri.getPort());
909+
}
910+
}
911+
}
912+
933913
private boolean isUpgradeRequest(final AsyncHandler handler) {
934914
return (handler instanceof UpgradeHandler);
935915
}
@@ -954,7 +934,8 @@ private void convertToUpgradeRequest(final HttpTransactionContext ctx) {
954934
}
955935

956936
private void addHeaders(final Request request,
957-
final HttpRequestPacket requestPacket) {
937+
final HttpRequestPacket requestPacket,
938+
final ProxyServer proxy) throws IOException {
958939

959940
final FluentCaseInsensitiveStringsMap map = request.getHeaders();
960941
if (isNonEmpty(map)) {
@@ -971,7 +952,8 @@ private void addHeaders(final Request request,
971952

972953
final MimeHeaders headers = requestPacket.getHeaders();
973954
if (!headers.contains(Header.Connection)) {
974-
requestPacket.addHeader(Header.Connection, "keep-alive");
955+
//final boolean canCache = context.provider.clientConfig.getAllowPoolingConnection();
956+
requestPacket.addHeader(Header.Connection, /*(canCache ? */"keep-alive" /*: "close")*/);
975957
}
976958

977959
if (!headers.contains(Header.Accept)) {
@@ -982,6 +964,18 @@ private void addHeaders(final Request request,
982964
requestPacket.addHeader(Header.UserAgent, config.getUserAgent());
983965
}
984966

967+
boolean avoidProxy = ProxyUtils.avoidProxy(proxy, request);
968+
if (!avoidProxy) {
969+
if (!requestPacket.getHeaders().contains(Header.ProxyConnection)) {
970+
requestPacket.setHeader(Header.ProxyConnection, "keep-alive");
971+
}
972+
973+
if (proxy.getPrincipal() != null && proxy.isBasic()) {
974+
requestPacket.setHeader(Header.ProxyAuthorization, AuthenticatorUtils.computeBasicAuthentication(proxy));
975+
}
976+
977+
}
978+
985979

986980
}
987981

@@ -2422,7 +2416,7 @@ Connection obtainConnection(final Request request,
24222416
throws IOException, ExecutionException, InterruptedException, TimeoutException {
24232417

24242418
final Connection c = obtainConnection0(request, requestFuture, requestFuture.getProxyServer());
2425-
DO_NOT_CACHE.set(c, Boolean.TRUE);
2419+
markConnectionAsDoNotCache(c);
24262420
return c;
24272421
}
24282422

@@ -2539,7 +2533,7 @@ private static String getPoolKey(final Request request, ProxyServer proxyServer)
25392533

25402534
// ------------------------------------------------------ Nested Classes
25412535

2542-
private static class ConnectionMonitor implements Connection.CloseListener {
2536+
private static class ConnectionMonitor implements CloseListener<Closeable,CloseType> {
25432537

25442538
private final Semaphore connections;
25452539

@@ -2564,7 +2558,7 @@ public boolean acquire() {
25642558
}
25652559

25662560
@Override
2567-
public void onClosed(Connection connection, Connection.CloseType closeType) throws IOException {
2561+
public void onClosed(Closeable closeable, CloseType closeType) throws IOException {
25682562

25692563
if (connections != null) {
25702564
connections.release();
@@ -2939,6 +2933,7 @@ public boolean equals(Object o) {
29392933

29402934
if (ahcListener != null ? !ahcListener.equals(that.ahcListener) : that.ahcListener != null)
29412935
return false;
2936+
//noinspection RedundantIfStatement
29422937
if (webSocket != null ? !webSocket.equals(that.webSocket) : that.webSocket != null)
29432938
return false;
29442939

0 commit comments

Comments
 (0)