Skip to content

Commit f84e2cc

Browse files
committed
update bug fixed request body phase event been overwritten
1 parent dcb4cb3 commit f84e2cc

File tree

1 file changed

+100
-102
lines changed

1 file changed

+100
-102
lines changed

src/ngx_http_c_func_module.c

+100-102
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ static void * ngx_http_c_func_create_loc_conf(ngx_conf_t *cf);
121121
static char * ngx_http_c_func_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
122122
static char *ngx_http_c_func_ext_req_headers_add_cmd(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
123123
static char *ngx_http_c_func_init_method(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
124+
static ngx_int_t ngx_http_c_func_content_handler(ngx_http_request_t *r);
124125
static ngx_int_t ngx_http_c_func_precontent_handler(ngx_http_request_t *r);
125126
static void ngx_http_c_func_parse_ext_request_headers(ngx_http_request_t *r, ngx_array_t *ext_req_headers);
126127
static ngx_int_t ngx_http_c_func_rewrite_handler(ngx_http_request_t *r);
@@ -134,11 +135,11 @@ static u_char* ngx_http_c_func_strdup_with_p(ngx_pool_t *pool, const char *src,
134135

135136
// static ngx_int_t ngx_http_c_func_get_resp_var(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
136137
// static void ngx_http_c_func_set_resp_var_with_r(ngx_http_request_t *r, ngx_http_c_func_ctx_t *ctx, const char* resp_content, size_t resp_len);
137-
#if (NGX_THREADS) && (nginx_version > 1013003)
138-
static void ngx_http_c_func_output_filter(ngx_http_request_t *r);
139-
#else
138+
// #if (NGX_THREADS) && (nginx_version > 1013003)
139+
// static void ngx_http_c_func_output_filter(ngx_http_request_t *r);
140+
// #else
140141
static ngx_int_t ngx_http_c_func_output_filter(ngx_http_request_t *r);
141-
#endif
142+
// #endif
142143

143144
#if (NGX_THREADS) && (nginx_version > 1013003)
144145
static void ngx_http_c_func_after_process(ngx_event_t *ev);
@@ -551,29 +552,26 @@ ngx_http_c_func_post_configuration(ngx_conf_t *cf) {
551552
/***Enable pre content phase for apps concurrent processing request layer, NGX_DONE and wait for finalize request ***/
552553
#if (NGX_THREADS) && (nginx_version > 1013003)
553554
h = ngx_array_push(&cmcf->phases[NGX_HTTP_PRECONTENT_PHASE].handlers);
555+
if (h == NULL) {
556+
return NGX_ERROR;
557+
}
558+
*h = ngx_http_c_func_precontent_handler;
559+
560+
h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
561+
if (h == NULL) {
562+
return NGX_ERROR;
563+
}
564+
*h = ngx_http_c_func_content_handler;
554565
#else
555-
/**Access Phase is the only last phase for multi thread, we need to mimic to trick nginx c function access phase at first to register,
556-
then it will be last to called as it is reverse order
557-
**/
558566
h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
559-
// if (h == NULL) {
560-
// return NGX_ERROR;
561-
// }
562567

563-
// h = cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers.elts;
564-
// ngx_uint_t i;
565-
// for (i = 0; i < cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers.nelts - 1; i++) {
566-
// h[i + 1] = h[i];
567-
// }
568-
569-
// h[0] = ngx_http_c_func_precontent_handler;
570-
#endif
571-
572568
if (h == NULL) {
573569
return NGX_ERROR;
574570
}
575571

576572
*h = ngx_http_c_func_precontent_handler;
573+
574+
#endif
577575

578576
}
579577

@@ -974,18 +972,8 @@ ngx_http_c_func_after_process(ngx_event_t *ev) {
974972
r->main->blocked--;
975973
r->aio = 0;
976974

977-
if (r->done) {
978-
/*
979-
* trigger connection event handler if the subrequest was
980-
* already finalized; this can happen if the handler is used
981-
* for sendfile() in threads
982-
*/
983-
c->write->handler(c->write);
984-
985-
} else {
986-
r->write_event_handler(r);
987-
ngx_http_run_posted_requests(c);
988-
}
975+
ngx_http_core_run_phases(r);
976+
ngx_http_run_posted_requests(c);
989977
}
990978
#endif
991979

@@ -1023,12 +1011,12 @@ ngx_http_c_func_precontent_handler(ngx_http_request_t *r) {
10231011
if (internal_ctx->aio_processing) {
10241012
return NGX_AGAIN;
10251013
} else {
1026-
#if (NGX_THREADS) && (nginx_version > 1013003)
1027-
ngx_http_c_func_output_filter(r);
1028-
return NGX_DONE;
1029-
#else
1030-
return ngx_http_c_func_output_filter(r);
1031-
#endif
1014+
// #if (NGX_THREADS) && (nginx_version > 1013003)
1015+
// ngx_http_c_func_output_filter(r);
1016+
// return NGX_OK;
1017+
// #else
1018+
return NGX_DECLINED;
1019+
// #endif
10321020
}
10331021

10341022
new_task:
@@ -1100,12 +1088,10 @@ ngx_http_c_func_precontent_handler(ngx_http_request_t *r) {
11001088
if ((len = ngx_buf_size(b)) == 0) {
11011089
goto REQUEST_BODY_DONE;
11021090
}
1103-
buf = ngx_palloc(r->pool, (len + 1) );
1104-
ngx_memcpy(buf, b->pos, len);
1091+
buf = b->pos;
11051092
// buf[len] = '\0';
11061093
}
11071094
/************End REading ****************/
1108-
11091095
REQUEST_BODY_DONE:
11101096
if (buf /*If got request body*/) {
11111097
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "request_line=%V \n \
@@ -1167,14 +1153,19 @@ ngx_http_c_func_precontent_handler(ngx_http_request_t *r) {
11671153
#endif
11681154
lcf->_handler(new_ctx);
11691155
#if (NGX_THREADS) && (nginx_version > 1013003)
1170-
ngx_http_c_func_output_filter(r);
1171-
return NGX_DONE;
1156+
// ngx_http_c_func_output_filter(r);
1157+
return NGX_DECLINED;
11721158
#else
1173-
return ngx_http_c_func_output_filter(r);
1159+
return ngx_http_c_func_content_handler(r);
11741160
#endif
11751161

11761162
} /* ngx_http_c_func_precontent_handler */
11771163

1164+
static ngx_int_t
1165+
ngx_http_c_func_content_handler(ngx_http_request_t *r) {
1166+
return ngx_http_c_func_output_filter(r);
1167+
} /* ngx_http_c_func_content_handler */
1168+
11781169
static void
11791170
ngx_http_c_func_parse_ext_request_headers(ngx_http_request_t *r, ngx_array_t *ext_req_headers) {
11801171
ngx_uint_t i, nelts;
@@ -1267,17 +1258,25 @@ ngx_http_c_func_rewrite_handler(ngx_http_request_t *r) {
12671258
/****Reading Body Request ****/
12681259
rc = ngx_http_read_client_request_body(r, ngx_http_c_func_client_body_handler);
12691260

1270-
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
1271-
#if (nginx_version < 1002006) || \
1272-
(nginx_version >= 1003000 && nginx_version < 1003009)
1261+
if (rc == NGX_ERROR) {
1262+
return NGX_ERROR;
1263+
}
1264+
1265+
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
1266+
#if (nginx_version >= 8011 && nginx_version < 1002006) \
1267+
|| (nginx_version >= 1003000 && nginx_version < 1003009)
12731268
r->main->count--;
12741269
#endif
12751270
return rc;
12761271
}
12771272

1273+
#if nginx_version >= 8011
1274+
r->main->count--;
1275+
#endif
1276+
12781277
if (rc == NGX_AGAIN) {
12791278
ctx->waiting_more_body = 1;
1280-
return NGX_DONE;
1279+
return NGX_AGAIN;
12811280
}
12821281

12831282
return NGX_DECLINED;
@@ -1301,14 +1300,12 @@ static void
13011300
ngx_http_c_func_client_body_handler(ngx_http_request_t *r) {
13021301
ngx_http_c_func_internal_ctx_t *ctx;
13031302
ctx = ngx_http_get_module_ctx(r, ngx_http_c_func_module);
1304-
ctx->done = 1;
13051303

1306-
#if defined(nginx_version) && nginx_version >= 8011
1307-
r->main->count--;
1308-
#endif
13091304
/* waiting_more_body my rewrite phase handler */
13101305
if (ctx->waiting_more_body) {
1306+
ctx->done = 1;
13111307
ctx->waiting_more_body = 0;
1308+
r->write_event_handler = ngx_http_core_run_phases;
13121309
ngx_http_core_run_phases(r);
13131310
}
13141311
}
@@ -1754,68 +1751,69 @@ ngx_http_c_func_write_resp(
17541751
);
17551752
}
17561753

1757-
#if (NGX_THREADS) && (nginx_version > 1013003)
1758-
static void
1759-
ngx_http_c_func_output_filter(
1760-
ngx_http_request_t *r
1761-
) {
1762-
ngx_int_t rc;
1763-
ngx_chain_t out;
1764-
ngx_http_c_func_internal_ctx_t *internal_ctx;
1765-
ngx_str_t *resp_content_type, *resp_status_line;
1766-
ngx_buf_t *b;
1754+
// #if (NGX_THREADS) && (nginx_version > 1013003)
1755+
// static void
1756+
// ngx_http_c_func_output_filter(
1757+
// ngx_http_request_t *r
1758+
// ) {
1759+
// ngx_int_t rc;
1760+
// ngx_chain_t out;
1761+
// ngx_http_c_func_internal_ctx_t *internal_ctx;
1762+
// ngx_str_t *resp_content_type, *resp_status_line;
1763+
// ngx_buf_t *b;
17671764

1768-
internal_ctx = ngx_http_get_module_ctx(r, ngx_http_c_func_module);
1765+
// internal_ctx = ngx_http_get_module_ctx(r, ngx_http_c_func_module);
17691766

1770-
if (internal_ctx == NULL) {
1771-
ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "Session is not valid");
1772-
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1773-
return;
1774-
}
1767+
// if (internal_ctx == NULL) {
1768+
// ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "Session is not valid");
1769+
// ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1770+
// return;
1771+
// }
17751772

1776-
if (internal_ctx->rc == NGX_HTTP_INTERNAL_SERVER_ERROR) {
1777-
ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "Apps Internal Server error");
1778-
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1779-
return;
1780-
}
1773+
// if (internal_ctx->rc == NGX_HTTP_INTERNAL_SERVER_ERROR) {
1774+
// ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "Apps Internal Server error");
1775+
// ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1776+
// return;
1777+
// }
17811778

1782-
resp_status_line = &internal_ctx->status_line;
1783-
resp_content_type = &internal_ctx->content_type;
1784-
b = internal_ctx->resp_content;
1779+
// resp_status_line = &internal_ctx->status_line;
1780+
// resp_content_type = &internal_ctx->content_type;
1781+
// b = internal_ctx->resp_content;
17851782

1786-
r->headers_out.status = internal_ctx->status_code;
1783+
// r->headers_out.status = internal_ctx->status_code;
17871784

1788-
if (resp_status_line->len) {
1789-
r->headers_out.status_line.len = resp_status_line->len;
1790-
r->headers_out.status_line.data = resp_status_line->data;
1791-
}
1785+
// if (resp_status_line->len) {
1786+
// r->headers_out.status_line.len = resp_status_line->len;
1787+
// r->headers_out.status_line.data = resp_status_line->data;
1788+
// }
17921789

1793-
/* Set the Content-Type header. */
1794-
r->headers_out.content_type.len = resp_content_type->len;
1795-
r->headers_out.content_type.data = resp_content_type->data;
1790+
// /* Set the Content-Type header. */
1791+
// r->headers_out.content_type.len = resp_content_type->len;
1792+
// r->headers_out.content_type.data = resp_content_type->data;
17961793

1797-
/* Get the content length of the body. */
1798-
r->headers_out.content_length_n = ngx_buf_size(b);
1794+
// /* Get the content length of the body. */
1795+
// r->headers_out.content_length_n = ngx_buf_size(b);
17991796

1800-
rc = ngx_http_send_header(r); /* Send the headers */
1801-
if (rc == NGX_ERROR) {
1802-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "response processing failed.");
1803-
// ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1804-
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
1805-
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1806-
return;
1797+
// rc = ngx_http_send_header(r); /* Send the headers */
1798+
// if (rc == NGX_ERROR) {
1799+
// ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "response processing failed.");
1800+
// // ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1801+
// rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
1802+
// ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1803+
// return;
18071804

1808-
}
1805+
// }
18091806

1810-
/* Insertion in the buffer chain. */
1811-
out.buf = b;
1812-
out.next = NULL; /* just one buffer */
1807+
// /* Insertion in the buffer chain. */
1808+
// out.buf = b;
1809+
// out.next = NULL; /* just one buffer */
18131810

1814-
/* Send the body, and return the status code of the output filter chain. */
1815-
ngx_http_finalize_request(r, ngx_http_output_filter(r, &out)); // only using when request client body
1816-
// rc = ngx_http_output_filter(r, &out);
1817-
}
1818-
#else
1811+
// /* Send the body, and return the status code of the output filter chain. */
1812+
// // ngx_http_finalize_request(r, ngx_http_output_filter(r, &out)); // only using when request client body
1813+
// // rc = ngx_http_output_filter(r, &out);
1814+
// ngx_http_output_filter(r, &out);
1815+
// }
1816+
// #else
18191817
static ngx_int_t
18201818
ngx_http_c_func_output_filter(
18211819
ngx_http_request_t *r
@@ -1871,7 +1869,7 @@ ngx_http_c_func_output_filter(
18711869
// ngx_http_finalize_request(r, ngx_http_output_filter(r, &out)); // only using when request client body
18721870
return ngx_http_output_filter(r, &out);
18731871
}
1874-
#endif
1872+
// #endif
18751873

18761874
/****Download Feature Support ****/
18771875
static int

0 commit comments

Comments
 (0)