View Javadoc
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a 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
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.http2;
17  
18  import io.netty.util.internal.StringUtil;
19  
20  import static io.netty.handler.codec.http2.Http2CodecUtil.verifyPadding;
21  import static io.netty.util.internal.ObjectUtil.checkNotNull;
22  
23  /**
24   * The default {@link Http2HeadersFrame} implementation.
25   */
26  public final class DefaultHttp2HeadersFrame extends AbstractHttp2StreamFrame implements Http2HeadersFrame {
27      private final Http2Headers headers;
28      private final boolean endStream;
29      private final int padding;
30  
31      /**
32       * Equivalent to {@code new DefaultHttp2HeadersFrame(headers, false)}.
33       *
34       * @param headers the non-{@code null} headers to send
35       */
36      public DefaultHttp2HeadersFrame(Http2Headers headers) {
37          this(headers, false);
38      }
39  
40      /**
41       * Equivalent to {@code new DefaultHttp2HeadersFrame(headers, endStream, 0)}.
42       *
43       * @param headers the non-{@code null} headers to send
44       */
45      public DefaultHttp2HeadersFrame(Http2Headers headers, boolean endStream) {
46          this(headers, endStream, 0);
47      }
48  
49      /**
50       * Construct a new headers message.
51       *
52       * @param headers the non-{@code null} headers to send
53       * @param endStream whether these headers should terminate the stream
54       * @param padding additional bytes that should be added to obscure the true content size. Must be between 0 and
55       *                256 (inclusive).
56       */
57      public DefaultHttp2HeadersFrame(Http2Headers headers, boolean endStream, int padding) {
58          this.headers = checkNotNull(headers, "headers");
59          this.endStream = endStream;
60          verifyPadding(padding);
61          this.padding = padding;
62      }
63  
64      @Override
65      public DefaultHttp2HeadersFrame stream(Http2FrameStream stream) {
66          super.stream(stream);
67          return this;
68      }
69  
70      @Override
71      public String name() {
72          return "HEADERS";
73      }
74  
75      @Override
76      public Http2Headers headers() {
77          return headers;
78      }
79  
80      @Override
81      public boolean isEndStream() {
82          return endStream;
83      }
84  
85      @Override
86      public int padding() {
87          return padding;
88      }
89  
90      @Override
91      public String toString() {
92          return StringUtil.simpleClassName(this) + "(stream=" + stream() + ", headers=" + headers
93                 + ", endStream=" + endStream + ", padding=" + padding + ')';
94      }
95  
96      @Override
97      public boolean equals(Object o) {
98          if (!(o instanceof DefaultHttp2HeadersFrame)) {
99              return false;
100         }
101         DefaultHttp2HeadersFrame other = (DefaultHttp2HeadersFrame) o;
102         return super.equals(other) && headers.equals(other.headers)
103                 && endStream == other.endStream && padding == other.padding;
104     }
105 
106     @Override
107     public int hashCode() {
108         int hash = super.hashCode();
109         hash = hash * 31 + headers.hashCode();
110         hash = hash * 31 + (endStream ? 0 : 1);
111         hash = hash * 31 + padding;
112         return hash;
113     }
114 }