Skip to content

Commit e1232e0

Browse files
committed
Merge ahc-1.7.x to master.
* ahc-1.7.x: Fix javadoc warnings. Integrate Grizzly 2.2.10. Fix for the Grizzly side of #101 : [websocket] onError must not be called when status code is != 101. Fix for #111 - [websocket] Both Netty and Grizzly issues with URI without port. Incremental fix for #101 [websocket] onError must not be called when status code is != 101. Fix Netty side Fix for #111 https://github.com/sonatype/async-http-client/issues/111 Fix errror with clirr Port #102 tests Port fix for #102 [websocket] Support redirect Bump to the latest Netty release
2 parents 743e746 + 477bb3a commit e1232e0

File tree

10 files changed

+63
-33
lines changed

10 files changed

+63
-33
lines changed

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,9 @@
448448
<exclude>**/Request$EntityWriter</exclude>
449449
<exclude>**/RequestBuilderBase</exclude>
450450
<exclude>**/Response</exclude>
451-
<exclude>**/Response$</exclude>
451+
<exclude>**/Response$*</exclude>
452452
<exclude>**/FilterContext</exclude>
453+
<exclude>**/FilterContext$*</exclude>
453454
<exclude>**/NettyResponseFuture</exclude>
454455
<exclude>**/**ResponseBodyPart</exclude>
455456
<exclude>**/**WebSocket</exclude>

src/main/java/com/ning/http/client/AsyncHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static enum STATE {
5555
*/
5656
CONTINUE,
5757
/**
58-
* Upgrade the protocol. When specified, the AsyncHttpProvider will try to invoke the {@link UpgradeHandler#onReady}
58+
* Upgrade the protocol.
5959
*/
6060
UPGRADE
6161
}

src/main/java/com/ning/http/client/UpgradeHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
package com.ning.http.client;
1414

