From c2a03aed71b8b011dda440900e0bac51a82243d3 Mon Sep 17 00:00:00 2001 From: HarryWRZ Date: Fri, 15 Sep 2023 11:19:52 +0800 Subject: [PATCH 1/2] feat: fix bug that len(last payloads) is less than 5 --- proto/proto.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/proto/proto.go b/proto/proto.go index 1c3abe6b..1e88f35e 100644 --- a/proto/proto.go +++ b/proto/proto.go @@ -559,12 +559,13 @@ func HasFullPayload(m ProtocolStateSetter, payloads ...[]byte) bool { } // check trailer headers + toCheck := getCheckBytes() if state.HasTrailer { - if bytes.HasSuffix(payloads[len(payloads)-1], []byte("\r\n\r\n")) { + if bytes.HasSuffix(toCheck, []byte("\r\n\r\n")) { return true } } else { - if bytes.HasSuffix(payloads[len(payloads)-1], []byte("0\r\n\r\n")) { + if bytes.HasSuffix(toCheck, []byte("0\r\n\r\n")) { state.HasFullPayload = true return true } @@ -577,6 +578,27 @@ func HasFullPayload(m ProtocolStateSetter, payloads ...[]byte) bool { return state.BodyLen == bodyLen } +func getCheckBytes(payloads ...[]byte) []byte { + out := make([]byte, 5) + if len(payloads) >= 2 { + i, j := len(payloads[len(payloads)-2])-1, len(payloads[len(payloads)-1])-1 + k := 4 + for k >= 0 { + if j >= 0 { + out[k] = payloads[len(payloads)-1][j] + j-- + } else if i >= 0 { + out[k] = payloads[len(payloads)-2][i] + i-- + } + k-- + } + } else { + out = payloads[len(payloads)-1] + } + return out +} + // this works with positive integers func atoI(s []byte, base int) (num int, ok bool) { var v int From 3888fad41c2523264677d3a449994525b6bc78f5 Mon Sep 17 00:00:00 2001 From: HarryWRZ Date: Fri, 15 Sep 2023 14:06:46 +0800 Subject: [PATCH 2/2] feat: fix bug that len(last payloads) is less than 5 --- proto/proto.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proto/proto.go b/proto/proto.go index 1e88f35e..f4721a76 100644 --- a/proto/proto.go +++ b/proto/proto.go @@ -559,7 +559,7 @@ func HasFullPayload(m ProtocolStateSetter, payloads ...[]byte) bool { } // check trailer headers - toCheck := getCheckBytes() + toCheck := getCheckBytes(payloads...) if state.HasTrailer { if bytes.HasSuffix(toCheck, []byte("\r\n\r\n")) { return true