route/vendor/github.com/lucas-clemente/quic-go/internal/wire/goaway_frame.go

69 lines
1.7 KiB
Go
Raw Normal View History

2018-01-03 19:19:49 +00:00
package wire
2017-12-12 02:51:45 +00:00
import (
"bytes"
"io"
2018-01-03 19:19:49 +00:00
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/utils"
2017-12-12 02:51:45 +00:00
"github.com/lucas-clemente/quic-go/qerr"
)
// A GoawayFrame is a GOAWAY frame
type GoawayFrame struct {
ErrorCode qerr.ErrorCode
LastGoodStream protocol.StreamID
ReasonPhrase string
}
// ParseGoawayFrame parses a GOAWAY frame
2018-01-03 19:19:49 +00:00
func ParseGoawayFrame(r *bytes.Reader, _ protocol.VersionNumber) (*GoawayFrame, error) {
2017-12-12 02:51:45 +00:00
frame := &GoawayFrame{}
2018-01-03 19:19:49 +00:00
if _, err := r.ReadByte(); err != nil {
2017-12-12 02:51:45 +00:00
return nil, err
}
2018-01-03 19:19:49 +00:00
errorCode, err := utils.BigEndian.ReadUint32(r)
2017-12-12 02:51:45 +00:00
if err != nil {
return nil, err
}
frame.ErrorCode = qerr.ErrorCode(errorCode)
2018-01-03 19:19:49 +00:00
lastGoodStream, err := utils.BigEndian.ReadUint32(r)
2017-12-12 02:51:45 +00:00
if err != nil {
return nil, err
}
frame.LastGoodStream = protocol.StreamID(lastGoodStream)
2018-01-03 19:19:49 +00:00
reasonPhraseLen, err := utils.BigEndian.ReadUint16(r)
2017-12-12 02:51:45 +00:00
if err != nil {
return nil, err
}
if reasonPhraseLen > uint16(protocol.MaxPacketSize) {
return nil, qerr.Error(qerr.InvalidGoawayData, "reason phrase too long")
}
reasonPhrase := make([]byte, reasonPhraseLen)
if _, err := io.ReadFull(r, reasonPhrase); err != nil {
return nil, err
}
frame.ReasonPhrase = string(reasonPhrase)
return frame, nil
}
2018-01-03 19:19:49 +00:00
func (f *GoawayFrame) Write(b *bytes.Buffer, _ protocol.VersionNumber) error {
b.WriteByte(0x03)
utils.BigEndian.WriteUint32(b, uint32(f.ErrorCode))
utils.BigEndian.WriteUint32(b, uint32(f.LastGoodStream))
utils.BigEndian.WriteUint16(b, uint16(len(f.ReasonPhrase)))
2017-12-12 02:51:45 +00:00
b.WriteString(f.ReasonPhrase)
return nil
}
// MinLength of a written frame
2018-01-20 18:07:01 +00:00
func (f *GoawayFrame) MinLength(version protocol.VersionNumber) protocol.ByteCount {
return protocol.ByteCount(1 + 4 + 4 + 2 + len(f.ReasonPhrase))
2017-12-12 02:51:45 +00:00
}