Skip to content

Commit 6126374

Browse files
committed
Merge pull request #96 from chaizhenhua/remotes/trunk
Nginx: Try to fix eats 100% cpu in ngx_event_pipe_write_to_downstream issue..
2 parents 1f4757d + bad4586 commit 6126374

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

nginx/modsecurity/apr_bucket_nginx.c

+11
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@ move_brigade_to_chain(apr_bucket_brigade *bb, ngx_chain_t **ll, ngx_pool_t *pool
204204

205205
if (APR_BUCKET_IS_EOS(e)) {
206206
if (cl == NULL) {
207+
cl = ngx_alloc_chain_link(pool);
208+
if (cl == NULL) {
209+
break;
210+
}
211+
212+
cl->buf = ngx_calloc_buf(pool);
213+
if (cl->buf == NULL) {
214+
break;
215+
}
216+
217+
cl->buf->last_buf = 1;
207218
*ll = cl;
208219
} else {
209220
cl->buf->last_buf = 1;

nginx/modsecurity/ngx_http_modsecurity.c

+44-16
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,8 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
11561156
ngx_http_modsecurity_ctx_t *ctx;
11571157
ngx_int_t rc;
11581158
apr_off_t content_length;
1159+
ngx_chain_t *cl, *out;
1160+
ngx_int_t last_buf = 0;
11591161

11601162
cf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity);
11611163
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity);
@@ -1166,29 +1168,53 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
11661168

11671169
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "modSecurity: body filter");
11681170

1169-
if (in == NULL) {
1170-
/* waiting for more buffer */
1171-
r->buffered |= NGX_HTTP_SSI_BUFFERED;
1172-
return NGX_AGAIN;
1171+
for (cl = in; cl; cl = cl->next) {
1172+
apr_bucket *e;
1173+
ngx_buf_t *buf = cl->buf;
1174+
apr_bucket_brigade *bb = ctx->brigade;
1175+
off_t size = ngx_buf_size(buf);
1176+
if (size) {
1177+
char *data = apr_pmemdup(bb->p, buf->pos, size);
1178+
if (data == NULL) {
1179+
return ngx_http_filter_finalize_request(r,
1180+
&ngx_http_modsecurity, NGX_HTTP_INTERNAL_SERVER_ERROR);
1181+
}
1182+
e = apr_bucket_pool_create(data , size, bb->p, bb->bucket_alloc);
1183+
if (e == NULL) {
1184+
return ngx_http_filter_finalize_request(r,
1185+
&ngx_http_modsecurity, NGX_HTTP_INTERNAL_SERVER_ERROR);
1186+
}
1187+
APR_BRIGADE_INSERT_TAIL(bb, e);
1188+
}
1189+
1190+
if (buf->last_buf) {
1191+
last_buf = 1;
1192+
buf->last_buf = 0;
1193+
e = apr_bucket_eos_create(bb->bucket_alloc);
1194+
if (e == NULL) {
1195+
return ngx_http_filter_finalize_request(r,
1196+
&ngx_http_modsecurity, NGX_HTTP_INTERNAL_SERVER_ERROR);
1197+
}
1198+
APR_BRIGADE_INSERT_TAIL(bb, e);
1199+
break;
1200+
}
1201+
1202+
buf->pos = buf->last;
11731203
}
11741204

1175-
rc = move_chain_to_brigade(in, ctx->brigade, r->pool, 0);
1176-
if (rc != NGX_OK) {
1177-
/* waiting for more buffer */
1178-
r->buffered |= NGX_HTTP_SSI_BUFFERED;
1179-
return rc;
1205+
if (!last_buf) {
1206+
return NGX_AGAIN;
11801207
}
11811208

11821209
/* last buf has been saved */
1183-
r->buffered &= ~NGX_HTTP_SSI_BUFFERED;
1184-
11851210
ctx->complete = 1;
11861211
modsecSetResponseBrigade(ctx->req, ctx->brigade);
11871212

11881213
if (ngx_http_modsecurity_load_headers_in(r) != NGX_OK
11891214
|| ngx_http_modsecurity_load_headers_out(r) != NGX_OK) {
11901215

1191-
return NGX_HTTP_INTERNAL_SERVER_ERROR;
1216+
return ngx_http_filter_finalize_request(r,
1217+
&ngx_http_modsecurity, NGX_HTTP_INTERNAL_SERVER_ERROR);
11921218
}
11931219

11941220
rc = ngx_http_modsecurity_status(r, modsecProcessResponse(ctx->req));
@@ -1199,15 +1225,17 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
11991225

12001226
apr_brigade_length(ctx->brigade, 0, &content_length);
12011227

1202-
rc = move_brigade_to_chain(ctx->brigade, &in, r->pool);
1228+
rc = move_brigade_to_chain(ctx->brigade, &out, r->pool);
12031229
if (rc == NGX_ERROR) {
1204-
return NGX_ERROR;
1230+
return ngx_http_filter_finalize_request(r,
1231+
&ngx_http_modsecurity, NGX_HTTP_INTERNAL_SERVER_ERROR);
12051232
}
12061233

12071234
if (ngx_http_modsecurity_save_headers_in(r) != NGX_OK
12081235
||ngx_http_modsecurity_save_headers_out(r) != NGX_OK) {
12091236

1210-
return ngx_http_filter_finalize_request(r, &ngx_http_modsecurity, NGX_HTTP_INTERNAL_SERVER_ERROR);
1237+
return ngx_http_filter_finalize_request(r,
1238+
&ngx_http_modsecurity, NGX_HTTP_INTERNAL_SERVER_ERROR);
12111239
}
12121240

12131241
if (r->headers_out.content_length_n != -1) {
@@ -1223,7 +1251,7 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
12231251
return ngx_http_filter_finalize_request(r, &ngx_http_modsecurity, rc);
12241252
}
12251253

1226-
return ngx_http_next_body_filter(r, in);
1254+
return ngx_http_next_body_filter(r, out);
12271255
}
12281256

12291257

0 commit comments

Comments
 (0)