github.com/MerlinKodo/quic-go@v0.39.2/internal/wire/new_token_frame.go (about)

     1  package wire
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"io"
     7  
     8  	"github.com/MerlinKodo/quic-go/internal/protocol"
     9  	"github.com/MerlinKodo/quic-go/quicvarint"
    10  )
    11  
    12  // A NewTokenFrame is a NEW_TOKEN frame
    13  type NewTokenFrame struct {
    14  	Token []byte
    15  }
    16  
    17  func parseNewTokenFrame(r *bytes.Reader, _ protocol.VersionNumber) (*NewTokenFrame, error) {
    18  	tokenLen, err := quicvarint.Read(r)
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  	if uint64(r.Len()) < tokenLen {
    23  		return nil, io.EOF
    24  	}
    25  	if tokenLen == 0 {
    26  		return nil, errors.New("token must not be empty")
    27  	}
    28  	token := make([]byte, int(tokenLen))
    29  	if _, err := io.ReadFull(r, token); err != nil {
    30  		return nil, err
    31  	}
    32  	return &NewTokenFrame{Token: token}, nil
    33  }
    34  
    35  func (f *NewTokenFrame) Append(b []byte, _ protocol.VersionNumber) ([]byte, error) {
    36  	b = append(b, newTokenFrameType)
    37  	b = quicvarint.Append(b, uint64(len(f.Token)))
    38  	b = append(b, f.Token...)
    39  	return b, nil
    40  }
    41  
    42  // Length of a written frame
    43  func (f *NewTokenFrame) Length(protocol.VersionNumber) protocol.ByteCount {
    44  	return 1 + quicvarint.Len(uint64(len(f.Token))) + protocol.ByteCount(len(f.Token))
    45  }