-
Notifications
You must be signed in to change notification settings - Fork 503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: define conditions when content-length should be sent #2305
Changes from 3 commits
670f31f
2461dbc
75aefbd
c8bf1f6
4587f37
194a60c
741b811
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1495,6 +1495,16 @@ function _resume (client, sync) { | |
} | ||
} | ||
|
||
function shouldSendContentLength (contentLength, method) { | ||
if (contentLength > 0) { | ||
if (method === 'POST' || method === 'PUT' || method === 'PATCH') { | ||
return true | ||
} | ||
return false | ||
} | ||
return (method === 'POST' || method === 'PUT' || method === 'PATCH') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what? function shouldSendContentLength(method) {
return (method === 'POST' || method === 'PUT' || method === 'PATCH');
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right! Is this what we want? We're basically saying all non Post/put/patch methods have no defined semantics for a body. Therefore we should not send content-length There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if someone still puts a content body in the request? Does it make sense to still put the content-length in that case? Still, the code must be changed to this: function shouldSendContentLength(contentLength, method) {
return contentLength > 0 || method === 'POST' || method === 'PUT' || method === 'PATCH';
} or this: function shouldSendContentLength(method) {
return method === 'POST' || method === 'PUT' || method === 'PATCH';
} depending on the logic that you want to obtain. |
||
} | ||
|
||
function write (client, request) { | ||
if (client[kHTTPConnVersion] === 'h2') { | ||
writeH2(client, client[kHTTP2Session], request) | ||
|
@@ -1538,7 +1548,9 @@ function write (client, request) { | |
contentLength = null | ||
} | ||
|
||
if (request.contentLength !== null && request.contentLength !== contentLength) { | ||
// https://github.com/nodejs/undici/issues/2046 | ||
// A user agent may send a Content-Length header with 0 value, this should be allowed. | ||
if (shouldSendContentLength(contentLength, method) && request.contentLength !== null && request.contentLength !== contentLength) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Following previous comment, it is better if we offload the |
||
if (client[kStrictContentLength]) { | ||
errorRequest(client, request, new RequestContentLengthMismatchError()) | ||
return false | ||
|
@@ -1755,7 +1767,7 @@ function writeH2 (client, session, request) { | |
contentLength = null | ||
} | ||
|
||
if (request.contentLength != null && request.contentLength !== contentLength) { | ||
if (shouldSendContentLength(contentLength, method) && request.contentLength != null && request.contentLength !== contentLength) { | ||
if (client[kStrictContentLength]) { | ||
errorRequest(client, request, new RequestContentLengthMismatchError()) | ||
return false | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking twice is better if we exclude those who don't explicitly support the request's body
semantics
, so methods likeDELETE
, whose bodies are up to the server to decide what to do with them, can still have acontent-length
header.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made the changes, glad we're getting closer.