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  
16  package io.netty.handler.codec.http2;
17  
18  import io.netty.buffer.ByteBuf;
19  
20  /**
21   * Encodes {@link Http2Headers} into HPACK-encoded headers blocks.
22   */
23  public interface Http2HeadersEncoder {
24      /**
25       * Configuration related elements for the {@link Http2HeadersEncoder} interface
26       */
27      interface Configuration {
28          /**
29           * Represents the value for
30           * <a href="https://tools.ietf.org/html/rfc7540#section-6.5.2">SETTINGS_HEADER_TABLE_SIZE</a>.
31           * This method should only be called by Netty (not users) as a result of a receiving a {@code SETTINGS} frame.
32           */
33          void maxHeaderTableSize(long max) throws Http2Exception;
34  
35          /**
36           * Represents the value for
37           * <a href="https://tools.ietf.org/html/rfc7540#section-6.5.2">SETTINGS_HEADER_TABLE_SIZE</a>.
38           * The initial value returned by this method must be {@link Http2CodecUtil#DEFAULT_HEADER_TABLE_SIZE}.
39           */
40          long maxHeaderTableSize();
41  
42          /**
43           * Represents the value for
44           * <a href="https://tools.ietf.org/html/rfc7540#section-6.5.2">SETTINGS_MAX_HEADER_LIST_SIZE</a>.
45           * This method should only be called by Netty (not users) as a result of a receiving a {@code SETTINGS} frame.
46           */
47          void maxHeaderListSize(long max) throws Http2Exception;
48  
49          /**
50           * Represents the value for
51           * <a href="https://tools.ietf.org/html/rfc7540#section-6.5.2">SETTINGS_MAX_HEADER_LIST_SIZE</a>.
52           */
53          long maxHeaderListSize();
54      }
55  
56      /**
57       * Determine if a header name/value pair is treated as
58       * <a href="https://tools.ietf.org/html/rfc7541#section-7.1.3">sensitive</a>.
59       * If the object can be dynamically modified and shared across multiple connections it may need to be thread safe.
60       */
61      interface SensitivityDetector {
62          /**
63           * Determine if a header {@code name}/{@code value} pair should be treated as
64           * <a href="https://tools.ietf.org/html/rfc7541#section-7.1.3">sensitive</a>.
65           *
66           * @param name The name for the header.
67           * @param value The value of the header.
68           * @return {@code true} if a header {@code name}/{@code value} pair should be treated as
69           * <a href="https://tools.ietf.org/html/rfc7541#section-7.1.3">sensitive</a>.
70           * {@code false} otherwise.
71           */
72          boolean isSensitive(CharSequence name, CharSequence value);
73      }
74  
75      /**
76       * Encodes the given headers and writes the output headers block to the given output buffer.
77       *
78       * @param streamId  the identifier of the stream for which the headers are encoded.
79       * @param headers the headers to be encoded.
80       * @param buffer the buffer to receive the encoded headers.
81       */
82      void encodeHeaders(int streamId, Http2Headers headers, ByteBuf buffer) throws Http2Exception;
83  
84      /**
85       * Get the {@link Configuration} for this {@link Http2HeadersEncoder}
86       */
87      Configuration configuration();
88  
89      /**
90       * Always return {@code false} for {@link SensitivityDetector#isSensitive(CharSequence, CharSequence)}.
91       */
92      SensitivityDetector NEVER_SENSITIVE = new SensitivityDetector() {
93          @Override
94          public boolean isSensitive(CharSequence name, CharSequence value) {
95              return false;
96          }
97      };
98  
99      /**
100      * Always return {@code true} for {@link SensitivityDetector#isSensitive(CharSequence, CharSequence)}.
101      */
102     SensitivityDetector ALWAYS_SENSITIVE = new SensitivityDetector() {
103         @Override
104         public boolean isSensitive(CharSequence name, CharSequence value) {
105             return true;
106         }
107     };
108 }