fastcgi: check for CONTENT_LENGTH when sending requests

This commit is contained in:
WeidiDeng 2024-10-24 10:25:12 +08:00
parent eaaa2e5872
commit 78265c6830
No known key found for this signature in database
GPG Key ID: 25F87CE1741EC7CD
1 changed files with 13 additions and 0 deletions

View File

@ -26,6 +26,7 @@ package fastcgi
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"io" "io"
"mime/multipart" "mime/multipart"
"net" "net"
@ -136,6 +137,18 @@ type client struct {
// Do made the request and returns a io.Reader that translates the data read // Do made the request and returns a io.Reader that translates the data read
// from fcgi responder out of fcgi packet before returning it. // from fcgi responder out of fcgi packet before returning it.
func (c *client) Do(p map[string]string, req io.Reader) (r io.Reader, err error) { func (c *client) Do(p map[string]string, req io.Reader) (r io.Reader, err error) {
// check for CONTENT_LENGTH, since the lack of it or wrong value will cause the backend to hang
if clStr, ok := p["CONTENT_LENGTH"]; !ok {
return nil, caddyhttp.Error(http.StatusLengthRequired, nil)
} else {
cl, err := strconv.ParseInt(clStr, 10, 64)
// stdlib won't return a negative Content-Length, but we check just in case,
// the most likely cause is from a missing content length, which is -1
if err != nil || cl < 0 {
return nil, caddyhttp.Error(http.StatusLengthRequired, err)
}
}
writer := &streamWriter{c: c} writer := &streamWriter{c: c}
writer.buf = bufPool.Get().(*bytes.Buffer) writer.buf = bufPool.Get().(*bytes.Buffer)
writer.buf.Reset() writer.buf.Reset()