Skip to content

Commit fea9449

Browse files
committed
Handle static file in body filter, better cleanup
1 parent 03343c0 commit fea9449

6 files changed

+130
-78
lines changed

Makefile.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ $(PATCH_FILES): %: nginx-@NGINX_VERSION@
2626

2727
nginx-@NGINX_VERSION@/objs/ngx_http_redirectionio_module.so: nginx-@NGINX_VERSION@ patch
2828
cd nginx-@NGINX_VERSION@; \
29-
./configure --prefix=@NGINX_PREFIX@ @NGINX_CONFIGURE@ --add-dynamic-module=../; \
29+
./configure --prefix=@NGINX_PREFIX@ @NGINX_CONFIGURE@ @ADD_MODULE@=../; \
3030
make -j12 @NGINX_MAKE_TARGET@
3131

3232
.PHONY: test

configure.ac

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ AS_IF(test x$TAR = xno,
1010
AC_MSG_ERROR([tar is required.])
1111
)
1212

13+
dnl Specify --enable-static to compile nginx with static module. By default,
14+
dnl we build a dynamic module.
15+
16+
AC_ARG_ENABLE(static,
17+
AC_HELP_STRING([--enable-static],
18+
[Build static module (not dynamix) [default=no]]),
19+
[static_module=$enableval],
20+
[static_module=no])
21+
1322
AC_ARG_WITH([nginx_version], [AS_HELP_STRING([--with-nginx-version],
1423
[nginx version to use])],
1524
[NGINX_VERSION=$withval], [NGINX_VERSION=])
@@ -64,6 +73,15 @@ CFLAGS="$CFLAGS -Wall -g -std=c99 -DPROXY_VERSION=$PROXY_VERSION $redirectionio_
6473
LIBS="$LIBS $redirectionio_LIBS"
6574
MODULE_DIR=$NGINX_PREFIX/modules
6675

76+
AC_MSG_CHECKING(whether to build Rust code with debugging information)
77+
if test "x$static_module" = "xyes" ; then
78+
ADD_MODULE=--add-module
79+
LOAD_MODULE=
80+
else
81+
ADD_MODULE=--add-dynamic-module
82+
LOAD_MODULE="load_module $MODULE_DIR/ngx_http_redirectionio_module.so;"
83+
fi
84+
6785
AC_SUBST(PREFIX)
6886
AC_SUBST(NGINX_VERSION)
6987
AC_SUBST(NGINX_CONFIGURE)
@@ -75,6 +93,8 @@ AC_SUBST(NGINX_BIN)
7593
AC_SUBST(NGINX_PREFIX)
7694
AC_SUBST(BUILD_MODULES_DIR)
7795
AC_SUBST(NGINX_MAKE_TARGET)
96+
AC_SUBST(ADD_MODULE)
97+
AC_SUBST(LOAD_MODULE)
7898