1515
/**
16-
* Invoked when an {@link AsyncHandler.STATE#UPGRADE} is returned. Currently the library only support {@link WebSocket}
16+
* Invoked when an {@link AsyncHandler.STATE#UPGRADE} is returned. Currently the library only support {@link com.ning.http.client.websocket.WebSocket}
1717
* as type.
1818
*
1919
* @param <T>

src/main/java/com/ning/http/client/filter/FilterContext.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public HttpResponseStatus getResponseStatus() {
7373

7474
/**
7575
* Return the response {@link HttpResponseHeaders}
76-
* @return
7776
*/
7877
public HttpResponseHeaders getResponseHeaders() {
7978
return b.headers;

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

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,9 @@ static int getPort(final URI uri, final int p) {
521521
int port = p;
522522
if (port == -1) {
523523
final String protocol = uri.getScheme().toLowerCase();
524-
if ("http".equals(protocol)) {
524+
if ("http".equals(protocol) || "ws".equals(protocol)) {
525525
port = 80;
526-
} else if ("https".equals(protocol)) {
526+
} else if ("https".equals(protocol) || "wss".equals(protocol)) {
527527
port = 443;
528528
} else {
529529
throw new IllegalArgumentException("Unknown protocol: " + protocol);
@@ -1283,39 +1283,46 @@ protected void onHttpHeadersParsed(HttpHeader httpHeader,
12831283
return;
12841284
}
12851285
}
1286-
1287-
if (context.currentState != AsyncHandler.STATE.ABORT) {
1288-
boolean upgrade = context.currentState == AsyncHandler.STATE.UPGRADE;
1286+
if (context.isWSRequest) {
12891287
try {
1290-
context.currentState = handler.onHeadersReceived(
1291-
responseHeaders);
1292-
} catch (Exception e) {
1293-
httpHeader.setSkipRemainder(true);
1294-
context.abort(e);
1295-
return;
1296-
}
1297-
if (upgrade) {
1298-
try {
1288+
context.protocolHandler.setConnection(ctx.getConnection());
1289+
DefaultWebSocket ws = new DefaultWebSocket(context.protocolHandler);
1290+
context.webSocket = new GrizzlyWebSocketAdapter(ws);
1291+
if (context.currentState == AsyncHandler.STATE.UPGRADE) {
12991292
httpHeader.setChunked(false);
1300-
context.protocolHandler.setConnection(ctx.getConnection());
1301-
DefaultWebSocket ws = new DefaultWebSocket(context.protocolHandler);
13021293
ws.onConnect();
1303-
context.webSocket = new GrizzlyWebSocketAdapter(ws);
13041294
WebSocketEngine.getEngine().setWebSocketHolder(ctx.getConnection(),
13051295
context.protocolHandler,
13061296
ws);
13071297
((WebSocketUpgradeHandler) context.handler).onSuccess(context.webSocket);
13081298
final int wsTimeout = context.provider.clientConfig.getWebSocketIdleTimeoutInMs();
13091299
IdleTimeoutFilter.setCustomTimeout(ctx.getConnection(),
1310-
((wsTimeout <= 0)
1311-
? IdleTimeoutFilter.FOREVER
1312-
: wsTimeout),
1313-
TimeUnit.MILLISECONDS);
1300+
((wsTimeout <= 0)
1301+
? IdleTimeoutFilter.FOREVER
1302+
: wsTimeout),
1303+
TimeUnit.MILLISECONDS);
13141304
context.result(handler.onCompleted());
1305+
} else {
1306+
httpHeader.setSkipRemainder(true);
1307+
((WebSocketUpgradeHandler) context.handler).
1308+
onClose(context.webSocket,
1309+
1002,
1310+
"WebSocket protocol error: unexpected HTTP response status during handshake.");
1311+
context.result(null);
1312+
}
1313+
} catch (Exception e) {
1314+
httpHeader.setSkipRemainder(true);
1315+
context.abort(e);
1316+
}
1317+
} else {
1318+
if (context.currentState != AsyncHandler.STATE.ABORT) {
1319+
try {
1320+
context.currentState = handler.onHeadersReceived(
1321+
responseHeaders);
13151322
} catch (Exception e) {
13161323
httpHeader.setSkipRemainder(true);
13171324
context.abort(e);
1318-
}
1325+
}
13191326
}
13201327
}
13211328

src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProvider.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ private static HttpRequest construct(AsyncHttpClientConfig config,
549549
ChannelBuffer buffer) throws IOException {
550550

551551
String host = AsyncHttpProviderUtils.getHost(uri);
552+
boolean webSocket = isWebSocket(uri);
552553

553554
if (request.getVirtualHost() != null) {
554555
host = request.getVirtualHost();
@@ -569,11 +570,12 @@ private static HttpRequest construct(AsyncHttpClientConfig config,
569570
}
570571
nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, m, path.toString());
571572
}
572-
boolean webSocket = isWebSocket(uri);
573+
573574
if (webSocket) {
574575
nettyRequest.addHeader(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET);
575576
nettyRequest.addHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE);
576-
nettyRequest.addHeader("Origin", "http://" + uri.getHost() + ":" + uri.getPort());
577+
nettyRequest.addHeader("Origin", "http://" + uri.getHost() + ":"
578+
+ (uri.getPort() == -1 ? isSecure(uri.getScheme()) ? 443 : 80 : uri.getPort()));
577579
nettyRequest.addHeader(WEBSOCKET_KEY, WebSocketUtil.getKey());
578580
nettyRequest.addHeader("Sec-WebSocket-Version", "13");
579581
}
@@ -2339,7 +2341,13 @@ public void handle(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
23392341
s = new ResponseStatus(future.getURI(), response, NettyAsyncHttpProvider.this);
23402342
final boolean statusReceived = h.onStatusReceived(s) == STATE.UPGRADE;
23412343

2342-
if (!validStatus || !validUpgrade || !validConnection || !statusReceived) {
2344+
if (!statusReceived) {
2345+
h.onClose(new NettyWebSocket(ctx.getChannel()), 1002, "Bad response status " + response.getStatus().getCode());
2346+
future.done(null);
2347+
return;
2348+
}
2349+
2350+
if (!validStatus || !validUpgrade || !validConnection) {
23432351
throw new IOException("Invalid handshake response");
23442352
}
23452353

src/main/java/com/ning/http/client/websocket/WebSocket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public interface WebSocket {
4141
* @param offset starting offset.
4242
* @param len length.
4343
* @param last flag indicating whether or not this is the last fragment.
44-
* @return
44+
* @return this.
4545
*/
4646
WebSocket stream(byte[] fragment, int offset, int len, boolean last);
4747

src/main/java/com/ning/http/client/websocket/WebSocketCloseCodeReasonListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
/**
1616
* Extend the normal close listener with one that support the WebSocket's code and reason.
17-
* @See http://tools.ietf.org/html/rfc6455#section-5.5.1
17+
* @see "http://tools.ietf.org/html/rfc6455#section-5.5.1"
1818
*/
1919
public interface WebSocketCloseCodeReasonListener {
2020

src/main/java/com/ning/http/client/websocket/WebSocketUpgradeHandler.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public final STATE onStatusReceived(HttpResponseStatus responseStatus) throws Ex
6767
if (responseStatus.getStatusCode() == 101) {
6868
return STATE.UPGRADE;
6969
} else {
70-
throw new IllegalStateException("Invalid upgrade protocol, status should be 101 but was " + responseStatus.getStatusCode());
70+
return STATE.ABORT;
7171
}
7272
}
7373

@@ -116,6 +116,21 @@ public final void onFailure(Throwable t) {
116116
}
117117
}
118118

119+
public final void onClose(WebSocket webSocket, int status, String reasonPhrase) {
120+
// Connect failure
121+
if (this.webSocket == null) this.webSocket = webSocket;
122+
123+
for (WebSocketListener w : l) {
124+
if (webSocket != null) {
125+
webSocket.addWebSocketListener(w);
126+
}
127+
w.onClose(webSocket);
128+
if (WebSocketCloseCodeReasonListener.class.isAssignableFrom(w.getClass())) {
129+
WebSocketCloseCodeReasonListener.class.cast(w).onClose(webSocket, status, reasonPhrase);
130+
}
131+
}
132+
}
133+
119134
/**
120135
* Build a {@link WebSocketUpgradeHandler}
121136
*/

src/main/java/com/ning/http/util/AsyncHttpProviderUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public final static URI getRedirectUri(URI uri, String location) {
291291
public final static int getPort(URI uri) {
292292
int port = uri.getPort();
293293
if (port == -1)
294-
port = uri.getScheme().equals("http") ? 80 : 443;
294+
port = uri.getScheme().equals("http") || uri.getScheme().equals("ws") ? 80 : 443;
295295
return port;
296296
}
297297

0 commit comments

Comments
 (0)