-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.go
More file actions
223 lines (194 loc) · 5.37 KB
/
decode.go
File metadata and controls
223 lines (194 loc) · 5.37 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
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
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package wrphttp
import (
"bufio"
"compress/flate"
"compress/gzip"
"compress/zlib"
"fmt"
"io"
"mime"
"mime/multipart"
"net/http"
"strings"
"github.com/tinylib/msgp/msgp"
"github.com/xmidt-org/wrp-go/v5"
)
// DecodeRequest converts an http.Request into the provided wrp messages if
// applicable. This will handle any of the valid forms the encoder can produce.
func DecodeRequest(req *http.Request, validators ...wrp.Processor) ([]wrp.Union, error) {
if req == nil {
return nil, fmt.Errorf("request is nil")
}
ct, _, err := mime.ParseMediaType(req.Header.Get("Content-Type"))
if err != nil {
return nil, err
}
if !strings.HasPrefix(ct, "multipart/") {
return fromPart(req.Header, req.Body, validators...)
}
mr, err := req.MultipartReader()
if err != nil {
return nil, err
}
var rv []wrp.Union
for {
part, err := mr.NextPart()
if err == io.EOF { // nolint: errorlint
return rv, nil
}
if err != nil {
return nil, err
}
msgs, err := fromPart(http.Header(part.Header), part, validators...)
if err != nil {
return nil, err
}
rv = append(rv, msgs...)
}
}
// DecodeResponse converts an http.Response into the provided wrp messages if
// applicable. This will handle any of the valid forms the encoder can produce.
func DecodeResponse(resp *http.Response, validators ...wrp.Processor) ([]wrp.Union, error) {
if resp == nil {
return nil, fmt.Errorf("response is nil")
}
return DecodeFromParts(resp.Header, resp.Body, validators...)
}
// DecodeFromParts converts an http.Header and io.ReadCloser into the provided wrp
// messages if applicable. This will handle any of the valid forms the encoder
// can produce.
func DecodeFromParts(headers http.Header, body io.ReadCloser, validators ...wrp.Processor) ([]wrp.Union, error) {
mediaType, params, err := mime.ParseMediaType(headers.Get("Content-Type"))
if err != nil {
body.Close()
return nil, fmt.Errorf("invalid Content-Type: %w", err)
}
if !strings.HasPrefix(mediaType, "multipart/") {
return fromPart(headers, body, validators...)
}
defer body.Close()
boundary := params["boundary"]
if boundary == "" {
return nil, fmt.Errorf("missing boundary in Content-Type: %s", headers.Get("Content-Type"))
}
if mediaType != "multipart/mixed" {
return nil, fmt.Errorf("unsupported media type: %s", mediaType)
}
mr := multipart.NewReader(body, boundary)
var rv []wrp.Union
for {
part, err := mr.NextPart()
if err == io.EOF { // nolint: errorlint
return rv, nil
}
if err != nil {
return nil, err
}
defer part.Close()
msgs, err := fromPart(http.Header(part.Header), part, validators...)
if err != nil {
return nil, err
}
rv = append(rv, msgs...)
}
}
func handleEncoding(h http.Header, body io.ReadCloser) (io.ReadCloser, error) {
et := h.Get("Content-Encoding")
switch et {
case "gzip":
return gzip.NewReader(body)
case "deflate":
return flate.NewReader(body), nil
case "zlib":
return zlib.NewReader(body)
case "identity", "":
return body, nil
default:
}
return nil, fmt.Errorf("unsupported content encoding: %s", et)
}
func fromPart(h http.Header, body io.ReadCloser, validators ...wrp.Processor) ([]wrp.Union, error) {
var err error
if body != nil {
defer body.Close()
}
body, err = handleEncoding(h, body)
if err != nil {
return nil, err
}
if body != nil {
defer body.Close()
}
ct, err := toMediaTypeFromMime(h.Get("Content-Type"))
if err != nil {
return nil, err
}
switch ct {
case mtJSON:
return fromFormat(wrp.JSON, body, validators...)
case mtMsgpack:
return fromFormat(wrp.Msgpack, body, validators...)
case mtOctetStream, mtOctetStreamXWebpa, mtOctetStreamXXmidt, mtOctetStreamXMidt, mtOctetStreamXmidt:
return fromOctetStream(h, body, validators...)
case mtJSONL:
return fromJSONL(body, validators...)
case mtMsgpackL:
return fromMsgpackL(body, validators...)
}
// Unreachable.
return nil, fmt.Errorf("unsupported media type: %s", ct)
}
func fromFormat(f wrp.Format, body io.ReadCloser, validators ...wrp.Processor) ([]wrp.Union, error) {
var msg wrp.Message
if err := f.Decoder(body).Decode(&msg, validators...); err != nil {
return nil, err
}
return []wrp.Union{&msg}, nil
}
func fromOctetStream(h http.Header, body io.ReadCloser, validators ...wrp.Processor) ([]wrp.Union, error) {
msg, err := fromHeaders(h, body, validators...)
if err != nil {
return nil, err
}
return []wrp.Union{msg}, nil
}
func fromJSONL(body io.ReadCloser, validators ...wrp.Processor) ([]wrp.Union, error) {
var msgs []wrp.Union
scanner := bufio.NewScanner(body)
for scanner.Scan() {
var msg wrp.Message
line := scanner.Bytes()
if err := wrp.JSON.DecoderBytes(line).Decode(&msg, validators...); err != nil {
return nil, err
}
msgs = append(msgs, &msg)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return msgs, nil
}
func fromMsgpackL(body io.ReadCloser, validators ...wrp.Processor) ([]wrp.Union, error) {
var msgs []wrp.Union
r := msgp.NewReader(body)
count, err := r.ReadArrayHeader()
if err != nil {
return nil, err
}
var i uint32
for ; i < count; i++ {
var msg wrp.Message
var item []byte
item, err = r.ReadBytes(nil)
if err == nil {
err = wrp.Msgpack.DecoderBytes(item).Decode(&msg, validators...)
}
if err != nil {
return nil, err
}
msgs = append(msgs, &msg)
}
return msgs, nil
}