@@ -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
4649type 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
0 commit comments