Skip to content

Commit fb5c3ea

Browse files
Segflowodeke-em
authored andcommitted
net/http: set Content-Length:0 for empty PATCH requests as with POST, PATCH
Sets Content-Length:0 for nil bodies in PATCH requests, as we already do for POST and PUT requests. RFC 2616 mentions that unless a method’s Content-Length is forbidden it can send one. In the wild, we’ve found that Microsoft Azure’s DataLake Gen2 storage API https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/update deliberately rejects PATCH requests without a Content-Length, yet there is no workaround for setting that header when trying to flush the content of a file which was uploaded in a previous request. Fixes #40978 Change-Id: Ib0a623b907d827a1c5ee431dca3c41024fa291c5 GitHub-Last-Rev: 12a3903 GitHub-Pull-Request: #40991 Reviewed-on: https://go-review.googlesource.com/c/go/+/250039 Run-TryBot: Emmanuel Odeke <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Emmanuel Odeke <[email protected]>
1 parent 494ec85 commit fb5c3ea

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/net/http/requestwrite_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,26 @@ var reqWriteTests = []reqWriteTest{
588588
},
589589
WantError: errors.New("net/http: can't write control character in Request.URL"),
590590
},
591+
592+
26: { // Request with nil body and PATCH method. Issue #40978
593+
Req: Request{
594+
Method: "PATCH",
595+
URL: mustParseURL("/"),
596+
Host: "example.com",
597+
ProtoMajor: 1,
598+
ProtoMinor: 1,
599+
ContentLength: 0, // as if unset by user
600+
},
601+
Body: nil,
602+
WantWrite: "PATCH / HTTP/1.1\r\n" +
603+
"Host: example.com\r\n" +
604+
"User-Agent: Go-http-client/1.1\r\n" +
605+
"Content-Length: 0\r\n\r\n",
606+
WantProxy: "PATCH / HTTP/1.1\r\n" +
607+
"Host: example.com\r\n" +
608+
"User-Agent: Go-http-client/1.1\r\n" +
609+
"Content-Length: 0\r\n\r\n",
610+
},
591611
}
592612

593613
func TestRequestWrite(t *testing.T) {

src/net/http/transfer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func (t *transferWriter) shouldSendContentLength() bool {
258258
return false
259259
}
260260
// Many servers expect a Content-Length for these methods
261-
if t.Method == "POST" || t.Method == "PUT" {
261+
if t.Method == "POST" || t.Method == "PUT" || t.Method == "PATCH" {
262262
return true
263263
}
264264
if t.ContentLength == 0 && isIdentity(t.TransferEncoding) {

0 commit comments

Comments
 (0)