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 bcea628
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions frankenphp.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,12 +580,18 @@ func go_sapi_flush(rh C.uintptr_t) bool {
}

//export go_read_post
func go_read_post(rh C.uintptr_t, cBuf *C.char, countBytes C.size_t) C.size_t {
func go_read_post(rh C.uintptr_t, cBuf *C.char, countBytes C.size_t) (readBytes 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 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 +601,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
}

//export go_read_cookies
Expand Down

0 comments on commit bcea628

Please sign in to comment.