View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at:
7    *
8    * https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package io.netty.handler.codec.http2;
16  
17  import io.netty.buffer.ByteBuf;
18  import io.netty.channel.ChannelHandler;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.handler.codec.base64.Base64;
21  import io.netty.handler.codec.http.FullHttpResponse;
22  import io.netty.handler.codec.http.HttpClientUpgradeHandler;
23  import io.netty.handler.codec.http.HttpRequest;
24  import io.netty.util.collection.CharObjectMap;
25  import io.netty.util.internal.UnstableApi;
26  
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.List;
30  
31  import static io.netty.handler.codec.base64.Base64Dialect.URL_SAFE;
32  import static io.netty.handler.codec.http2.Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME;
33  import static io.netty.handler.codec.http2.Http2CodecUtil.HTTP_UPGRADE_SETTINGS_HEADER;
34  import static io.netty.handler.codec.http2.Http2CodecUtil.SETTING_ENTRY_LENGTH;
35  import static io.netty.util.CharsetUtil.UTF_8;
36  import static io.netty.util.ReferenceCountUtil.release;
37  import static io.netty.util.internal.ObjectUtil.checkNotNull;
38  
39  /**
40   * Client-side cleartext upgrade codec from HTTP to HTTP/2.
41   */
42  @UnstableApi
43  public class Http2ClientUpgradeCodec implements HttpClientUpgradeHandler.UpgradeCodec {
44  
45      private static final List<CharSequence> UPGRADE_HEADERS = Collections.singletonList(HTTP_UPGRADE_SETTINGS_HEADER);
46  
47      private final String handlerName;
48      private final Http2ConnectionHandler connectionHandler;
49      private final ChannelHandler upgradeToHandler;
50      private final ChannelHandler http2MultiplexHandler;
51  
52      public Http2ClientUpgradeCodec(Http2FrameCodec frameCodec, ChannelHandler upgradeToHandler) {
53          this(null, frameCodec, upgradeToHandler);
54      }
55  
56      public Http2ClientUpgradeCodec(String handlerName, Http2FrameCodec frameCodec, ChannelHandler upgradeToHandler) {
57          this(handlerName, (Http2ConnectionHandler) frameCodec, upgradeToHandler, null);
58      }
59  
60      /**
61       * Creates the codec using a default name for the connection handler when adding to the
62       * pipeline.
63       *
64       * @param connectionHandler the HTTP/2 connection handler
65       */
66      public Http2ClientUpgradeCodec(Http2ConnectionHandler connectionHandler) {
67          this((String) null, connectionHandler);
68      }
69  
70      /**
71       * Creates the codec using a default name for the connection handler when adding to the
72       * pipeline.
73       *
74       * @param connectionHandler the HTTP/2 connection handler
75       * @param http2MultiplexHandler the Http2 Multiplexer handler to work with Http2FrameCodec
76       */
77      public Http2ClientUpgradeCodec(Http2ConnectionHandler connectionHandler,
78          Http2MultiplexHandler http2MultiplexHandler) {
79          this((String) null, connectionHandler, http2MultiplexHandler);
80      }
81  
82      /**
83       * Creates the codec providing an upgrade to the given handler for HTTP/2.
84       *
85       * @param handlerName the name of the HTTP/2 connection handler to be used in the pipeline,
86       *                    or {@code null} to auto-generate the name
87       * @param connectionHandler the HTTP/2 connection handler
88       */
89      public Http2ClientUpgradeCodec(String handlerName, Http2ConnectionHandler connectionHandler) {
90          this(handlerName, connectionHandler, connectionHandler, null);
91      }
92  
93      /**
94       * Creates the codec providing an upgrade to the given handler for HTTP/2.
95       *
96       * @param handlerName the name of the HTTP/2 connection handler to be used in the pipeline,
97       *                    or {@code null} to auto-generate the name
98       * @param connectionHandler the HTTP/2 connection handler
99       */
100     public Http2ClientUpgradeCodec(String handlerName, Http2ConnectionHandler connectionHandler,
101         Http2MultiplexHandler http2MultiplexHandler) {
102         this(handlerName, connectionHandler, connectionHandler, http2MultiplexHandler);
103     }
104 
105     private Http2ClientUpgradeCodec(String handlerName, Http2ConnectionHandler connectionHandler, ChannelHandler
106         upgradeToHandler, Http2MultiplexHandler http2MultiplexHandler) {
107         this.handlerName = handlerName;
108         this.connectionHandler = checkNotNull(connectionHandler, "connectionHandler");
109         this.upgradeToHandler = checkNotNull(upgradeToHandler, "upgradeToHandler");
110         this.http2MultiplexHandler = http2MultiplexHandler;
111     }
112 
113     @Override
114     public CharSequence protocol() {
115         return HTTP_UPGRADE_PROTOCOL_NAME;
116     }
117 
118     @Override
119     public Collection<CharSequence> setUpgradeHeaders(ChannelHandlerContext ctx,
120         HttpRequest upgradeRequest) {
121         CharSequence settingsValue = getSettingsHeaderValue(ctx);
122         upgradeRequest.headers().set(HTTP_UPGRADE_SETTINGS_HEADER, settingsValue);
123         return UPGRADE_HEADERS;
124     }
125 
126     @Override
127     public void upgradeTo(ChannelHandlerContext ctx, FullHttpResponse upgradeResponse)
128         throws Exception {
129         try {
130             // Add the handler to the pipeline.
131             ctx.pipeline().addAfter(ctx.name(), handlerName, upgradeToHandler);
132 
133             // Add the Http2 Multiplex handler as this handler handle events produced by the connectionHandler.
134             // See https://github.com/netty/netty/issues/9495
135             if (http2MultiplexHandler != null) {
136                 final String name = ctx.pipeline().context(connectionHandler).name();
137                 ctx.pipeline().addAfter(name, null, http2MultiplexHandler);
138             }
139 
140             // Reserve local stream 1 for the response.
141             connectionHandler.onHttpClientUpgrade();
142         } catch (Http2Exception e) {
143             ctx.fireExceptionCaught(e);
144             ctx.close();
145         }
146     }
147 
148     /**
149      * Converts the current settings for the handler to the Base64-encoded representation used in
150      * the HTTP2-Settings upgrade header.
151      */
152     private CharSequence getSettingsHeaderValue(ChannelHandlerContext ctx) {
153         ByteBuf buf = null;
154         ByteBuf encodedBuf = null;
155         try {
156             // Get the local settings for the handler.
157             Http2Settings settings = connectionHandler.decoder().localSettings();
158 
159             // Serialize the payload of the SETTINGS frame.
160             int payloadLength = SETTING_ENTRY_LENGTH * settings.size();
161             buf = ctx.alloc().buffer(payloadLength);
162             for (CharObjectMap.PrimitiveEntry<Long> entry : settings.entries()) {
163                 buf.writeChar(entry.key());
164                 buf.writeInt(entry.value().intValue());
165             }
166 
167             // Base64 encode the payload and then convert to a string for the header.
168             encodedBuf = Base64.encode(buf, URL_SAFE);
169             return encodedBuf.toString(UTF_8);
170         } finally {
171             release(buf);
172             release(encodedBuf);
173         }
174     }
175 }