Skip to content

Commit cd9f3c5

Browse files
committed
Fix connection reuse strategy on HTTP/1.0, close AsyncHttpClient#911
1 parent 834c345 commit cd9f3c5

File tree

6 files changed

+123
-83
lines changed

6 files changed

+123
-83
lines changed

providers/netty3/src/main/java/org/asynchttpclient/netty/NettyAsyncHttpProviderConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.asynchttpclient.AsyncHttpProviderConfig;
2323
import org.asynchttpclient.channel.pool.ConnectionStrategy;
2424
import org.asynchttpclient.netty.channel.pool.ChannelPool;
25-
import org.asynchttpclient.netty.handler.Http1Point1ConnectionStrategy;
25+
import org.asynchttpclient.netty.handler.DefaultConnectionStrategy;
2626
import org.asynchttpclient.netty.ws.NettyWebSocket;
2727
import org.jboss.netty.channel.Channel;
2828
import org.jboss.netty.channel.ChannelPipeline;
@@ -121,7 +121,7 @@ public Set<Map.Entry<String, Object>> propertiesSet() {
121121

122122
private NettyWebSocketFactory nettyWebSocketFactory = new DefaultNettyWebSocketFactory();
123123

124-
private ConnectionStrategy<HttpRequest, HttpResponse> connectionStrategy = new Http1Point1ConnectionStrategy();
124+
private ConnectionStrategy<HttpRequest, HttpResponse> connectionStrategy = new DefaultConnectionStrategy();
125125

126126
public boolean isUseDeadLockChecker() {
127127
return useDeadLockChecker;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
package org.asynchttpclient.netty.handler;
15+
16+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;
17+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Values.CLOSE;
18+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Values.KEEP_ALIVE;
19+
20+
import org.asynchttpclient.channel.pool.ConnectionStrategy;
21+
import org.jboss.netty.handler.codec.http.HttpMessage;
22+
import org.jboss.netty.handler.codec.http.HttpRequest;
23+
import org.jboss.netty.handler.codec.http.HttpResponse;
24+
import org.jboss.netty.handler.codec.http.HttpVersion;
25+
26+
/**
27+
* Connection strategy implementing standard HTTP 1.0/1.1 behaviour.
28+
*/
29+
public class DefaultConnectionStrategy implements ConnectionStrategy<HttpRequest, HttpResponse> {
30+
31+
/**
32+
* Implemented in accordance with RFC 7230 section 6.1
33+
* https://tools.ietf.org/html/rfc7230#section-6.1
34+
*/
35+
@Override
36+
public boolean keepAlive(HttpRequest request, HttpResponse response) {
37+
38+
String responseConnectionHeader = connectionHeader(response);
39+
40+
41+
if (CLOSE.equalsIgnoreCase(responseConnectionHeader)) {
42+
return false;
43+
} else {
44+
String requestConnectionHeader = connectionHeader(request);
45+
46+
if (request.getProtocolVersion() == HttpVersion.HTTP_1_0) {
47+
// only use keep-alive if both parties agreed upon it
48+
return KEEP_ALIVE.equalsIgnoreCase(requestConnectionHeader) && KEEP_ALIVE.equalsIgnoreCase(responseConnectionHeader);
49+
50+
} else {
51+
// 1.1+, keep-alive is default behavior
52+
return !CLOSE.equalsIgnoreCase(requestConnectionHeader);
53+
}
54+
}
55+
}
56+
57+
private String connectionHeader(HttpMessage message) {
58+
return message.headers().get(CONNECTION);
59+
}
60+
}

providers/netty3/src/main/java/org/asynchttpclient/netty/handler/Http1Point1ConnectionStrategy.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

providers/netty4/src/main/java/org/asynchttpclient/netty/NettyAsyncHttpProviderConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.asynchttpclient.AsyncHttpProviderConfig;
3131
import org.asynchttpclient.channel.pool.ConnectionStrategy;
3232
import org.asynchttpclient.netty.channel.pool.ChannelPool;
33-
import org.asynchttpclient.netty.handler.Http1Point1ConnectionStrategy;
33+
import org.asynchttpclient.netty.handler.DefaultConnectionStrategy;
3434
import org.asynchttpclient.netty.ws.NettyWebSocket;
3535

3636
/**
@@ -147,7 +147,7 @@ public NettyWebSocket newNettyWebSocket(Channel channel, AsyncHttpClientConfig c
147147

148148
private NettyWebSocketFactory nettyWebSocketFactory = new DefaultNettyWebSocketFactory();
149149

150-
private ConnectionStrategy<HttpRequest, HttpResponse> connectionStrategy = new Http1Point1ConnectionStrategy();
150+
private ConnectionStrategy<HttpRequest, HttpResponse> connectionStrategy = new DefaultConnectionStrategy();
151151

152152
public EventLoopGroup getEventLoopGroup() {
153153
return eventLoopGroup;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
package org.asynchttpclient.netty.handler;
15+
16+
import static io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;
17+
import static io.netty.handler.codec.http.HttpHeaders.Values.CLOSE;
18+
import static io.netty.handler.codec.http.HttpHeaders.Values.KEEP_ALIVE;
19+
import io.netty.handler.codec.http.HttpMessage;
20+
import io.netty.handler.codec.http.HttpRequest;
21+
import io.netty.handler.codec.http.HttpResponse;
22+
import io.netty.handler.codec.http.HttpVersion;
23+
24+
import org.asynchttpclient.channel.pool.ConnectionStrategy;
25+
26+
/**
27+
* Connection strategy implementing standard HTTP 1.0/1.1 behaviour.
28+
*/
29+
public class DefaultConnectionStrategy implements ConnectionStrategy<HttpRequest, HttpResponse> {
30+
31+
/**
32+
* Implemented in accordance with RFC 7230 section 6.1
33+
* https://tools.ietf.org/html/rfc7230#section-6.1
34+
*/
35+
@Override
36+
public boolean keepAlive(HttpRequest request, HttpResponse response) {
37+
38+
String responseConnectionHeader = connectionHeader(response);
39+
40+
if (CLOSE.equalsIgnoreCase(responseConnectionHeader)) {
41+
return false;
42+
} else {
43+
String requestConnectionHeader = connectionHeader(request);
44+
45+
if (request.getProtocolVersion() == HttpVersion.HTTP_1_0) {
46+
// only use keep-alive if both parties agreed upon it
47+
return KEEP_ALIVE.equalsIgnoreCase(requestConnectionHeader) && KEEP_ALIVE.equalsIgnoreCase(responseConnectionHeader);
48+
49+
} else {
50+
// 1.1+, keep-alive is default behavior
51+
return !CLOSE.equalsIgnoreCase(requestConnectionHeader);
52+
}
53+
}
54+
}
55+
56+
private String connectionHeader(HttpMessage message) {
57+
return message.headers().get(CONNECTION);
58+
}
59+
}

providers/netty4/src/main/java/org/asynchttpclient/netty/handler/Http1Point1ConnectionStrategy.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)