diff --git a/config b/config index 395749444c..3b98e0a920 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 \ @@ -270,6 +271,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_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 be99d927fc..dcba95c1ce 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" @@ -2157,6 +2158,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");