1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package io.netty.handler.codec.http2;
16
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.ByteBufUtil;
19 import io.netty.channel.ChannelHandler;
20 import io.netty.channel.ChannelHandlerContext;
21 import io.netty.handler.codec.base64.Base64;
22 import io.netty.handler.codec.http.FullHttpRequest;
23 import io.netty.handler.codec.http.HttpHeaders;
24 import io.netty.handler.codec.http.HttpServerUpgradeHandler;
25 import io.netty.util.CharsetUtil;
26 import io.netty.util.internal.UnstableApi;
27 import io.netty.util.internal.logging.InternalLogger;
28 import io.netty.util.internal.logging.InternalLoggerFactory;
29
30 import java.nio.CharBuffer;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.List;
34
35 import static io.netty.handler.codec.base64.Base64Dialect.URL_SAFE;
36 import static io.netty.handler.codec.http2.Http2CodecUtil.FRAME_HEADER_LENGTH;
37 import static io.netty.handler.codec.http2.Http2CodecUtil.HTTP_UPGRADE_SETTINGS_HEADER;
38 import static io.netty.handler.codec.http2.Http2CodecUtil.writeFrameHeader;
39 import static io.netty.handler.codec.http2.Http2FrameTypes.SETTINGS;
40
41
42
43
44 @UnstableApi
45 public class Http2ServerUpgradeCodec implements HttpServerUpgradeHandler.UpgradeCodec {
46
47 private static final InternalLogger logger = InternalLoggerFactory.getInstance(Http2ServerUpgradeCodec.class);
48 private static final List<CharSequence> REQUIRED_UPGRADE_HEADERS =
49 Collections.singletonList(HTTP_UPGRADE_SETTINGS_HEADER);
50 private static final ChannelHandler[] EMPTY_HANDLERS = new ChannelHandler[0];
51
52 private final String handlerName;
53 private final Http2ConnectionHandler connectionHandler;
54 private final ChannelHandler[] handlers;
55 private final Http2FrameReader frameReader;
56
57 private Http2Settings settings;
58
59
60
61
62
63
64
65 public Http2ServerUpgradeCodec(Http2ConnectionHandler connectionHandler) {
66 this(null, connectionHandler, EMPTY_HANDLERS);
67 }
68
69
70
71
72
73
74
75 public Http2ServerUpgradeCodec(Http2MultiplexCodec http2Codec) {
76 this(null, http2Codec, EMPTY_HANDLERS);
77 }
78
79
80
81
82
83
84
85
86 public Http2ServerUpgradeCodec(String handlerName, Http2ConnectionHandler connectionHandler) {
87 this(handlerName, connectionHandler, EMPTY_HANDLERS);
88 }
89
90
91
92
93
94
95
96 public Http2ServerUpgradeCodec(String handlerName, Http2MultiplexCodec http2Codec) {
97 this(handlerName, http2Codec, EMPTY_HANDLERS);
98 }
99
100
101
102
103
104
105
106
107 public Http2ServerUpgradeCodec(Http2FrameCodec http2Codec, ChannelHandler... handlers) {
108 this(null, http2Codec, handlers);
109 }
110
111 private Http2ServerUpgradeCodec(String handlerName, Http2ConnectionHandler connectionHandler,
112 ChannelHandler... handlers) {
113 this.handlerName = handlerName;
114 this.connectionHandler = connectionHandler;
115 this.handlers = handlers;
116 frameReader = new DefaultHttp2FrameReader();
117 }
118
119 @Override
120 public Collection<CharSequence> requiredUpgradeHeaders() {
121 return REQUIRED_UPGRADE_HEADERS;
122 }
123
124 @Override
125 public boolean prepareUpgradeResponse(ChannelHandlerContext ctx, FullHttpRequest upgradeRequest,
126 HttpHeaders headers) {
127 try {
128
129
130 List<String> upgradeHeaders = upgradeRequest.headers().getAll(HTTP_UPGRADE_SETTINGS_HEADER);
131 if (upgradeHeaders.size() != 1) {
132 throw new IllegalArgumentException("There must be 1 and only 1 "
133 + HTTP_UPGRADE_SETTINGS_HEADER + " header.");
134 }
135 settings = decodeSettingsHeader(ctx, upgradeHeaders.get(0));
136
137 return true;
138 } catch (Throwable cause) {
139 logger.info("Error during upgrade to HTTP/2", cause);
140 return false;
141 }
142 }
143
144 @Override
145 public void upgradeTo(final ChannelHandlerContext ctx, FullHttpRequest upgradeRequest) {
146 try {
147
148 ctx.pipeline().addAfter(ctx.name(), handlerName, connectionHandler);
149
150
151
152 if (handlers != null) {
153 final String name = ctx.pipeline().context(connectionHandler).name();
154 for (int i = handlers.length - 1; i >= 0; i--) {
155 ctx.pipeline().addAfter(name, null, handlers[i]);
156 }
157 }
158 connectionHandler.onHttpServerUpgrade(settings);
159 } catch (Http2Exception e) {
160 ctx.fireExceptionCaught(e);
161 ctx.close();
162 }
163 }
164
165
166
167
168 private Http2Settings decodeSettingsHeader(ChannelHandlerContext ctx, CharSequence settingsHeader)
169 throws Http2Exception {
170 ByteBuf header = ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(settingsHeader), CharsetUtil.UTF_8);
171 try {
172
173 ByteBuf payload = Base64.decode(header, URL_SAFE);
174
175
176 ByteBuf frame = createSettingsFrame(ctx, payload);
177
178
179 return decodeSettings(ctx, frame);
180 } finally {
181 header.release();
182 }
183 }
184
185
186
187
188 private Http2Settings decodeSettings(ChannelHandlerContext ctx, ByteBuf frame) throws Http2Exception {
189 try {
190 final Http2Settings decodedSettings = new Http2Settings();
191 frameReader.readFrame(ctx, frame, new Http2FrameAdapter() {
192 @Override
193 public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings settings) {
194 decodedSettings.copyFrom(settings);
195 }
196 });
197 return decodedSettings;
198 } finally {
199 frame.release();
200 }
201 }
202
203
204
205
206 private static ByteBuf createSettingsFrame(ChannelHandlerContext ctx, ByteBuf payload) {
207 ByteBuf frame = ctx.alloc().buffer(FRAME_HEADER_LENGTH + payload.readableBytes());
208 writeFrameHeader(frame, payload.readableBytes(), SETTINGS, new Http2Flags(), 0);
209 frame.writeBytes(payload);
210 payload.release();
211 return frame;
212 }
213 }