https://wicg.github.io/webpackage/draft-yasskin-dispatch-bundled-exchanges.html#load-response
(req requestEntryWithOffset, bs []byte)
| 568 | |
| 569 | // https://wicg.github.io/webpackage/draft-yasskin-dispatch-bundled-exchanges.html#load-response |
| 570 | func loadResponse(req requestEntryWithOffset, bs []byte) (Response, error) { |
| 571 | r := bytes.NewBuffer(bs[req.Offset : req.Offset+req.Length]) |
| 572 | |
| 573 | b, err := r.ReadByte() |
| 574 | if err != nil { |
| 575 | return Response{}, fmt.Errorf("bundle: Failed to read first byte of the encoded response: %v", err) |
| 576 | } |
| 577 | if b != 0x82 { |
| 578 | return Response{}, fmt.Errorf("bundle: The first byte of the encoded response is %x, expected 0x82", b) |
| 579 | } |
| 580 | |
| 581 | dec := cbor.NewDecoder(r) |
| 582 | headerCborBytes, err := dec.DecodeByteString() |
| 583 | if err != nil { |
| 584 | return Response{}, fmt.Errorf("bundle: Failed to decode response header cbor bytestring: %v", err) |
| 585 | } |
| 586 | |
| 587 | rhdr := bytes.NewBuffer(headerCborBytes) |
| 588 | dechdr := cbor.NewDecoder(rhdr) |
| 589 | headers, pseudos, err := decodeCborHeaders(dechdr) |
| 590 | if err != nil { |
| 591 | return Response{}, fmt.Errorf("bundle.response headerCbor: %v", err) |
| 592 | } |
| 593 | |
| 594 | status, exists := pseudos[":status"] |
| 595 | if !exists { |
| 596 | return Response{}, fmt.Errorf("bundle.response headerCbor: pseudos don't have a key named \":status\"") |
| 597 | } |
| 598 | if len(pseudos) != 1 { |
| 599 | return Response{}, fmt.Errorf("bundle.response headerCbor: len(pseudos) is %d, expected to be 1", len(pseudos)) |
| 600 | } |
| 601 | |
| 602 | if !reStatus.MatchString(status) { |
| 603 | return Response{}, fmt.Errorf("bundle.response headerCbor: pseudos['status'] %q invalid", status) |
| 604 | } |
| 605 | |
| 606 | body, err := dec.DecodeByteString() |
| 607 | if err != nil { |
| 608 | return Response{}, fmt.Errorf("bundle.response.body: %v", err) |
| 609 | } |
| 610 | |
| 611 | if r.Len() != 0 { |
| 612 | return Response{}, fmt.Errorf("bundle.response: invalid request stream end") |
| 613 | } |
| 614 | |
| 615 | nstatus, err := strconv.Atoi(status) |
| 616 | if err != nil { |
| 617 | panic(err) |
| 618 | } |
| 619 | |
| 620 | res := Response{ |
| 621 | Status: nstatus, |
| 622 | Header: headers, |
| 623 | Body: body, |
| 624 | } |
| 625 | |
| 626 | return res, nil |
| 627 | } |
no test coverage detected