2 // Copyright © 2011-2019 Guy M. Allard
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
24 Add appends a key and value pair as a header to a set of Headers.
26 func (h Headers
) Add(k
, v
string) Headers
{
32 AddHeaders appends one set of Headers to another.
34 func (h Headers
) AddHeaders(o Headers
) Headers
{
40 Compare compares one set of Headers with another.
42 func (h Headers
) Compare(other Headers
) bool {
43 if len(h
) != len(other
) {
55 Contains returns true if a set of Headers contains a key.
57 func (h Headers
) Contains(k
string) (string, bool) {
58 for i
:= 0; i
< len(h
); i
+= 2 {
67 ContainsKV returns true if a set of Headers contains a key and value pair.
69 func (h Headers
) ContainsKV(k
string, v
string) bool {
70 for i
:= 0; i
< len(h
); i
+= 2 {
71 if h
[i
] == k
&& h
[i
+1] == v
{
79 Value returns a header value for a specified key. If the key is not present
80 an empty string is returned.
82 func (h Headers
) Value(k
string) string {
83 for i
:= 0; i
< len(h
); i
+= 2 {
92 Index returns the index of a keader key in Headers. Return -1 if the
95 func (h Headers
) Index(k
string) int {
97 for i
:= 0; i
< len(h
); i
+= 2 {
107 Validate performs bacic validation of a set of Headers.
109 func (h Headers
) Validate() error
{
117 ValidateUTF8 validates that header strings are UTF8.
119 func (h Headers
) ValidateUTF8() (string, error
) {
121 if !utf8
.ValidString(h
[i
]) {
122 return h
[i
], EHDRUTF8
129 Clone copies a set of Headers.
131 func (h Headers
) Clone() Headers
{
132 r
:= make(Headers
, len(h
))
138 Delete removes a key and value pair from a set of Headers.
140 func (h Headers
) Delete(k
string) Headers
{
144 r
= append(r
[:i
], r
[i
+2:]...)
150 Size returns the size of Headers on the wire, in bytes.
152 func (h Headers
) Size(e
bool) int64 {
154 for i
:= 0; i
< len(h
); i
+= 2 {
156 l
+= len(encode(h
[i
])) + 1 + len(encode(h
[i
+1])) + 1
158 l
+= len(h
[i
]) + 1 + len(h
[i
+1]) + 1
165 String makes Headers a Stringer.
167 func (h Headers
) String() string {
172 b
:= make([]byte, 0, 1024)
173 for i
:= 0; i
< len(h
); i
+= 2 {
174 b
= append(b
, h
[i
]+":"+h
[i
+1]+"\n"...)
180 Bytes returns a byte slice of the headers
182 func (h Headers
) Bytes() []byte {
183 b
:= make([]byte, 0, 1024)
184 for i
:= 0; i
< len(h
); i
+= 2 {
185 b
= append(b
, h
[i
]+":"+h
[i
+1]+"\n"...)