Skip to content

integrate response content-type's expected value #936

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/ngx_http_lua_headers.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ ngx_http_lua_ngx_resp_get_headers(lua_State *L)
ngx_list_part_t *part;
ngx_table_elt_t *header;
ngx_http_request_t *r;
ngx_http_lua_ctx_t *ctx;
ngx_int_t rc;
u_char *lowcase_key = NULL;
size_t lowcase_key_sz = 0;
ngx_uint_t i;
Expand Down Expand Up @@ -475,6 +477,22 @@ ngx_http_lua_ngx_resp_get_headers(lua_State *L)
return luaL_error(L, "no request object found");
}

ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no ctx found");
}

if (!ctx->headers_set) {
rc = ngx_http_lua_set_content_type(r);
if (rc != NGX_OK) {
return luaL_error(L,
"failed to set default content type: %d",
(int) rc);
}

Copy link
Member

@agentzh agentzh Dec 27, 2016

Choose a reason for hiding this comment

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

This line has trailing spaces. Better use the ngx-releng tool to check such things. For this case, ngx-releng reports the following error (the error message was actually in red):

src/ngx_http_lua_headers.c:
found line tailing spaces
492:

ctx->headers_set = 1;
Copy link
Member

Choose a reason for hiding this comment

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

Style: need a blank line after }.

}

ngx_http_lua_check_fake_request(L, r);

part = &r->headers_out.headers.part;
Expand Down Expand Up @@ -603,12 +621,19 @@ ngx_http_lua_ngx_header_get(lua_State *L)
ngx_uint_t i;
size_t len;
ngx_http_lua_loc_conf_t *llcf;
ngx_http_lua_ctx_t *ctx;
ngx_int_t rc;

r = ngx_http_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request object found");
}

ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no ctx found");
}

ngx_http_lua_check_fake_request(L, r);

/* we skip the first argument that is the table */
Expand Down Expand Up @@ -640,6 +665,17 @@ ngx_http_lua_ngx_header_get(lua_State *L)

key.len = len;

if (!ctx->headers_set) {
rc = ngx_http_lua_set_content_type(r);
if (rc != NGX_OK) {
return luaL_error(L,
"failed to set default content type: %d",
(int) rc);
}

ctx->headers_set = 1;
Copy link
Member

Choose a reason for hiding this comment

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

I'm afraid this is wrong here, better remove this line, like this case

local content_type = ngx.header.content_type
ngx.header.foo = "foo"

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, it's my mistake, just forgot it:)

}

return ngx_http_lua_get_output_header(L, r, &key);
}

Expand Down
70 changes: 69 additions & 1 deletion t/016-resp-header.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use Test::Nginx::Socket::Lua;

repeat_each(2);

plan tests => repeat_each() * (blocks() * 3 + 38);
plan tests => repeat_each() * (blocks() * 3 + 41);

#no_diff();
no_long_string();
Expand Down Expand Up @@ -1441,3 +1441,71 @@ Content-Type: ; blah
test
--- no_error_log
[error]



=== TEST 69: return the matched content-type instead of default_type
--- http_config
types {
image/png png;
}
--- config
location /set/ {
default_type text/html;
content_by_lua_block {
ngx.say(ngx.header["content-type"])
}
}
--- request
GET /set/hello.png
--- response_headers
Content-Type: image/png
--- response_body
image/png
--- no_error_log
[error]



=== TEST 70: always return the matched content-type
--- config
location /set/ {
default_type "image/png";
content_by_lua_block {
ngx.say(ngx.header["content-type"])
ngx.say(ngx.header["content-type"])
}
}
--- request
GET /set/hello.png
--- response_headers
Content-Type: image/png
--- response_body
image/png
image/png
--- no_error_log
[error]



=== TEST 71: return the matched content-type after ngx.resp.get_headers()
--- http_config
types {
image/png png;
}
--- config
location /set/ {
default_type text/html;
content_by_lua_block {
local h = ngx.resp.get_headers()
ngx.say(h["content-type"])
}
}
--- request
GET /set/hello.png
--- response_headers
Content-Type: image/png
--- response_body
image/png
--- no_error_log
[error]