Skip to content

Commit 617e09c

Browse files
spacewanderthibaultcha
authored andcommitted
change: avoided generating Content-Type when getting 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]>
1 parent cec2a09 commit 617e09c

5 files changed

+92
-204
lines changed

README.markdown

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4147,25 +4147,6 @@ to be returned when reading `ngx.header.Foo`.
41474147

41484148
Note that `ngx.header` is not a normal Lua table and as such, it is not possible to iterate through it using the Lua `ipairs` function.
41494149

4150-
Note: if the current response does not have a Content-Type header, but you use
4151-
`ngx.header` to attempt retrieving it, a side effect will be that a
4152-
Content-Type header will be created and returned according to your nginx
4153-
configuration.
4154-
4155-
For example:
4156-
4157-
```nginx
4158-
4159-
location = /t {
4160-
default_type text/html;
4161-
content_by_lua_block {
4162-
-- even if the response had no Content-Type header,
4163-
-- it will now have: Content-Type: text/html
4164-
ngx.say(ngx.header['Content-Type'])
4165-
}
4166-
}
4167-
```
4168-
41694150
For reading *request* headers, use the [ngx.req.get_headers](#ngxreqget_headers) function instead.
41704151

41714152
[Back to TOC](#nginx-api-for-lua)
@@ -4191,24 +4172,6 @@ Returns a Lua table holding all the current response headers for the current req
41914172
end
41924173
```
41934174

4194-
Note: if the current response does not have a Content-Type header, but you use
4195-
`ngx.resp.get_headers` to attempt retrieving it, a side effect will
4196-
be that a Content-Type header will be created and returned according to your
4197-
nginx configuration.
4198-
4199-
For example:
4200-
4201-
```nginx
4202-
4203-
location = /t {
4204-
default_type text/html;
4205-
content_by_lua_block {
4206-
-- the Content-Type header will be text/html
4207-
ngx.say(ngx.resp.get_headers()['Content-Type'])
4208-
}
4209-
}
4210-
```
4211-
42124175
This function has the same signature as [ngx.req.get_headers](#ngxreqget_headers) except getting response headers instead of request headers.
42134176

42144177
Note that a maximum of 100 response headers are parsed by default (including those with the same name) and that additional response headers are silently discarded to guard against potential denial of service attacks. Since `v0.10.13`, when the limit is exceeded, it will return a second value which is the string `"truncated"`.

doc/HttpLuaModule.wiki

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3433,24 +3433,6 @@ to be returned when reading <code>ngx.header.Foo</code>.
34333433
34343434
Note that <code>ngx.header</code> is not a normal Lua table and as such, it is not possible to iterate through it using the Lua <code>ipairs</code> function.
34353435
3436-
Note: if the current response does not have a Content-Type header, but you use
3437-
<code>ngx.header</code> to attempt retrieving it, a side effect will be that a
3438-
Content-Type header will be created and returned according to your nginx
3439-
configuration.
3440-
3441-
For example:
3442-
3443-
<geshi lang="nginx">
3444-
location = /t {
3445-
default_type text/html;
3446-
content_by_lua_block {
3447-
-- even if the response had no Content-Type header,
3448-
-- it will now have: Content-Type: text/html
3449-
ngx.say(ngx.header['Content-Type'])
3450-
}
3451-
}
3452-
</geshi>
3453-
34543436
For reading ''request'' headers, use the [[#ngx.req.get_headers|ngx.req.get_headers]] function instead.
34553437
34563438
== ngx.resp.get_headers ==
@@ -3472,23 +3454,6 @@ for k, v in pairs(h) do
34723454
end
34733455
</geshi>
34743456
3475-
Note: if the current response does not have a Content-Type header, but you use
3476-
<code>ngx.resp.get_headers</code>, a side effect will be that a Content-Type
3477-
header will be created and returned according to your nginx configuration.
3478-
3479-
For example:
3480-
3481-
<geshi lang="nginx">
3482-
location = /t {
3483-
default_type text/html;
3484-
content_by_lua_block {
3485-
-- even if the response had no Content-Type header,
3486-
-- it will now have: Content-Type: text/html
3487-
ngx.say(ngx.resp.get_headers()['Content-Type'])
3488-
}
3489-
}
3490-
</geshi>
3491-
34923457
This function has the same signature as [[#ngx.req.get_headers|ngx.req.get_headers]] except getting response headers instead of request headers.
34933458
34943459
Note that a maximum of 100 response headers are parsed by default (including those with the same name) and that additional response headers are silently discarded to guard against potential denial of service attacks. Since <code>v0.10.13</code>, when the limit is exceeded, it will return a second value which is the string `"truncated"`.

src/ngx_http_lua_headers.c

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,6 @@ ngx_http_lua_ngx_resp_get_headers(lua_State *L)
505505
ngx_table_elt_t *header;
506506
ngx_http_request_t *r;
507507
ngx_http_lua_ctx_t *ctx;
508-
ngx_int_t rc;
509508
u_char *lowcase_key = NULL;
510509
size_t lowcase_key_sz = 0;
511510
ngx_uint_t i;
@@ -562,17 +561,6 @@ ngx_http_lua_ngx_resp_get_headers(lua_State *L)
562561
lua_setmetatable(L, -2);
563562
}
564563

565-
if (!r->headers_out.content_type.len) {
566-
rc = ngx_http_lua_set_content_type(r, ctx);
567-
if (rc != NGX_OK) {
568-
return luaL_error(L,
569-
"failed to set default content type: %d",
570-
(int) rc);
571-
}
572-
573-
ctx->headers_set = 1;
574-
}
575-
576564
#if 1
577565
if (r->headers_out.content_type.len) {
578566
extra++;
@@ -1405,21 +1393,12 @@ ngx_http_lua_ffi_get_resp_header(ngx_http_request_t *r,
14051393
break;
14061394

14071395
case 12:
1408-
if (ngx_strncasecmp(key_buf, (u_char *) "Content-Type", 12) == 0) {
1409-
if (!r->headers_out.content_type.len) {
1410-
if (ngx_http_lua_set_content_type(r, ctx) != NGX_OK) {
1411-
*errmsg = "failed to set default content type";
1412-
return NGX_ERROR;
1413-
}
1414-
1415-
ctx->headers_set = 1;
1416-
}
1417-
1418-
if (r->headers_out.content_type.len) {
1419-
values[0].data = r->headers_out.content_type.data;
1420-
values[0].len = r->headers_out.content_type.len;
1421-
return 1;
1422-
}
1396+
if (ngx_strncasecmp(key_buf, (u_char *) "Content-Type", 12) == 0
1397+
&& r->headers_out.content_type.len)
1398+
{
1399+
values[0].data = r->headers_out.content_type.data;
1400+
values[0].len = r->headers_out.content_type.len;
1401+
return 1;
14231402
}
14241403

14251404
break;

src/ngx_http_lua_headers_out.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,6 @@ ngx_http_lua_get_output_header(lua_State *L, ngx_http_request_t *r,
536536
{
537537
ngx_table_elt_t *h;
538538
ngx_list_part_t *part;
539-
ngx_int_t rc;
540539
ngx_uint_t i;
541540
unsigned found;
542541

@@ -555,23 +554,12 @@ ngx_http_lua_get_output_header(lua_State *L, ngx_http_request_t *r,
555554
break;
556555

557556
case 12:
558-
if (ngx_strncasecmp(key->data, (u_char *) "Content-Type", 12) == 0) {
559-
if (r->headers_out.content_type.len == 0) {
560-
rc = ngx_http_lua_set_content_type(r, ctx);
561-
if (rc != NGX_OK) {
562-
return luaL_error(L,
563-
"failed to set default content type: %d",
564-
(int) rc);
565-
}
566-
567-
ctx->headers_set = 1;
568-
}
569-
570-
if (r->headers_out.content_type.len) {
571-
lua_pushlstring(L, (char *) r->headers_out.content_type.data,
572-
r->headers_out.content_type.len);
573-
return 1;
574-
}
557+
if (ngx_strncasecmp(key->data, (u_char *) "Content-Type", 12) == 0
558+
&& r->headers_out.content_type.len)
559+
{
560+
lua_pushlstring(L, (char *) r->headers_out.content_type.data,
561+
r->headers_out.content_type.len);
562+
return 1;
575563
}
576564

577565
break;

0 commit comments

Comments
 (0)