7999
AC_MSG_NOTICE([summary of build options:
80100
Install prefix: ${PREFIX}

nginx-test.conf.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ worker_processes 4;
33
daemon off;
44
master_process on;
55

6-
load_module @abs_srcdir@/@BUILD_MODULES_DIR@/ngx_http_redirectionio_module.so;
6+
@LOAD_MODULE@
77

88
events {
99
worker_connections 1024;

src/ngx_http_redirectionio_module.c

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static void ngx_http_redirectionio_write_match_action_handler(ngx_event_t *wev);
2323
static void ngx_http_redirectionio_read_match_action_handler(ngx_event_t *rev, const char *action_serialized);
2424
static void ngx_http_redirectionio_log_callback(const char* log_str, const void* data, short level);
2525

26-
static void ngx_http_redirectionio_action_cleanup(void *data);
26+
static void ngx_http_redirectionio_context_cleanup(void *context);
2727

2828
/**
2929
* Commands definitions
@@ -175,6 +175,7 @@ static ngx_int_t ngx_http_redirectionio_postconfiguration(ngx_conf_t *cf) {
175175
static ngx_int_t ngx_http_redirectionio_create_ctx_handler(ngx_http_request_t *r) {
176176
ngx_http_redirectionio_ctx_t *ctx;
177177
ngx_http_redirectionio_conf_t *conf;
178+
ngx_http_cleanup_t *cln;
178179

179180
// Disallow in sub request
180181
if (r != r->main) {
@@ -222,6 +223,15 @@ static ngx_int_t ngx_http_redirectionio_create_ctx_handler(ngx_http_request_t *r
222223
return NGX_DECLINED;
223224
}
224225

226+
cln = ngx_http_cleanup_add(r, 0);
227+
228+
if (cln == NULL) {
229+
return NGX_DECLINED;
230+
}
231+
232+
cln->data = ctx;
233+
cln->handler = ngx_http_redirectionio_context_cleanup;
234+
225235
ngx_http_set_ctx(r, ctx, ngx_http_redirectionio_module);
226236

227237
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http redirectionio init context");
@@ -504,7 +514,6 @@ static void ngx_http_redirectionio_read_match_action_handler(ngx_event_t *rev, c
504514
ngx_http_redirectionio_ctx_t *ctx;
505515
ngx_http_request_t *r;
506516
ngx_connection_t *c;
507-
ngx_pool_cleanup_t *cln;
508517

509518
c = rev->data;
510519
r = c->data;
@@ -522,15 +531,6 @@ static void ngx_http_redirectionio_read_match_action_handler(ngx_event_t *rev, c
522531

523532
ctx->action = (struct REDIRECTIONIO_Action *)redirectionio_action_json_deserialize((char *)action_serialized);
524533

525-
if (ctx->action != NULL) {
526-
cln = ngx_pool_cleanup_add(r->pool, 0);
527-
528-
if (cln != NULL) {
529-
cln->data = ctx->action;
530-
cln->handler = ngx_http_redirectionio_action_cleanup;
531-
}
532-
}
533-
534534
ngx_http_core_run_phases(r);
535535
}
536536

@@ -544,6 +544,38 @@ static void ngx_http_redirectionio_log_callback(const char* log_str, const void*
544544
}
545545
}
546546

547-
static void ngx_http_redirectionio_action_cleanup(void *data) {
548-
redirectionio_action_drop(data);
547+
static void ngx_http_redirectionio_context_cleanup(void *context) {
548+
struct REDIRECTIONIO_HeaderMap *first_header, *tmp_header;
549+
ngx_http_redirectionio_ctx_t *ctx = (ngx_http_redirectionio_ctx_t *)context;
550+
551+
if (ctx->action != NULL) {
552+
redirectionio_action_drop(ctx->action);
553+
ctx->action = NULL;
554+
}
555+
556+
if (ctx->request != NULL) {
557+
redirectionio_request_drop(ctx->request);
558+
ctx->request = NULL;
559+
}
560+
561+
if (ctx->response_headers != NULL) {
562+
first_header = (struct REDIRECTIONIO_HeaderMap *)ctx->response_headers;
563+
564+
while (first_header != NULL) {
565+
tmp_header = first_header->next;
566+
567+
free((void *)first_header->name);
568+
free((void *)first_header->value);
569+
free((void *)first_header);
570+
571+
first_header = tmp_header;
572+
}
573+
574+
ctx->response_headers = NULL;
575+
}
576+
577+
if (ctx->body_filter != NULL) {
578+
redirectionio_action_body_filter_drop(ctx->body_filter);
579+
ctx->body_filter = NULL;
580+
}
549581
}

src/ngx_http_redirectionio_module_filter.c

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#include <ngx_http_redirectionio_module.h>
22

3-
static ngx_chain_t* ngx_http_redirectionio_body_filter_replace(struct REDIRECTIONIO_FilterBodyAction *body_filter, ngx_pool_t *pool, ngx_chain_t *cl);
4-
5-
static void ngx_http_redirectionio_response_headers_cleanup(void *response_headers);
3+
static ngx_chain_t* ngx_http_redirectionio_body_filter_replace(ngx_http_redirectionio_ctx_t *ctx, ngx_pool_t *pool, ngx_chain_t *cl);
64

75
static ngx_int_t ngx_http_redirectionio_create_filter_body(ngx_http_request_t *r);
86

97
static ngx_int_t ngx_http_redirectionio_header_read(ngx_http_request_t *r, ngx_table_elt_t *header, struct REDIRECTIONIO_HeaderMap **first);
108

119
static ngx_int_t ngx_http_redirectionio_header_content_type_read(ngx_http_request_t *r, struct REDIRECTIONIO_HeaderMap **first);
1210

11+
static ngx_int_t ngx_http_redirectionio_buffer_read(ngx_buf_t *buffer, char **output);
12+
1313
ngx_int_t ngx_http_redirectionio_match_on_response_status_header_filter(ngx_http_request_t *r) {
1414
ngx_http_redirectionio_ctx_t *ctx;
1515
ngx_http_redirectionio_conf_t *conf;
@@ -53,7 +53,6 @@ ngx_int_t ngx_http_redirectionio_headers_filter(ngx_http_request_t *r) {
5353
ngx_table_elt_t *h;
5454
ngx_list_part_t *part;
5555
struct REDIRECTIONIO_HeaderMap *header_map = NULL;
56-
ngx_pool_cleanup_t *cln;
5756

5857
conf = ngx_http_get_module_loc_conf(r, ngx_http_redirectionio_module);
5958

@@ -110,13 +109,6 @@ ngx_int_t ngx_http_redirectionio_headers_filter(ngx_http_request_t *r) {
110109
return ngx_http_redirectionio_create_filter_body(r);
111110
}
112111

113-
cln = ngx_pool_cleanup_add(r->pool, 0);
114-
115-
if (cln != NULL) {
116-
cln->data = header_map;
117-
cln->handler = ngx_http_redirectionio_response_headers_cleanup;
118-
}
119-
120112
ctx->response_headers = header_map;
121113

122114
// Deactivate all old headers
@@ -250,7 +242,7 @@ ngx_int_t ngx_http_redirectionio_body_filter(ngx_http_request_t *r, ngx_chain_t
250242
tsize = 0;
251243

252244
for (cl = in; cl; cl = cl->next) {
253-
tl = ngx_http_redirectionio_body_filter_replace(ctx->body_filter, r->pool, cl);
245+
tl = ngx_http_redirectionio_body_filter_replace(ctx, r->pool, cl);
254246

255247
if (tl == NULL) {
256248
continue;
@@ -284,24 +276,21 @@ ngx_int_t ngx_http_redirectionio_body_filter(ngx_http_request_t *r, ngx_chain_t
284276
return rc;
285277
}
286278

287-
static ngx_chain_t* ngx_http_redirectionio_body_filter_replace(struct REDIRECTIONIO_FilterBodyAction *body_filter, ngx_pool_t *pool, ngx_chain_t *cl) {
279+
static ngx_chain_t* ngx_http_redirectionio_body_filter_replace(ngx_http_redirectionio_ctx_t *ctx, ngx_pool_t *pool, ngx_chain_t *cl) {
288280
ngx_chain_t *out = NULL, *el = NULL;
289281
const char *buf_in, *buf_out;
290282
char *memory_buf;
291-
size_t bsize, mbsize;
283+
size_t mbsize;
292284

293-
bsize = ngx_buf_size(cl->buf);
294-
295-
if (bsize <= 0) {
285+
if (ngx_http_redirectionio_buffer_read(cl->buf, (char **)&buf_in) != NGX_OK) {
296286
return cl;
297287
}
298288

299-
buf_in = malloc(bsize + 1);
300-
ngx_memcpy((char *)buf_in, cl->buf->pos, bsize);
301-
*((char *)buf_in + bsize) = '\0';
289+
buf_out = redirectionio_action_body_filter_filter(ctx->body_filter, buf_in);
302290

303-
buf_out = redirectionio_action_body_filter_filter(body_filter, buf_in);
304-
free((char *)buf_in);
291+
if (buf_out != buf_in) {
292+
free((char *)buf_in);
293+
}
305294

306295
if (buf_out != NULL && strlen(buf_out) > 0) {
307296
mbsize = strlen(buf_out);
@@ -331,7 +320,8 @@ static ngx_chain_t* ngx_http_redirectionio_body_filter_replace(struct REDIRECTIO
331320
}
332321

333322
if (cl->buf->last_buf == 1) {
334-
buf_out = redirectionio_action_body_filter_close(body_filter);
323+
buf_out = redirectionio_action_body_filter_close(ctx->body_filter);
324+
ctx->body_filter = NULL;
335325

336326
if (buf_out == NULL || strlen(buf_out) == 0) {
337327
if (out != NULL) {
@@ -386,27 +376,7 @@ static ngx_chain_t* ngx_http_redirectionio_body_filter_replace(struct REDIRECTIO
386376
out->next = el;
387377
}
388378

389-
if (out != NULL) {
390-
return out;
391-
}
392-
393-
return NULL;
394-
}
395-
396-
static void ngx_http_redirectionio_response_headers_cleanup(void *response_headers) {
397-
struct REDIRECTIONIO_HeaderMap *first_header, *tmp_header;
398-
399-
first_header = (struct REDIRECTIONIO_HeaderMap *)response_headers;
400-
401-
while (first_header != NULL) {
402-
tmp_header = first_header->next;
403-
404-
free((void *)first_header->name);
405-
free((void *)first_header->value);
406-
free((void *)first_header);
407-
408-
first_header = tmp_header;
409-
}
379+
return out;
410380
}
411381

412382
static ngx_int_t ngx_http_redirectionio_header_read(ngx_http_request_t *r, ngx_table_elt_t *header, struct REDIRECTIONIO_HeaderMap **first) {
@@ -461,3 +431,52 @@ static ngx_int_t ngx_http_redirectionio_header_content_type_read(ngx_http_reques
461431

462432
return NGX_OK;
463433
}
434+
435+
static ngx_int_t ngx_http_redirectionio_buffer_read(ngx_buf_t *buffer, char **output) {
436+
#if (NGX_HAVE_SENDFILE64)
437+
off_t offset;
438+
#else
439+
int32_t offset;
440+
#endif
441+
size_t bsize, readed = 0;
442+
ssize_t n;
443+
char *data;
444+
445+
bsize = ngx_buf_size(buffer);
446+
447+
if (bsize <= 0) {
448+
return NGX_DONE;
449+
}
450+
451+
if (!ngx_buf_in_memory(buffer) && !buffer->in_file) {
452+
return NGX_DONE;
453+
}
454+
455+
data = malloc(bsize + 1);
456+
457+
if (ngx_buf_in_memory(buffer)) {
458+
ngx_memcpy(data, buffer->pos, bsize);
459+
} else if (buffer->in_file) {
460+
#if (NGX_HAVE_SENDFILE64)
461+
offset = buffer->file_pos;
462+
#else
463+
offset = (int32_t) buffer->file_pos;
464+
#endif
465+
while (readed < bsize) {
466+
n = pread(buffer->file->fd, data, bsize - readed, offset + readed);
467+
468+
if (n <= 0) {
469+
free(data);
470+
471+
return NGX_ERROR;
472+
}
473+
474+
readed += n;
475+
}
476+
}
477+
478+
*(data + bsize) = '\0';
479+
*output = data;
480+
481+
return NGX_OK;
482+
}

src/ngx_http_redirectionio_protocol.c

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ static ngx_int_t ngx_http_redirectionio_send_string(ngx_connection_t *c, const c
1414

1515
static ngx_int_t ngx_http_redirectionio_send_protocol_header(ngx_connection_t *c, ngx_str_t *project_key, uint16_t command);
1616

17-
static void ngx_http_redirectionio_request_cleanup(void *request);
18-
1917
void ngx_http_redirectionio_protocol_send_match(ngx_connection_t *c, ngx_http_request_t *r, ngx_http_redirectionio_ctx_t *ctx, ngx_str_t *project_key) {
2018
ngx_int_t rv;
2119
ngx_table_elt_t *h;
@@ -24,7 +22,6 @@ void ngx_http_redirectionio_protocol_send_match(ngx_connection_t *c, ngx_http_re
2422
const char *request_serialized;
2523
char *method, *uri, *host = NULL, *scheme = NULL;
2624
ngx_uint_t i;
27-
ngx_pool_cleanup_t *cln;
2825

2926
// Create header map
3027
part = &r->headers_in.headers.part;
@@ -81,18 +78,6 @@ void ngx_http_redirectionio_protocol_send_match(ngx_connection_t *c, ngx_http_re
8178
return;
8279
}
8380

84-
cln = ngx_pool_cleanup_add(r->pool, 0);
85-
86-
if (cln == NULL) {
87-
redirectionio_request_drop(ctx->request);
88-
ctx->request = NULL;
89-
90-
return;
91-
}
92-
93-
cln->data = ctx->request;
94-
cln->handler = ngx_http_redirectionio_request_cleanup;
95-
9681
// Serialize request
9782
request_serialized = redirectionio_request_json_serialize(ctx->request);
9883

@@ -324,7 +309,3 @@ static ngx_int_t ngx_http_redirectionio_send_protocol_header(ngx_connection_t *c
324309

325310
return NGX_OK;
326311
}
327-
328-
static void ngx_http_redirectionio_request_cleanup(void *request) {
329-
redirectionio_request_drop(request);
330-
}

0 commit comments

Comments
 (0)