-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathencoder.go
629 lines (557 loc) · 16.5 KB
/
encoder.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
package bundle
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/WICG/webpackage/go/bundle/version"
"github.com/WICG/webpackage/go/internal/cbor"
"github.com/WICG/webpackage/go/signedexchange/structuredheader"
)
const maxNumVariantsForSingleURL = 10000
func normalizeHeaderValues(values []string) string {
// RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1
// 4.2 Message Headers
// https://tools.ietf.org/html/rfc2616#section-4.2
//
// Multiple message-header fields with the same field-name MAY be
// present in a message if and only if the entire field-value for that
// header field is defined as a comma-separated list [i.e., #(values)].
// It MUST be possible to combine the multiple header fields into one
// "field-name: field-value" pair, without changing the semantics of the
// message, by appending each subsequent field-value to the first, each
// separated by a comma. The order in which header fields with the same
// field-name are received is therefore significant to the
// interpretation of the combined field value, and thus a proxy MUST NOT
// change the order of these field values when a message is forwarded.
return strings.Join(values, ",")
}
func (r Response) EncodeHeader() ([]byte, error) {
var b bytes.Buffer
enc := cbor.NewEncoder(&b)
mes := []*cbor.MapEntryEncoder{
cbor.GenerateMapEntry(func(keyE *cbor.Encoder, valueE *cbor.Encoder) {
keyE.EncodeByteString([]byte(":status"))
valueE.EncodeByteString([]byte(strconv.Itoa(r.Status)))
}),
}
for name, value := range r.Header {
mes = append(mes,
cbor.GenerateMapEntry(func(keyE *cbor.Encoder, valueE *cbor.Encoder) {
keyE.EncodeByteString([]byte(strings.ToLower(name)))
valueE.EncodeByteString([]byte(normalizeHeaderValues(value)))
}))
}
if err := enc.EncodeMap(mes); err != nil {
return nil, fmt.Errorf("bundle: Failed to encode response header: %v", err)
}
return b.Bytes(), nil
}
func (r Response) HeaderSha256() ([]byte, error) {
headerBytes, err := r.EncodeHeader()
if err != nil {
return nil, err
}
sum := sha256.Sum256(headerBytes)
return sum[:], nil
}
var _ = io.WriterTo(&Bundle{})
type indexEntry struct {
Request
Variants string
VariantKey string
Offset uint64 // Offset within the responses section
Length uint64
}
func (r indexEntry) String() string {
return fmt.Sprintf("{URL: %v, Header: %v, Offset: %d, Length: %d}", r.URL, r.Header, r.Offset, r.Length)
}
type section interface {
Name() string
Len() int
io.WriterTo
}
// staging area for writing index section
type indexSection struct {
es []*indexEntry
bytes []byte
}
func (is *indexSection) addExchange(e *Exchange, offset, length int) error {
variants := normalizeHeaderValues(e.Response.Header[http.CanonicalHeaderKey("variants")])
variantKey := normalizeHeaderValues(e.Response.Header[http.CanonicalHeaderKey("variant-key")])
ent := &indexEntry{
Request: e.Request,
Variants: variants,
VariantKey: variantKey,
Offset: uint64(offset),
Length: uint64(length),
}
is.es = append(is.es, ent)
return nil
}
func (is *indexSection) Finalize(ver version.Version) error {
if is.bytes != nil {
panic("indexSection must be Finalize()-d only once.")
}
var b bytes.Buffer
enc := cbor.NewEncoder(&b)
if ver.SupportsVariants() {
// CDDL:
// index = {* whatwg-url => [ variants-value, +location-in-responses ] }
// variants-value = bstr
// location-in-responses = (offset: uint, length: uint)
m := make(map[string][]*indexEntry)
for _, e := range is.es {
url := e.URL.String()
m[url] = append(m[url], e)
}
mes := []*cbor.MapEntryEncoder{}
for url, es := range m {
var variantsValue []byte
if len(es) > 1 {
variantsValue = []byte(es[0].Variants)
var err error
es, err = entriesInPossibleKeyOrder(es)
if err != nil {
return fmt.Errorf("bundle: cannot construct index entry for %s: %v", url, err)
}
}
me := cbor.GenerateMapEntry(func(keyE *cbor.Encoder, valueE *cbor.Encoder) {
if err := keyE.EncodeTextString(url); err != nil {
panic(err)
}
if err := valueE.EncodeArrayHeader(1 + len(es)*2); err != nil {
panic(err)
}
if err := valueE.EncodeByteString(variantsValue); err != nil {
panic(err)
}
for _, e := range es {
if err := valueE.EncodeUint(e.Offset); err != nil {
panic(err)
}
if err := valueE.EncodeUint(e.Length); err != nil {
panic(err)
}
}
})
mes = append(mes, me)
}
if err := enc.EncodeMap(mes); err != nil {
return err
}
} else {
// CDDL:
// index = {* whatwg-url => [ location-in-responses ] }
// whatwg-url = tstr
// location-in-responses = (offset: uint, length: uint)
m := make(map[string][]*indexEntry)
for _, e := range is.es {
url := e.URL.String()
m[url] = append(m[url], e)
}
mes := []*cbor.MapEntryEncoder{}
for url, es := range m {
if len(es) > 1 {
return errors.New("This WebBundle version '" + string(ver) + "' does not support variants, so we cannot have multiple resources per URL.")
}
me := cbor.GenerateMapEntry(func(keyE *cbor.Encoder, valueE *cbor.Encoder) {
if err := keyE.EncodeTextString(url); err != nil {
panic(err)
}
if err := valueE.EncodeArrayHeader(2); err != nil {
panic(err)
}
if err := valueE.EncodeUint(es[0].Offset); err != nil {
panic(err)
}
if err := valueE.EncodeUint(es[0].Length); err != nil {
panic(err)
}
})
mes = append(mes, me)
}
if err := enc.EncodeMap(mes); err != nil {
return err
}
}
is.bytes = b.Bytes()
return nil
}
// entriesInPossibleKeyOrder reorders es by VariantKey, in the order they should
// appear in the index section; the row-major order of possible keys for
// Variants. All entries in es must have the same Variants value.
//
// For example, if the Variants value is
// "Accept-Language;en;fr, Accept-Encoding:gzip;br", the result would satisfy
// this:
// result[0].VariantKey == "en;gzip"
// result[1].VariantKey == "en;br"
// result[2].VariantKey == "fr;gzip"
// result[3].VariantKey == "fr;br"
//
// Note that VariantKey can have multiple keys (e.g. "en;gzip, fr;gzip"). Such
// entrys will appear multiple times in the result. e.g.:
// result[0].VariantKey == "en;gzip, fr;gzip"
// result[1].VariantKey == "en;br"
// result[2] == result[0]
// result[3].VariantKey == "fr;br"
//
// If entries in es do not cover all combination of possible keys or two entries
// have overwrapping possible keys, this returns an error.
func entriesInPossibleKeyOrder(es []*indexEntry) ([]*indexEntry, error) {
if es[0].Variants == "" {
return nil, errors.New("no Variants header")
}
variants, err := parseVariants(es[0].Variants)
if err != nil {
return nil, fmt.Errorf("cannot parse Variants header value %q: %v", es[0].Variants, err)
}
numPossibleKeys, err := variants.numberOfPossibleKeys()
if err != nil {
return nil, fmt.Errorf("invalid Variants header value %q: %v", es[0].Variants, err)
}
result := make([]*indexEntry, numPossibleKeys)
for _, e := range es {
// TODO: Compare Variants values as lists
// (e.g. "Accept;foo;bar" == "Accept; foo; bar").
if e.Variants != es[0].Variants {
return nil, fmt.Errorf("inconsistent Variants value. %q != %q", e.Variants, es[0].Variants)
}
vks, err := parseListOfStringLists(e.VariantKey)
if err != nil {
return nil, fmt.Errorf("cannot parse Variant-Key header %q: %v", e.VariantKey, err)
}
for _, vk := range vks {
i := variants.indexInPossibleKeys(vk)
if i == -1 {
return nil, fmt.Errorf("Variant-Key %q is not covered by variants %q", e.VariantKey, e.Variants)
}
if result[i] != nil {
return nil, fmt.Errorf("duplicated entries with Variant-Key %q", vk)
}
result[i] = e
}
}
for i, e := range result {
if e == nil {
return nil, fmt.Errorf("no entry for Variant-Key %v", variants.possibleKeyAt(i))
}
}
return result, nil
}
func parseListOfStringLists(s string) ([][]string, error) {
ll, err := structuredheader.ParseListOfLists(s)
if err != nil {
return nil, err
}
// Convert [][]structuredheader.Item to [][]string.
var result [][]string
for _, l := range ll {
var sl []string
for _, item := range l {
switch v := item.(type) {
case string:
sl = append(sl, v)
case structuredheader.Token:
sl = append(sl, string(v))
default:
return nil, fmt.Errorf("unexpected value of type %T", v)
}
}
result = append(result, sl)
}
return result, nil
}
// Variants represents a Variants: header value.
type Variants [][]string
func parseVariants(s string) (Variants, error) {
vs, err := parseListOfStringLists(s)
return Variants(vs), err
}
func (v Variants) numberOfPossibleKeys() (int, error) {
n := 1
for _, vals := range v {
// vals is [header-name, possible-value1, possible-value2, ...]
if len(vals) <= 1 {
return 0, errors.New("no possible key")
}
n *= len(vals) - 1
if n > maxNumVariantsForSingleURL {
return 0, errors.New("too many possible keys")
}
}
return n, nil
}
// indexInPossibleKeys returns the index of variantKey within the all possible
// key combinations for v. If variantKey is not a possible key for v, this
// returns -1.
func (v Variants) indexInPossibleKeys(variantKey []string) int {
if len(v) != len(variantKey) {
return -1
}
index := 0
OuterLoop:
for i, vals := range v {
vals = vals[1:] // Drop header-name
for indexInAxis, val := range vals {
if val == variantKey[i] {
index = index*len(vals) + indexInAxis
continue OuterLoop
}
}
return -1
}
return index
}
// possibleKeyAt returns a variant key at given index within the all possible
// key combinations for v, or nil if index is out of range.
func (v Variants) possibleKeyAt(index int) []string {
keys := make([]string, len(v))
for i := len(v) - 1; i >= 0; i-- {
vals := v[i][1:] // Drop header-name
keys[i] = vals[index%len(vals)]
index /= len(vals)
}
if index != 0 {
return nil // index out of range
}
return keys
}
func (is *indexSection) Name() string {
return "index"
}
func (is *indexSection) Len() int {
if is.bytes == nil {
panic("indexSection must be Finalize()-d before calling Len()")
}
return len(is.bytes)
}
func (is *indexSection) WriteTo(w io.Writer) (int64, error) {
if is.bytes == nil {
panic("indexSection must be Finalize()-d before calling Bytes()")
}
n, err := w.Write(is.bytes)
return int64(n), err
}
// staging area for writing responses section
type responsesSection struct {
buf bytes.Buffer
}
func newResponsesSection(n int) *responsesSection {
ret := &responsesSection{}
enc := cbor.NewEncoder(&ret.buf)
if err := enc.EncodeArrayHeader(n); err != nil {
panic(err)
}
return ret
}
func (rs *responsesSection) addResponse(r Response) (int, int, error) {
offset := rs.buf.Len()
headerCbor, err := r.EncodeHeader()
if err != nil {
return 0, 0, err
}
enc := cbor.NewEncoder(&rs.buf)
if err := enc.EncodeArrayHeader(2); err != nil {
return 0, 0, fmt.Errorf("bundle: failed to encode response array header: %v", err)
}
if err := enc.EncodeByteString(headerCbor); err != nil {
return 0, 0, fmt.Errorf("bundle: failed to encode response header cbor bytestring: %v", err)
}
if err := enc.EncodeByteString(r.Body); err != nil {
return 0, 0, fmt.Errorf("bundle: failed to encode response payload bytestring: %v", err)
}
length := rs.buf.Len() - offset
return offset, length, nil
}
func (rs *responsesSection) Name() string { return "responses" }
func (rs *responsesSection) Len() int { return rs.buf.Len() }
func (rs *responsesSection) WriteTo(w io.Writer) (int64, error) {
return rs.buf.WriteTo(w)
}
type primarySection struct {
bytes.Buffer
}
func (ps *primarySection) Name() string { return "primary" }
func newPrimarySection(url *url.URL) (*primarySection, error) {
var ps primarySection
enc := cbor.NewEncoder(&ps)
if err := enc.EncodeTextString(url.String()); err != nil {
return nil, err
}
return &ps, nil
}
type manifestSection struct {
bytes.Buffer
}
func (ms *manifestSection) Name() string { return "manifest" }
func newManifestSection(url *url.URL) (*manifestSection, error) {
var ms manifestSection
enc := cbor.NewEncoder(&ms)
if err := enc.EncodeTextString(url.String()); err != nil {
return nil, err
}
return &ms, nil
}
type signaturesSection struct {
bytes.Buffer
}
func (rs *signaturesSection) Name() string { return "signatures" }
func newSignaturesSection(sigs *Signatures) (*signaturesSection, error) {
var ss signaturesSection
enc := cbor.NewEncoder(&ss)
enc.EncodeArrayHeader(2)
enc.EncodeArrayHeader(len(sigs.Authorities))
for _, auth := range sigs.Authorities {
if err := auth.EncodeTo(enc); err != nil {
return nil, err
}
}
enc.EncodeArrayHeader(len(sigs.VouchedSubsets))
for _, vs := range sigs.VouchedSubsets {
mes := []*cbor.MapEntryEncoder{
cbor.GenerateMapEntry(func(keyE *cbor.Encoder, valueE *cbor.Encoder) {
keyE.EncodeTextString("authority")
valueE.EncodeUint(vs.Authority)
}),
cbor.GenerateMapEntry(func(keyE *cbor.Encoder, valueE *cbor.Encoder) {
keyE.EncodeTextString("sig")
valueE.EncodeByteString(vs.Sig)
}),
cbor.GenerateMapEntry(func(keyE *cbor.Encoder, valueE *cbor.Encoder) {
keyE.EncodeTextString("signed")
valueE.EncodeByteString(vs.Signed)
}),
}
if err := enc.EncodeMap(mes); err != nil {
return nil, err
}
}
return &ss, nil
}
func addExchange(is *indexSection, rs *responsesSection, e *Exchange) error {
offset, length, err := rs.addResponse(e.Response)
if err != nil {
return err
}
if err := is.addExchange(e, offset, length); err != nil {
return err
}
return nil
}
func writePrimaryURL(w io.Writer, url *url.URL) error {
enc := cbor.NewEncoder(w)
return enc.EncodeTextString(url.String())
}
// https://wicg.github.io/webpackage/draft-yasskin-dispatch-bundled-exchanges.html#load-metadata
// Steps 3-7.
func writeSectionOffsets(w io.Writer, sections []section) error {
var b bytes.Buffer
nenc := cbor.NewEncoder(&b)
if err := nenc.EncodeArrayHeader(len(sections) * 2); err != nil {
return err
}
for _, s := range sections {
if err := nenc.EncodeTextString(s.Name()); err != nil {
return err
}
if err := nenc.EncodeUint(uint64(s.Len())); err != nil {
return err
}
}
enc := cbor.NewEncoder(w)
if err := enc.EncodeByteString(b.Bytes()); err != nil {
return err
}
return nil
}
func writeSectionHeader(w io.Writer, numSections int) error {
enc := cbor.NewEncoder(w)
return enc.EncodeArrayHeader(numSections)
}
func writeFooter(w io.Writer, offset int) error {
const footerLength = 9
bundleSize := uint64(offset) + footerLength
var b bytes.Buffer
if err := binary.Write(&b, binary.BigEndian, bundleSize); err != nil {
return err
}
if b.Len() != 8 {
panic("assert")
}
enc := cbor.NewEncoder(w)
if err := enc.EncodeByteString(b.Bytes()); err != nil {
return err
}
return nil
}
func (b *Bundle) WriteTo(w io.Writer) (int64, error) {
cw := NewCountingWriter(w)
is := &indexSection{}
rs := newResponsesSection(len(b.Exchanges))
for _, e := range b.Exchanges {
if err := addExchange(is, rs, e); err != nil {
return cw.Written, err
}
}
if err := is.Finalize(b.Version); err != nil {
return cw.Written, err
}
sections := []section{}
sections = append(sections, is)
if !b.Version.HasPrimaryURLFieldInHeader() && b.PrimaryURL != nil {
ps, err := newPrimarySection(b.PrimaryURL)
if err != nil {
return cw.Written, err
}
sections = append(sections, ps)
}
if b.ManifestURL != nil {
if !b.Version.SupportsManifestSection() {
return cw.Written, errors.New("This version of the WebBundle does not support storing manifest URL.")
}
ms, err := newManifestSection(b.ManifestURL)
if err != nil {
return cw.Written, err
}
sections = append(sections, ms)
}
if b.Signatures != nil && b.Version.SupportsSignatures() {
ss, err := newSignaturesSection(b.Signatures)
if err != nil {
return cw.Written, err
}
sections = append(sections, ss)
}
sections = append(sections, rs) // resources section must be the last.
if _, err := cw.Write(b.Version.HeaderMagicBytes()); err != nil {
return cw.Written, err
}
if b.Version.HasPrimaryURLFieldInHeader() {
if err := writePrimaryURL(cw, b.PrimaryURL); err != nil {
return cw.Written, err
}
}
if err := writeSectionOffsets(cw, sections); err != nil {
return cw.Written, err
}
if err := writeSectionHeader(cw, len(sections)); err != nil {
return cw.Written, err
}
for _, s := range sections {
if _, err := s.WriteTo(cw); err != nil {
return cw.Written, err
}
}
if err := writeFooter(cw, int(cw.Written)); err != nil {
return cw.Written, err
}
return cw.Written, nil
}