Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fix bug that len(last payloads) is less than 5 #1226

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,12 +559,13 @@ func HasFullPayload(m ProtocolStateSetter, payloads ...[]byte) bool {
}

// check trailer headers
toCheck := getCheckBytes(payloads...)
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
}
Expand All @@ -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
Expand Down