-
-
Notifications
You must be signed in to change notification settings - Fork 749
Fix/missing content length header on empty post req #1003
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/missing content length header on empty post req #1003
Conversation
…ies SetContentLength true
…h SetContentLength override scenario
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.
@Libero-Dev I have read the issue #996 and this PR description along with the code changes. Thanks for the details.
Can you please have a look at these lines and apply the fix accordingly instead of adding new repeated code lines to fix?
Lines 208 to 215 in 1e19d6b
// by default resty won't set content length, you can if you want to :) | |
if c.setContentLength || r.setContentLength { | |
if r.bodyBuf == nil { | |
r.Header.Set(hdrContentLengthKey, "0") | |
} else { | |
r.Header.Set(hdrContentLengthKey, strconv.Itoa(r.bodyBuf.Len())) | |
} | |
} |
@jeevatkm there is reliance on the handleRequestBody() function being triggered before proceeding with the rest of the logic. Placing the conditional check where you have recommended will break tests and continue to exhibit missing content-length header for recipients of the request. I have compromised with a cleaner alternative that attempts to keep both requirements. |
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.
@Libero-Dev Thanks for the updates.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## v2 #1003 +/- ##
=======================================
Coverage 95.69% 95.70%
=======================================
Files 14 14
Lines 2207 2211 +4
=======================================
+ Hits 2112 2116 +4
Misses 71 71
Partials 24 24 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This change aims to resolve issue #996 by changing requests that contain nil body to http.NoBody when they have also specified to "SetContentLength" as true.
Validity as to whether to merge this or not depends on multiple factors.
Setting the bodyBuf value to http.NoBody signals to the http library when creating the request that the body is no longer nil and a Content-Length header component can be created for the request.
Considering this is a feature, albeit a poorly documented one in the standard http library it does seem to be behaving as intended?
In resty setting SetContentLength(true) and r.Body = nil, could be considered as the user intentionally overriding this requirement to force the Content-Length header on whilst passing in a nil body.
Ultimately, either the intention of the http libraries functionality should be obeyed or we should consider this unique resty request setting combination as an intentional force override to achieve a valid Content-Length header with a nil body.
Additional Notes:
I have had to update middleware_test.go:Test_parseRequestBody() for the reason being that now that nil body + SetContentLength(true) is supported in this PR change this now results in the expectedBodyBuf value to change since in this scenario the bodyBuf changes to http.NoBody.