Skip to content

Commit

Permalink
Merge pull request #437 from tucksaun/fix-chunked-transfer-encoding
Browse files Browse the repository at this point in the history
Fix chunked requests are not supported with FPM or CGI, fix #169
  • Loading branch information
fabpot committed Mar 13, 2024
2 parents f2f7add + f3ee4ea commit 7e327d0
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions local/php/cgi.go
@@ -1,8 +1,10 @@
package php

import (
"bytes"
"io"
"net/http"
"strconv"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -30,6 +32,24 @@ func (p *cgiTransport) RoundTrip(req *http.Request) (*http.Response, error) {
time.Sleep(time.Millisecond * 50)
}

// The CGI spec doesn't allow chunked requests. Go is already assembling the
// chunks from the request to a usable Reader (see net/http.readTransfer and
// net/http/internal.NewChunkedReader), so the only thing we have to
// do to is get the content length and add it to the header but to do so we
// have to read and buffer the body content.
if len(req.TransferEncoding) > 0 && req.TransferEncoding[0] == "chunked" {
bodyBuffer := &bytes.Buffer{}
bodyBytes, err := io.Copy(bodyBuffer, req.Body)
if err != nil {
return nil, err
}

req.Body = io.NopCloser(bodyBuffer)
req.TransferEncoding = nil
env["CONTENT_LENGTH"] = strconv.FormatInt(bodyBytes, 10)
env["HTTP_CONTENT_LENGTH"] = env["CONTENT_LENGTH"]
}

// fetching the response from the fastcgi backend, and check for errors
resp, err := fcgi.Request(env, req.Body)
if err != nil {
Expand Down

0 comments on commit 7e327d0

Please sign in to comment.