Skip to content

change: avoided generating Content-Type header when getting response … #1445

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

Conversation

spacewander
Copy link
Member

…headers.

The previous behavior assumed that Nginx will always generate the
Content-Type header if it is missing.
However, in some cases, like the upstream response doesn't have the Content-Type
header, Nginx will not generate the Content-Type header. So generating
Content-Type header when getting response headers may create an unexpected
Content-Type header in the response.

I hereby granted the copyright of the changes in this pull request
to the authors of this lua-nginx-module project.

@thibaultcha
Copy link
Member

Are we fine with breaking the behavior described in #92? Using the nginx configuration provided in the issue, the current master branch produces the following responses (which seem correct):

$ http :8080/set/  
HTTP/1.1 200 OK
Connection: keep-alive
Content-Type: text/html
Date: Fri, 04 Jan 2019 00:05:06 GMT
Server: nginx/1.13.6 (no pool)
Transfer-Encoding: chunked
x-hello: world

text/html

$ http :8080/noset/                      
HTTP/1.1 200 OK
Connection: keep-alive
Content-Type: text/html
Date: Fri, 04 Jan 2019 00:05:12 GMT
Server: nginx/1.13.6 (no pool)
Transfer-Encoding: chunked

text/html

But with this patch, the Content-Type header isn't even set in the first case, and is not retrieved by the ngx_lua API at all (while lua_use_default_type is enabled):

$ http :8080/set/
HTTP/1.1 200 OK
Connection: keep-alive
Date: Fri, 04 Jan 2019 00:04:14 GMT
Server: nginx/1.13.6 (no pool)
Transfer-Encoding: chunked
x-hello: world

nil

$ http :8080/noset/
HTTP/1.1 200 OK
Connection: keep-alive
Content-Type: text/html
Date: Fri, 04 Jan 2019 00:04:20 GMT
Server: nginx/1.13.6 (no pool)
Transfer-Encoding: chunked

nil

@spacewander
Copy link
Member Author

@thibaultcha
I have submitted another PR to fix the problem you pointed out: #1449

@thibaultcha
Copy link
Member

@spacewander It seems to work well 👍 However, since breaking the use case mentioned above is a regression introduced by this commit, the commit in your other PR (#1449) should be squashed into this PR (in the resulting commit). This is in order to ensure atomicity. After all, the behavior described above is not failing in the master branch, only because of this commit, so it is this commit that must be updated accordingly.

Will you close your other PR and squash its patch in this PR? Thanks!

@thibaultcha
Copy link
Member

When rebasing #1449 on top of this branch, we do get the Content-Type header in the response as expected:

$ http :8080/set/
HTTP/1.1 200 OK
Connection: keep-alive
Content-Type: text/html
Date: Sat, 05 Jan 2019 03:10:52 GMT
Server: nginx/1.13.6 (no pool)
Transfer-Encoding: chunked
x-hello: world

nil

chasum@x1c ~ $ http :8080/noset/
HTTP/1.1 200 OK
Connection: keep-alive
Content-Type: text/html
Date: Sat, 05 Jan 2019 03:10:56 GMT
Server: nginx/1.13.6 (no pool)
Transfer-Encoding: chunked

nil

But we still experience a regression in that accessing ngx.header["content-type"] returns nil.

@spacewander
Copy link
Member Author

However, since breaking the use case mentioned above is a regression introduced by this commit

It's my bad that I didn't mention that the regression is introduced by the 017e004. Currently, if one has set the response header (which doesn't need to be Content-Type), OpenResty will not generate the default Content-Type. This is OK before that commit, because we used to generate the Content-Type once the response header is set. You could reproduce the issue via replacing ngx.print(ngx.header["content-type"]) with ngx.print('ok').

But we still experience a regression in that accessing ngx.header["content-type"] returns nil.

I think it is not a regression, but a better way to deal with missing Content-Type header.
Previously, ngx.header["content-type"] returns the default type when there is not Content-Type header, because it assumed the default type will be used eventually. However, this assumption is incorrect (for instance, in the test cases in this PR).

Instead of overdoing, I think it would be better to return the real Content-Type header. When the header is missing, return nil instead of the default one. If you run ngx.header["content-type"] in header_filter_by_lua phase, you could get the final, correct Content-Type header.

@thibaultcha
Copy link
Member

@spacewander Gotcha. I confirmed the regression indeed comes from 017e004 and pushed fixes and tweaks to your proposed patch in #1449. Thank you for clarifying.

I think it is not a regression, but a better way to deal with missing Content-Type header.

Yes, I have to say I agree.

@thibaultcha thibaultcha force-pushed the avoid_generating_content_type_in_getting_resp_header branch from ddd4fd5 to 617e09c Compare January 5, 2019 19:12
… getting all response headers.

The previous behavior assumed that nginx will always generate the
Content-Type header if it is missing. However in some cases (such as the
upstream response not having a Content-Type header), nginx will not
generate the Content-Type header itself.

Thus, generating the Content-Type when getting response headers
may create an unexpected Content-Type header in the response.

Signed-off-by: Thibault Charbonnier <[email protected]>
@thibaultcha thibaultcha force-pushed the avoid_generating_content_type_in_getting_resp_header branch from 617e09c to efdae5a Compare January 5, 2019 19:36
@thibaultcha thibaultcha merged commit fbb8919 into openresty:master Jan 5, 2019
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.

2 participants