1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty5.example.http2.tiles;
18
19 import io.netty5.channel.ChannelFutureListeners;
20 import io.netty5.channel.ChannelHandlerContext;
21 import io.netty5.handler.codec.http.DefaultFullHttpResponse;
22 import io.netty5.handler.codec.http.FullHttpRequest;
23 import io.netty5.handler.codec.http.FullHttpResponse;
24 import io.netty5.handler.codec.http.HttpUtil;
25
26 import java.util.concurrent.TimeUnit;
27
28 import static io.netty5.handler.codec.http.HttpHeaderNames.CONNECTION;
29 import static io.netty5.handler.codec.http.HttpHeaderValues.CLOSE;
30 import static io.netty5.handler.codec.http.HttpHeaderValues.KEEP_ALIVE;
31 import static io.netty5.handler.codec.http.HttpResponseStatus.CONTINUE;
32 import static io.netty5.handler.codec.http.HttpUtil.isKeepAlive;
33 import static io.netty5.handler.codec.http.HttpVersion.HTTP_1_0;
34 import static io.netty5.handler.codec.http.HttpVersion.HTTP_1_1;
35
36
37
38
39 public final class Http1RequestHandler extends Http2RequestHandler {
40
41 @Override
42 protected void messageReceived(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
43 if (HttpUtil.is100ContinueExpected(request)) {
44 ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE, ctx.bufferAllocator().allocate(0)));
45 }
46 super.messageReceived(ctx, request);
47 }
48
49 @Override
50 protected void sendResponse(final ChannelHandlerContext ctx, String streamId, int latency,
51 final FullHttpResponse response, final FullHttpRequest request) {
52 HttpUtil.setContentLength(response, response.payload().readableBytes());
53 ctx.executor().schedule(() -> {
54 if (isKeepAlive(request)) {
55 if (request.protocolVersion().equals(HTTP_1_0)) {
56 response.headers().set(CONNECTION, KEEP_ALIVE);
57 }
58 ctx.writeAndFlush(response);
59 } else {
60
61 response.headers().set(CONNECTION, CLOSE);
62 ctx.writeAndFlush(response).addListener(ctx, ChannelFutureListeners.CLOSE);
63 }
64 }, latency, TimeUnit.MILLISECONDS);
65 }
66 }