-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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(responses) to not set content type header for 204 (no content) #3351
Conversation
kong/tools/responses.lua
Outdated
@@ -123,7 +123,11 @@ local function send_response(status_code) | |||
end | |||
|
|||
ngx.status = status_code | |||
ngx.header["Content-Type"] = "application/json; charset=utf-8" | |||
|
|||
if status_code ~= 204 then |
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.
Should it be _M.status_codes.HTTP_NO_CONTENT
instead of 204?
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.
@aleksandr-vin thanks!
@@ -123,7 +123,11 @@ local function send_response(status_code) | |||
end | |||
|
|||
ngx.status = status_code | |||
ngx.header["Content-Type"] = "application/json; charset=utf-8" |
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.
Does it really fix #3342?
Here it is about application/json
and in the issue it was text/plain
.
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.
@aleksandr-vin good point, does this come out of Nginx custom error pages, then?
Edit: actually not, it comes from Nginx directive default_type
being text/plain
(updated PR to remove the default).
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.
@aleksandr-vin I made a new push, this one solves text/plain
case as well.
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.
I don't think this should be removed? This is Kong-produced responses, not upstream responses.
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.
^- that was discussed below.
685f80f
to
f65b9fa
Compare
kong/templates/nginx_kong.lua
Outdated
@@ -1,5 +1,6 @@ | |||
return [[ | |||
charset UTF-8; | |||
default_type ''; |
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.
unfortunately any configuration template change means it is breaking (for custom nginx configs) and should be opened against next
instead...
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.
Also, all changed applied to nginx_kong.lua
must be applied to the test template as well. And also, it would be better to nest this setting under the proxy server {}
block.
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.
Thanks for reminding me about the test ones.
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.
Test template is updated. This is now rebased on next
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.
I think we should still have it moved to the proxy server {}
block?
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.
I moved it to proxy location /
block. I think it is fine like that.
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.
Yep, there is good!
kong/tools/responses.lua
Outdated
@@ -144,6 +143,7 @@ local function send_response(status_code) | |||
ngx.log(ngx.ERR, "[admin] could not encode value: ", err) | |||
end | |||
|
|||
ngx.header["Content-Type"] = "application/json; charset=utf-8" |
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.
Oh, you moved it here... Better not moving it down here unnecessarily? This instruction is currently voluntarily placed before the response body is produced for cognitive reasons.
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.
Do you feel it needs to be added even when:
Line 139 in f65b9fa
if content then |
(there is no content)?
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.
Yeah, maybe better to keep it in this branch then (but if we hit an error with the JSON-encoding, we might not want to set this header either).
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.
Yes, I made it only happen when we are actually outputting someting.
f65b9fa
to
8b2726b
Compare
8b2726b
to
9e98db8
Compare
9e98db8
to
7074537
Compare
ngx.say(encoded) | ||
else | ||
ngx.header["Content-Type"] = "application/json; charset=utf-8" | ||
ngx.say(encoded) |
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.
👍
We may need some unit tests for this (they exist for this module already)
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.
Ah, my bad, I see you already added it :)
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.
LGTM!
Let's wait on CI and merge if it passes, thanks! |
Summary
When there is no content (204 status code), there is no need to set Content-Type header. It may even be misleading.
Issues resolved
Fix #3342