MCPcopy Create free account
hub / github.com/WICG/webpackage / loadResponse

Function loadResponse

go/bundle/decoder.go:570–627  ·  view source on GitHub ↗

https://wicg.github.io/webpackage/draft-yasskin-dispatch-bundled-exchanges.html#load-response

(req requestEntryWithOffset, bs []byte)

Source from the content-addressed store, hash-verified

568
569// https://wicg.github.io/webpackage/draft-yasskin-dispatch-bundled-exchanges.html#load-response
570func 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}

Callers 1

ReadFunction · 0.85

Calls 5

DecodeByteStringMethod · 0.95
NewDecoderFunction · 0.92
decodeCborHeadersFunction · 0.85
ReadByteMethod · 0.80
LenMethod · 0.65

Tested by

no test coverage detected