mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-27 17:56:27 +03:00
Do not set Content-Length in GET and HEAD requests according to pull request #31 (thanks to @buaazp )
This commit is contained in:
@@ -717,11 +717,14 @@ func (req *Request) Write(w *bufio.Writer) error {
|
||||
req.Header.SetMultipartFormBoundary(req.multipartFormBoundary)
|
||||
}
|
||||
|
||||
req.Header.SetContentLength(len(body))
|
||||
hasBody := !req.Header.noBody()
|
||||
if hasBody {
|
||||
req.Header.SetContentLength(len(body))
|
||||
}
|
||||
if err = req.Header.Write(w); err != nil {
|
||||
return err
|
||||
}
|
||||
if !req.Header.noBody() {
|
||||
if hasBody {
|
||||
_, err = w.Write(body)
|
||||
} else if len(body) > 0 {
|
||||
return fmt.Errorf("Non-zero body for non-POST request. body=%q", body)
|
||||
|
||||
@@ -10,6 +10,25 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRequestNoContentLength(t *testing.T) {
|
||||
var r Request
|
||||
|
||||
r.Header.SetMethod("HEAD")
|
||||
r.Header.SetHost("foobar")
|
||||
|
||||
s := r.String()
|
||||
if strings.Contains(s, "Content-Length: ") {
|
||||
t.Fatalf("unexpected content-length in HEAD request %q", s)
|
||||
}
|
||||
|
||||
r.Header.SetMethod("POST")
|
||||
fmt.Fprintf(r.BodyWriter(), "foobar body")
|
||||
s = r.String()
|
||||
if !strings.Contains(s, "Content-Length: ") {
|
||||
t.Fatalf("missing content-length header in non-GET request %q", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestReadGzippedBody(t *testing.T) {
|
||||
var r Request
|
||||
|
||||
|
||||
Reference in New Issue
Block a user