-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathheader.go
More file actions
124 lines (96 loc) · 2.77 KB
/
header.go
File metadata and controls
124 lines (96 loc) · 2.77 KB
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
package icapclient
import (
"bytes"
"io/ioutil"
"net/http"
"os"
"strconv"
)
// SetPreview sets the preview bytes in the icap header
func (r *Request) SetPreview(maxBytes int) error {
bodyBytes := []byte{}
previewBytes := 0
// receiving the body bites to determine the preview bytes depending on the request ICAP method
if r.Method == MethodREQMOD {
if r.HTTPRequest == nil {
return nil
}
if r.HTTPRequest.Body != nil {
var err error
bodyBytes, err = ioutil.ReadAll(r.HTTPRequest.Body)
if err != nil {
return err
}
defer r.HTTPRequest.Body.Close()
}
}
if r.Method == MethodRESPMOD {
if r.HTTPResponse == nil {
return nil
}
if r.HTTPResponse.Body != nil {
var err error
bodyBytes, err = ioutil.ReadAll(r.HTTPResponse.Body)
if err != nil {
return err
}
defer r.HTTPResponse.Body.Close()
}
}
previewBytes = len(bodyBytes)
if previewBytes > 0 { // if the preview byte is 0 or less, there is no question of the body fitting insides
r.bodyFittedInPreview = true
}
if previewBytes > maxBytes { // if the preview bytes is greater than what was mentioned by the ICAP Server(did not fit in the body)
previewBytes = maxBytes
r.bodyFittedInPreview = false
r.remainingPreviewBytes = bodyBytes[maxBytes:] // storing the rest of the body byte which were not sent as preview for further operations
}
// returning the body back to the http message depending on the request method
if r.Method == MethodREQMOD {
r.HTTPRequest.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes))
}
if r.Method == MethodRESPMOD {
r.HTTPResponse.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes))
}
// finally assinging the preview informations including setting the header
r.Header.Set("Preview", strconv.Itoa(previewBytes))
r.PreviewBytes = previewBytes
r.previewSet = true
return nil
}
// SetDefaultRequestHeaders assigns some of the headers with its default value if they are not set already
func (r *Request) SetDefaultRequestHeaders() {
if _, exists := r.Header["Allow"]; !exists {
r.Header.Add("Allow", "204") // assigning 204 by default if Allow not provided
}
if _, exists := r.Header["Host"]; !exists {
hostName, _ := os.Hostname()
r.Header.Add("Host", hostName)
}
}
// ExtendHeader extends the current ICAP Request header with a new header
func (r *Request) ExtendHeader(hdr http.Header) error {
for header, values := range hdr {
if header == PreviewHeader && r.previewSet {
continue
}
if header == EncapsulatedHeader {
continue
}
for _, value := range values {
if header == PreviewHeader {
pb, err := strconv.Atoi(value)
if err != nil {
return err
}
if err := r.SetPreview(pb); err != nil {
return err
}
continue
}
r.Header.Add(header, value)
}
}
return nil
}