-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.go
359 lines (319 loc) · 11.7 KB
/
header.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package go_requests
import (
"bytes"
"fmt"
"net/http"
"strconv"
)
// HeaderType is the type of the header. It is used to set the header value to the header object using the Set method.
type HeaderType string
const (
//HeaderTypeAccept is the Accept header
HeaderTypeAccept HeaderType = "Accept"
//HeaderTypeAcceptCharset is the Accept-Charset header
HeaderTypeAcceptCharset HeaderType = "Accept-Charset"
//HeaderTypeAcceptEncoding is the Accept-Encoding header
HeaderTypeAcceptEncoding HeaderType = "Accept-Encoding"
//HeaderTypeAcceptLanguage is the Accept-Language header
HeaderTypeAcceptLanguage HeaderType = "Accept-Language"
//HeaderTypeAcceptRanges is the Accept-Ranges header
HeaderTypeAcceptRanges HeaderType = "Accept-Ranges"
//HeaderTypeAge is the Age header
HeaderTypeAge HeaderType = "Age"
//HeaderTypeAllow is the Allow header
HeaderTypeAllow HeaderType = "Allow"
//HeaderTypeContentDisposition is the Content-Disposition header
HeaderTypeContentDisposition HeaderType = "Content-Disposition"
//HeaderTypeContentEncoding is the Content-Encoding header
HeaderTypeContentEncoding HeaderType = "Content-Encoding"
//HeaderTypeContentLanguage is the Content-Language header
HeaderTypeContentLanguage HeaderType = "Content-Language"
//HeaderTypeContentLength is the Content-Length header
HeaderTypeContentLength HeaderType = "Content-Length"
//HeaderTypeContentLocation is the Content-Location header
HeaderTypeContentLocation HeaderType = "Content-Location"
//HeaderTypeContentMD5 is the Content-MD5 header
HeaderTypeContentMD5 HeaderType = "Content-MD5"
//HeaderTypeContentRange is the Content-Range header
HeaderTypeContentRange HeaderType = "Content-Range"
//HeaderTypeContentType is the Content-Type header
HeaderTypeContentType HeaderType = "Content-Type"
//HeaderTypeCookie is the Cookie header
HeaderTypeCookie HeaderType = "Cookie"
//HeaderTypeDate is the Date header
HeaderTypeDate HeaderType = "Date"
//HeaderTypeETag is the ETag header
HeaderTypeETag HeaderType = "ETag"
//HeaderTypeExpires is the Expires header
HeaderTypeExpires HeaderType = "Expires"
)
// Headers is the interface for the http headers object of the http package of the standard library of Go (golang)
type Headers interface {
// Set sets a header to the header
Set(key, value string) Headers
// SetContentType sets the content type to the header
SetContentType(contentType string) Headers
// SetContentLength sets the content length to the header
SetContentLength(contentLength int) Headers
// SetContentDisposition sets the content disposition to the header
SetContentDisposition(contentDisposition string) Headers
// SetContentEncoding sets the content encoding to the header
SetContentEncoding(contentEncoding string) Headers
// SetContentLanguage sets the content language to the header
SetContentLanguage(contentLanguage string) Headers
// SetContentLocation sets the content location to the header
SetContentLocation(contentLocation string) Headers
// SetContentMD5 sets the content md5 to the header
SetContentMD5(contentMD5 string) Headers
// SetContentRange sets the content range to the header
SetContentRange(contentRange string) Headers
// SetCookie sets the cookie to the header
SetCookie(cookie string) Headers
// SetDate sets the date to the header
SetDate(date string) Headers
// SetETag sets the etag to the header
SetETag(etag string) Headers
// SetExpires sets the expires to the header
SetExpires(expires string) Headers
// SetAccept sets to accept to the header
SetAccept(accept string) Headers
// SetAcceptCharset sets to accept charset to the header
SetAcceptCharset(acceptCharset string) Headers
// SetAcceptEncoding sets to accept encoding to the header
SetAcceptEncoding(acceptEncoding string) Headers
// SetAcceptLanguage sets to accept language to the header
SetAcceptLanguage(acceptLanguage string) Headers
// SetAcceptRanges sets to accept ranges to the header
SetAcceptRanges(acceptRanges string) Headers
// SetAge sets the age to the header
SetAge(age string) Headers
// SetAllow sets to allow to the header
SetAllow(allow string) Headers
// SetCustom sets a custom header to the header
SetCustom(key, value string) Headers
// Get returns the value of the header
Get(key string) string
// Del deletes a header from the header
Del(key string) Headers
// Clone returns a clone of the header
Clone() Headers
// IsEmpty returns true if the header is empty
IsEmpty() bool
// IsSet returns true if the header is set
IsSet() bool
// String returns the string representation of the header
String() string
// Values returns the values of the header
Values() map[string]string
// Keys returns the keys of the header
Keys() []string
// Len returns the length of the header
Len() int
// GetAll returns all Headers with key and value
GetAll() map[string][]string
//GetAllHttpHeaders returns all http headers as http.Header object
GetAllHttpHeaders() http.Header
//SetUserAgent sets the user agent to the header.
SetUserAgent(userAgent string)
}
// headerImpl is the implementation of the Headers interface
type headerImpl struct {
values map[string]string
}
// Set sets a header to the header object
// key is the key of the header
// value is the value of the header
// returns the header object
//
// Example:
//
// header := NewHeaders()
// header.Set("Content-Type", "application/json")
// header.Set("Content-Length", "100")
func (h *headerImpl) Set(key, value string) Headers {
h.values[key] = value
return h
}
// SetContentType sets the content type to the header object
func (h *headerImpl) SetContentType(contentType string) Headers {
h.values[string(HeaderTypeContentType)] = contentType
return h
}
// SetContentLength sets the content length to the header object
func (h *headerImpl) SetContentLength(contentLength int) Headers {
h.values[string(HeaderTypeContentLength)] = strconv.Itoa(contentLength)
return h
}
// SetContentDisposition sets the content disposition to the header object
func (h *headerImpl) SetContentDisposition(contentDisposition string) Headers {
h.values[string(HeaderTypeContentDisposition)] = contentDisposition
return h
}
// SetContentEncoding sets the content encoding to the header object
func (h *headerImpl) SetContentEncoding(contentEncoding string) Headers {
h.values[string(HeaderTypeContentEncoding)] = contentEncoding
return h
}
// SetContentLanguage sets the content language to the header object
func (h *headerImpl) SetContentLanguage(contentLanguage string) Headers {
h.values[string(HeaderTypeContentLanguage)] = contentLanguage
return h
}
// SetContentLocation sets the content location to the header object
func (h *headerImpl) SetContentLocation(contentLocation string) Headers {
h.values[string(HeaderTypeContentLocation)] = contentLocation
return h
}
// SetContentMD5 sets the content md5 to the header object
func (h *headerImpl) SetContentMD5(contentMD5 string) Headers {
h.values[string(HeaderTypeContentMD5)] = contentMD5
return h
}
// SetContentRange sets the content range to the header object. The content range is a string in the format "bytes 0-100/1000"
func (h *headerImpl) SetContentRange(contentRange string) Headers {
h.values[string(HeaderTypeContentRange)] = contentRange
return h
}
// SetCookie sets the cookie to the header object
func (h *headerImpl) SetCookie(cookie string) Headers {
h.values[string(HeaderTypeCookie)] = cookie
return h
}
// SetDate sets the date to the header object
func (h *headerImpl) SetDate(date string) Headers {
h.values[string(HeaderTypeDate)] = date
return h
}
// SetETag sets the etag to the header object
func (h *headerImpl) SetETag(etag string) Headers {
h.values[string(HeaderTypeETag)] = etag
return h
}
// SetExpires sets the expires to the header object
func (h *headerImpl) SetExpires(expires string) Headers {
h.values[string(HeaderTypeExpires)] = expires
return h
}
// SetAccept sets to accept to the header object
func (h *headerImpl) SetAccept(accept string) Headers {
h.values[string(HeaderTypeAccept)] = accept
return h
}
// SetAcceptCharset sets to accept charset to the header object
func (h *headerImpl) SetAcceptCharset(acceptCharset string) Headers {
h.values[string(HeaderTypeAcceptCharset)] = acceptCharset
return h
}
// SetAcceptEncoding sets to accept encoding to the header object
func (h *headerImpl) SetAcceptEncoding(acceptEncoding string) Headers {
h.values[string(HeaderTypeAcceptEncoding)] = acceptEncoding
return h
}
// SetAcceptLanguage sets to accept language to the header object
func (h *headerImpl) SetAcceptLanguage(acceptLanguage string) Headers {
h.values[string(HeaderTypeAcceptLanguage)] = acceptLanguage
return h
}
// SetAcceptRanges sets to accept ranges to the header object
func (h *headerImpl) SetAcceptRanges(acceptRanges string) Headers {
h.values[string(HeaderTypeAcceptRanges)] = acceptRanges
return h
}
// SetAge sets the age to the header object
func (h *headerImpl) SetAge(age string) Headers {
h.values[string(HeaderTypeAge)] = age
return h
}
// SetAllow sets to allow to the header object
func (h *headerImpl) SetAllow(allow string) Headers {
h.values[string(HeaderTypeAllow)] = allow
return h
}
// SetCustom sets a custom header to the header object
func (h *headerImpl) SetCustom(key, value string) Headers {
h.values[key] = value
return h
}
// Get returns the value of the header
func (h *headerImpl) Get(key string) string {
return h.values[key]
}
// SetUserAgent sets the user agent to the header. If the user agent is empty, it will be set to the default user agent.
func (h *headerImpl) SetUserAgent(s string) {
h.Set("User-Agent", s)
}
// GetAllHttpHeaders returns all http headers as http.Header object
func (h *headerImpl) GetAllHttpHeaders() http.Header {
headers := http.Header{}
for k, v := range h.values {
headers.Set(k, v)
}
return headers
}
// Del deletes a header from the header object
func (h *headerImpl) Del(key string) Headers {
delete(h.values, key)
return h
}
// Clone clones the header object
func (h *headerImpl) Clone() Headers {
clone := make(map[string]string)
for k, v := range h.values {
clone[k] = v
}
return &headerImpl{values: clone}
}
// IsEmpty checks if the header object is empty
// returns true if the header object is empty, otherwise false
func (h *headerImpl) IsEmpty() bool {
return len(h.values) == 0
}
// IsSet checks if the header object has a header with the given key
func (h *headerImpl) IsSet() bool {
return !h.IsEmpty()
}
// GetAllHttpHeaders returns all http headers as http.Header object
func (h *headerImpl) String() string {
var buffer bytes.Buffer
for k, v := range h.values {
buffer.WriteString(fmt.Sprintf("%s: %s", k, v))
}
return buffer.String()
}
// Values returns the values of the header object
func (h *headerImpl) Values() map[string]string {
return h.values
}
// Keys returns the keys of the header object
func (h *headerImpl) Keys() []string {
var keys []string
for k := range h.values {
keys = append(keys, k)
}
return keys
}
// Len returns the length of the header object
func (h *headerImpl) Len() int {
return len(h.values)
}
// GetAll returns all headers as map of string and string array object (key, value) pairs of the header object as map
func (h *headerImpl) GetAll() map[string][]string {
all := make(map[string][]string)
for k, v := range h.values {
all[k] = []string{v}
}
return all
}
// NewHeaders returns a new header object
func NewHeaders() Headers {
return &headerImpl{values: make(map[string]string)}
}
// getHeader returns the header object of the ...http.Header object ad http.Header object.
// If the header object is nil, it returns a new header object
// If the header object is not nil, it returns the header object
func getHeader(headers ...http.Header) http.Header {
if len(headers) == 0 {
return http.Header{}
}
return headers[0]
}