Skip to content

Commit a7a41fd

Browse files
committed
feature: added pure C API for FFI-based API for ngx.status.
1 parent 7358557 commit a7a41fd

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

src/ngx_http_lua_common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ typedef struct {
8282
#define NGX_HTTP_LUA_CONTEXT_TIMER 0x80
8383

8484

85+
#ifndef NGX_HTTP_LUA_NO_FFI_API
86+
#define NGX_HTTP_LUA_FFI_NO_REQ_CTX -1
87+
#define NGX_HTTP_LUA_FFI_BAD_CONTEXT -2
88+
#endif
89+
90+
8591
typedef struct ngx_http_lua_main_conf_s ngx_http_lua_main_conf_t;
8692

8793

src/ngx_http_lua_misc.c

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ ngx_http_lua_ngx_set(lua_State *L)
115115
ngx_http_request_t *r;
116116
u_char *p;
117117
size_t len;
118-
ngx_http_lua_ctx_t *ctx;
119118

120119
/* we skip the first argument that is the table */
121120
p = (u_char *) luaL_checklstring(L, 2, &len);
@@ -128,16 +127,14 @@ ngx_http_lua_ngx_set(lua_State *L)
128127
return luaL_error(L, "no request object found");
129128
}
130129

131-
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
132-
133130
if (r->header_sent) {
134131
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
135132
"attempt to set ngx.status after sending out "
136133
"response headers");
137134
return 0;
138135
}
139136

140-
ngx_http_lua_check_fake_request2(L, r, ctx);
137+
ngx_http_lua_check_fake_request(L, r);
141138

142139
/* get the value */
143140
r->headers_out.status = (ngx_uint_t) luaL_checknumber(L, 3);
@@ -172,4 +169,61 @@ ngx_http_lua_ngx_set(lua_State *L)
172169
return 0;
173170
}
174171

172+
173+
#ifndef NGX_HTTP_LUA_NO_FFI_API
174+
int
175+
ngx_http_lua_ffi_get_resp_status(ngx_http_request_t *r)
176+
{
177+
if (r->connection->fd == -1) {
178+
return NGX_HTTP_LUA_FFI_BAD_CONTEXT;
179+
}
180+
181+
if (r->err_status) {
182+
return r->err_status;
183+
184+
} else if (r->headers_out.status) {
185+
return r->headers_out.status;
186+
187+
} else if (r->http_version == NGX_HTTP_VERSION_9) {
188+
return 9;
189+
190+
} else {
191+
return 0;
192+
}
193+
}
194+
195+
196+
int
197+
ngx_http_lua_ffi_set_resp_status(ngx_http_request_t *r, int status)
198+
{
199+
if (r->connection->fd == -1) {
200+
return NGX_HTTP_LUA_FFI_BAD_CONTEXT;
201+
}
202+
203+
if (r->header_sent) {
204+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
205+
"attempt to set ngx.status after sending out "
206+
"response headers");
207+
return NGX_DECLINED;
208+
}
209+
210+
r->headers_out.status = status;
211+
212+
if (status == 101) {
213+
/*
214+
* XXX work-around a bug in the Nginx core older than 1.5.5
215+
* that 101 does not have a default status line
216+
*/
217+
218+
ngx_str_set(&r->headers_out.status_line, "101 Switching Protocols");
219+
220+
} else {
221+
r->headers_out.status_line.len = 0;
222+
}
223+
224+
return NGX_OK;
225+
}
226+
#endif /* NGX_HTTP_LUA_NO_FFI_API */
227+
228+
175229
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

t/107-timer-errors.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ registered timer
127127

128128
--- error_log eval
129129
[
130-
qr/\[error\] .*? runtime error: \[string "content_by_lua"\]:3: API disabled in the context of ngx\.timer/,
130+
qr/\[error\] .*? runtime error: \[string "content_by_lua"\]:3: API disabled in the current context/,
131131
"lua ngx.timer expired",
132132
"http lua close fake http connection"
133133
]

0 commit comments

Comments
 (0)