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.netty5.handler.codec.http2;
17  
18  import io.netty5.util.internal.StringUtil;
19  import io.netty5.util.internal.UnstableApi;
20  
21  import static io.netty5.handler.codec.http2.Http2CodecUtil.verifyPadding;
22  import static java.util.Objects.requireNonNull;
23  
24  /**
25   * The default {@link Http2HeadersFrame} implementation.
26   */
27  @UnstableApi
28  public final class DefaultHttp2HeadersFrame extends AbstractHttp2StreamFrame implements Http2HeadersFrame {
29      private final Http2Headers headers;
30      private final boolean endStream;
31      private final int padding;
32  
33      /**
34       * Equivalent to {@code new DefaultHttp2HeadersFrame(headers, false)}.
35       *
36       * @param headers the non-{@code null} headers to send
37       */
38      public DefaultHttp2HeadersFrame(Http2Headers headers) {
39          this(headers, false);
40      }
41  
42      /**
43       * Equivalent to {@code new DefaultHttp2HeadersFrame(headers, endStream, 0)}.
44       *
45       * @param headers the non-{@code null} headers to send
46       */
47      public DefaultHttp2HeadersFrame(Http2Headers headers, boolean endStream) {
48          this(headers, endStream, 0);
49      }
50  
51      /**
52       * Construct a new headers message.
53       *
54       * @param headers the non-{@code null} headers to send
55       * @param endStream whether these headers should terminate the stream
56       * @param padding additional bytes that should be added to obscure the true content size. Must be between 0 and
57       *                256 (inclusive).
58       */
59      public DefaultHttp2HeadersFrame(Http2Headers headers, boolean endStream, int padding) {
60          this.headers = requireNonNull(headers, "headers");
61          this.endStream = endStream;
62          verifyPadding(padding);
63          this.padding = padding;
64      }
65  
66      @Override
67      public DefaultHttp2HeadersFrame stream(Http2FrameStream stream) {
68          super.stream(stream);
69          return this;
70      }
71  
72      @Override
73      public String name() {
74          return "HEADERS";
75      }
76  
77      @Override
78      public Http2Headers headers() {
79          return headers;
80      }
81  
82      @Override
83      public boolean isEndStream() {
84          return endStream;
85      }
86  
87      @Override
88      public int padding() {
89          return padding;
90      }
91  
92      @Override
93      public String toString() {
94          return StringUtil.simpleClassName(this) + "(stream=" + stream() + ", headers=" + headers
95                 + ", endStream=" + endStream + ", padding=" + padding + ')';
96      }
97  
98      @Override
99      public boolean equals(Object o) {
100         if (!(o instanceof DefaultHttp2HeadersFrame)) {
101             return false;
102         }
103         DefaultHttp2HeadersFrame other = (DefaultHttp2HeadersFrame) o;
104         return super.equals(other) && headers.equals(other.headers)
105                 && endStream == other.endStream && padding == other.padding;
106     }
107 
108     @Override
109     public int hashCode() {
110         int hash = super.hashCode();
111         hash = hash * 31 + headers.hashCode();
112         hash = hash * 31 + (endStream ? 0 : 1);
113         hash = hash * 31 + padding;
114         return hash;
115     }
116 }