Skip to content

Commit 2c61a18

Browse files
committed
netty: fix keepAlive support
- fix #3782
1 parent fe6fc88 commit 2c61a18

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ public NettyContext(
169169
this.streamId = req.headers().get(STREAM_ID);
170170
ifStreamId(this.streamId);
171171
}
172+
if (!HttpUtil.isKeepAlive(req)) {
173+
setHeaders.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
174+
getOrCreateResponsePromise().addListener(ChannelFutureListener.CLOSE);
175+
}
172176
}
173177

174178
@NonNull @Override
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.i3782;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertNull;
10+
11+
import java.io.IOException;
12+
13+
import io.jooby.Context;
14+
import io.jooby.StatusCode;
15+
import io.jooby.junit.ServerTest;
16+
import io.jooby.junit.ServerTestRunner;
17+
18+
public class Issue3782 {
19+
20+
@ServerTest
21+
public void shouldFollowKeepAliveHeader(ServerTestRunner runner) throws IOException {
22+
runner
23+
.define(
24+
app -> {
25+
app.get("/3782/keep-alive", Context::getRequestPath);
26+
app.get("/3782/keep-alive-empty", ctx -> ctx.send(StatusCode.OK));
27+
})
28+
.ready(
29+
http -> {
30+
// Keep alive by default
31+
http.get(
32+
"/3782/keep-alive",
33+
rsp -> {
34+
assertEquals("/3782/keep-alive", rsp.body().string());
35+
assertNull(rsp.header("Connection"));
36+
});
37+
http.get(
38+
"/3782/keep-alive-empty",
39+
rsp -> {
40+
assertEquals("", rsp.body().string());
41+
assertNull(rsp.header("Connection"));
42+
});
43+
44+
// Keep alive by explicit
45+
http.header("connection", "keep-alive");
46+
http.get(
47+
"/3782/keep-alive",
48+
rsp -> {
49+
assertEquals("/3782/keep-alive", rsp.body().string());
50+
assertNull(rsp.header("Connection"));
51+
});
52+
http.header("connection", "keep-alive");
53+
http.get(
54+
"/3782/keep-alive-empty",
55+
rsp -> {
56+
assertEquals("", rsp.body().string());
57+
assertNull(rsp.header("Connection"));
58+
});
59+
60+
// Close
61+
http.header("connection", "close");
62+
http.get(
63+
"/3782/keep-alive",
64+
rsp -> {
65+
assertEquals("/3782/keep-alive", rsp.body().string());
66+
assertEquals("close", rsp.header("Connection").toLowerCase());
67+
});
68+
http.header("connection", "close");
69+
http.get(
70+
"/3782/keep-alive-empty",
71+
rsp -> {
72+
assertEquals("", rsp.body().string());
73+
assertEquals("close", rsp.header("Connection").toLowerCase());
74+
});
75+
});
76+
}
77+
}

0 commit comments

Comments
 (0)