1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
27
28 package org.apache.hc.core5.http2.hpack;
29
30 import org.apache.hc.core5.http.Header;
31
32 /**
33 * Internal HPack header representation that also contains binary length of
34 * header name and header value.
35 */
36 final class HPackHeader implements Header {
37
38 static private final int ENTRY_SIZE_OVERHEAD = 32;
39
40 private final String name;
41 private final int nameLen;
42 private final String value;
43 private final int valueLen;
44 private final boolean sensitive;
45
46 HPackHeader(final String name, final int nameLen, final String value, final int valueLen, final boolean sensitive) {
47 this.name = name;
48 this.nameLen = nameLen;
49 this.value = value;
50 this.valueLen = valueLen;
51 this.sensitive = sensitive;
52 }
53
54 HPackHeader(final String name, final String value, final boolean sensitive) {
55 this(name, name.length(), value, value.length(), sensitive);
56 }
57
58 HPackHeader(final String name, final String value) {
59 this(name, value, false);
60 }
61
62 HPackHeader(final Header header) {
63 this(header.getName(), header.getValue(), header.isSensitive());
64 }
65
66 @Override
67 public String getName() {
68 return name;
69 }
70
71 public int getNameLen() {
72 return nameLen;
73 }
74
75 @Override
76 public String getValue() {
77 return value;
78 }
79
80 public int getValueLen() {
81 return valueLen;
82 }
83
84 @Override
85 public boolean isSensitive() {
86 return sensitive;
87 }
88
89 public int getTotalSize() {
90 return nameLen + valueLen + ENTRY_SIZE_OVERHEAD;
91 }
92
93 @Override
94 public String toString() {
95 final StringBuilder buf = new StringBuilder();
96 buf.append(this.name).append(": ");
97 if (this.value != null) {
98 buf.append(this.value);
99 }
100 return buf.toString();
101 }
102
103 }