From 3f3953599a2de05f82f09cb0efe442d3d58f3e16 Mon Sep 17 00:00:00 2001 From: Aviram Date: Wed, 24 Apr 2013 10:36:55 +0300 Subject: [PATCH 1/8] 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. --- src/ngx_http_lua_headers.c | 40 ++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/ngx_http_lua_headers.c b/src/ngx_http_lua_headers.c index 0f30a78ff8..a91a3c9314 100644 --- a/src/ngx_http_lua_headers.c +++ b/src/ngx_http_lua_headers.c @@ -581,12 +581,26 @@ ngx_http_lua_ngx_header_set(lua_State *L) static int ngx_http_lua_ngx_req_header_clear(lua_State *L) { - if (lua_gettop(L) != 1) { - return luaL_error(L, "expecting one arguments, but seen %d", + ngx_uint_t n; + n = lua_gettop(L); + if ((n != 1) && (n != 2)) { + return luaL_error(L, "expecting one or two arguments, but seen %d", lua_gettop(L)); } - lua_pushnil(L); + if (n == 2) { + u_char *p; + size_t len; + int replace_underscores = 1; + p = (u_char *) luaL_checklstring(L, 1, &len); + replace_underscores = lua_toboolean(L, 2); + lua_pop(L, 2); + lua_pushlstring(L, (char *) p, len); + lua_pushnil(L); + lua_pushboolean(L, replace_underscores); + } else { + lua_pushnil(L); + } return ngx_http_lua_ngx_req_header_set_helper(L); } @@ -595,7 +609,7 @@ ngx_http_lua_ngx_req_header_clear(lua_State *L) static int ngx_http_lua_ngx_req_header_set(lua_State *L) { - if (lua_gettop(L) != 2) { + if ((lua_gettop(L) != 2) && (lua_gettop(L) != 3)) { return luaL_error(L, "expecting two arguments, but seen %d", lua_gettop(L)); } @@ -615,6 +629,7 @@ ngx_http_lua_ngx_req_header_set_helper(lua_State *L) size_t len; ngx_int_t rc; ngx_uint_t n; + int replace_underscores = 1; lua_pushlightuserdata(L, &ngx_http_lua_request_key); lua_rawget(L, LUA_GLOBALSINDEX); @@ -627,17 +642,26 @@ ngx_http_lua_ngx_req_header_set_helper(lua_State *L) ngx_http_lua_check_fake_request(L, r); + n = lua_gettop(L); + if (n >= 3) { + replace_underscores = lua_toboolean(L, 3); + } else { + replace_underscores = 1; + } + p = (u_char *) luaL_checklstring(L, 1, &len); dd("key: %.*s, len %d", (int) len, p, (int) len); + if (replace_underscores) { /* replace "_" with "-" */ - for (i = 0; i < len; i++) { - if (p[i] == '_') { - p[i] = '-'; + for (i = 0; i < len; i++) { + if (p[i] == '_') { + p[i] = '-'; + } } } - + key.data = ngx_palloc(r->pool, len + 1); if (key.data == NULL) { return luaL_error(L, "out of memory"); From 1c5e5b9f0d030f66aec123b4cd22c7e23feaf34e Mon Sep 17 00:00:00 2001 From: aviramc Date: Wed, 8 May 2013 13:23:50 +0300 Subject: [PATCH 2/8] Changed the replace_underscores parameter to a table of parameters (options), in which the only possible option currently is replace_underscores. --- src/ngx_http_lua_headers.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ngx_http_lua_headers.c b/src/ngx_http_lua_headers.c index a91a3c9314..95df6111c8 100644 --- a/src/ngx_http_lua_headers.c +++ b/src/ngx_http_lua_headers.c @@ -643,8 +643,11 @@ ngx_http_lua_ngx_req_header_set_helper(lua_State *L) ngx_http_lua_check_fake_request(L, r); n = lua_gettop(L); - if (n >= 3) { - replace_underscores = lua_toboolean(L, 3); + if (n == 3) { + luaL_checktype(L, 3, LUA_TTABLE); + lua_getfield(L, 3, "replace_underscores"); + replace_underscores = lua_toboolean(L, -1); + lua_pop(L, 1); } else { replace_underscores = 1; } From f2e849a7efc23f86971596110a819d1057aeee37 Mon Sep 17 00:00:00 2001 From: aviramc Date: Wed, 8 May 2013 13:44:32 +0300 Subject: [PATCH 3/8] Added an options table to clean_header as well. --- src/ngx_http_lua_headers.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/ngx_http_lua_headers.c b/src/ngx_http_lua_headers.c index 95df6111c8..36f6a01c5a 100644 --- a/src/ngx_http_lua_headers.c +++ b/src/ngx_http_lua_headers.c @@ -589,15 +589,9 @@ ngx_http_lua_ngx_req_header_clear(lua_State *L) } if (n == 2) { - u_char *p; - size_t len; - int replace_underscores = 1; - p = (u_char *) luaL_checklstring(L, 1, &len); - replace_underscores = lua_toboolean(L, 2); - lua_pop(L, 2); - lua_pushlstring(L, (char *) p, len); lua_pushnil(L); - lua_pushboolean(L, replace_underscores); + /* Top element is now 3, replace it with element 3 */ + lua_insert(L, 2); } else { lua_pushnil(L); } From f602d0fed547ee7aa8f2d82b25761fe80865fb45 Mon Sep 17 00:00:00 2001 From: aviram Date: Sun, 29 Sep 2013 10:53:49 +0200 Subject: [PATCH 4/8] Added ngx.req.set_keepalive and ngx.req.get_keepalive to control the keepalive option for the request. --- config | 2 + src/ngx_http_lua_headers.c | 1 - src/ngx_http_lua_req_keepalive.c | 79 ++++++++++++++++++++++++++++++++ src/ngx_http_lua_req_keepalive.h | 19 ++++++++ src/ngx_http_lua_util.c | 2 + 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/ngx_http_lua_req_keepalive.c create mode 100644 src/ngx_http_lua_req_keepalive.h diff --git a/config b/config index 84f717218f..c7c750e549 100644 --- a/config +++ b/config @@ -219,6 +219,7 @@ NGX_ADDON_SRCS="$NGX_ADDON_SRCS \ $ngx_addon_dir/src/ngx_http_lua_initby.c \ $ngx_addon_dir/src/ngx_http_lua_socket_udp.c \ $ngx_addon_dir/src/ngx_http_lua_req_method.c \ + $ngx_addon_dir/src/ngx_http_lua_req_keepalive.c \ $ngx_addon_dir/src/ngx_http_lua_phase.c \ $ngx_addon_dir/src/ngx_http_lua_uthread.c \ $ngx_addon_dir/src/ngx_http_lua_timer.c \ @@ -269,6 +270,7 @@ NGX_ADDON_DEPS="$NGX_ADDON_DEPS \ $ngx_addon_dir/src/ngx_http_lua_initby.h \ $ngx_addon_dir/src/ngx_http_lua_socket_udp.h \ $ngx_addon_dir/src/ngx_http_lua_req_method.h \ + $ngx_addon_dir/src/ngx_http_lua_req_keepalive.h \ $ngx_addon_dir/src/ngx_http_lua_phase.h \ $ngx_addon_dir/src/ngx_http_lua_probe.h \ $ngx_addon_dir/src/ngx_http_lua_uthread.h \ diff --git a/src/ngx_http_lua_headers.c b/src/ngx_http_lua_headers.c index 6960a3c238..3a0226517d 100644 --- a/src/ngx_http_lua_headers.c +++ b/src/ngx_http_lua_headers.c @@ -604,7 +604,6 @@ ngx_http_lua_ngx_req_header_set_helper(lua_State *L) size_t len; ngx_int_t rc; ngx_uint_t n; - int replace_underscores = 1; r = ngx_http_lua_get_req(L); if (r == NULL) { diff --git a/src/ngx_http_lua_req_keepalive.c b/src/ngx_http_lua_req_keepalive.c new file mode 100644 index 0000000000..73aa510cbb --- /dev/null +++ b/src/ngx_http_lua_req_keepalive.c @@ -0,0 +1,79 @@ + +/* + * Copyright (C) Yichun Zhang (agentzh) + */ + + +#ifndef DDEBUG +#define DDEBUG 0 +#endif + + +#include "ddebug.h" +#include "ngx_http_lua_req_keepalive.h" +#include "ngx_http_lua_util.h" + + +static int ngx_http_lua_ngx_req_get_keepalive(lua_State *L); +static int ngx_http_lua_ngx_req_set_keepalive(lua_State *L); + + +void +ngx_http_lua_inject_req_keepalive_api(lua_State *L) +{ + lua_pushcfunction(L, ngx_http_lua_ngx_req_get_keepalive); + lua_setfield(L, -2, "get_keepalive"); + + lua_pushcfunction(L, ngx_http_lua_ngx_req_set_keepalive); + lua_setfield(L, -2, "set_keepalive"); +} + + +static int +ngx_http_lua_ngx_req_get_keepalive(lua_State *L) +{ + int n; + ngx_http_request_t *r; + + n = lua_gettop(L); + if (n != 0) { + return luaL_error(L, "no arguments expected but got %d", n); + } + + r = ngx_http_lua_get_req(L); + if (r == NULL) { + return luaL_error(L, "request object not found"); + } + + ngx_http_lua_check_fake_request(L, r); + + lua_pushboolean(L, r->keepalive); + return 1; +} + + +static int +ngx_http_lua_ngx_req_set_keepalive(lua_State *L) +{ + int n; + ngx_http_request_t *r; + int keepalive; + + n = lua_gettop(L); + if (n != 1) { + return luaL_error(L, "only one argument expected but got %d", n); + } + + keepalive = lua_toboolean(L, 1); + + r = ngx_http_lua_get_req(L); + if (r == NULL) { + return luaL_error(L, "request object not found"); + } + + ngx_http_lua_check_fake_request(L, r); + + r->keepalive = keepalive; + + return 1; +} diff --git a/src/ngx_http_lua_req_keepalive.h b/src/ngx_http_lua_req_keepalive.h new file mode 100644 index 0000000000..9c8da71c55 --- /dev/null +++ b/src/ngx_http_lua_req_keepalive.h @@ -0,0 +1,19 @@ + +/* + * Copyright (C) Yichun Zhang (agentzh) + */ + + +#ifndef _NGX_HTTP_LUA_KEEPALIVE_H_INCLUDED_ +#define _NGX_HTTP_LUA_KEEPALIVE_H_INCLUDED_ + + +#include "ngx_http_lua_common.h" + + +void ngx_http_lua_inject_req_keepalive_api(lua_State *L); + + +#endif /* _NGX_HTTP_LUA_KEEPALIVE_H_INCLUDED_ */ + +/* vi:set ft=c ts=4 sw=4 et fdm=marker: */ diff --git a/src/ngx_http_lua_util.c b/src/ngx_http_lua_util.c index 481f95a135..8247e66650 100644 --- a/src/ngx_http_lua_util.c +++ b/src/ngx_http_lua_util.c @@ -32,6 +32,7 @@ #include "ngx_http_lua_misc.h" #include "ngx_http_lua_consts.h" #include "ngx_http_lua_req_method.h" +#include "ngx_http_lua_req_keepalive.h" #include "ngx_http_lua_shdict.h" #include "ngx_http_lua_coroutine.h" #include "ngx_http_lua_socket_tcp.h" @@ -2128,6 +2129,7 @@ ngx_http_lua_inject_req_api(ngx_log_t *log, lua_State *L) ngx_http_lua_inject_req_body_api(L); ngx_http_lua_inject_req_socket_api(L); ngx_http_lua_inject_req_method_api(L); + ngx_http_lua_inject_req_keepalive_api(L); ngx_http_lua_inject_req_time_api(L); lua_setfield(L, -2, "req"); From d4d2988488c5a6758d2e87a47c592d833513329d Mon Sep 17 00:00:00 2001 From: aviram Date: Sun, 29 Sep 2013 10:54:33 +0200 Subject: [PATCH 5/8] Removed unneeded variable. --- src/ngx_http_lua_headers.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ngx_http_lua_headers.c b/src/ngx_http_lua_headers.c index 6960a3c238..3a0226517d 100644 --- a/src/ngx_http_lua_headers.c +++ b/src/ngx_http_lua_headers.c @@ -604,7 +604,6 @@ ngx_http_lua_ngx_req_header_set_helper(lua_State *L) size_t len; ngx_int_t rc; ngx_uint_t n; - int replace_underscores = 1; r = ngx_http_lua_get_req(L); if (r == NULL) { From e9e1dec69b88803a0080cbdbcb28151e31674fd9 Mon Sep 17 00:00:00 2001 From: aviram Date: Thu, 3 Oct 2013 17:40:18 +0200 Subject: [PATCH 6/8] Reverted unnecessary changes. --- src/ngx_http_lua_headers.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/ngx_http_lua_headers.c b/src/ngx_http_lua_headers.c index af56a2d8b0..3fd4672216 100644 --- a/src/ngx_http_lua_headers.c +++ b/src/ngx_http_lua_headers.c @@ -574,19 +574,13 @@ ngx_http_lua_ngx_req_header_clear(lua_State *L) { ngx_uint_t n; n = lua_gettop(L); - if ((n != 1) && (n != 2)) { - return luaL_error(L, "expecting one or two arguments, but seen %d", + if (lua_gettop(L) != 1) { + return luaL_error(L, "expecting one arguments, but seen %d", lua_gettop(L)); } - if (n == 2) { - lua_pushnil(L); - /* Top element is now 3, replace it with element 3 */ - lua_insert(L, 2); - } else { - lua_pushnil(L); - } - + lua_pushnil(L); + return ngx_http_lua_ngx_req_header_set_helper(L); } @@ -594,7 +588,7 @@ ngx_http_lua_ngx_req_header_clear(lua_State *L) static int ngx_http_lua_ngx_req_header_set(lua_State *L) { - if ((lua_gettop(L) != 2) && (lua_gettop(L) != 3)) { + if (lua_gettop(L) != 2) { return luaL_error(L, "expecting two arguments, but seen %d", lua_gettop(L)); } @@ -632,10 +626,9 @@ ngx_http_lua_ngx_req_header_set_helper(lua_State *L) #if 0 /* replace "_" with "-" */ - for (i = 0; i < len; i++) { - if (p[i] == '_') { - p[i] = '-'; - } + for (i = 0; i < len; i++) { + if (p[i] == '_') { + p[i] = '-'; } } #endif From 24cb6857e2a1452733be9185a59f600e962dc029 Mon Sep 17 00:00:00 2001 From: aviram Date: Thu, 3 Oct 2013 17:41:25 +0200 Subject: [PATCH 7/8] Reverted unnecessary changes. --- src/ngx_http_lua_headers.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ngx_http_lua_headers.c b/src/ngx_http_lua_headers.c index 3fd4672216..c9d89aa941 100644 --- a/src/ngx_http_lua_headers.c +++ b/src/ngx_http_lua_headers.c @@ -572,15 +572,13 @@ ngx_http_lua_ngx_header_set(lua_State *L) static int ngx_http_lua_ngx_req_header_clear(lua_State *L) { - ngx_uint_t n; - n = lua_gettop(L); if (lua_gettop(L) != 1) { return luaL_error(L, "expecting one arguments, but seen %d", lua_gettop(L)); } lua_pushnil(L); - + return ngx_http_lua_ngx_req_header_set_helper(L); } From 2496f393c56c9b8227ece69622e007af5db9b353 Mon Sep 17 00:00:00 2001 From: aviram Date: Thu, 3 Oct 2013 17:40:18 +0200 Subject: [PATCH 8/8] Reverted unnecessary changes. (cherry picked from commit e9e1dec69b88803a0080cbdbcb28151e31674fd9) --- src/ngx_http_lua_headers.c | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/ngx_http_lua_headers.c b/src/ngx_http_lua_headers.c index 3a0226517d..3afbd7ba76 100644 --- a/src/ngx_http_lua_headers.c +++ b/src/ngx_http_lua_headers.c @@ -562,20 +562,12 @@ ngx_http_lua_ngx_header_set(lua_State *L) static int ngx_http_lua_ngx_req_header_clear(lua_State *L) { - ngx_uint_t n; - n = lua_gettop(L); - if ((n != 1) && (n != 2)) { - return luaL_error(L, "expecting one or two arguments, but seen %d", + if (lua_gettop(L) != 1) { + return luaL_error(L, "expecting one arguments, but seen %d", lua_gettop(L)); } - if (n == 2) { - lua_pushnil(L); - /* Top element is now 3, replace it with element 3 */ - lua_insert(L, 2); - } else { - lua_pushnil(L); - } + lua_pushnil(L); return ngx_http_lua_ngx_req_header_set_helper(L); } @@ -584,7 +576,7 @@ ngx_http_lua_ngx_req_header_clear(lua_State *L) static int ngx_http_lua_ngx_req_header_set(lua_State *L) { - if ((lua_gettop(L) != 2) && (lua_gettop(L) != 3)) { + if (lua_gettop(L) != 2) { return luaL_error(L, "expecting two arguments, but seen %d", lua_gettop(L)); } @@ -622,10 +614,9 @@ ngx_http_lua_ngx_req_header_set_helper(lua_State *L) #if 0 /* replace "_" with "-" */ - for (i = 0; i < len; i++) { - if (p[i] == '_') { - p[i] = '-'; - } + for (i = 0; i < len; i++) { + if (p[i] == '_') { + p[i] = '-'; } } #endif