54
54
import com .ning .http .util .SslUtils ;
55
55
56
56
import org .glassfish .grizzly .Buffer ;
57
+ import org .glassfish .grizzly .CloseListener ;
58
+ import org .glassfish .grizzly .CloseType ;
59
+ import org .glassfish .grizzly .Closeable ;
57
60
import org .glassfish .grizzly .CompletionHandler ;
58
61
import org .glassfish .grizzly .Connection ;
59
62
import org .glassfish .grizzly .EmptyCompletionHandler ;
@@ -219,7 +222,7 @@ public void completed(final Connection c) {
219
222
try {
220
223
execute (c , request , handler , future );
221
224
} catch (Exception e ) {
222
- if (e instanceof RuntimeException || e instanceof IOException ) {
225
+ if (e instanceof IOException ) {
223
226
failed (e );
224
227
}
225
228
if (LOGGER .isWarnEnabled ()) {
@@ -299,9 +302,7 @@ protected <T> ListenableFuture<T> execute(final Connection c,
299
302
}
300
303
c .write (request , createWriteCompletionHandler (future ));
301
304
} catch (Exception e ) {
302
- if (e instanceof RuntimeException ) {
303
- throw (RuntimeException ) e ;
304
- } else if (e instanceof IOException ) {
305
+ if (e instanceof IOException ) {
305
306
throw (IOException ) e ;
306
307
}
307
308
if (LOGGER .isWarnEnabled ()) {
@@ -740,7 +741,6 @@ public void failed(Throwable throwable) {
740
741
if (throwable instanceof EOFException ) {
741
742
context .abort (new IOException ("Remotely Closed" ));
742
743
}
743
- context .abort (throwable );
744
744
}
745
745
746
746
@ Override
@@ -808,23 +808,6 @@ public NextAction handleEvent(final FilterChainContext ctx,
808
808
809
809
}
810
810
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
- // }
828
811
829
812
// ----------------------------------------------------- Private Methods
830
813
@@ -841,19 +824,10 @@ private boolean sendAsGrizzlyRequest(final Request request,
841
824
final URI uri = httpCtx .request .getURI ();
842
825
final HttpRequestPacket .Builder builder = HttpRequestPacket .builder ();
843
826
final String scheme = uri .getScheme ();
844
- boolean secure = "https" . equals ( scheme ) || "wss" . equals (scheme );
827
+ boolean secure = isSecure (scheme );
845
828
builder .method (request .getMethod ());
846
829
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 );
857
831
final ProxyServer proxy = ProxyUtils .getProxyServer (config , request );
858
832
final boolean useProxy = proxy != null ;
859
833
if (useProxy ) {
@@ -902,22 +876,9 @@ private boolean sendAsGrizzlyRequest(final Request request,
902
876
if (!useProxy && !httpCtx .isWSRequest ) {
903
877
addQueryString (request , requestPacket );
904
878
}
905
- addHeaders (request , requestPacket );
879
+ addHeaders (request , requestPacket , proxy );
906
880
addCookies (request , requestPacket );
907
881
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
- }
921
882
final AsyncHandler h = httpCtx .handler ;
922
883
if (h != null ) {
923
884
if (TransferCompletionHandler .class .isAssignableFrom (h .getClass ())) {
@@ -930,6 +891,25 @@ private boolean sendAsGrizzlyRequest(final Request request,
930
891
931
892
}
932
893
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
+
933
913
private boolean isUpgradeRequest (final AsyncHandler handler ) {
934
914
return (handler instanceof UpgradeHandler );
935
915
}
@@ -954,7 +934,8 @@ private void convertToUpgradeRequest(final HttpTransactionContext ctx) {
954
934
}
955
935
956
936
private void addHeaders (final Request request ,
957
- final HttpRequestPacket requestPacket ) {
937
+ final HttpRequestPacket requestPacket ,
938
+ final ProxyServer proxy ) throws IOException {
958
939
959
940
final FluentCaseInsensitiveStringsMap map = request .getHeaders ();
960
941
if (isNonEmpty (map )) {
@@ -971,7 +952,8 @@ private void addHeaders(final Request request,
971
952
972
953
final MimeHeaders headers = requestPacket .getHeaders ();
973
954
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")*/ );
975
957
}
976
958
977
959
if (!headers .contains (Header .Accept )) {
@@ -982,6 +964,18 @@ private void addHeaders(final Request request,
982
964
requestPacket .addHeader (Header .UserAgent , config .getUserAgent ());
983
965
}
984
966
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
+
985
979
986
980
}
987
981
@@ -2422,7 +2416,7 @@ Connection obtainConnection(final Request request,
2422
2416
throws IOException , ExecutionException , InterruptedException , TimeoutException {
2423
2417
2424
2418
final Connection c = obtainConnection0 (request , requestFuture , requestFuture .getProxyServer ());
2425
- DO_NOT_CACHE . set ( c , Boolean . TRUE );
2419
+ markConnectionAsDoNotCache ( c );
2426
2420
return c ;
2427
2421
}
2428
2422
@@ -2539,7 +2533,7 @@ private static String getPoolKey(final Request request, ProxyServer proxyServer)
2539
2533
2540
2534
// ------------------------------------------------------ Nested Classes
2541
2535
2542
- private static class ConnectionMonitor implements Connection . CloseListener {
2536
+ private static class ConnectionMonitor implements CloseListener < Closeable , CloseType > {
2543
2537
2544
2538
private final Semaphore connections ;
2545
2539
@@ -2564,7 +2558,7 @@ public boolean acquire() {
2564
2558
}
2565
2559
2566
2560
@ Override
2567
- public void onClosed (Connection connection , Connection . CloseType closeType ) throws IOException {
2561
+ public void onClosed (Closeable closeable , CloseType closeType ) throws IOException {
2568
2562
2569
2563
if (connections != null ) {
2570
2564
connections .release ();
@@ -2939,6 +2933,7 @@ public boolean equals(Object o) {
2939
2933
2940
2934
if (ahcListener != null ? !ahcListener .equals (that .ahcListener ) : that .ahcListener != null )
2941
2935
return false ;
2936
+ //noinspection RedundantIfStatement
2942
2937
if (webSocket != null ? !webSocket .equals (that .webSocket ) : that .webSocket != null )
2943
2938
return false ;
2944
2939
0 commit comments