View Javadoc
1   /*
2    * Copyright 2015 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    *   http://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  
17  /*
18   * Copyright 2014 Twitter, Inc.
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *     http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package io.netty.handler.codec.http2;
33  
34  import static io.netty.util.internal.ObjectUtil.checkNotNull;
35  
36  class HpackHeaderField {
37  
38      // Section 4.1. Calculating Table Size
39      // The additional 32 octets account for an estimated
40      // overhead associated with the structure.
41      static final int HEADER_ENTRY_OVERHEAD = 32;
42  
43      static long sizeOf(CharSequence name, CharSequence value) {
44          return name.length() + value.length() + HEADER_ENTRY_OVERHEAD;
45      }
46  
47      final CharSequence name;
48      final CharSequence value;
49  
50      // This constructor can only be used if name and value are ISO-8859-1 encoded.
51      HpackHeaderField(CharSequence name, CharSequence value) {
52          this.name = checkNotNull(name, "name");
53          this.value = checkNotNull(value, "value");
54      }
55  
56      final int size() {
57          return name.length() + value.length() + HEADER_ENTRY_OVERHEAD;
58      }
59  
60      @Override
61      public final int hashCode() {
62          // TODO(nmittler): Netty's build rules require this. Probably need a better implementation.
63          return super.hashCode();
64      }
65  
66      @Override
67      public final boolean equals(Object obj) {
68          if (obj == this) {
69              return true;
70          }
71          if (!(obj instanceof HpackHeaderField)) {
72              return false;
73          }
74          HpackHeaderField other = (HpackHeaderField) obj;
75          // To avoid short circuit behavior a bitwise operator is used instead of a boolean operator.
76          return (HpackUtil.equalsConstantTime(name, other.name) & HpackUtil.equalsConstantTime(value, other.value)) != 0;
77      }
78  
79      @Override
80      public String toString() {
81          return name + ": " + value;
82      }
83  }