Skip to content

Commit

Permalink
net/http: set Content-Length:0 for empty PATCH requests as with POST,…
Browse files Browse the repository at this point in the history
… 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]>
  • Loading branch information
Segflow authored and odeke-em committed Aug 24, 2020
1 parent 494ec85 commit fb5c3ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/net/http/requestwrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/net/http/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit fb5c3ea

Please sign in to comment.