Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an API to set the keepalive property of the request #287

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down
79 changes: 79 additions & 0 deletions src/ngx_http_lua_req_keepalive.c
Original file line number Diff line number Diff line change
@@ -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;
}
19 changes: 19 additions & 0 deletions src/ngx_http_lua_req_keepalive.h
Original file line number Diff line number Diff line change
@@ -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: */
2 changes: 2 additions & 0 deletions src/ngx_http_lua_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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");
Expand Down