Skip to content

Commit 5fdbb56

Browse files
moonmingagentzh
authored andcommitted
bugfix: setting response headers would change the Content-Type response header.
thanks leafo for the report in openresty/openresty#92. Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
1 parent b3c872e commit 5fdbb56

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

src/ngx_http_lua_headers.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,8 @@ ngx_http_lua_ngx_resp_get_headers(lua_State *L)
444444
ngx_list_part_t *part;
445445
ngx_table_elt_t *header;
446446
ngx_http_request_t *r;
447+
ngx_http_lua_ctx_t *ctx;
448+
ngx_int_t rc;
447449
u_char *lowcase_key = NULL;
448450
size_t lowcase_key_sz = 0;
449451
ngx_uint_t i;
@@ -475,6 +477,22 @@ ngx_http_lua_ngx_resp_get_headers(lua_State *L)
475477
return luaL_error(L, "no request object found");
476478
}
477479

480+
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
481+
if (ctx == NULL) {
482+
return luaL_error(L, "no ctx found");
483+
}
484+
485+
if (!ctx->headers_set) {
486+
rc = ngx_http_lua_set_content_type(r);
487+
if (rc != NGX_OK) {
488+
return luaL_error(L,
489+
"failed to set default content type: %d",
490+
(int) rc);
491+
}
492+
493+
ctx->headers_set = 1;
494+
}
495+
478496
ngx_http_lua_check_fake_request(L, r);
479497

480498
part = &r->headers_out.headers.part;
@@ -603,12 +621,19 @@ ngx_http_lua_ngx_header_get(lua_State *L)
603621
ngx_uint_t i;
604622
size_t len;
605623
ngx_http_lua_loc_conf_t *llcf;
624+
ngx_http_lua_ctx_t *ctx;
625+
ngx_int_t rc;
606626

607627
r = ngx_http_lua_get_req(L);
608628
if (r == NULL) {
609629
return luaL_error(L, "no request object found");
610630
}
611631

632+
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
633+
if (ctx == NULL) {
634+
return luaL_error(L, "no ctx found");
635+
}
636+
612637
ngx_http_lua_check_fake_request(L, r);
613638

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

641666
key.len = len;
642667

668+
if (!ctx->headers_set) {
669+
rc = ngx_http_lua_set_content_type(r);
670+
if (rc != NGX_OK) {
671+
return luaL_error(L,
672+
"failed to set default content type: %d",
673+
(int) rc);
674+
}
675+
676+
ctx->headers_set = 1;
677+
}
678+
643679
return ngx_http_lua_get_output_header(L, r, &key);
644680
}
645681

t/016-resp-header.t

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use Test::Nginx::Socket::Lua;
88

99
repeat_each(2);
1010

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

1313
#no_diff();
1414
no_long_string();
@@ -1441,3 +1441,71 @@ Content-Type: ; blah
14411441
test
14421442
--- no_error_log
14431443
[error]
1444+
1445+
1446+
1447+
=== TEST 69: return the matched content-type instead of default_type
1448+
--- http_config
1449+
types {
1450+
image/png png;
1451+
}
1452+
--- config
1453+
location /set/ {
1454+
default_type text/html;
1455+
content_by_lua_block {
1456+
ngx.say(ngx.header["content-type"])
1457+
}
1458+
}
1459+
--- request
1460+
GET /set/hello.png
1461+
--- response_headers
1462+
Content-Type: image/png
1463+
--- response_body
1464+
image/png
1465+
--- no_error_log
1466+
[error]
1467+
1468+
1469+
1470+
=== TEST 70: always return the matched content-type
1471+
--- config
1472+
location /set/ {
1473+
default_type "image/png";
1474+
content_by_lua_block {
1475+
ngx.say(ngx.header["content-type"])
1476+
ngx.say(ngx.header["content-type"])
1477+
}
1478+
}
1479+
--- request
1480+
GET /set/hello.png
1481+
--- response_headers
1482+
Content-Type: image/png
1483+
--- response_body
1484+
image/png
1485+
image/png
1486+
--- no_error_log
1487+
[error]
1488+
1489+
1490+
1491+
=== TEST 71: return the matched content-type after ngx.resp.get_headers()
1492+
--- http_config
1493+
types {
1494+
image/png png;
1495+
}
1496+
--- config
1497+
location /set/ {
1498+
default_type text/html;
1499+
content_by_lua_block {
1500+
local h = ngx.resp.get_headers()
1501+
ngx.say(h["content-type"])
1502+
}
1503+
}
1504+
--- request
1505+
GET /set/hello.png
1506+
--- response_headers
1507+
Content-Type: image/png
1508+
--- response_body
1509+
image/png
1510+
--- no_error_log
1511+
[error]

0 commit comments

Comments
 (0)