Skip to content

Commit 55ca94b

Browse files
committed
Get POST working on FastCGI proxy
1 parent c0520a3 commit 55ca94b

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

fastcgi.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ import (
3737
"fmt"
3838
"github.com/Sirupsen/logrus"
3939
"github.com/moriyoshi/devproxy/fcgiclient"
40+
"io"
4041
"io/ioutil"
42+
"math"
4143
"net"
4244
"net/http"
4345
"net/textproto"
46+
"strconv"
4447
)
4548

4649
type fastCGIRoundTripper struct {
@@ -68,8 +71,10 @@ func (rt *fastCGIRoundTripper) RoundTrip(req *http.Request) (*http.Response, err
6871
"SCRIPT_FILENAME": []byte(pop(req.Header, "X-Cgi-Script-Filename")),
6972
"SCRIPT_NAME": []byte(pop(req.Header, "X-Cgi-Script-Name")),
7073
"PATH_INFO": []byte(pop(req.Header, "X-Cgi-Path-Info")),
74+
"PATH_TRANSLATED": []byte(pop(req.Header, "X-Cgi-Path-Translated")),
7175
"SERVER_PROTOCOL": []byte(req.Proto),
7276
"REMOTE_ADDR": []byte(req.Header.Get("X-Forwarded-For")),
77+
"CONTENT_TYPE": []byte(req.Header.Get("Content-Type")),
7378
"QUERY_STRING": []byte(req.URL.RawQuery),
7479
}
7580
if req.Header.Get("X-Forwarded-Proto") == "https" {
@@ -97,10 +102,19 @@ func (rt *fastCGIRoundTripper) RoundTrip(req *http.Request) (*http.Response, err
97102
}
98103
var reqBody []byte
99104
if req.Body != nil {
100-
reqBody, err = ioutil.ReadAll(req.Body)
105+
if req.ContentLength == 0 {
106+
reqBody, err = ioutil.ReadAll(req.Body)
107+
} else {
108+
if req.ContentLength > math.MaxInt32 {
109+
return nil, fmt.Errorf("Request body too long (%d bytes)", req.ContentLength)
110+
}
111+
reqBody = make([]byte, int(req.ContentLength))
112+
_, err = io.ReadAtLeast(req.Body, reqBody, int(req.ContentLength))
113+
}
101114
if err != nil {
102115
return nil, err
103116
}
117+
env["CONTENT_LENGTH"] = []byte(strconv.Itoa(len(reqBody)))
104118
}
105119
respBytes, errBytes, err := fcgi.Request(env, reqBody, rt.reqId)
106120
rt.reqId += 1

fcgiclient/fcgiclient.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,21 @@ func (this *FCGIClient) Request(env map[string][]byte, req []byte, reqId uint16)
237237
if req == nil {
238238
req = nullByteSlice
239239
}
240-
err = this.writeRecord(FCGI_STDIN, reqId, req)
240+
for b := req; len(b) > 0; {
241+
var l int
242+
if len(b) < maxWrite {
243+
l = len(b)
244+
} else {
245+
l = maxWrite
246+
}
247+
consumed := b[:l]
248+
err = this.writeRecord(FCGI_STDIN, reqId, consumed)
249+
if err != nil {
250+
return
251+
}
252+
b = b[len(consumed):]
253+
}
254+
err = this.writeRecord(FCGI_STDIN, reqId, nullByteSlice)
241255
if err != nil {
242256
return
243257
}

0 commit comments

Comments
 (0)