1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http2;
17
18 import io.netty.channel.ChannelHandlerContext;
19 import io.netty.channel.ChannelInboundHandlerAdapter;
20 import io.netty.handler.codec.http.FullHttpMessage;
21 import io.netty.handler.codec.http.HttpHeaders;
22 import io.netty.handler.codec.http.HttpScheme;
23 import io.netty.util.internal.UnstableApi;
24
25
26
27
28 @UnstableApi
29 public class InboundHttpToHttp2Adapter extends ChannelInboundHandlerAdapter {
30 private final Http2Connection connection;
31 private final Http2FrameListener listener;
32
33 public InboundHttpToHttp2Adapter(Http2Connection connection, Http2FrameListener listener) {
34 this.connection = connection;
35 this.listener = listener;
36 }
37
38 private static int getStreamId(Http2Connection connection, HttpHeaders httpHeaders) {
39 return httpHeaders.getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(),
40 connection.remote().incrementAndGetNextStreamId());
41 }
42
43 @Override
44 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
45 if (msg instanceof FullHttpMessage) {
46 handle(ctx, connection, listener, (FullHttpMessage) msg);
47 } else {
48 super.channelRead(ctx, msg);
49 }
50 }
51
52
53
54
55 static void handle(ChannelHandlerContext ctx, Http2Connection connection,
56 Http2FrameListener listener, FullHttpMessage message) throws Http2Exception {
57 try {
58 int streamId = getStreamId(connection, message.headers());
59 Http2Stream stream = connection.stream(streamId);
60 if (stream == null) {
61 stream = connection.remote().createStream(streamId, false);
62 }
63 message.headers().set(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), HttpScheme.HTTP.name());
64 Http2Headers messageHeaders = HttpConversionUtil.toHttp2Headers(message, true);
65 boolean hasContent = message.content().isReadable();
66 boolean hasTrailers = !message.trailingHeaders().isEmpty();
67 listener.onHeadersRead(
68 ctx, streamId, messageHeaders, 0, !(hasContent || hasTrailers));
69 if (hasContent) {
70 listener.onDataRead(ctx, streamId, message.content(), 0, !hasTrailers);
71 }
72 if (hasTrailers) {
73 Http2Headers headers = HttpConversionUtil.toHttp2Headers(message.trailingHeaders(), true);
74 listener.onHeadersRead(ctx, streamId, headers, 0, true);
75 }
76 stream.closeRemoteSide();
77 } finally {
78 message.release();
79 }
80 }
81 }