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