@@ -148,7 +148,7 @@ public class GrizzlyAsyncHttpProvider implements AsyncHttpProvider {
148
148
private final static Logger LOGGER = LoggerFactory .getLogger (GrizzlyAsyncHttpProvider .class );
149
149
private static final boolean SEND_FILE_SUPPORT ;
150
150
static {
151
- SEND_FILE_SUPPORT = configSendFileSupport ();
151
+ SEND_FILE_SUPPORT = /* configSendFileSupport();*/ false ;
152
152
}
153
153
private final Attribute <HttpTransactionContext > REQUEST_STATE_ATTR =
154
154
Grizzly .DEFAULT_ATTRIBUTE_BUILDER .createAttribute (HttpTransactionContext .class .getName ());
@@ -616,6 +616,7 @@ final class HttpTransactionContext {
616
616
HandShake handshake ;
617
617
ProtocolHandler protocolHandler ;
618
618
WebSocket webSocket ;
619
+ boolean establishingTunnel ;
619
620
620
621
621
622
// -------------------------------------------------------- Constructors
@@ -674,6 +675,15 @@ void result(Object result) {
674
675
}
675
676
}
676
677
678
+ boolean isTunnelEstablished (final Connection c ) {
679
+ return c .getAttributes ().getAttribute ("tunnel-established" ) != null ;
680
+ }
681
+
682
+
683
+ void tunnelEstablished (final Connection c ) {
684
+ c .getAttributes ().setAttribute ("tunnel-established" , Boolean .TRUE );
685
+ }
686
+
677
687
678
688
} // END HttpTransactionContext
679
689
@@ -841,7 +851,9 @@ private boolean sendAsGrizzlyRequest(final Request request,
841
851
final ProxyServer proxy = getProxyServer (request );
842
852
final boolean useProxy = (proxy != null );
843
853
if (useProxy ) {
844
- if (secure ) {
854
+ if ((secure || httpCtx .isWSRequest ) && !httpCtx .isTunnelEstablished (ctx .getConnection ())) {
855
+ secure = false ;
856
+ httpCtx .establishingTunnel = true ;
845
857
builder .method (Method .CONNECT );
846
858
builder .uri (AsyncHttpProviderUtils .getAuthority (uri ));
847
859
} else {
@@ -861,7 +873,7 @@ private boolean sendAsGrizzlyRequest(final Request request,
861
873
}
862
874
863
875
HttpRequestPacket requestPacket ;
864
- if (httpCtx .isWSRequest ) {
876
+ if (httpCtx .isWSRequest && ! httpCtx . establishingTunnel ) {
865
877
try {
866
878
final URI wsURI = new URI (httpCtx .wsRequestURI );
867
879
httpCtx .protocolHandler = Version .DRAFT17 .createHandler (true );
@@ -874,7 +886,10 @@ private boolean sendAsGrizzlyRequest(final Request request,
874
886
} else {
875
887
requestPacket = builder .build ();
876
888
}
877
- requestPacket .setSecure (true );
889
+ requestPacket .setSecure (secure );
890
+ if (secure ) {
891
+ ctx .notifyDownstream (new SwitchingSSLFilter .SSLSwitchingEvent (true , ctx .getConnection ()));
892
+ }
878
893
if (!useProxy && !httpCtx .isWSRequest ) {
879
894
addQueryString (request , requestPacket );
880
895
}
@@ -906,7 +921,6 @@ private boolean sendAsGrizzlyRequest(final Request request,
906
921
907
922
}
908
923
909
-
910
924
private boolean isUpgradeRequest (final AsyncHandler handler ) {
911
925
return (handler instanceof UpgradeHandler );
912
926
}
@@ -1136,14 +1150,19 @@ protected void onInitialLineParsed(HttpHeader httpHeader,
1136
1150
if (httpHeader .isSkipRemainder ()) {
1137
1151
return ;
1138
1152
}
1153
+ final Connection connection = ctx .getConnection ();
1139
1154
final HttpTransactionContext context =
1140
- provider .getHttpTransactionContext (ctx . getConnection () );
1155
+ provider .getHttpTransactionContext (connection );
1141
1156
final int status = ((HttpResponsePacket ) httpHeader ).getStatus ();
1157
+ if (context .establishingTunnel && HttpStatus .OK_200 .statusMatches (status )) {
1158
+ return ;
1159
+ }
1142
1160
if (HttpStatus .CONINTUE_100 .statusMatches (status )) {
1143
1161
ctx .notifyUpstream (new ContinueEvent (context ));
1144
1162
return ;
1145
1163
}
1146
1164
1165
+
1147
1166
if (context .statusHandler != null && !context .statusHandler .handlesStatus (status )) {
1148
1167
context .statusHandler = null ;
1149
1168
context .invocationStatus = StatusHandler .InvocationStatus .CONTINUE ;
@@ -1224,11 +1243,12 @@ protected void onHttpHeadersParsed(HttpHeader httpHeader,
1224
1243
ConnectionManager .markConnectionAsDoNotCache (ctx .getConnection ());
1225
1244
}
1226
1245
}
1227
- if (httpHeader .isSkipRemainder ()) {
1246
+ final HttpTransactionContext context =
1247
+ provider .getHttpTransactionContext (ctx .getConnection ());
1248
+ if (httpHeader .isSkipRemainder () || context .establishingTunnel ) {
1228
1249
return ;
1229
1250
}
1230
- final HttpTransactionContext context =
1231
- provider .getHttpTransactionContext (ctx .getConnection ());
1251
+
1232
1252
final AsyncHandler handler = context .handler ;
1233
1253
final List <ResponseFilter > filters = context .provider .clientConfig .getResponseFilters ();
1234
1254
final GrizzlyResponseHeaders responseHeaders = new GrizzlyResponseHeaders ((HttpResponsePacket ) httpHeader ,
@@ -1328,6 +1348,7 @@ protected void onHttpHeadersParsed(HttpHeader httpHeader,
1328
1348
1329
1349
}
1330
1350
1351
+ @ SuppressWarnings ("unchecked" )
1331
1352
@ Override
1332
1353
protected boolean onHttpPacketParsed (HttpHeader httpHeader , FilterChainContext ctx ) {
1333
1354
@@ -1340,20 +1361,38 @@ protected boolean onHttpPacketParsed(HttpHeader httpHeader, FilterChainContext c
1340
1361
1341
1362
result = super .onHttpPacketParsed (httpHeader , ctx );
1342
1363
1343
- final HttpTransactionContext context = cleanup (ctx , provider );
1344
-
1345
- final AsyncHandler handler = context .handler ;
1346
- if (handler != null ) {
1364
+ final HttpTransactionContext context = provider .getHttpTransactionContext (ctx .getConnection ());
1365
+ if (context .establishingTunnel
1366
+ && HttpStatus .OK_200 .statusMatches (
1367
+ ((HttpResponsePacket ) httpHeader ).getStatus ())) {
1368
+ context .establishingTunnel = false ;
1369
+ final Connection c = ctx .getConnection ();
1370
+ context .tunnelEstablished (c );
1347
1371
try {
1348
- context .result (handler .onCompleted ());
1349
- } catch (Exception e ) {
1372
+ context .provider .execute (c ,
1373
+ context .request ,
1374
+ context .handler ,
1375
+ context .future );
1376
+ return result ;
1377
+ } catch (IOException e ) {
1350
1378
context .abort (e );
1379
+ return result ;
1351
1380
}
1352
1381
} else {
1353
- context .done (null );
1354
- }
1382
+ cleanup (ctx , provider );
1383
+ final AsyncHandler handler = context .handler ;
1384
+ if (handler != null ) {
1385
+ try {
1386
+ context .result (handler .onCompleted ());
1387
+ } catch (Exception e ) {
1388
+ context .abort (e );
1389
+ }
1390
+ } else {
1391
+ context .done (null );
1392
+ }
1355
1393
1356
- return result ;
1394
+ return result ;
1395
+ }
1357
1396
}
1358
1397
1359
1398
@@ -1382,7 +1421,7 @@ private static HttpTransactionContext cleanup(final FilterChainContext ctx,
1382
1421
context .abort (new IOException ("Maximum pooled connections exceeded" ));
1383
1422
} else {
1384
1423
if (!context .provider .connectionManager .returnConnection (context .requestUrl , c )) {
1385
- ctx .getConnection ().close (). markForRecycle ( true ) ;
1424
+ ctx .getConnection ().close ();
1386
1425
}
1387
1426
}
1388
1427
0 commit comments