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    * http://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  
16  package io.netty.handler.codec.http2;
17  
18  import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_HEADER_TABLE_SIZE;
19  import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_MAX_HEADER_SIZE;
20  import static io.netty.handler.codec.http2.Http2Error.INTERNAL_ERROR;
21  import static io.netty.handler.codec.http2.Http2Error.PROTOCOL_ERROR;
22  import static io.netty.handler.codec.http2.Http2Error.COMPRESSION_ERROR;
23  import static io.netty.handler.codec.http2.Http2Exception.connectionError;
24  import io.netty.buffer.ByteBuf;
25  import io.netty.buffer.ByteBufInputStream;
26  import io.netty.handler.codec.AsciiString;
27  
28  import java.io.IOException;
29  import java.io.InputStream;
30  
31  import com.twitter.hpack.Decoder;
32  import com.twitter.hpack.HeaderListener;
33  
34  public class DefaultHttp2HeadersDecoder implements Http2HeadersDecoder, Http2HeadersDecoder.Configuration {
35      private final Decoder decoder;
36      private final Http2HeaderTable headerTable;
37  
38      public DefaultHttp2HeadersDecoder() {
39          this(DEFAULT_MAX_HEADER_SIZE, DEFAULT_HEADER_TABLE_SIZE);
40      }
41  
42      public DefaultHttp2HeadersDecoder(int maxHeaderSize, int maxHeaderTableSize) {
43          decoder = new Decoder(maxHeaderSize, maxHeaderTableSize);
44          headerTable = new Http2HeaderTableDecoder();
45      }
46  
47      @Override
48      public Http2HeaderTable headerTable() {
49          return headerTable;
50      }
51  
52      @Override
53      public Configuration configuration() {
54          return this;
55      }
56  
57      @Override
58      public Http2Headers decodeHeaders(ByteBuf headerBlock) throws Http2Exception {
59          InputStream in = new ByteBufInputStream(headerBlock);
60          try {
61              final Http2Headers headers = new DefaultHttp2Headers();
62              HeaderListener listener = new HeaderListener() {
63                  @Override
64                  public void addHeader(byte[] key, byte[] value, boolean sensitive) {
65                      headers.add(new AsciiString(key, false), new AsciiString(value, false));
66                  }
67              };
68  
69              decoder.decode(in, listener);
70              boolean truncated = decoder.endHeaderBlock();
71              if (truncated) {
72                  // TODO: what's the right thing to do here?
73              }
74  
75              if (headers.size() > headerTable.maxHeaderListSize()) {
76                  throw connectionError(PROTOCOL_ERROR, "Number of headers (%d) exceeds maxHeaderListSize (%d)",
77                          headers.size(), headerTable.maxHeaderListSize());
78              }
79  
80              return headers;
81          } catch (IOException e) {
82              throw connectionError(COMPRESSION_ERROR, e, e.getMessage());
83          } catch (Http2Exception e) {
84              throw e;
85          } catch (Throwable e) {
86              // Default handler for any other types of errors that may have occurred. For example,
87              // the the Header builder throws IllegalArgumentException if the key or value was invalid
88              // for any reason (e.g. the key was an invalid pseudo-header).
89              throw connectionError(PROTOCOL_ERROR, e, e.getMessage());
90          } finally {
91              try {
92                  in.close();
93              } catch (IOException e) {
94                  throw connectionError(INTERNAL_ERROR, e, e.getMessage());
95              }
96          }
97      }
98  
99      /**
100      * {@link Http2HeaderTable} implementation to support {@link Http2HeadersDecoder}
101      */
102     private final class Http2HeaderTableDecoder extends DefaultHttp2HeaderTableListSize implements Http2HeaderTable {
103         @Override
104         public void maxHeaderTableSize(int max) throws Http2Exception {
105             if (max < 0) {
106                 throw connectionError(PROTOCOL_ERROR, "Header Table Size must be non-negative but was %d", max);
107             }
108             try {
109                 decoder.setMaxHeaderTableSize(max);
110             } catch (Throwable t) {
111                 throw connectionError(PROTOCOL_ERROR, t.getMessage(), t);
112             }
113         }
114 
115         @Override
116         public int maxHeaderTableSize() {
117             return decoder.getMaxHeaderTableSize();
118         }
119     }
120 }