Merge pull request #2073 from lucas-clemente/conn-close-frame-frame-type

parse and write the frame type field in the CONNECTION_CLOSE frame
This commit is contained in:
Marten Seemann
2019-08-18 20:16:22 +07:00
committed by GitHub
2 changed files with 9 additions and 4 deletions

View File

@@ -13,6 +13,7 @@ import (
type ConnectionCloseFrame struct {
IsApplicationError bool
ErrorCode qerr.ErrorCode
FrameType uint64
ReasonPhrase string
}
@@ -30,9 +31,11 @@ func parseConnectionCloseFrame(r *bytes.Reader, version protocol.VersionNumber)
f.ErrorCode = qerr.ErrorCode(ec)
// read the Frame Type, if this is not an application error
if !f.IsApplicationError {
if _, err := utils.ReadVarInt(r); err != nil {
ft, err := utils.ReadVarInt(r)
if err != nil {
return nil, err
}
f.FrameType = ft
}
var reasonPhraseLen uint64
reasonPhraseLen, err = utils.ReadVarInt(r)
@@ -73,7 +76,7 @@ func (f *ConnectionCloseFrame) Write(b *bytes.Buffer, version protocol.VersionNu
utils.WriteVarInt(b, uint64(f.ErrorCode))
if !f.IsApplicationError {
utils.WriteVarInt(b, 0)
utils.WriteVarInt(b, f.FrameType)
}
utils.WriteVarInt(b, uint64(len(f.ReasonPhrase)))
b.WriteString(f.ReasonPhrase)

View File

@@ -25,6 +25,7 @@ var _ = Describe("CONNECTION_CLOSE Frame", func() {
Expect(err).ToNot(HaveOccurred())
Expect(frame.IsApplicationError).To(BeFalse())
Expect(frame.ErrorCode).To(Equal(qerr.ErrorCode(0x19)))
Expect(frame.FrameType).To(Equal(uint64(0x1337)))
Expect(frame.ReasonPhrase).To(Equal(reason))
Expect(b.Len()).To(BeZero())
})
@@ -87,12 +88,13 @@ var _ = Describe("CONNECTION_CLOSE Frame", func() {
b := &bytes.Buffer{}
frame := &ConnectionCloseFrame{
ErrorCode: 0xbeef,
FrameType: 0x12345,
}
Expect(frame.Write(b, versionIETFFrames)).To(Succeed())
expected := []byte{0x1c}
expected = append(expected, encodeVarInt(0xbeef)...)
expected = append(expected, encodeVarInt(0)...) // frame type
expected = append(expected, encodeVarInt(0)...) // reason phrase length
expected = append(expected, encodeVarInt(0x12345)...) // frame type
expected = append(expected, encodeVarInt(0)...) // reason phrase length
Expect(b.Bytes()).To(Equal(expected))
})