-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
221 lines (180 loc) · 5.44 KB
/
message.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
package httpsig
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/url"
"github.com/dunglas/httpsfv"
)
func MessageFromRequest(req *http.Request) *Message {
var (
getBody func() (io.ReadCloser, error)
bodySnapshot []byte
)
// for client requests req.Body can be nil, but not for server requests
switch {
case req.GetBody == nil && req.Body == nil:
getBody = func() (io.ReadCloser, error) { return http.NoBody, nil }
case req.GetBody != nil:
getBody = req.GetBody
default:
getBody = func() (io.ReadCloser, error) {
if len(bodySnapshot) != 0 {
return io.NopCloser(bytes.NewReader(bodySnapshot)), nil
}
save, newBody, err := drainBody(req.Body)
if err != nil {
return nil, err
}
req.Body = newBody
bodySnapshot = save
return io.NopCloser(bytes.NewReader(bodySnapshot)), nil
}
}
return &Message{
Method: req.Method,
Authority: req.Host,
URL: req.URL,
Header: req.Header.Clone(),
Body: getBody,
IsRequest: true,
Context: req.Context(),
}
}
func MessageFromResponse(rw *http.Response) *Message {
var bodySnapshot []byte
getResponseBody := func() (io.ReadCloser, error) {
if len(bodySnapshot) != 0 {
return io.NopCloser(bytes.NewReader(bodySnapshot)), nil
}
save, newBody, err := drainBody(rw.Body)
if err != nil {
return nil, err
}
rw.Body = newBody
bodySnapshot = save
return io.NopCloser(bytes.NewReader(bodySnapshot)), nil
}
var getRequestBody func() (io.ReadCloser, error)
if rw.Request.GetBody != nil {
getRequestBody = rw.Request.GetBody
} else {
getRequestBody = func() (io.ReadCloser, error) { return http.NoBody, nil }
}
return &Message{
Method: rw.Request.Method,
Authority: rw.Request.Host,
URL: rw.Request.URL,
Header: rw.Header.Clone(),
Body: getResponseBody,
StatusCode: rw.StatusCode,
RequestHeader: rw.Request.Header.Clone(),
RequestBody: getRequestBody,
IsRequest: false,
Context: rw.Request.Context(),
}
}
func MessageForResponse(req *http.Request, respHeader http.Header, body []byte, respCode int) *Message {
var bodySnapshot []byte
getBody := func() (io.ReadCloser, error) {
if len(bodySnapshot) != 0 {
return io.NopCloser(bytes.NewReader(bodySnapshot)), nil
}
save, newBody, err := drainBody(req.Body)
if err != nil {
return nil, err
}
req.Body = newBody
bodySnapshot = save
return io.NopCloser(bytes.NewReader(bodySnapshot)), nil
}
return &Message{
Method: req.Method,
Authority: req.Host,
URL: req.URL,
Header: respHeader.Clone(),
Body: func() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(body)), nil },
StatusCode: respCode,
RequestHeader: req.Header.Clone(),
RequestBody: getBody,
IsRequest: false,
Context: req.Context(),
}
}
// Message is a representation of an HTTP request or response, containing the values
// needed to construct or validate a signature.
type Message struct {
Context context.Context //nolint: containedctx
Method string
Authority string
URL *url.URL
Header http.Header
Body func() (io.ReadCloser, error)
RequestHeader http.Header
RequestBody func() (io.ReadCloser, error)
StatusCode int
IsRequest bool
}
func (m *Message) addSignature(label string, signature []byte, signatureInput httpsfv.InnerList) (http.Header, error) {
// check to see if there are already signature/signature-input headers
// if there are we want to store the current (case-sensitive) name of the header,
// and we want to parse out the current values, so we can append our new signature
var (
signatureHeaderDict *httpsfv.Dictionary
inputHeaderDict *httpsfv.Dictionary
err error
)
if signatureHeader := m.Header.Values(headerSignature); len(signatureHeader) != 0 {
signatureHeaderDict, err = httpsfv.UnmarshalDictionary(signatureHeader)
} else {
signatureHeaderDict = httpsfv.NewDictionary()
}
if inputHeader := m.Header.Values(headerSignatureInput); len(inputHeader) != 0 {
inputHeaderDict, err = httpsfv.UnmarshalDictionary(inputHeader)
} else {
inputHeaderDict = httpsfv.NewDictionary()
}
if err != nil {
return nil, err
}
label = createUniqueLabel(label, inputHeaderDict)
signatureHeaderDict.Add(label, httpsfv.NewItem(signature))
inputHeaderDict.Add(label, signatureInput)
marshalledSignatureHeader, err := httpsfv.Marshal(signatureHeaderDict)
if err != nil {
return nil, err
}
marshalledInputHeader, err := httpsfv.Marshal(inputHeaderDict)
if err != nil {
return nil, err
}
m.Header.Set(headerSignature, marshalledSignatureHeader)
m.Header.Set(headerSignatureInput, marshalledInputHeader)
return m.Header, nil
}
func createUniqueLabel(label string, dict *httpsfv.Dictionary) string {
uniqueLabel := label
_, labelPresent := dict.Get(uniqueLabel)
for count := 1; labelPresent; count++ {
uniqueLabel = fmt.Sprintf("%s%d", label, count)
_, labelPresent = dict.Get(uniqueLabel)
}
return uniqueLabel
}
func drainBody(body io.ReadCloser) ([]byte, io.ReadCloser, error) {
if body == nil || body == http.NoBody {
// No copying needed. Preserve the magic sentinel meaning of NoBody.
return nil, http.NoBody, nil
}
var buf bytes.Buffer
if _, err := buf.ReadFrom(body); err != nil {
return nil, body, err
}
if err := body.Close(); err != nil {
return nil, body, err
}
data := buf.Bytes()
return data, io.NopCloser(bytes.NewReader(data)), nil
}