Skip to content
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

Merged
merged 1 commit into from
Mar 29, 2018

Conversation

bungle
Copy link
Member

@bungle bungle commented Mar 28, 2018

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

@@ -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

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?

Copy link
Member Author

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"

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.

Copy link
Member Author

@bungle bungle Mar 28, 2018

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).

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^- that was discussed below.

@bungle bungle force-pushed the fix/remove-content-type-204 branch from 685f80f to f65b9fa Compare March 28, 2018 20:04
@bungle bungle requested a review from thibaultcha March 28, 2018 20:18
@@ -1,5 +1,6 @@
return [[
charset UTF-8;
default_type '';
Copy link
Member

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...

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

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

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, there is good!

@@ -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"
Copy link
Member

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.

Copy link
Member Author

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:

if content then

(there is no content)?

Copy link
Member

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).

Copy link
Member Author

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.

@bungle bungle force-pushed the fix/remove-content-type-204 branch from f65b9fa to 8b2726b Compare March 29, 2018 00:14
@bungle bungle changed the base branch from master to next March 29, 2018 00:14
@bungle bungle force-pushed the fix/remove-content-type-204 branch from 8b2726b to 9e98db8 Compare March 29, 2018 00:16
@bungle bungle changed the title fix(responses) to no set content type header for 204 (no content) fix(responses) to not set content type header for 204 (no content) Mar 29, 2018
@bungle bungle force-pushed the fix/remove-content-type-204 branch from 9e98db8 to 7074537 Compare March 29, 2018 00:52
ngx.say(encoded)
else
ngx.header["Content-Type"] = "application/json; charset=utf-8"
ngx.say(encoded)
Copy link
Member

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)

Copy link
Member

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 :)

Copy link
Member

@thibaultcha thibaultcha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@thibaultcha
Copy link
Member

Let's wait on CI and merge if it passes, thanks!

@thibaultcha thibaultcha merged commit cbe4a61 into next Mar 29, 2018
@thibaultcha thibaultcha deleted the fix/remove-content-type-204 branch March 29, 2018 02:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants