From c57b2ca317d9428bcfd3f8a003de3ed119643bba Mon Sep 17 00:00:00 2001 From: "xielei.xielei" Date: Wed, 7 Aug 2024 13:18:41 +0800 Subject: [PATCH] fix empty biz body unwrapBinary EOF error --- thrift/binary.go | 6 +++++- thrift/binary_test.go | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/thrift/binary.go b/thrift/binary.go index 09abf3fc..df19fb41 100644 --- a/thrift/binary.go +++ b/thrift/binary.go @@ -204,10 +204,14 @@ func (p BinaryProtocol) UnwrapBody() (string, TMessageType, int32, FieldID, []by return name, rTyp, seqID, 0, nil, err } // read the success struct - _, _, structID, err := p.ReadFieldBegin() + _, typeId, structID, err := p.ReadFieldBegin() if err != nil { return name, rTyp, seqID, structID, nil, err } + //empty response + if typeId == STOP { + return name, rTyp, seqID, structID, []byte{}, nil + } // there's alway a struct stop by success struct if p.Read > len(p.Buf)-1 { return name, rTyp, seqID, structID, nil, io.EOF diff --git a/thrift/binary_test.go b/thrift/binary_test.go index 99262c01..695e4ee9 100644 --- a/thrift/binary_test.go +++ b/thrift/binary_test.go @@ -171,3 +171,25 @@ func TestBinaryProtocol_WriteAny_ReadAny(t *testing.T) { }) } } + +func TestBinaryUnwrap(t *testing.T) { + b := BinaryProtocol{ + Buf: nil, + Read: 0, + } + var i32 int32 = -2147418110 + var methodName = "DummyNew" + b.WriteI32(i32) + b.WriteString(methodName) + b.WriteI32(1) + b.WriteByte(byte(STOP)) + + name, rType, _, _, bs, err := UnwrapBinaryMessage(b.Buf) + + require.Equal(t, bs, []byte{}) + require.Equal(t, name, methodName) + require.Equal(t, rType, REPLY) + var nilErr error + require.Equal(t, err, nilErr) + +}