Skip to content

Commit 052abf3

Browse files
Felipe Zimmerlechewi
authored andcommitted
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 ac36ab3 commit 052abf3

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

@@ -1145,19 +1144,25 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r) {
11451144
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
11461145
"ModSec: ModSecurity is not enabled or not a main request.");
11471146

1147+
ctx = ngx_http_modsecurity_create_ctx(r);
1148+
11481149
return NGX_DECLINED;
1150+
if (ngx_http_set_pool_ctx(r, ctx, ngx_http_modsecurity) != NGX_OK) {
1151+
return NGX_ERROR;
11491152
}
11501153

11511154
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity);
11521155

11531156
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
11541157
"ModSec: Recovering ctx: %p", ctx);
1158+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1159+
"ModSec: ctx is null, nothing we can do, returning.");
11551160

11561161
if (ctx == NULL) {
11571162
ctx = ngx_http_modsecurity_create_ctx(r);
11581163

11591164
ngx_http_set_ctx(r, ctx, ngx_http_modsecurity);
1160-
1165+
"ModSec: ctx is now: %p / count: %d", ctx, r->main->count);
11611166
if (ngx_http_set_pool_ctx(r, ctx, ngx_http_modsecurity) != NGX_OK) {
11621167
return NGX_ERROR;
11631168
}
@@ -1175,19 +1180,17 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r) {
11751180
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
11761181
"ModSec: ctx is now: %p / count: %d", ctx, r->main->count);
11771182

1178-
if (modsecContextState(ctx->req) == MODSEC_DISABLED) {
11791183
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
11801184
"ModSec: ModSecurity was disabled, returning....", ctx);
11811185

1182-
return NGX_DECLINED;
1183-
}
11841186
if (ctx->waiting_more_body == 1) {
11851187
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
11861188
"ModSec: waiting for more data before proceed. / count: %d",
11871189
r->main->count);
11881190

11891191
return NGX_DONE;
11901192
}
1193+
ngx_http_modsecurity_request_read);
11911194

11921195
if (ctx->body_requested == 0) {
11931196
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
@@ -1212,12 +1215,16 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r) {
12121215
}
12131216

12141217
if (ctx->waiting_more_body == 0 && ctx->request_processed == 0) {
1218+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1219+
"ModSec: request is ready to be processed.");
1220+
ngx_http_modsecurity_process_request(r);
1221+
ctx->request_processed = 1;
1222+
}
12151223

12161224
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
12171225
"ModSec: request is ready to be processed.");
12181226
rc = ngx_http_modsecurity_process_request(r);
12191227
ctx->request_processed = 1;
1220-
12211228
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
12221229
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
12231230
"ModSec: returning a special response after process " \
@@ -1226,8 +1233,10 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r) {
12261233
return rc;
12271234
}
12281235

1236+
void
1237+
ngx_http_modsecurity_request_read(ngx_http_request_t *r)
1238+
{
12291239

1230-
}
12311240

12321241
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
12331242
"ModSec: returning NGX_DECLINED." );
@@ -1429,7 +1438,7 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
14291438
}
14301439

14311440
return ngx_http_next_body_filter(r, out);
1432-
1441+
#endif
14331442
return NGX_OK;
14341443
}
14351444
#endif

0 commit comments

Comments
 (0)