Skip to content

Commit

Permalink
fastcgi: Fix for missing content-length header when using QUIC (#1501)
Browse files Browse the repository at this point in the history
* Fix for missing content-length header when using QUIC

If request.ContentLength is set then it will be used instead of getting
it from request.Header map since quic-go(quic-go/quic-go@bb24be8)
will not store (and pass) the Content-Length header using its header
map.

This fixes a potential issue where FastCGI POST requests body empty when
QUIC is enabled. (#1370)

* Change the data type for fastcgi contentLength to int64

quic-go uses int64 for contentLength

* Fix an error for undeclared variable

* Fix test for fcgiclient

the data type for contentLength
  • Loading branch information
ssut authored and mholt committed Mar 10, 2017
1 parent 52584f7 commit c62b6b9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
9 changes: 8 additions & 1 deletion caddyhttp/fastcgi/fastcgi.go
Expand Up @@ -91,7 +91,14 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
fcgiBackend.SetSendTimeout(rule.SendTimeout)

var resp *http.Response
contentLength, _ := strconv.Atoi(r.Header.Get("Content-Length"))

var contentLength int64
// if ContentLength is already set
if r.ContentLength > 0 {
contentLength = r.ContentLength
} else {
contentLength, _ = strconv.ParseInt(r.Header.Get("Content-Length"), 10, 64)
}
switch r.Method {
case "HEAD":
resp, err = fcgiBackend.Head(env)
Expand Down
10 changes: 5 additions & 5 deletions caddyhttp/fastcgi/fcgiclient.go
Expand Up @@ -112,7 +112,7 @@ type Client interface {
Get(pair map[string]string) (response *http.Response, err error)
Head(pair map[string]string) (response *http.Response, err error)
Options(pairs map[string]string) (response *http.Response, err error)
Post(pairs map[string]string, method string, bodyType string, body io.Reader, contentLength int) (response *http.Response, err error)
Post(pairs map[string]string, method string, bodyType string, body io.Reader, contentLength int64) (response *http.Response, err error)
Close() error
StdErr() bytes.Buffer
SetReadTimeout(time.Duration) error
Expand Down Expand Up @@ -493,7 +493,7 @@ func (c *FCGIClient) Options(p map[string]string) (resp *http.Response, err erro

// Post issues a POST request to the fcgi responder. with request body
// in the format that bodyType specified
func (c *FCGIClient) Post(p map[string]string, method string, bodyType string, body io.Reader, l int) (resp *http.Response, err error) {
func (c *FCGIClient) Post(p map[string]string, method string, bodyType string, body io.Reader, l int64) (resp *http.Response, err error) {
if p == nil {
p = make(map[string]string)
}
Expand All @@ -504,7 +504,7 @@ func (c *FCGIClient) Post(p map[string]string, method string, bodyType string, b
p["REQUEST_METHOD"] = "POST"
}

p["CONTENT_LENGTH"] = strconv.Itoa(l)
p["CONTENT_LENGTH"] = strconv.FormatInt(l, 10)
if len(bodyType) > 0 {
p["CONTENT_TYPE"] = bodyType
} else {
Expand All @@ -518,7 +518,7 @@ func (c *FCGIClient) Post(p map[string]string, method string, bodyType string, b
// as a string key to a list values (url.Values)
func (c *FCGIClient) PostForm(p map[string]string, data url.Values) (resp *http.Response, err error) {
body := bytes.NewReader([]byte(data.Encode()))
return c.Post(p, "POST", "application/x-www-form-urlencoded", body, body.Len())
return c.Post(p, "POST", "application/x-www-form-urlencoded", body, int64(body.Len()))
}

// PostFile issues a POST to the fcgi responder in multipart(RFC 2046) standard,
Expand Down Expand Up @@ -560,7 +560,7 @@ func (c *FCGIClient) PostFile(p map[string]string, data url.Values, file map[str
return
}

return c.Post(p, "POST", bodyType, buf, buf.Len())
return c.Post(p, "POST", bodyType, buf, int64(buf.Len()))
}

// SetReadTimeout sets the read timeout for future calls that read from the
Expand Down
2 changes: 1 addition & 1 deletion caddyhttp/fastcgi/fcgiclient_test.go
Expand Up @@ -117,7 +117,7 @@ func sendFcgi(reqType int, fcgiParams map[string]string, data []byte, posts map[
if len(data) > 0 {
length = len(data)
rd := bytes.NewReader(data)
resp, err = fcgi.Post(fcgiParams, "", "", rd, rd.Len())
resp, err = fcgi.Post(fcgiParams, "", "", rd, int64(rd.Len()))
} else if len(posts) > 0 {
values := url.Values{}
for k, v := range posts {
Expand Down

0 comments on commit c62b6b9

Please sign in to comment.