Klaus Demo nginx / b9409a8
use already available r and u instead of ev Igor Sysoev 13 years ago
2 changed file(s) with 76 addition(s) and 83 deletion(s). Raw diff Collapse all Expand all
1919 ngx_http_upstream_t *u);
2020 static void ngx_http_upstream_send_request(ngx_http_request_t *r,
2121 ngx_http_upstream_t *u);
22 static void ngx_http_upstream_send_request_handler(ngx_event_t *wev);
23 static void ngx_http_upstream_process_header(ngx_event_t *rev);
22 static void ngx_http_upstream_send_request_handler(ngx_http_request_t *r,
23 ngx_http_upstream_t *u);
24 static void ngx_http_upstream_process_header(ngx_http_request_t *r,
25 ngx_http_upstream_t *u);
2426 static ngx_int_t ngx_http_upstream_test_next(ngx_http_request_t *r,
2527 ngx_http_upstream_t *u);
2628 static ngx_int_t ngx_http_upstream_intercept_errors(ngx_http_request_t *r,
2729 ngx_http_upstream_t *u);
2830 static ngx_int_t ngx_http_upstream_test_connect(ngx_connection_t *c);
29 static void ngx_http_upstream_process_body_in_memory(ngx_event_t *rev);
31 static void ngx_http_upstream_process_body_in_memory(ngx_http_request_t *r,
32 ngx_http_upstream_t *u);
3033 static void ngx_http_upstream_send_response(ngx_http_request_t *r,
3134 ngx_http_upstream_t *u);
3235 static void
3336 ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r);
34 static void ngx_http_upstream_process_non_buffered_upstream(ngx_event_t *ev);
37 static void
38 ngx_http_upstream_process_non_buffered_upstream(ngx_http_request_t *r,
39 ngx_http_upstream_t *u);
3540 static void
3641 ngx_http_upstream_process_non_buffered_request(ngx_http_request_t *r,
3742 ngx_uint_t do_write);
3944 static ngx_int_t ngx_http_upstream_non_buffered_filter(void *data,
4045 ssize_t bytes);
4146 static void ngx_http_upstream_process_downstream(ngx_http_request_t *r);
42 static void ngx_http_upstream_process_upstream(ngx_event_t *rev);
47 static void ngx_http_upstream_process_upstream(ngx_http_request_t *r,
48 ngx_http_upstream_t *u);
4349 static void ngx_http_upstream_process_request(ngx_http_request_t *r);
4450 static void ngx_http_upstream_store(ngx_http_request_t *r,
4551 ngx_http_upstream_t *u);
46 static void ngx_http_upstream_dummy_handler(ngx_event_t *wev);
52 static void ngx_http_upstream_dummy_handler(ngx_http_request_t *r,
53 ngx_http_upstream_t *u);
4754 static void ngx_http_upstream_next(ngx_http_request_t *r,
4855 ngx_http_upstream_t *u, ngx_uint_t ft_type);
4956 static void ngx_http_upstream_cleanup(void *data);
537544 "http upstream request: \"%V?%V\"", &r->uri, &r->args);
538545
539546 if (ev->write) {
540 u->write_event_handler(ev);
547 u->write_event_handler(r, u);
541548
542549 } else {
543 u->read_event_handler(ev);
550 u->read_event_handler(r, u);
544551 }
545552
546553 ngx_http_run_posted_requests(c);
9951002 if (rc == NGX_AGAIN) {
9961003 ngx_add_timer(c->write, u->conf->send_timeout);
9971004
998 if (ngx_handle_write_event(c->write, u->conf->send_lowat) == NGX_ERROR)
999 {
1005 if (ngx_handle_write_event(c->write, u->conf->send_lowat) != NGX_OK) {
10001006 ngx_http_upstream_finalize_request(r, u,
10011007 NGX_HTTP_INTERNAL_SERVER_ERROR);
10021008 return;
10331039 * it's better to do here because we postpone header buffer allocation
10341040 */
10351041
1036 ngx_http_upstream_process_header(c->read);
1042 ngx_http_upstream_process_header(r, u);
10371043 return;
10381044 }
10391045 #endif
10401046
10411047 u->write_event_handler = ngx_http_upstream_dummy_handler;
10421048
1043 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
1049 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
10441050 ngx_http_upstream_finalize_request(r, u,
10451051 NGX_HTTP_INTERNAL_SERVER_ERROR);
10461052 return;
10491055
10501056
10511057 static void
1052 ngx_http_upstream_send_request_handler(ngx_event_t *wev)
1053 {
1054 ngx_connection_t *c;
1055 ngx_http_request_t *r;
1056 ngx_http_upstream_t *u;
1057
1058 c = wev->data;
1059 r = c->data;
1060 u = r->upstream;
1061
1062 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
1058 ngx_http_upstream_send_request_handler(ngx_http_request_t *r,
1059 ngx_http_upstream_t *u)
1060 {
1061 ngx_connection_t *c;
1062
1063 c = u->peer.connection;
1064
1065 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
10631066 "http upstream send request handler");
10641067
1065 if (wev->timedout) {
1068 if (c->write->timedout) {
10661069 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT);
10671070 return;
10681071 }
10791082 if (u->header_sent) {
10801083 u->write_event_handler = ngx_http_upstream_dummy_handler;
10811084
1082 (void) ngx_handle_write_event(wev, 0);
1085 (void) ngx_handle_write_event(c->write, 0);
10831086
10841087 return;
10851088 }
10891092
10901093
10911094 static void
1092 ngx_http_upstream_process_header(ngx_event_t *rev)
1095 ngx_http_upstream_process_header(ngx_http_request_t *r, ngx_http_upstream_t *u)
10931096 {
10941097 ssize_t n;
10951098 ngx_int_t rc;
10981101 ngx_list_part_t *part;
10991102 ngx_table_elt_t *h;
11001103 ngx_connection_t *c;
1101 ngx_http_request_t *r;
1102 ngx_http_upstream_t *u;
11031104 ngx_http_upstream_header_t *hh;
11041105 ngx_http_upstream_main_conf_t *umcf;
11051106
1106 c = rev->data;
1107 r = c->data;
1108 u = r->upstream;
1109
1110 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
1107 c = u->peer.connection;
1108
1109 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
11111110 "http upstream process header");
11121111
11131112 c->log->action = "reading response header from upstream";
11141113
1115 if (rev->timedout) {
1114 if (c->read->timedout) {
11161115 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT);
11171116 return;
11181117 }
11631162 ngx_add_timer(rev, u->read_timeout);
11641163 #endif
11651164
1166 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
1165 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
11671166 ngx_http_upstream_finalize_request(r, u,
11681167 NGX_HTTP_INTERNAL_SERVER_ERROR);
11691168 return;
11731172 }
11741173
11751174 if (n == 0) {
1176 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
1175 ngx_log_error(NGX_LOG_ERR, c->log, 0,
11771176 "upstream prematurely closed connection");
11781177 }
11791178
11951194 if (rc == NGX_AGAIN) {
11961195
11971196 if (u->buffer.pos == u->buffer.end) {
1198 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
1197 ngx_log_error(NGX_LOG_ERR, c->log, 0,
11991198 "upstream sent too big header");
12001199
12011200 ngx_http_upstream_next(r, u,
13921391
13931392 u->read_event_handler = ngx_http_upstream_process_body_in_memory;
13941393
1395 ngx_http_upstream_process_body_in_memory(rev);
1394 ngx_http_upstream_process_body_in_memory(r, u);
13961395 }
13971396
13981397
15361535
15371536
15381537 static void
1539 ngx_http_upstream_process_body_in_memory(ngx_event_t *rev)
1540 {
1541 size_t size;
1542 ssize_t n;
1543 ngx_buf_t *b;
1544 ngx_connection_t *c;
1545 ngx_http_request_t *r;
1546 ngx_http_upstream_t *u;
1547
1548 c = rev->data;
1549 r = c->data;
1550 u = r->upstream;
1538 ngx_http_upstream_process_body_in_memory(ngx_http_request_t *r,
1539 ngx_http_upstream_t *u)
1540 {
1541 size_t size;
1542 ssize_t n;
1543 ngx_buf_t *b;
1544 ngx_event_t *rev;
1545 ngx_connection_t *c;
1546
1547 c = u->peer.connection;
1548 rev = c->read;
15511549
15521550 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
15531551 "http upstream process body on memory");
15921590 }
15931591 }
15941592
1595 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
1593 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
15961594 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
15971595 return;
15981596 }
17041702 }
17051703
17061704 if (u->peer.connection->read->ready) {
1707 ngx_http_upstream_process_non_buffered_upstream(
1708 u->peer.connection->read);
1705 ngx_http_upstream_process_non_buffered_upstream(r, u);
17091706 }
17101707 }
17111708
18381835 u->read_event_handler = ngx_http_upstream_process_upstream;
18391836 r->write_event_handler = ngx_http_upstream_process_downstream;
18401837
1841 ngx_http_upstream_process_upstream(u->peer.connection->read);
1838 ngx_http_upstream_process_upstream(r, u);
18421839 }
18431840
18441841
18701867
18711868
18721869 static void
1873 ngx_http_upstream_process_non_buffered_upstream(ngx_event_t *rev)
1874 {
1875 ngx_connection_t *c;
1876 ngx_http_request_t *r;
1877 ngx_http_upstream_t *u;
1878
1879 c = rev->data;
1880 r = c->data;
1881 u = r->upstream;
1870 ngx_http_upstream_process_non_buffered_upstream(ngx_http_request_t *r,
1871 ngx_http_upstream_t *u)
1872 {
1873 ngx_connection_t *c;
1874
1875 c = u->peer.connection;
18821876
18831877 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
18841878 "http upstream process non buffered upstream");
18851879
18861880 c->log->action = "reading upstream";
18871881
1888 if (rev->timedout) {
1882 if (c->read->timedout) {
18891883 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
18901884 ngx_http_upstream_finalize_request(r, u, 0);
18911885 return;
21412135
21422136
21432137 static void
2144 ngx_http_upstream_process_upstream(ngx_event_t *rev)
2145 {
2146 ngx_connection_t *c;
2147 ngx_event_pipe_t *p;
2148 ngx_http_request_t *r;
2149 ngx_http_upstream_t *u;
2150
2151 c = rev->data;
2152 r = c->data;
2153 u = r->upstream;
2154 p = u->pipe;
2138 ngx_http_upstream_process_upstream(ngx_http_request_t *r,
2139 ngx_http_upstream_t *u)
2140 {
2141 ngx_connection_t *c;
2142
2143 c = u->peer.connection;
21552144
21562145 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
21572146 "http upstream process upstream");
21582147
21592148 c->log->action = "reading upstream";
21602149
2161 if (rev->timedout) {
2162 p->upstream_error = 1;
2150 if (c->read->timedout) {
2151 u->pipe->upstream_error = 1;
21632152 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
21642153
21652154 } else {
21662155 c = r->connection;
21672156
2168 if (ngx_event_pipe(p, 0) == NGX_ABORT) {
2157 if (ngx_event_pipe(u->pipe, 0) == NGX_ABORT) {
21692158
21702159 if (c->destroyed) {
21712160 return;
23362325
23372326
23382327 static void
2339 ngx_http_upstream_dummy_handler(ngx_event_t *wev)
2340 {
2341 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
2328 ngx_http_upstream_dummy_handler(ngx_http_request_t *r, ngx_http_upstream_t *u)
2329 {
2330 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
23422331 "http upstream dummy handler");
23432332 }
23442333
215215 } ngx_http_upstream_resolved_t;
216216
217217
218 typedef void (*ngx_http_upstream_handler_pt)(ngx_http_request_t *r,
219 ngx_http_upstream_t *u);
220
221
218222 struct ngx_http_upstream_s {
219 ngx_event_handler_pt read_event_handler;
220 ngx_event_handler_pt write_event_handler;
223 ngx_http_upstream_handler_pt read_event_handler;
224 ngx_http_upstream_handler_pt write_event_handler;
221225
222226 ngx_peer_connection_t peer;
223227