1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.example.http2.helloworld.server;
17
18 import io.netty5.buffer.api.Buffer;
19 import io.netty5.channel.ChannelFutureListeners;
20 import io.netty5.channel.ChannelHandlerContext;
21 import io.netty5.channel.SimpleChannelInboundHandler;
22 import io.netty5.handler.codec.http.DefaultFullHttpResponse;
23 import io.netty5.handler.codec.http.FullHttpRequest;
24 import io.netty5.handler.codec.http.FullHttpResponse;
25 import io.netty5.handler.codec.http.HttpUtil;
26
27 import static io.netty5.example.http2.helloworld.server.HelloWorldHttp2Handler.RESPONSE_BYTES;
28 import static io.netty5.handler.codec.http.HttpHeaderNames.CONNECTION;
29 import static io.netty5.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
30 import static io.netty5.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
31 import static io.netty5.handler.codec.http.HttpHeaderValues.CLOSE;
32 import static io.netty5.handler.codec.http.HttpHeaderValues.KEEP_ALIVE;
33 import static io.netty5.handler.codec.http.HttpResponseStatus.CONTINUE;
34 import static io.netty5.handler.codec.http.HttpResponseStatus.OK;
35 import static io.netty5.handler.codec.http.HttpVersion.HTTP_1_0;
36 import static io.netty5.handler.codec.http.HttpVersion.HTTP_1_1;
37 import static java.nio.charset.StandardCharsets.US_ASCII;
38 import static java.util.Objects.requireNonNull;
39
40
41
42
43 public class HelloWorldHttp1Handler extends SimpleChannelInboundHandler<FullHttpRequest> {
44 private final String establishApproach;
45
46 public HelloWorldHttp1Handler(String establishApproach) {
47 this.establishApproach = requireNonNull(establishApproach, "establishApproach");
48 }
49
50 @Override
51 public void messageReceived(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
52 if (HttpUtil.is100ContinueExpected(req)) {
53 ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE, ctx.bufferAllocator().allocate(0)));
54 }
55
56 final byte[] sourceBytes = (" - via " + req.protocolVersion() + " (" + establishApproach + ")")
57 .getBytes(US_ASCII);
58 final Buffer content = ctx.bufferAllocator().allocate(RESPONSE_BYTES.length + sourceBytes.length)
59 .writeBytes(RESPONSE_BYTES).writeBytes(sourceBytes);
60
61 FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
62 response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
63 response.headers().setInt(CONTENT_LENGTH, response.payload().readableBytes());
64
65 boolean keepAlive = HttpUtil.isKeepAlive(req);
66 if (keepAlive) {
67 if (req.protocolVersion().equals(HTTP_1_0)) {
68 response.headers().set(CONNECTION, KEEP_ALIVE);
69 }
70 ctx.write(response);
71 } else {
72
73 response.headers().set(CONNECTION, CLOSE);
74 ctx.write(response).addListener(ctx, ChannelFutureListeners.CLOSE);
75 }
76 }
77
78 @Override
79 public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
80 ctx.flush();
81 }
82
83 @Override
84 public void channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
85 cause.printStackTrace();
86 ctx.close();
87 }
88 }