Skip to content

Commit 3f39535

Browse files
author
Aviram
committed
Added an optional parameter for ngx.req.set_header and ngx.req.clear_header that determines whether or not to replace underscores with hyphens.
Previously, underscores were replaced unconditionally. Currently each of the functions has another boolean argument. If it's false, underscores would not be touched. If it's true, they would. The default value of the argument is true.
1 parent 0ac676f commit 3f39535

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

src/ngx_http_lua_headers.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -581,12 +581,26 @@ ngx_http_lua_ngx_header_set(lua_State *L)
581581
static int
582582
ngx_http_lua_ngx_req_header_clear(lua_State *L)
583583
{
584-
if (lua_gettop(L) != 1) {
585-
return luaL_error(L, "expecting one arguments, but seen %d",
584+
ngx_uint_t n;
585+
n = lua_gettop(L);
586+
if ((n != 1) && (n != 2)) {
587+
return luaL_error(L, "expecting one or two arguments, but seen %d",
586588
lua_gettop(L));
587589
}
588590

589-
lua_pushnil(L);
591+
if (n == 2) {
592+
u_char *p;
593+
size_t len;
594+
int replace_underscores = 1;
595+
p = (u_char *) luaL_checklstring(L, 1, &len);
596+
replace_underscores = lua_toboolean(L, 2);
597+
lua_pop(L, 2);
598+
lua_pushlstring(L, (char *) p, len);
599+
lua_pushnil(L);
600+
lua_pushboolean(L, replace_underscores);
601+
} else {
602+
lua_pushnil(L);
603+
}
590604

591605
return ngx_http_lua_ngx_req_header_set_helper(L);
592606
}
@@ -595,7 +609,7 @@ ngx_http_lua_ngx_req_header_clear(lua_State *L)
595609
static int
596610
ngx_http_lua_ngx_req_header_set(lua_State *L)
597611
{
598-
if (lua_gettop(L) != 2) {
612+
if ((lua_gettop(L) != 2) && (lua_gettop(L) != 3)) {
599613
return luaL_error(L, "expecting two arguments, but seen %d",
600614
lua_gettop(L));
601615
}
@@ -615,6 +629,7 @@ ngx_http_lua_ngx_req_header_set_helper(lua_State *L)
615629
size_t len;
616630
ngx_int_t rc;
617631
ngx_uint_t n;
632+
int replace_underscores = 1;
618633

619634
lua_pushlightuserdata(L, &ngx_http_lua_request_key);
620635
lua_rawget(L, LUA_GLOBALSINDEX);
@@ -627,17 +642,26 @@ ngx_http_lua_ngx_req_header_set_helper(lua_State *L)
627642

628643
ngx_http_lua_check_fake_request(L, r);
629644

645+
n = lua_gettop(L);
646+
if (n >= 3) {
647+
replace_underscores = lua_toboolean(L, 3);
648+
} else {
649+
replace_underscores = 1;
650+
}
651+
630652
p = (u_char *) luaL_checklstring(L, 1, &len);
631653

632654
dd("key: %.*s, len %d", (int) len, p, (int) len);
633655

656+
if (replace_underscores) {
634657
/* replace "_" with "-" */
635-
for (i = 0; i < len; i++) {
636-
if (p[i] == '_') {
637-
p[i] = '-';
658+
for (i = 0; i < len; i++) {
659+
if (p[i] == '_') {
660+
p[i] = '-';
661+
}
638662
}
639663
}
640-
664+
641665
key.data = ngx_palloc(r->pool, len + 1);
642666
if (key.data == NULL) {
643667
return luaL_error(L, "out of memory");

0 commit comments

Comments
 (0)