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.Http2Error.INTERNAL_ERROR;
20  import static io.netty.handler.codec.http2.Http2Error.COMPRESSION_ERROR;
21  import static io.netty.handler.codec.http2.Http2Error.PROTOCOL_ERROR;
22  import static io.netty.handler.codec.http2.Http2Exception.connectionError;
23  import io.netty.buffer.ByteBuf;
24  import io.netty.buffer.ByteBufOutputStream;
25  import io.netty.handler.codec.AsciiString;
26  import io.netty.handler.codec.BinaryHeaders.EntryVisitor;
27  
28  import java.io.ByteArrayOutputStream;
29  import java.io.IOException;
30  import java.io.OutputStream;
31  import java.util.Collections;
32  import java.util.Map.Entry;
33  import java.util.Set;
34  import java.util.TreeSet;
35  
36  import com.twitter.hpack.Encoder;
37  
38  public class DefaultHttp2HeadersEncoder implements Http2HeadersEncoder, Http2HeadersEncoder.Configuration {
39      private final Encoder encoder;
40      private final ByteArrayOutputStream tableSizeChangeOutput = new ByteArrayOutputStream();
41      private final Set<String> sensitiveHeaders = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
42      private final Http2HeaderTable headerTable;
43  
44      public DefaultHttp2HeadersEncoder() {
45          this(DEFAULT_HEADER_TABLE_SIZE, Collections.<String>emptySet());
46      }
47  
48      public DefaultHttp2HeadersEncoder(int maxHeaderTableSize, Set<String> sensitiveHeaders) {
49          encoder = new Encoder(maxHeaderTableSize);
50          this.sensitiveHeaders.addAll(sensitiveHeaders);
51          headerTable = new Http2HeaderTableEncoder();
52      }
53  
54      @Override
55      public void encodeHeaders(Http2Headers headers, ByteBuf buffer) throws Http2Exception {
56          final OutputStream stream = new ByteBufOutputStream(buffer);
57          try {
58              if (headers.size() > headerTable.maxHeaderListSize()) {
59                  throw connectionError(PROTOCOL_ERROR, "Number of headers (%d) exceeds maxHeaderListSize (%d)",
60                          headers.size(), headerTable.maxHeaderListSize());
61              }
62  
63              // If there was a change in the table size, serialize the output from the encoder
64              // resulting from that change.
65              if (tableSizeChangeOutput.size() > 0) {
66                  buffer.writeBytes(tableSizeChangeOutput.toByteArray());
67                  tableSizeChangeOutput.reset();
68              }
69  
70              // Write pseudo headers first as required by the HTTP/2 spec.
71              for (Http2Headers.PseudoHeaderName pseudoHeader : Http2Headers.PseudoHeaderName.values()) {
72                  AsciiString name = pseudoHeader.value();
73                  AsciiString value = headers.get(name);
74                  if (value != null) {
75                      encodeHeader(name, value, stream);
76                  }
77              }
78  
79              headers.forEachEntry(new EntryVisitor() {
80                  @Override
81                  public boolean visit(Entry<AsciiString, AsciiString> entry) throws Exception {
82                      final AsciiString name = entry.getKey();
83                      final AsciiString value = entry.getValue();
84                      if (!Http2Headers.PseudoHeaderName.isPseudoHeader(name)) {
85                          encodeHeader(name, value, stream);
86                      }
87                      return true;
88                  }
89              });
90          } catch (Http2Exception e) {
91              throw e;
92          } catch (Throwable t) {
93              throw connectionError(COMPRESSION_ERROR, t, "Failed encoding headers block: %s", t.getMessage());
94          } finally {
95              try {
96                  stream.close();
97              } catch (IOException e) {
98                  throw connectionError(INTERNAL_ERROR, e, e.getMessage());
99              }
100         }
101     }
102 
103     @Override
104     public Http2HeaderTable headerTable() {
105         return headerTable;
106     }
107 
108     @Override
109     public Configuration configuration() {
110         return this;
111     }
112 
113     private void encodeHeader(AsciiString key, AsciiString value, OutputStream stream) throws IOException {
114         boolean sensitive = sensitiveHeaders.contains(key.toString());
115         encoder.encodeHeader(stream, key.array(), value.array(), sensitive);
116     }
117 
118     /**
119      * {@link Http2HeaderTable} implementation to support {@link Http2HeadersEncoder}
120      */
121     private final class Http2HeaderTableEncoder extends DefaultHttp2HeaderTableListSize implements Http2HeaderTable {
122         @Override
123         public void maxHeaderTableSize(int max) throws Http2Exception {
124             if (max < 0) {
125                 throw connectionError(PROTOCOL_ERROR, "Header Table Size must be non-negative but was %d", max);
126             }
127             try {
128                 // No headers should be emitted. If they are, we throw.
129                 encoder.setMaxHeaderTableSize(tableSizeChangeOutput, max);
130             } catch (IOException e) {
131                 throw new Http2Exception(COMPRESSION_ERROR, e.getMessage(), e);
132             } catch (Throwable t) {
133                 throw new Http2Exception(PROTOCOL_ERROR, t.getMessage(), t);
134             }
135         }
136 
137         @Override
138         public int maxHeaderTableSize() {
139             return encoder.getMaxHeaderTableSize();
140         }
141     }
142 }