1
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
2
#include "ngx_http_lua_filter.h"
3
#include "ngx_http_lua_util.h"
5
ngx_http_output_header_filter_pt ngx_http_lua_next_header_filter;
6
ngx_http_output_body_filter_pt ngx_http_lua_next_body_filter;
8
static ngx_int_t ngx_http_lua_header_filter(ngx_http_request_t *r);
9
static ngx_int_t ngx_http_lua_body_filter(ngx_http_request_t *r, ngx_chain_t *in);
10
static ngx_int_t ngx_http_lua_rewrite_phase_handler(ngx_http_request_t *r);
11
static void ngx_http_lua_post_read(ngx_http_request_t *r);
14
ngx_http_lua_filter_init(ngx_conf_t *cf)
16
ngx_http_handler_pt *h;
17
ngx_http_core_main_conf_t *cmcf;
19
/* setting up rewrite phase handler to force reading request body */
20
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
21
h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
27
*h = ngx_http_lua_rewrite_phase_handler;
29
/* setting up output filters to intercept subrequest responses */
30
ngx_http_lua_next_header_filter = ngx_http_top_header_filter;
31
ngx_http_top_header_filter = ngx_http_lua_header_filter;
33
ngx_http_lua_next_body_filter = ngx_http_top_body_filter;
34
ngx_http_top_body_filter = ngx_http_lua_body_filter;
40
ngx_http_lua_header_filter(ngx_http_request_t *r)
42
ngx_http_lua_ctx_t *ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
44
if (ctx && ctx->capture) {
45
/* force subrequest response body buffer in memory */
46
r->filter_need_in_memory = 1;
51
return ngx_http_lua_next_header_filter(r);
55
ngx_http_lua_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
58
ngx_http_lua_ctx_t *ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
60
if (!ctx || !ctx->capture) {
61
return ngx_http_lua_next_body_filter(r, in);
64
rc = ngx_http_lua_add_copy_chain(r->pool,
71
ngx_http_lua_discard_bufs(r->pool, in);
77
ngx_http_lua_rewrite_phase_handler(ngx_http_request_t *r)
79
ngx_http_lua_loc_conf_t *llcf;
80
ngx_http_lua_ctx_t *ctx;
83
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
85
if (!llcf->force_read_body) {
86
dd("no need to reading request body");
90
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
93
if (ctx->read_body_done) {
94
dd("request body has been read");
100
if (r->method != NGX_HTTP_POST && r->method != NGX_HTTP_PUT) {
101
dd("request method should not have a body: %d", (int) r->method);
105
ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_lua_ctx_t));
111
ngx_http_set_ctx(r, ctx, ngx_http_lua_module);
113
dd("start to read request body");
115
rc = ngx_http_read_client_request_body(r, ngx_http_lua_post_read);
117
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
121
if (rc == NGX_AGAIN) {
122
ctx->waiting_more_body = 1;
130
ngx_http_lua_post_read(ngx_http_request_t *r)
132
ngx_http_lua_ctx_t *ctx;
134
r->read_event_handler = ngx_http_request_empty_handler;
136
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
138
ctx->read_body_done = 1;
140
#if defined(nginx_version) && nginx_version >= 8011
144
if (ctx->waiting_more_body) {
145
ctx->waiting_more_body = 0;
146
ngx_http_core_run_phases(r);