Skip to content

Commit a3433c3

Browse files
author
Felipe Zimmerle
committed
nginx refactoring
Refactoring on the nginx module, including: - Better handling larger posts; - Now using nginx echo module during the regression tests. - Better interacting with neginx chain rules - Separation of the request handling and content filters. - Better handling nginx sessions and resource counts to allow a more efficient garbage collector. - Handling both http/1.0 and 1.1, including keep-alive. - Tests are now capable to test nginx as a proxy or end-server. - Tested agains nginx 1.6 and 1.7.
1 parent fda2032 commit a3433c3

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

nginx/modsecurity/ngx_http_modsecurity.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
/* Those are defined twice, lets keep it defined just once by `undef`
2222
* the first one.
2323
*/
24+
*/
2425
#undef CR
2526
#undef LF
2627
#undef CRLF
@@ -228,7 +229,6 @@ ngx_pstrdup0(ngx_pool_t *pool, ngx_str_t *src)
228229
return dst;
229230
}
230231

231-
232232
/*
233233
* MultiplyDeBruijnBitPosition
234234
* http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup
@@ -563,6 +563,10 @@ ngx_http_modsecurity_load_request_body(ngx_http_request_t *r)
563563
ngx_http_modsecurity_ctx_t *ctx;
564564
ngx_chain_t *chain;
565565

566+
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
567+
"ModSec: loading request body.");
568+
569+
566570
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
567571
"ModSec: loading request body.");
568572

@@ -598,7 +602,6 @@ ngx_http_modsecurity_load_request_body(ngx_http_request_t *r)
598602

599603
return NGX_OK;
600604
}
601-
602605
static ngx_inline ngx_int_t
603606
ngx_http_modsecurity_save_request_body(ngx_http_request_t *r)
604607
{
@@ -607,7 +610,6 @@ ngx_http_modsecurity_save_request_body(ngx_http_request_t *r)
607610
apr_off_t content_length;
608611
ngx_buf_t *buf;
609612
#endif
610-
611613
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity);
612614

613615
#ifdef MOVE_REQUEST_CHAIN_TO_MODSEC
@@ -619,10 +621,8 @@ ngx_http_modsecurity_save_request_body(ngx_http_request_t *r)
619621
apr_brigade_cleanup(ctx->brigade);
620622
buf->last += content_length;
621623
r->header_in = buf;
622-
623624
if (r->headers_in.content_length) {
624625
ngx_str_t *str = NULL;
625-
626626
str = &r->headers_in.content_length->value;
627627
str->data = ngx_palloc(r->pool, NGX_OFF_T_LEN);
628628
if (str->data == NULL) {
@@ -631,18 +631,17 @@ ngx_http_modsecurity_save_request_body(ngx_http_request_t *r)
631631
}
632632
str->len = ngx_snprintf(str->data, NGX_OFF_T_LEN, "%O",
633633
content_length) - str->data;
634-
635-
}
636-
637634
r->headers_in.content_length_n = content_length;
638-
639635
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
640636
"ModSec: Content length: %O, Content length n: %O", content_length,
641637
r->headers_in.content_length_n);
642638
#else
643639
apr_brigade_cleanup(ctx->brigade);
644640
#endif
645641

642+
r->headers_in.content_length_n = content_length;
643+
644+
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ModSec: Content length: %O, Content length n: %O", content_length, r->headers_in.content_length_n);
646645
return NGX_OK;
647646
}
648647

@@ -1151,19 +1150,25 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r) {
11511150
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
11521151
"ModSec: ModSecurity is not enabled or not a main request.");
11531152

1153+
ctx = ngx_http_modsecurity_create_ctx(r);
1154+
11541155
return NGX_DECLINED;
1156+
if (ngx_http_set_pool_ctx(r, ctx, ngx_http_modsecurity) != NGX_OK) {
1157+
return NGX_ERROR;
11551158
}
11561159

11571160
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity);
11581161

11591162
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
11601163
"ModSec: Recovering ctx: %p", ctx);
1164+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1165+
"ModSec: ctx is null, nothing we can do, returning.");
11611166

11621167
if (ctx == NULL) {
11631168
ctx = ngx_http_modsecurity_create_ctx(r);
11641169

11651170
ngx_http_set_ctx(r, ctx, ngx_http_modsecurity);
1166-
1171+
"ModSec: ctx is now: %p / count: %d", ctx, r->main->count);
11671172
if (ngx_http_set_pool_ctx(r, ctx, ngx_http_modsecurity) != NGX_OK) {
11681173
return NGX_ERROR;
11691174
}
@@ -1181,19 +1186,17 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r) {
11811186
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
11821187
"ModSec: ctx is now: %p / count: %d", ctx, r->main->count);
11831188

1184-
if (modsecContextState(ctx->req) == MODSEC_DISABLED) {
11851189
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
11861190
"ModSec: ModSecurity was disabled, returning....", ctx);
11871191

1188-
return NGX_DECLINED;
1189-
}
11901192
if (ctx->waiting_more_body == 1) {
11911193
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
11921194
"ModSec: waiting for more data before proceed. / count: %d",
11931195
r->main->count);
11941196

11951197
return NGX_DONE;
11961198
}
1199+
ngx_http_modsecurity_request_read);
11971200

11981201
if (ctx->body_requested == 0) {
11991202
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
@@ -1218,12 +1221,16 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r) {
12181221
}
12191222

12201223
if (ctx->waiting_more_body == 0 && ctx->request_processed == 0) {
1224+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1225+
"ModSec: request is ready to be processed.");
1226+
ngx_http_modsecurity_process_request(r);
1227+
ctx->request_processed = 1;
1228+
}
12211229

12221230
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
12231231
"ModSec: request is ready to be processed.");
12241232
rc = ngx_http_modsecurity_process_request(r);
12251233
ctx->request_processed = 1;
1226-
12271234
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
12281235
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
12291236
"ModSec: returning a special response after process " \
@@ -1232,8 +1239,10 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r) {
12321239
return rc;
12331240
}
12341241

1242+
void
1243+
ngx_http_modsecurity_request_read(ngx_http_request_t *r)
1244+
{
12351245

1236-
}
12371246

12381247
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
12391248
"ModSec: returning NGX_DECLINED." );
@@ -1435,7 +1444,7 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
14351444
}
14361445

14371446
return ngx_http_next_body_filter(r, out);
1438-
1447+
#endif
14391448
return NGX_OK;
14401449
}
14411450
#endif

0 commit comments

Comments
 (0)