https://wicg.github.io/webpackage/draft-yasskin-dispatch-bundled-exchanges.html#load-metadata
(bs []byte)
| 439 | |
| 440 | // https://wicg.github.io/webpackage/draft-yasskin-dispatch-bundled-exchanges.html#load-metadata |
| 441 | func loadMetadata(bs []byte) (*meta, error) { |
| 442 | |
| 443 | r := bytes.NewBuffer(bs) |
| 444 | |
| 445 | ver, err := version.ParseMagicBytes(r) |
| 446 | // TODO(ksakamoto): Continue and return VersionError after parsing fallbackUrl. |
| 447 | if err != nil { |
| 448 | return nil, &LoadMetadataError{err, FormatError, nil} |
| 449 | } |
| 450 | |
| 451 | var fallbackURL *url.URL |
| 452 | dec := cbor.NewDecoder(r) |
| 453 | if ver.HasPrimaryURLFieldInHeader() { |
| 454 | fallbackURLBytes, err := dec.DecodeTextString() |
| 455 | if err != nil { |
| 456 | return nil, &LoadMetadataError{fmt.Errorf("bundle: Failed to read fallbackURL string: %v", err), FormatError, nil} |
| 457 | } |
| 458 | fallbackURL, err = url.Parse(fallbackURLBytes) |
| 459 | if err != nil { |
| 460 | return nil, &LoadMetadataError{fmt.Errorf("bundle: Failed to parse fallbackURL: %v", err), FormatError, nil} |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | slbytes, err := dec.DecodeByteString() |
| 465 | if err != nil { |
| 466 | return nil, &LoadMetadataError{fmt.Errorf("bundle: Failed to read sectionLengths byte string: %v", err), FormatError, fallbackURL} |
| 467 | } |
| 468 | if len(slbytes) >= 8192 { |
| 469 | return nil, &LoadMetadataError{fmt.Errorf("bundle: sectionLengthsLength is too long (%d bytes)", slbytes), FormatError, fallbackURL} |
| 470 | } |
| 471 | |
| 472 | sos, err := decodeSectionLengthsCBOR(slbytes) |
| 473 | if err != nil { |
| 474 | return nil, &LoadMetadataError{err, FormatError, fallbackURL} |
| 475 | } |
| 476 | |
| 477 | numSections, err := dec.DecodeArrayHeader() |
| 478 | if err != nil { |
| 479 | return nil, &LoadMetadataError{fmt.Errorf("bundle: Failed to read section header."), FormatError, fallbackURL} |
| 480 | } |
| 481 | if numSections != uint64(len(sos)) { |
| 482 | return nil, &LoadMetadataError{fmt.Errorf("bundle: Expected %d sections, got %d sections", len(sos), numSections), FormatError, fallbackURL} |
| 483 | } |
| 484 | |
| 485 | sectionsStart := uint64(len(bs) - r.Len()) |
| 486 | |
| 487 | if len(sos) == 0 || sos[len(sos)-1].Name != "responses" { |
| 488 | return nil, &LoadMetadataError{fmt.Errorf("bundle: Last section is not \"responses\""), FormatError, fallbackURL} |
| 489 | } |
| 490 | |
| 491 | meta := &meta{ |
| 492 | version: ver, |
| 493 | primaryURL: fallbackURL, |
| 494 | sectionOffsets: sos, |
| 495 | sectionsStart: sectionsStart, |
| 496 | } |
| 497 | |
| 498 | offset := sectionsStart |
no test coverage detected