-
Notifications
You must be signed in to change notification settings - Fork 29
/
header_methods.go
188 lines (172 loc) · 3.48 KB
/
header_methods.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// Copyright © 2011-2019 Guy M. Allard
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package stompngo
import (
"unicode/utf8"
)
/*
Add appends a key and value pair as a header to a set of Headers.
*/
func (h Headers) Add(k, v string) Headers {
r := append(h, k, v)
return r
}
/*
AddHeaders appends one set of Headers to another.
*/
func (h Headers) AddHeaders(o Headers) Headers {
r := append(h, o...)
return r
}
/*
Compare compares one set of Headers with another.
*/
func (h Headers) Compare(other Headers) bool {
if len(h) != len(other) {
return false
}
for i, v := range h {
if v != other[i] {
return false
}
}
return true
}
/*
Contains returns true if a set of Headers contains a key.
*/
func (h Headers) Contains(k string) (string, bool) {
for i := 0; i < len(h); i += 2 {
if h[i] == k {
return h[i+1], true
}
}
return "", false
}
/*
ContainsKV returns true if a set of Headers contains a key and value pair.
*/
func (h Headers) ContainsKV(k string, v string) bool {
for i := 0; i < len(h); i += 2 {
if h[i] == k && h[i+1] == v {
return true
}
}
return false
}
/*
Value returns a header value for a specified key. If the key is not present
an empty string is returned.
*/
func (h Headers) Value(k string) string {
for i := 0; i < len(h); i += 2 {
if h[i] == k {
return h[i+1]
}
}
return ""
}
/*
Index returns the index of a keader key in Headers. Return -1 if the
key is not present.
*/
func (h Headers) Index(k string) int {
r := -1
for i := 0; i < len(h); i += 2 {
if h[i] == k {
r = i
break
}
}
return r
}
/*
Validate performs bacic validation of a set of Headers.
*/
func (h Headers) Validate() error {
if len(h)%2 != 0 {
return EHDRLEN
}
return nil
}
/*
ValidateUTF8 validates that header strings are UTF8.
*/
func (h Headers) ValidateUTF8() (string, error) {
for i := range h {
if !utf8.ValidString(h[i]) {
return h[i], EHDRUTF8
}
}
return "", nil
}
/*
Clone copies a set of Headers.
*/
func (h Headers) Clone() Headers {
r := make(Headers, len(h))
copy(r, h)
return r
}
/*
Delete removes a key and value pair from a set of Headers.
*/
func (h Headers) Delete(k string) Headers {
r := h.Clone()
i := r.Index(k)
if i >= 0 {
r = append(r[:i], r[i+2:]...)
}
return r
}
/*
Size returns the size of Headers on the wire, in bytes.
*/
func (h Headers) Size(e bool) int64 {
l := 0
for i := 0; i < len(h); i += 2 {
if e {
l += len(encode(h[i])) + 1 + len(encode(h[i+1])) + 1
} else {
l += len(h[i]) + 1 + len(h[i+1]) + 1
}
}
return int64(l)
}
/*
String makes Headers a Stringer.
*/
func (h Headers) String() string {
ec := h.Validate()
if ec != nil {
return ec.Error()
}
b := make([]byte, 0, 1024)
for i := 0; i < len(h); i += 2 {
b = append(b, h[i]+":"+h[i+1]+"\n"...)
}
return string(b)
}
/*
Bytes returns a byte slice of the headers
*/
func (h Headers) Bytes() []byte {
b := make([]byte, 0, 1024)
for i := 0; i < len(h); i += 2 {
b = append(b, h[i]+":"+h[i+1]+"\n"...)
}
return b
}