Skip to content

Commit

Permalink
refactor: microoptim
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Aug 16, 2023
1 parent 8bc13bf commit 714e81f
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions frankenphp.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,18 @@ func go_sapi_flush(rh C.uintptr_t) bool {
func go_read_post(rh C.uintptr_t, cBuf *C.char, countBytes C.size_t) C.size_t {
r := cgo.Handle(rh).Value().(*http.Request)

p := make([]byte, int(countBytes))
readBytes, err := io.ReadFull(r.Body, p)
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
p := make([]byte, countBytes)
var (
readBytes C.size_t
err error
)
for readBytes < countBytes && err == nil {
var n int
n, err = r.Body.Read(p[readBytes:])
readBytes += C.size_t(n)
}

if err != nil && err != io.EOF {
// invalid Read on closed Body may happen because of https://github.com/golang/go/issues/15527
fc, _ := FromContext(r.Context())
fc.Logger.Error("error while reading the request body", zap.Error(err))
Expand All @@ -595,7 +604,7 @@ func go_read_post(rh C.uintptr_t, cBuf *C.char, countBytes C.size_t) C.size_t {
C.memcpy(unsafe.Pointer(cBuf), unsafe.Pointer(&p[0]), C.size_t(readBytes))
}

return C.size_t(readBytes)
return readBytes
}

//export go_read_cookies
Expand Down

0 comments on commit 714e81f

Please sign in to comment.