MCPcopy
hub / github.com/grpc/grpc-go / readDataFrame

Method readDataFrame

internal/transport/http_util.go:522–588  ·  view source on GitHub ↗
(fh http2.FrameHeader)

Source from the content-addressed store, hash-verified

520}
521
522func (f *framer) readDataFrame(fh http2.FrameHeader) (err error) {
523 if fh.StreamID == 0 {
524 // DATA frames MUST be associated with a stream. If a
525 // DATA frame is received whose stream identifier
526 // field is 0x0, the recipient MUST respond with a
527 // connection error (Section 5.4.1) of type
528 // PROTOCOL_ERROR.
529 f.errDetail = errors.New("DATA frame with stream ID 0")
530 return http2.ConnectionError(http2.ErrCodeProtocol)
531 }
532 // Converting a *[]byte to a mem.SliceBuffer incurs a heap allocation. This
533 // conversion is performed by mem.NewBuffer. To avoid the extra allocation
534 // a []byte is allocated directly if required and cast to a mem.SliceBuffer.
535 var buf []byte
536 // poolHandle is the pointer returned by the buffer pool (if it's used.).
537 var poolHandle *[]byte
538 useBufferPool := !mem.IsBelowBufferPoolingThreshold(int(fh.Length))
539 if useBufferPool {
540 poolHandle = f.pool.Get(int(fh.Length))
541 buf = *poolHandle
542 defer func() {
543 if err != nil {
544 f.pool.Put(poolHandle)
545 }
546 }()
547 } else {
548 buf = make([]byte, int(fh.Length))
549 }
550 if fh.Flags.Has(http2.FlagDataPadded) {
551 if fh.Length == 0 {
552 return io.ErrUnexpectedEOF
553 }
554 // This initial 1-byte read can be inefficient for unbuffered readers,
555 // but it allows the rest of the payload to be read directly to the
556 // start of the destination slice. This makes it easy to return the
557 // original slice back to the buffer pool.
558 if _, err := io.ReadFull(f.reader, buf[:1]); err != nil {
559 return err
560 }
561 padSize := buf[0]
562 buf = buf[:len(buf)-1]
563 if int(padSize) > len(buf) {
564 // If the length of the padding is greater than the
565 // length of the frame payload, the recipient MUST
566 // treat this as a connection error.
567 // Filed: https://github.com/http2/http2-spec/issues/610
568 f.errDetail = errors.New("pad size larger than data payload")
569 return http2.ConnectionError(http2.ErrCodeProtocol)
570 }
571 if _, err := io.ReadFull(f.reader, buf); err != nil {
572 return err
573 }
574 buf = buf[:len(buf)-int(padSize)]
575 } else if _, err := io.ReadFull(f.reader, buf); err != nil {
576 return err
577 }
578
579 f.dataFrame.FrameHeader = fh

Callers 1

readFrameMethod · 0.95

Calls 5

NewBufferFunction · 0.92
SliceBufferTypeAlias · 0.92
GetMethod · 0.65
PutMethod · 0.65

Tested by

no test coverage detected