From 12a3903f2bc22bcc4f5f8e2abcc3922b612b8871 Mon Sep 17 00:00:00 2001 From: Segflow Date: Sun, 23 Aug 2020 12:27:46 +0100 Subject: [PATCH] set Content-Length on PATCH requests simliar to POST and PUT --- src/net/http/requestwrite_test.go | 20 ++++++++++++++++++++ src/net/http/transfer.go | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/net/http/requestwrite_test.go b/src/net/http/requestwrite_test.go index fc6257cb3380b3..9ac6701cfdd523 100644 --- a/src/net/http/requestwrite_test.go +++ b/src/net/http/requestwrite_test.go @@ -588,6 +588,26 @@ var reqWriteTests = []reqWriteTest{ }, WantError: errors.New("net/http: can't write control character in Request.URL"), }, + + 26: { // Request with nil body and PATCH method. Issue #40978 + Req: Request{ + Method: "PATCH", + URL: mustParseURL("/"), + Host: "example.com", + ProtoMajor: 1, + ProtoMinor: 1, + ContentLength: 0, // as if unset by user + }, + Body: nil, + WantWrite: "PATCH / HTTP/1.1\r\n" + + "Host: example.com\r\n" + + "User-Agent: Go-http-client/1.1\r\n" + + "Content-Length: 0\r\n\r\n", + WantProxy: "PATCH / HTTP/1.1\r\n" + + "Host: example.com\r\n" + + "User-Agent: Go-http-client/1.1\r\n" + + "Content-Length: 0\r\n\r\n", + }, } func TestRequestWrite(t *testing.T) { diff --git a/src/net/http/transfer.go b/src/net/http/transfer.go index 50d434b1fb0960..ab009177bc7bb3 100644 --- a/src/net/http/transfer.go +++ b/src/net/http/transfer.go @@ -258,7 +258,7 @@ func (t *transferWriter) shouldSendContentLength() bool { return false } // Many servers expect a Content-Length for these methods - if t.Method == "POST" || t.Method == "PUT" { + if t.Method == "POST" || t.Method == "PUT" || t.Method == "PATCH" { return true } if t.ContentLength == 0 && isIdentity(t.TransferEncoding) {