-
Notifications
You must be signed in to change notification settings - Fork 2
/
warcfields.go
263 lines (234 loc) · 7.46 KB
/
warcfields.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* Copyright 2021 National Library of Norway.
*
* 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 gowarc
import (
"fmt"
"io"
"net/http"
"sort"
"strconv"
"strings"
"time"
)
type nameValue struct {
Name string
Value string
}
func (n *nameValue) String() string {
return n.Name + ": " + n.Value
}
// WarcFields represents the key value pairs in a WARC-record header.
//
// It is also used for representing the record block of records with content-type "application/warc-fields".
//
// All key-manipulating functions take case-insensitive keys and modify them to their canonical form.
type WarcFields []*nameValue
func (wf *WarcFields) CanonicalHeaderKey(s string) string {
return http.CanonicalHeaderKey(s)
}
// Get gets the first value associated with the given key. It is case-insensitive.
// If the key doesn't exist or there are no values associated with the key, Get returns the empty string.
// To access multiple values of a key, use GetAll.
func (wf *WarcFields) Get(key string) string {
key, _ = normalizeName(key)
for _, nv := range *wf {
if nv.Name == key {
return nv.Value
}
}
return ""
}
// GetInt is like Get, but converts the field value to int.
func (wf *WarcFields) GetInt(key string) (int, error) {
if !wf.Has(key) {
return 0, fmt.Errorf("missing field %s", key)
}
return strconv.Atoi(wf.Get(key))
}
// GetInt64 is like Get, but converts the field value to int64.
func (wf *WarcFields) GetInt64(name string) (int64, error) {
if !wf.Has(name) {
return 0, fmt.Errorf("missing field %s", name)
}
return strconv.ParseInt(wf.Get(name), 10, 64)
}
// GetTime is like Get, but converts the field value to time.Time.
// The field is expected to be in RFC 3339 format.
func (wf *WarcFields) GetTime(name string) (time.Time, error) {
if !wf.Has(name) {
return time.Time{}, fmt.Errorf("missing field %s", name)
}
return time.Parse(time.RFC3339, wf.Get(name))
}
// GetId is like Get, but removes the surrounding '<' and '>' from the field value.
func (wf *WarcFields) GetId(name string) string {
return strings.Trim(wf.Get(name), "<>")
}
// GetAll returns all values associated with the given key. It is case-insensitive.
func (wf *WarcFields) GetAll(name string) []string {
name, _ = normalizeName(name)
var result []string
for _, nv := range *wf {
if nv.Name == name {
result = append(result, nv.Value)
}
}
return result
}
// Has returns true if field exists.
// This can be used to separate a missing field from a field for which value is the empty string.
func (wf *WarcFields) Has(name string) bool {
name, _ = normalizeName(name)
for _, nv := range *wf {
if nv.Name == name {
return true
}
}
return false
}
// Add adds the key, value pair to the header.
// It appends to any existing values associated with key. The key is case-insensitive.
func (wf *WarcFields) Add(name string, value string) {
name, _ = normalizeName(name)
*wf = append(*wf, &nameValue{Name: name, Value: value})
}
// AddInt adds the key, value pair to the header.
// It appends to any existing values associated with key. The key is case-insensitive.
func (wf *WarcFields) AddInt(name string, value int) {
wf.Add(name, strconv.Itoa(value))
}
// AddInt64 adds the key, value pair to the header.
// It appends to any existing values associated with key. The key is case-insensitive.
func (wf *WarcFields) AddInt64(name string, value int64) {
wf.Add(name, strconv.FormatInt(value, 10))
}
// AddTime adds the key, value pair to the header.
// It appends to any existing values associated with key. The key is case-insensitive.
//
// The value is converted to RFC 3339 format.
func (wf *WarcFields) AddTime(name string, value time.Time) {
wf.Add(name, value.UTC().Format(time.RFC3339))
}
// AddId adds the key, value pair to the header.
// It appends to any existing values associated with key. The key is case-insensitive.
//
// The value is surrounded with '<' and '>' if not already present.
func (wf *WarcFields) AddId(name, value string) {
if len(value) == 0 {
return
}
if value[0] != '<' && value[len(value)-1] != '>' {
value = "<" + value + ">"
}
wf.Add(name, value)
}
// Set sets the header entries associated with key to the single element value.
// It replaces any existing values associated with key. The key is case-insensitive
func (wf *WarcFields) Set(name string, value string) {
name, _ = normalizeName(name)
isSet := false
for idx, nv := range *wf {
if nv.Name == name {
if isSet {
*wf = append((*wf)[:idx], (*wf)[idx+1:]...)
} else {
nv.Value = value
isSet = true
}
}
}
if !isSet {
*wf = append(*wf, &nameValue{Name: name, Value: value})
}
}
// SetInt sets the header entries associated with key to the single element value.
// It replaces any existing values associated with key. The key is case-insensitive
func (wf *WarcFields) SetInt(name string, value int) {
wf.Set(name, strconv.Itoa(value))
}
// SetInt64 sets the header entries associated with key to the single element value.
// It replaces any existing values associated with key. The key is case-insensitive
func (wf *WarcFields) SetInt64(name string, value int64) {
wf.Set(name, strconv.FormatInt(value, 10))
}
// SetTime sets the header entries associated with key to the single element value.
// It replaces any existing values associated with key. The key is case-insensitive
//
// The value is converted to RFC 3339 format.
func (wf *WarcFields) SetTime(name string, value time.Time) {
wf.Set(name, value.UTC().Format(time.RFC3339))
}
// SetId sets the header entries associated with key to the single element value.
// It replaces any existing values associated with key. The key is case-insensitive
//
// The value is surrounded with '<' and '>' if not already present.
func (wf *WarcFields) SetId(name, value string) {
if len(value) == 0 {
return
}
if value[0] != '<' && value[len(value)-1] != '>' {
value = "<" + value + ">"
}
wf.Set(name, value)
}
// Delete deletes the values associated with key. The key is case-insensitive.
func (wf *WarcFields) Delete(key string) {
key, _ = normalizeName(key)
var result []*nameValue
for _, nv := range *wf {
if nv.Name != key {
result = append(result, nv)
}
}
*wf = result
}
// Sort sorts the fields in lexicographical order.
//
// Only field names are sorted. Order of values for a repeated field is kept as is.
func (wf *WarcFields) Sort() {
sort.SliceStable(*wf, func(i, j int) bool {
return (*wf)[i].Name < (*wf)[j].Name
})
}
// Write implements the io.Writer interface.
func (wf *WarcFields) Write(w io.Writer) (bytesWritten int64, err error) {
var n int
for _, field := range *wf {
n, err = fmt.Fprintf(w, "%s: %s\r\n", field.Name, field.Value)
bytesWritten += int64(n)
if err != nil {
return
}
}
return
}
func (wf *WarcFields) String() string {
sb := &strings.Builder{}
if _, err := wf.Write(sb); err != nil {
panic(err)
}
return sb.String()
}
// clone creates a new deep copy.
func (wf WarcFields) clone() *WarcFields {
r := WarcFields{}
for _, p := range wf {
v := *p
v2 := v
r = append(r, &v2)
}
return &r
}