1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.example.http2.helloworld.frame.server;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.ByteBufUtil;
20 import io.netty.channel.ChannelDuplexHandler;
21 import io.netty.channel.ChannelHandler.Sharable;
22 import io.netty.channel.ChannelHandlerContext;
23 import io.netty.handler.codec.http2.DefaultHttp2DataFrame;
24 import io.netty.handler.codec.http2.DefaultHttp2Headers;
25 import io.netty.handler.codec.http2.DefaultHttp2HeadersFrame;
26 import io.netty.handler.codec.http2.DefaultHttp2WindowUpdateFrame;
27 import io.netty.handler.codec.http2.Http2DataFrame;
28 import io.netty.handler.codec.http2.Http2FrameStream;
29 import io.netty.handler.codec.http2.Http2Headers;
30 import io.netty.handler.codec.http2.Http2HeadersFrame;
31 import io.netty.util.CharsetUtil;
32
33 import static io.netty.buffer.Unpooled.copiedBuffer;
34 import static io.netty.buffer.Unpooled.unreleasableBuffer;
35 import static io.netty.handler.codec.http.HttpResponseStatus.OK;
36
37
38
39
40
41
42 @Sharable
43 public class HelloWorldHttp2Handler extends ChannelDuplexHandler {
44
45 static final ByteBuf RESPONSE_BYTES = unreleasableBuffer(
46 copiedBuffer("Hello World", CharsetUtil.UTF_8)).asReadOnly();
47
48 @Override
49 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
50 super.exceptionCaught(ctx, cause);
51 cause.printStackTrace();
52 ctx.close();
53 }
54
55 @Override
56 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
57 if (msg instanceof Http2HeadersFrame) {
58 onHeadersRead(ctx, (Http2HeadersFrame) msg);
59 } else if (msg instanceof Http2DataFrame) {
60 onDataRead(ctx, (Http2DataFrame) msg);
61 } else {
62 super.channelRead(ctx, msg);
63 }
64 }
65
66 @Override
67 public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
68 ctx.flush();
69 }
70
71
72
73
74 private static void onDataRead(ChannelHandlerContext ctx, Http2DataFrame data) throws Exception {
75 Http2FrameStream stream = data.stream();
76
77 if (data.isEndStream()) {
78 sendResponse(ctx, stream, data.content());
79 } else {
80
81 data.release();
82 }
83
84
85 ctx.write(new DefaultHttp2WindowUpdateFrame(data.initialFlowControlledBytes()).stream(stream));
86 }
87
88
89
90
91 private static void onHeadersRead(ChannelHandlerContext ctx, Http2HeadersFrame headers)
92 throws Exception {
93 if (headers.isEndStream()) {
94 ByteBuf content = ctx.alloc().buffer();
95 content.writeBytes(RESPONSE_BYTES.duplicate());
96 ByteBufUtil.writeAscii(content, " - via HTTP/2");
97 sendResponse(ctx, headers.stream(), content);
98 }
99 }
100
101
102
103
104 private static void sendResponse(ChannelHandlerContext ctx, Http2FrameStream stream, ByteBuf payload) {
105
106 Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText());
107 ctx.write(new DefaultHttp2HeadersFrame(headers).stream(stream));
108 ctx.write(new DefaultHttp2DataFrame(payload, true).stream(stream));
109 }
110 }