-
Notifications
You must be signed in to change notification settings - Fork 113
/
decoder.go
658 lines (578 loc) · 20.7 KB
/
decoder.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
package bundle
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"github.com/WICG/webpackage/go/bundle/version"
"github.com/WICG/webpackage/go/internal/cbor"
"github.com/WICG/webpackage/go/signedexchange/certurl"
)
type requestEntryWithOffset struct {
Request
Length uint64
Offset uint64 // Offset within the bundle stream
}
type sectionOffset struct {
Name string
Length uint64
}
func FindSection(sos []sectionOffset, name string) (sectionOffset, uint64, bool) {
offset := uint64(0)
for _, e := range sos {
if name == e.Name {
return e, offset, true
}
offset += e.Length
}
return sectionOffset{}, 0, false
}
type meta struct {
version version.Version
primaryURL *url.URL
sectionOffsets []sectionOffset
sectionsStart uint64
manifestURL *url.URL
signatures *Signatures
requests []requestEntryWithOffset
}
func decodeSectionLengthsCBOR(bs []byte) ([]sectionOffset, error) {
// section-lengths = [* (section-name: tstr, length: uint) ],
sos := []sectionOffset{}
dec := cbor.NewDecoder(bytes.NewBuffer(bs))
n, err := dec.DecodeArrayHeader()
if err != nil {
return nil, fmt.Errorf("bundle: Failed to decode sectionOffset array header: %v", err)
}
for i := uint64(0); i < n; i += 2 {
name, err := dec.DecodeTextString()
if err != nil {
return nil, fmt.Errorf("bundle.sectionLengths[%d]: Failed to decode sectionOffset name: %v", i, err)
}
if _, _, exists := FindSection(sos, name); exists {
return nil, fmt.Errorf("bundle.sectionLengths[%d]: Duplicate section in sectionOffset array: %q", i, name)
}
length, err := dec.DecodeUint()
if err != nil {
return nil, fmt.Errorf("bundle.sectionLengths[%d]: Failed to decode sectionOffset[%q].length: %v", i, name, err)
}
sos = append(sos, sectionOffset{Name: name, Length: length})
}
return sos, nil
}
var reIsAscii = regexp.MustCompile("^[[:ascii:]]*$")
func isAscii(s string) bool {
return reIsAscii.MatchString(s)
}
// https://wicg.github.io/webpackage/draft-yasskin-dispatch-bundled-exchanges.html#cbor-headers
func decodeCborHeaders(dec *cbor.Decoder) (http.Header, map[string]string, error) {
n, err := dec.DecodeMapHeader()
if err != nil {
return nil, nil, fmt.Errorf("Failed to decode request headers map header: %v", err)
}
headers := make(http.Header)
pseudos := make(map[string]string)
for j := uint64(0); j < n; j++ {
namebs, err := dec.DecodeByteString()
if err != nil {
return nil, nil, fmt.Errorf("Failed to decode request headers map key: %v", err)
}
valuebs, err := dec.DecodeByteString()
if err != nil {
return nil, nil, fmt.Errorf("Failed to decode request headers map value: %v", err)
}
name := string(namebs)
value := string(valuebs)
if !isAscii(name) {
return nil, nil, fmt.Errorf("Failed to decode request headers map key: non-ascii %q", name)
}
if !isAscii(value) {
return nil, nil, fmt.Errorf("Failed to decode request headers map value: non-ascii %q", value)
}
if strings.ToLower(name) != name {
return nil, nil, fmt.Errorf("Failed to decode request headers map key: %q contains upper-case.", name)
}
if strings.HasPrefix(name, ":") {
if _, exists := pseudos[name]; exists {
return nil, nil, fmt.Errorf("Failed to decode request headers map entry. Pseudo %q appeared twice.", name)
}
pseudos[name] = value
continue
}
if _, exists := headers[name]; exists {
return nil, nil, fmt.Errorf("Failed to decode request headers map entry. Header %q appeared twice.", name)
}
headers.Set(name, value)
}
return headers, pseudos, nil
}
// https://wpack-wg.github.io/bundled-responses/draft-ietf-wpack-bundled-responses.html#name-the-index-section
func parseIndexSection(sectionContents []byte, sectionsStart uint64, sos []sectionOffset) ([]requestEntryWithOffset, error) {
dec := cbor.NewDecoder(bytes.NewBuffer(sectionContents))
numUrls, err := dec.DecodeMapHeader()
if err != nil {
return nil, fmt.Errorf("bundle.index: failed to decode index section map header: %v", err)
}
requests := []requestEntryWithOffset{}
respso, respSectionRelOffset, found := FindSection(sos, "responses")
if !found {
return nil, fmt.Errorf("bundle.index: \"responses\" section not found")
}
respSectionOffset := sectionsStart + respSectionRelOffset
makeRelativeToStream := func(offset, length uint64) (uint64, uint64, error) {
if offset+length > respso.Length {
return 0, 0, errors.New("bundle.index: response length out-of-range")
}
return respSectionOffset + offset, length, nil
}
for i := uint64(0); i < numUrls; i++ {
rawUrl, err := dec.DecodeTextString()
if err != nil {
return nil, fmt.Errorf("bundle.index[%d]: Failed to decode map key: %v", i, err)
}
parsedUrl, err := url.Parse(rawUrl)
if err != nil {
return nil, fmt.Errorf("bundle.index[%d]: Failed to parse URL: %v", i, err)
}
if parsedUrl.Fragment != "" {
return nil, fmt.Errorf("bundle.index[%d]: URL contains fragment: %q", i, rawUrl)
}
if parsedUrl.User != nil {
return nil, fmt.Errorf("bundle.index[%d]: URL contains credentials: %q", i, rawUrl)
}
numItems, err := dec.DecodeArrayHeader()
if err != nil {
return nil, fmt.Errorf("bundle.index[%d]: Failed to decode value array header: %v", i, err)
}
if numItems != 2 {
return nil, fmt.Errorf("bundle.index[%d]: value array must be exactly 2 elements: offset and length.", i)
}
offset, err := dec.DecodeUint()
if err != nil {
return nil, fmt.Errorf("bundle.index[%d]: Failed to decode offset: %v", i, err)
}
length, err := dec.DecodeUint()
if err != nil {
return nil, fmt.Errorf("bundle.index[%d]: Failed to decode length: %v", i, err)
}
offset, length, err = makeRelativeToStream(offset, length)
if err != nil {
return nil, fmt.Errorf("bundle.index[%d]: %v", i, err)
}
requests = append(requests, requestEntryWithOffset{Request: Request{URL: parsedUrl}, Offset: offset, Length: length})
}
return requests, nil
}
// https://wpack-wg.github.io/bundled-responses/draft-ietf-wpack-bundled-responses.html#name-the-index-section
func parseIndexSectionWithVariants(sectionContents []byte, sectionsStart uint64, sos []sectionOffset) ([]requestEntryWithOffset, error) {
dec := cbor.NewDecoder(bytes.NewBuffer(sectionContents))
numUrls, err := dec.DecodeMapHeader()
if err != nil {
return nil, fmt.Errorf("bundle.index: failed to decode index section map header: %v", err)
}
requests := []requestEntryWithOffset{}
respso, respSectionRelOffset, found := FindSection(sos, "responses")
if !found {
return nil, fmt.Errorf("bundle.index: \"responses\" section not found")
}
respSectionOffset := sectionsStart + respSectionRelOffset
makeRelativeToStream := func(offset, length uint64) (uint64, uint64, error) {
if offset+length > respso.Length {
return 0, 0, errors.New("bundle.index: response length out-of-range")
}
return respSectionOffset + offset, length, nil
}
for i := uint64(0); i < numUrls; i++ {
rawUrl, err := dec.DecodeTextString()
if err != nil {
return nil, fmt.Errorf("bundle.index[%d]: Failed to decode map key: %v", i, err)
}
parsedUrl, err := url.Parse(rawUrl)
if err != nil {
return nil, fmt.Errorf("bundle.index[%d]: Failed to parse URL: %v", i, err)
}
if parsedUrl.Fragment != "" {
return nil, fmt.Errorf("bundle.index[%d]: URL contains fragment: %q", i, rawUrl)
}
if parsedUrl.User != nil {
return nil, fmt.Errorf("bundle.index[%d]: URL contains credentials: %q", i, rawUrl)
}
numItems, err := dec.DecodeArrayHeader()
if err != nil {
return nil, fmt.Errorf("bundle.index[%d]: Failed to decode value array header: %v", i, err)
}
if numItems == 0 {
return nil, fmt.Errorf("bundle.index[%d]: value array must not be empty.", i)
}
variants_value, err := dec.DecodeByteString()
if err != nil {
return nil, fmt.Errorf("bundle.index[%d]: Failed to decode variants-value: %v", i, err)
}
if len(variants_value) == 0 {
if numItems != 3 {
return nil, fmt.Errorf("bundle.index[%d]: The size of value array must be 3", i)
}
offset, err := dec.DecodeUint()
if err != nil {
return nil, fmt.Errorf("bundle.index[%d]: Failed to decode offset: %v", i, err)
}
length, err := dec.DecodeUint()
if err != nil {
return nil, fmt.Errorf("bundle.index[%d]: Failed to decode length: %v", i, err)
}
offset, length, err = makeRelativeToStream(offset, length)
if err != nil {
return nil, fmt.Errorf("bundle.index[%d]: %v", i, err)
}
requests = append(requests, requestEntryWithOffset{Request: Request{URL: parsedUrl}, Offset: offset, Length: length})
} else {
variants, err := parseVariants(string(variants_value))
if err != nil {
return nil, fmt.Errorf("bundle.index[%d]: Cannot parse variants value %q: %v", i, string(variants_value), err)
}
numVariantKeys, err := variants.numberOfPossibleKeys()
if err != nil {
return nil, fmt.Errorf("bundle.index[%d]: Invalid variants value %q: %v", i, string(variants_value), err)
}
if numItems != 2*uint64(numVariantKeys)+1 {
return nil, fmt.Errorf("bundle.index[%d]: Unexpected size of value array: %d", i, numItems)
}
// Currently this implementation just appends all entries to `requests`.
// TODO: Preserve the map structure from variant-key to location-in-stream.
for j := 0; j < numVariantKeys; j++ {
offset, err := dec.DecodeUint()
if err != nil {
return nil, fmt.Errorf("bundle.index[%d][%d]: Failed to decode offset: %v", i, j, err)
}
length, err := dec.DecodeUint()
if err != nil {
return nil, fmt.Errorf("bundle.index[%d][%d]: Failed to decode length: %v", i, j, err)
}
offset, length, err = makeRelativeToStream(offset, length)
if err != nil {
return nil, fmt.Errorf("bundle.index[%d][%d]: %v", i, j, err)
}
requests = append(requests, requestEntryWithOffset{Request: Request{URL: parsedUrl}, Offset: offset, Length: length})
}
}
}
return requests, nil
}
// The "primary" section records a single URL identifying the primary URL of the bundle. The URL MUST refer to a resource with representations contained in the bundle itself.
func parsePrimarySection(sectionContents []byte) (*url.URL, error) {
dec := cbor.NewDecoder(bytes.NewBuffer(sectionContents))
urlString, err := dec.DecodeTextString()
if err != nil {
return nil, fmt.Errorf("bundle: failed to parse primary section: %v", err)
}
primaryURL, err := url.Parse(urlString)
// If url is a failure, its fragment is not null, or it includes credentials, return an error.
if err != nil {
return nil, fmt.Errorf("bundle: failed to parse primary URL (%s): %v", urlString, err)
}
if !primaryURL.IsAbs() || primaryURL.Fragment != "" || primaryURL.User != nil {
return nil, fmt.Errorf("bundle: primary URL (%s) must be an absolute url without fragment or credentials.", urlString)
}
return primaryURL, nil
}
// https://wpack-wg.github.io/bundled-responses/draft-ietf-wpack-bundled-responses.html#manifest-section
func parseManifestSection(sectionContents []byte) (*url.URL, error) {
dec := cbor.NewDecoder(bytes.NewBuffer(sectionContents))
urlString, err := dec.DecodeTextString()
if err != nil {
return nil, fmt.Errorf("bundle: failed to parse manifest section: %v", err)
}
manifestURL, err := url.Parse(urlString)
if err != nil {
return nil, fmt.Errorf("bundle: failed to parse manifest URL (%s): %v", urlString, err)
}
if !manifestURL.IsAbs() || manifestURL.Fragment != "" || manifestURL.User != nil {
return nil, fmt.Errorf("bundle: manifest URL (%s) must be an absolute url without fragment or credentials.", urlString)
}
return manifestURL, nil
}
// https://wpack-wg.github.io/bundled-responses/draft-ietf-wpack-bundled-responses.html#signatures-section
func parseSignaturesSection(sectionContents []byte) (*Signatures, error) {
// signatures = [
// authorities: [*authority],
// vouched-subsets: [*{
// authority: index-in-authorities,
// sig: bstr,
// signed: bstr ; Expected to hold a signed-subset item.
// }],
// ]
dec := cbor.NewDecoder(bytes.NewBuffer(sectionContents))
signaturesLength, err := dec.DecodeArrayHeader()
if err != nil {
return nil, fmt.Errorf("bundle.signatures: failed to decode array header: %v", err)
}
if signaturesLength != 2 {
return nil, fmt.Errorf("bundle.signatures: unexpected array length: %d", signaturesLength)
}
authoritiesLength, err := dec.DecodeArrayHeader()
if err != nil {
return nil, fmt.Errorf("bundle.signatures: failed to decode array header: %v", err)
}
var authorities []*certurl.AugmentedCertificate
for i := uint64(0); i < authoritiesLength; i++ {
a, err := certurl.DecodeAugmentedCertificateFrom(dec)
if err != nil {
return nil, fmt.Errorf("bundle.signatures: cannot parse certificate: %v", err)
}
authorities = append(authorities, a)
}
vouchedSubsetsLength, err := dec.DecodeArrayHeader()
if err != nil {
return nil, fmt.Errorf("bundle.signatures: failed to decode array header: %v", err)
}
var vouchedSubsets []*VouchedSubset
for i := uint64(0); i < vouchedSubsetsLength; i++ {
n, err := dec.DecodeMapHeader()
if err != nil {
return nil, fmt.Errorf("bundle.signatures: cannot decode map header: %v", err)
}
if n != 3 {
return nil, fmt.Errorf("bundle.signatures: unexpected map size: %d", n)
}
vs := &VouchedSubset{}
for i := uint64(0); i < n; i++ {
label, err := dec.DecodeTextString()
if err != nil {
return nil, fmt.Errorf("bundle.signatures: cannot decode map key: %v", err)
}
switch label {
case "authority":
vs.Authority, err = dec.DecodeUint()
if err != nil {
return nil, fmt.Errorf("bundle.signatures: cannot decode authority: %v", err)
}
case "sig":
vs.Sig, err = dec.DecodeByteString()
if err != nil {
return nil, fmt.Errorf("bundle.signatures: cannot decode sig: %v", err)
}
case "signed":
vs.Signed, err = dec.DecodeByteString()
if err != nil {
return nil, fmt.Errorf("bundle.signatures: cannot decode signed: %v", err)
}
default:
return nil, fmt.Errorf("bundle.signatures: unexpected map key %q", label)
}
}
vouchedSubsets = append(vouchedSubsets, vs)
}
return &Signatures{
Authorities: authorities,
VouchedSubsets: vouchedSubsets,
}, nil
}
var knownSections = map[string]struct{}{
"index": {},
"manifest": {},
"primary": {},
"signatures": {},
"responses": {},
}
type MetadataErrorType int
const (
FormatError MetadataErrorType = iota
VersionError
)
type LoadMetadataError struct {
error
Type MetadataErrorType
FallbackURL *url.URL
}
// https://wicg.github.io/webpackage/draft-yasskin-dispatch-bundled-exchanges.html#load-metadata
func loadMetadata(bs []byte) (*meta, error) {
r := bytes.NewBuffer(bs)
ver, err := version.ParseMagicBytes(r)
// TODO(ksakamoto): Continue and return VersionError after parsing fallbackUrl.
if err != nil {
return nil, &LoadMetadataError{err, FormatError, nil}
}
var fallbackURL *url.URL
dec := cbor.NewDecoder(r)
if ver.HasPrimaryURLFieldInHeader() {
fallbackURLBytes, err := dec.DecodeTextString()
if err != nil {
return nil, &LoadMetadataError{fmt.Errorf("bundle: Failed to read fallbackURL string: %v", err), FormatError, nil}
}
fallbackURL, err = url.Parse(fallbackURLBytes)
if err != nil {
return nil, &LoadMetadataError{fmt.Errorf("bundle: Failed to parse fallbackURL: %v", err), FormatError, nil}
}
}
slbytes, err := dec.DecodeByteString()
if err != nil {
return nil, &LoadMetadataError{fmt.Errorf("bundle: Failed to read sectionLengths byte string: %v", err), FormatError, fallbackURL}
}
if len(slbytes) >= 8192 {
return nil, &LoadMetadataError{fmt.Errorf("bundle: sectionLengthsLength is too long (%d bytes)", slbytes), FormatError, fallbackURL}
}
sos, err := decodeSectionLengthsCBOR(slbytes)
if err != nil {
return nil, &LoadMetadataError{err, FormatError, fallbackURL}
}
numSections, err := dec.DecodeArrayHeader()
if err != nil {
return nil, &LoadMetadataError{fmt.Errorf("bundle: Failed to read section header."), FormatError, fallbackURL}
}
if numSections != uint64(len(sos)) {
return nil, &LoadMetadataError{fmt.Errorf("bundle: Expected %d sections, got %d sections", len(sos), numSections), FormatError, fallbackURL}
}
sectionsStart := uint64(len(bs) - r.Len())
if len(sos) == 0 || sos[len(sos)-1].Name != "responses" {
return nil, &LoadMetadataError{fmt.Errorf("bundle: Last section is not \"responses\""), FormatError, fallbackURL}
}
meta := &meta{
version: ver,
primaryURL: fallbackURL,
sectionOffsets: sos,
sectionsStart: sectionsStart,
}
offset := sectionsStart
for _, so := range sos {
if _, exists := knownSections[so.Name]; !exists {
continue
}
if so.Name == "responses" {
continue
}
if uint64(len(bs)) <= offset {
return nil, &LoadMetadataError{fmt.Errorf("bundle: section %q's computed offset %q out-of-range.", so.Name, offset), FormatError, fallbackURL}
}
end := offset + so.Length
if uint64(len(bs)) <= end {
return nil, &LoadMetadataError{fmt.Errorf("bundle: section %q's end %q out-of-range.", so.Name, end), FormatError, fallbackURL}
}
sectionContents := bs[offset:end]
switch so.Name {
case "index":
if ver.SupportsVariants() {
requests, err := parseIndexSectionWithVariants(sectionContents, sectionsStart, sos)
if err != nil {
return nil, &LoadMetadataError{err, FormatError, fallbackURL}
}
meta.requests = requests
} else {
requests, err := parseIndexSection(sectionContents, sectionsStart, sos)
if err != nil {
return nil, &LoadMetadataError{err, FormatError, fallbackURL}
}
meta.requests = requests
}
case "primary":
primaryURL, err := parsePrimarySection(sectionContents)
if err != nil {
return nil, &LoadMetadataError{err, FormatError, fallbackURL}
}
fallbackURL = primaryURL
meta.primaryURL = primaryURL
case "manifest":
manifestURL, err := parseManifestSection(sectionContents)
if err != nil {
return nil, &LoadMetadataError{err, FormatError, fallbackURL}
}
meta.manifestURL = manifestURL
case "signatures":
if ver.SupportsSignatures() {
signatures, err := parseSignaturesSection(sectionContents)
if err != nil {
return nil, &LoadMetadataError{err, FormatError, fallbackURL}
}
meta.signatures = signatures
} else {
return nil, &LoadMetadataError{errors.New("bundle: signatures section not allowed in this version of bundle"), FormatError, fallbackURL}
}
case "responses":
continue
default:
return nil, &LoadMetadataError{fmt.Errorf("bundle: unknown section: %q", so.Name), FormatError, fallbackURL}
}
offset = end
}
return meta, nil
}
var reStatus = regexp.MustCompile("^\\d\\d\\d$")
// https://wicg.github.io/webpackage/draft-yasskin-dispatch-bundled-exchanges.html#load-response
func loadResponse(req requestEntryWithOffset, bs []byte) (Response, error) {
r := bytes.NewBuffer(bs[req.Offset : req.Offset+req.Length])
b, err := r.ReadByte()
if err != nil {
return Response{}, fmt.Errorf("bundle: Failed to read first byte of the encoded response: %v", err)
}
if b != 0x82 {
return Response{}, fmt.Errorf("bundle: The first byte of the encoded response is %x, expected 0x82", b)
}
dec := cbor.NewDecoder(r)
headerCborBytes, err := dec.DecodeByteString()
if err != nil {
return Response{}, fmt.Errorf("bundle: Failed to decode response header cbor bytestring: %v", err)
}
rhdr := bytes.NewBuffer(headerCborBytes)
dechdr := cbor.NewDecoder(rhdr)
headers, pseudos, err := decodeCborHeaders(dechdr)
if err != nil {
return Response{}, fmt.Errorf("bundle.response headerCbor: %v", err)
}
status, exists := pseudos[":status"]
if !exists {
return Response{}, fmt.Errorf("bundle.response headerCbor: pseudos don't have a key named \":status\"")
}
if len(pseudos) != 1 {
return Response{}, fmt.Errorf("bundle.response headerCbor: len(pseudos) is %d, expected to be 1", len(pseudos))
}
if !reStatus.MatchString(status) {
return Response{}, fmt.Errorf("bundle.response headerCbor: pseudos['status'] %q invalid", status)
}
body, err := dec.DecodeByteString()
if err != nil {
return Response{}, fmt.Errorf("bundle.response.body: %v", err)
}
if r.Len() != 0 {
return Response{}, fmt.Errorf("bundle.response: invalid request stream end")
}
nstatus, err := strconv.Atoi(status)
if err != nil {
panic(err)
}
res := Response{
Status: nstatus,
Header: headers,
Body: body,
}
return res, nil
}
func Read(r io.Reader) (*Bundle, error) {
bs, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
m, err := loadMetadata(bs)
if err != nil {
return nil, err
}
// log.Printf("meta: %+v", m)
es := []*Exchange{}
for _, req := range m.requests {
res, err := loadResponse(req, bs)
if err != nil {
return nil, err
}
e := &Exchange{
Request: req.Request,
Response: res,
}
es = append(es, e)
}
b := &Bundle{Version: m.version, PrimaryURL: m.primaryURL, Exchanges: es, ManifestURL: m.manifestURL, Signatures: m.signatures}
return b, nil
}