0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10 #include <ngx_event_connect.h>
|
|
11 #include <ngx_event_pipe.h>
|
|
12 #include <ngx_http.h>
|
|
13 #include <ngx_http_proxy_handler.h>
|
|
14
|
|
15
|
|
16 static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_proxy_ctx_t *p);
|
28
|
17 static void ngx_http_proxy_init_upstream(ngx_http_request_t *r);
|
0
|
18 static void ngx_http_proxy_reinit_upstream(ngx_http_proxy_ctx_t *p);
|
|
19 static void ngx_http_proxy_connect(ngx_http_proxy_ctx_t *p);
|
|
20 static void ngx_http_proxy_send_request(ngx_http_proxy_ctx_t *p);
|
|
21 static void ngx_http_proxy_send_request_handler(ngx_event_t *wev);
|
|
22 static void ngx_http_proxy_dummy_handler(ngx_event_t *wev);
|
|
23 static void ngx_http_proxy_process_upstream_status_line(ngx_event_t *rev);
|
|
24 static void ngx_http_proxy_process_upstream_headers(ngx_event_t *rev);
|
|
25 static ssize_t ngx_http_proxy_read_upstream_header(ngx_http_proxy_ctx_t *);
|
|
26 static void ngx_http_proxy_send_response(ngx_http_proxy_ctx_t *p);
|
|
27 static void ngx_http_proxy_process_body(ngx_event_t *ev);
|
10
|
28 static void ngx_http_proxy_next_upstream(ngx_http_proxy_ctx_t *p,
|
|
29 ngx_uint_t ft_type);
|
0
|
30
|
|
31
|
|
32 static ngx_str_t http_methods[] = {
|
|
33 ngx_string("GET "),
|
|
34 ngx_string("HEAD "),
|
|
35 ngx_string("POST ")
|
|
36 };
|
|
37
|
|
38
|
|
39 static char *upstream_header_errors[] = {
|
|
40 "upstream sent invalid header",
|
|
41 "upstream sent too long header line"
|
|
42 };
|
|
43
|
|
44
|
|
45 static char http_version[] = " HTTP/1.0" CRLF;
|
|
46 static char host_header[] = "Host: ";
|
6
|
47 static char x_url_header[] = "X-URL: http";
|
0
|
48 static char x_real_ip_header[] = "X-Real-IP: ";
|
|
49 static char x_forwarded_for_header[] = "X-Forwarded-For: ";
|
|
50 static char connection_close_header[] = "Connection: close" CRLF;
|
|
51
|
|
52
|
|
53 int ngx_http_proxy_request_upstream(ngx_http_proxy_ctx_t *p)
|
|
54 {
|
|
55 int rc;
|
|
56 ngx_http_request_t *r;
|
|
57 ngx_http_proxy_upstream_t *u;
|
|
58
|
|
59 r = p->request;
|
|
60
|
50
|
61 u = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_upstream_t));
|
|
62 if (u == NULL) {
|
0
|
63 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
64 }
|
|
65
|
|
66 p->upstream = u;
|
|
67
|
|
68 u->peer.log_error = NGX_ERROR_ERR;
|
|
69 u->peer.peers = p->lcf->peers;
|
|
70 u->peer.tries = p->lcf->peers->number;
|
|
71 #if (NGX_THREADS)
|
|
72 u->peer.lock = &r->connection->lock;
|
|
73 #endif
|
|
74
|
|
75 u->method = r->method;
|
|
76
|
28
|
77 rc = ngx_http_read_client_request_body(r, ngx_http_proxy_init_upstream);
|
0
|
78
|
|
79 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
|
|
80 return rc;
|
|
81 }
|
|
82
|
|
83 return NGX_DONE;
|
|
84 }
|
|
85
|
|
86
|
|
87 static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_proxy_ctx_t *p)
|
|
88 {
|
54
|
89 size_t len;
|
34
|
90 ngx_uint_t i, escape, *index;
|
0
|
91 ngx_buf_t *b;
|
|
92 ngx_chain_t *chain;
|
|
93 ngx_list_part_t *part;
|
|
94 ngx_table_elt_t *header;
|
|
95 ngx_http_request_t *r;
|
34
|
96 ngx_http_variable_t *var;
|
|
97 ngx_http_variable_value_t *value;
|
|
98 ngx_http_core_main_conf_t *cmcf;
|
0
|
99 ngx_http_proxy_upstream_conf_t *uc;
|
|
100
|
|
101 r = p->request;
|
|
102 uc = p->lcf->upstream;
|
|
103
|
36
|
104 #if (NGX_SUPPRESS_WARN)
|
|
105 var = NULL;
|
|
106 index = NULL;
|
|
107 #endif
|
|
108
|
54
|
109 escape = 0;
|
|
110
|
0
|
111 if (p->upstream->method) {
|
54
|
112 len = http_methods[p->upstream->method - 1].len + uc->uri.len;
|
0
|
113
|
|
114 } else {
|
54
|
115 len = r->method_name.len + uc->uri.len;
|
0
|
116 }
|
|
117
|
54
|
118 if (p->lcf->pass_unparsed_uri && r->valid_unparsed_uri) {
|
|
119 len += r->unparsed_uri.len - 1;
|
50
|
120
|
54
|
121 } else {
|
|
122 if (r->quoted_uri) {
|
|
123 escape = 2 * ngx_escape_uri(NULL, r->uri.data + uc->location->len,
|
|
124 r->uri.len - uc->location->len,
|
|
125 NGX_ESCAPE_URI);
|
|
126 }
|
50
|
127
|
54
|
128 len += r->uri.len - uc->location->len + escape
|
|
129 + sizeof("?") - 1 + r->args.len;
|
2
|
130 }
|
|
131
|
54
|
132 len += sizeof(http_version) - 1
|
6
|
133 + sizeof(connection_close_header) - 1
|
34
|
134 + sizeof(CRLF) - 1;
|
6
|
135
|
|
136
|
|
137 if (p->lcf->set_x_url) {
|
|
138 len += sizeof(x_url_header) - 1
|
34
|
139 + sizeof("s://") - 1
|
6
|
140 + r->port_text->len
|
|
141 + r->unparsed_uri.len
|
34
|
142 + sizeof(CRLF) - 1;
|
6
|
143
|
|
144 if (r->headers_in.host) {
|
|
145 len += r->headers_in.host_name_len;
|
|
146
|
|
147 } else {
|
|
148 len += r->server_name.len;
|
|
149 }
|
|
150
|
|
151 }
|
|
152
|
0
|
153
|
46
|
154 if (p->lcf->preserve_host) {
|
|
155 if (r->headers_in.host) {
|
|
156 len += sizeof(host_header) - 1
|
|
157 + r->headers_in.host_name_len + sizeof(":") - 1
|
|
158 + uc->port_text.len + sizeof(CRLF) - 1;
|
|
159
|
|
160 } else {
|
|
161 len += sizeof(host_header) - 1
|
|
162 + r->server_name.len + sizeof(":") - 1
|
|
163 + uc->port_text.len + sizeof(CRLF) - 1;
|
|
164 }
|
|
165
|
34
|
166 } else {
|
|
167 len += sizeof(host_header) - 1 + uc->host_header.len
|
|
168 + sizeof(CRLF) - 1;
|
0
|
169 }
|
|
170
|
|
171
|
34
|
172 if (p->lcf->set_x_real_ip) {
|
|
173 len += sizeof(x_real_ip_header) - 1 + INET_ADDRSTRLEN - 1
|
|
174 + sizeof(CRLF) - 1;
|
0
|
175 }
|
|
176
|
|
177
|
|
178 if (p->lcf->add_x_forwarded_for) {
|
|
179 if (r->headers_in.x_forwarded_for) {
|
|
180 len += sizeof(x_forwarded_for_header) - 1
|
6
|
181 + r->headers_in.x_forwarded_for->value.len
|
34
|
182 + sizeof(", ") - 1 + INET_ADDRSTRLEN - 1 + sizeof(CRLF) - 1;
|
|
183
|
0
|
184 } else {
|
34
|
185 len += sizeof(x_forwarded_for_header) - 1 + INET_ADDRSTRLEN - 1
|
|
186 + sizeof(CRLF) - 1;
|
|
187 }
|
|
188 }
|
|
189
|
|
190
|
36
|
191 if (p->lcf->x_vars) {
|
|
192 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
34
|
193
|
36
|
194 var = cmcf->variables.elts;
|
|
195 index = p->lcf->x_vars->elts;
|
34
|
196
|
36
|
197 for (i = 0; i < p->lcf->x_vars->nelts; i++) {
|
34
|
198
|
50
|
199 value = ngx_http_get_indexed_variable(r, index[i]);
|
|
200 if (value == NULL) {
|
36
|
201 continue;
|
|
202 }
|
34
|
203
|
36
|
204 if (value->text.len) {
|
|
205 len += sizeof("X-") - 1 + var[index[i]].name.len
|
|
206 + sizeof(": ") - 1 + value->text.len + sizeof(CRLF) - 1;
|
|
207 }
|
0
|
208 }
|
|
209 }
|
|
210
|
|
211
|
|
212 part = &r->headers_in.headers.part;
|
|
213 header = part->elts;
|
|
214
|
|
215 for (i = 0; /* void */; i++) {
|
|
216
|
|
217 if (i >= part->nelts) {
|
|
218 if (part->next == NULL) {
|
|
219 break;
|
|
220 }
|
|
221
|
|
222 part = part->next;
|
|
223 header = part->elts;
|
|
224 i = 0;
|
|
225 }
|
|
226
|
|
227 if (&header[i] == r->headers_in.host) {
|
|
228 continue;
|
|
229 }
|
|
230
|
|
231 if (&header[i] == r->headers_in.connection) {
|
|
232 continue;
|
|
233 }
|
|
234
|
34
|
235 len += header[i].key.len + sizeof(": ") - 1
|
|
236 + header[i].value.len + sizeof(CRLF) - 1;
|
0
|
237 }
|
|
238
|
|
239 #if (NGX_DEBUG)
|
|
240 len++;
|
|
241 #endif
|
|
242
|
50
|
243 b = ngx_create_temp_buf(r->pool, len);
|
|
244 if (b == NULL) {
|
34
|
245 return NULL;
|
|
246 }
|
|
247
|
50
|
248 chain = ngx_alloc_chain_link(r->pool);
|
|
249 if (chain == NULL) {
|
34
|
250 return NULL;
|
|
251 }
|
|
252
|
|
253 chain->buf = b;
|
|
254 chain->next = NULL;
|
0
|
255
|
|
256
|
|
257 /* the request line */
|
|
258
|
|
259 if (p->upstream->method) {
|
|
260 b->last = ngx_cpymem(b->last,
|
|
261 http_methods[p->upstream->method - 1].data,
|
|
262 http_methods[p->upstream->method - 1].len);
|
|
263 } else {
|
|
264 b->last = ngx_cpymem(b->last, r->method_name.data, r->method_name.len);
|
|
265 }
|
|
266
|
|
267 b->last = ngx_cpymem(b->last, uc->uri.data, uc->uri.len);
|
|
268
|
54
|
269 if (p->lcf->pass_unparsed_uri && r->valid_unparsed_uri) {
|
|
270 b->last = ngx_cpymem(b->last, r->unparsed_uri.data + 1,
|
|
271 r->unparsed_uri.len - 1);
|
2
|
272 } else {
|
54
|
273 if (escape) {
|
|
274 ngx_escape_uri(b->last, r->uri.data + uc->location->len,
|
|
275 r->uri.len - uc->location->len, NGX_ESCAPE_URI);
|
|
276 b->last += r->uri.len - uc->location->len + escape;
|
0
|
277
|
54
|
278 } else {
|
|
279 b->last = ngx_cpymem(b->last, r->uri.data + uc->location->len,
|
|
280 r->uri.len - uc->location->len);
|
|
281 }
|
|
282
|
|
283 if (r->args.len > 0) {
|
|
284 *b->last++ = '?';
|
|
285 b->last = ngx_cpymem(b->last, r->args.data, r->args.len);
|
|
286 }
|
0
|
287 }
|
|
288
|
|
289 b->last = ngx_cpymem(b->last, http_version, sizeof(http_version) - 1);
|
|
290
|
|
291
|
|
292 /* the "Connection: close" header */
|
|
293
|
|
294 b->last = ngx_cpymem(b->last, connection_close_header,
|
|
295 sizeof(connection_close_header) - 1);
|
|
296
|
|
297
|
|
298 /* the "Host" header */
|
|
299
|
|
300 b->last = ngx_cpymem(b->last, host_header, sizeof(host_header) - 1);
|
|
301
|
46
|
302 if (p->lcf->preserve_host) {
|
|
303 if (r->headers_in.host) {
|
|
304 b->last = ngx_cpymem(b->last, r->headers_in.host->value.data,
|
|
305 r->headers_in.host_name_len);
|
|
306 } else {
|
|
307 b->last = ngx_cpymem(b->last, r->server_name.data,
|
|
308 r->server_name.len);
|
|
309 }
|
0
|
310
|
|
311 if (!uc->default_port) {
|
34
|
312 *b->last++ = ':';
|
0
|
313 b->last = ngx_cpymem(b->last, uc->port_text.data,
|
|
314 uc->port_text.len);
|
|
315 }
|
|
316
|
|
317 } else {
|
|
318 b->last = ngx_cpymem(b->last, uc->host_header.data,
|
|
319 uc->host_header.len);
|
|
320 }
|
34
|
321 *b->last++ = CR; *b->last++ = LF;
|
0
|
322
|
|
323
|
6
|
324 /* the "X-URL" header */
|
|
325
|
|
326 if (p->lcf->set_x_url) {
|
|
327
|
|
328 b->last = ngx_cpymem(b->last, x_url_header,
|
|
329 sizeof(x_url_header) - 1);
|
|
330
|
|
331 #if (NGX_OPENSSL)
|
|
332
|
|
333 if (r->connection->ssl) {
|
34
|
334 *b->last++ = 's';
|
6
|
335 }
|
|
336
|
|
337 #endif
|
|
338
|
34
|
339 *b->last++ = ':'; *b->last++ = '/'; *b->last++ = '/';
|
6
|
340
|
|
341 if (r->headers_in.host) {
|
|
342 b->last = ngx_cpymem(b->last, r->headers_in.host->value.data,
|
|
343 r->headers_in.host_name_len);
|
|
344 } else {
|
|
345 b->last = ngx_cpymem(b->last, r->server_name.data,
|
|
346 r->server_name.len);
|
|
347 }
|
|
348
|
|
349 b->last = ngx_cpymem(b->last, r->port_text->data, r->port_text->len);
|
|
350 b->last = ngx_cpymem(b->last, r->unparsed_uri.data,
|
|
351 r->unparsed_uri.len);
|
|
352
|
34
|
353 *b->last++ = CR; *b->last++ = LF;
|
6
|
354 }
|
|
355
|
|
356
|
0
|
357 /* the "X-Real-IP" header */
|
|
358
|
|
359 if (p->lcf->set_x_real_ip) {
|
|
360 b->last = ngx_cpymem(b->last, x_real_ip_header,
|
|
361 sizeof(x_real_ip_header) - 1);
|
|
362 b->last = ngx_cpymem(b->last, r->connection->addr_text.data,
|
|
363 r->connection->addr_text.len);
|
34
|
364 *b->last++ = CR; *b->last++ = LF;
|
0
|
365 }
|
|
366
|
|
367
|
|
368 /* the "X-Forwarded-For" header */
|
|
369
|
|
370 if (p->lcf->add_x_forwarded_for) {
|
|
371 if (r->headers_in.x_forwarded_for) {
|
|
372 b->last = ngx_cpymem(b->last, x_forwarded_for_header,
|
|
373 sizeof(x_forwarded_for_header) - 1);
|
|
374
|
|
375 b->last = ngx_cpymem(b->last,
|
|
376 r->headers_in.x_forwarded_for->value.data,
|
|
377 r->headers_in.x_forwarded_for->value.len);
|
|
378
|
34
|
379 *b->last++ = ','; *b->last++ = ' ';
|
0
|
380
|
|
381 } else {
|
|
382 b->last = ngx_cpymem(b->last, x_forwarded_for_header,
|
|
383 sizeof(x_forwarded_for_header) - 1);
|
|
384 }
|
|
385
|
|
386 b->last = ngx_cpymem(b->last, r->connection->addr_text.data,
|
|
387 r->connection->addr_text.len);
|
34
|
388 *b->last++ = CR; *b->last++ = LF;
|
|
389 }
|
|
390
|
|
391
|
36
|
392 if (p->lcf->x_vars) {
|
|
393 for (i = 0; i < p->lcf->x_vars->nelts; i++) {
|
34
|
394
|
50
|
395 value = ngx_http_get_indexed_variable(r, index[i]);
|
|
396 if (value == NULL) {
|
36
|
397 continue;
|
|
398 }
|
34
|
399
|
36
|
400 if (value->text.len == 0) {
|
|
401 continue;
|
|
402 }
|
34
|
403
|
36
|
404 *b->last++ = 'X'; *b->last++ = '-';
|
34
|
405
|
36
|
406 b->last = ngx_cpymem(b->last, var[index[i]].name.data,
|
|
407 var[index[i]].name.len);
|
34
|
408
|
36
|
409 *b->last++ = ':'; *b->last++ = ' ';
|
34
|
410
|
36
|
411 b->last = ngx_cpymem(b->last, value->text.data, value->text.len);
|
34
|
412
|
36
|
413 *b->last++ = CR; *b->last++ = LF;
|
|
414 }
|
0
|
415 }
|
|
416
|
|
417
|
|
418 part = &r->headers_in.headers.part;
|
|
419 header = part->elts;
|
|
420
|
|
421 for (i = 0; /* void */; i++) {
|
|
422
|
|
423 if (i >= part->nelts) {
|
|
424 if (part->next == NULL) {
|
|
425 break;
|
|
426 }
|
|
427
|
|
428 part = part->next;
|
|
429 header = part->elts;
|
|
430 i = 0;
|
|
431 }
|
|
432
|
|
433 if (&header[i] == r->headers_in.host) {
|
|
434 continue;
|
|
435 }
|
|
436
|
|
437 if (&header[i] == r->headers_in.connection) {
|
|
438 continue;
|
|
439 }
|
|
440
|
|
441 if (&header[i] == r->headers_in.keep_alive) {
|
|
442 continue;
|
|
443 }
|
|
444
|
|
445 if (&header[i] == r->headers_in.x_forwarded_for
|
|
446 && p->lcf->add_x_forwarded_for)
|
|
447 {
|
|
448 continue;
|
|
449 }
|
|
450
|
6
|
451 if (&header[i] == r->headers_in.x_real_ip && p->lcf->set_x_real_ip) {
|
|
452 continue;
|
|
453 }
|
|
454
|
|
455 if (&header[i] == r->headers_in.x_url && p->lcf->set_x_url) {
|
|
456 continue;
|
|
457 }
|
|
458
|
0
|
459 b->last = ngx_cpymem(b->last, header[i].key.data, header[i].key.len);
|
|
460
|
34
|
461 *b->last++ = ':'; *b->last++ = ' ';
|
0
|
462
|
|
463 b->last = ngx_cpymem(b->last, header[i].value.data,
|
|
464 header[i].value.len);
|
|
465
|
34
|
466 *b->last++ = CR; *b->last++ = LF;
|
0
|
467
|
|
468 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
469 "http proxy header: \"%V: %V\"",
|
|
470 &header[i].key, &header[i].value);
|
0
|
471 }
|
|
472
|
|
473 /* add "\r\n" at the header end */
|
34
|
474 *b->last++ = CR; *b->last++ = LF;
|
0
|
475
|
|
476 #if (NGX_DEBUG)
|
34
|
477 *b->last = '\0';
|
0
|
478 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
479 "http proxy header:\n\"%s\"", b->pos);
|
|
480 #endif
|
|
481
|
|
482 return chain;
|
|
483 }
|
|
484
|
|
485
|
28
|
486 static void ngx_http_proxy_init_upstream(ngx_http_request_t *r)
|
0
|
487 {
|
|
488
|
|
489 ngx_chain_t *cl;
|
28
|
490 ngx_http_proxy_ctx_t *p;
|
0
|
491 ngx_output_chain_ctx_t *output;
|
|
492 ngx_chain_writer_ctx_t *writer;
|
|
493 ngx_http_proxy_log_ctx_t *ctx;
|
|
494
|
28
|
495 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
0
|
496
|
|
497 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
498 "http proxy init upstream, client timer: %d",
|
|
499 r->connection->read->timer_set);
|
|
500
|
|
501 if (r->connection->read->timer_set) {
|
|
502 ngx_del_timer(r->connection->read);
|
|
503 }
|
|
504
|
|
505 r->connection->read->event_handler = ngx_http_proxy_check_broken_connection;
|
|
506
|
|
507 if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
|
|
508
|
|
509 r->connection->write->event_handler =
|
|
510 ngx_http_proxy_check_broken_connection;
|
|
511
|
|
512 if (!r->connection->write->active) {
|
|
513 if (ngx_add_event(r->connection->write, NGX_WRITE_EVENT,
|
|
514 NGX_CLEAR_EVENT) == NGX_ERROR)
|
|
515 {
|
|
516 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
517 return;
|
|
518 }
|
|
519 }
|
|
520 }
|
|
521
|
|
522
|
50
|
523 cl = ngx_http_proxy_create_request(p);
|
|
524 if (cl == NULL) {
|
0
|
525 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
526 return;
|
|
527 }
|
|
528
|
|
529 if (r->request_body->bufs) {
|
|
530 cl->next = r->request_body->bufs;
|
|
531 }
|
|
532
|
|
533 r->request_body->bufs = cl;
|
|
534
|
50
|
535 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_log_ctx_t));
|
|
536 if (ctx == NULL) {
|
0
|
537 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
538 return;
|
|
539 }
|
|
540 ctx->connection = r->connection->number;
|
|
541 ctx->proxy = p;
|
|
542
|
|
543 p->upstream->peer.log = r->connection->log;
|
|
544 p->saved_ctx = r->connection->log->data;
|
|
545 p->saved_handler = r->connection->log->handler;
|
|
546 r->connection->log->data = ctx;
|
|
547 r->connection->log->handler = ngx_http_proxy_log_error;
|
|
548 p->action = "connecting to upstream";
|
|
549
|
50
|
550 output = ngx_pcalloc(r->pool, sizeof(ngx_output_chain_ctx_t));
|
|
551 if (output == NULL) {
|
0
|
552 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
553 return;
|
|
554 }
|
|
555
|
|
556 p->upstream->output_chain_ctx = output;
|
|
557
|
2
|
558 output->sendfile = r->connection->sendfile;
|
0
|
559 output->pool = r->pool;
|
|
560 output->bufs.num = 1;
|
|
561 output->tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
|
28
|
562 output->output_filter = ngx_chain_writer;
|
0
|
563
|
50
|
564 writer = ngx_palloc(r->pool, sizeof(ngx_chain_writer_ctx_t));
|
|
565 if (writer == NULL) {
|
0
|
566 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
567 return;
|
|
568 }
|
|
569
|
|
570 output->filter_ctx = writer;
|
|
571 writer->pool = r->pool;
|
|
572
|
|
573 #if 0
|
|
574 if (p->lcf->busy_lock && p->busy_lock == NULL) {
|
|
575 #else
|
|
576 if (p->lcf->busy_lock && !p->busy_locked) {
|
|
577 #endif
|
|
578 ngx_http_proxy_upstream_busy_lock(p);
|
|
579 } else {
|
|
580 ngx_http_proxy_connect(p);
|
|
581 }
|
|
582 }
|
|
583
|
|
584
|
|
585 static void ngx_http_proxy_reinit_upstream(ngx_http_proxy_ctx_t *p)
|
|
586 {
|
|
587 ngx_chain_t *cl;
|
|
588 ngx_output_chain_ctx_t *output;
|
22
|
589 ngx_http_proxy_state_e state;
|
0
|
590
|
|
591 /* reinit the request chain */
|
|
592
|
|
593 for (cl = p->request->request_body->bufs; cl; cl = cl->next) {
|
|
594 cl->buf->pos = cl->buf->start;
|
|
595 cl->buf->file_pos = 0;
|
|
596 }
|
|
597
|
|
598 /* reinit the ngx_output_chain() context */
|
|
599
|
|
600 output = p->upstream->output_chain_ctx;
|
|
601
|
|
602 output->buf = NULL;
|
|
603 output->in = NULL;
|
|
604 output->free = NULL;
|
|
605 output->busy = NULL;
|
|
606
|
|
607 /* reinit r->header_in buffer */
|
|
608
|
|
609 if (p->header_in) {
|
|
610 if (p->cache) {
|
|
611 p->header_in->pos = p->header_in->start + p->cache->ctx.header_size;
|
|
612 p->header_in->last = p->header_in->pos;
|
|
613
|
|
614 } else {
|
|
615 p->header_in->pos = p->header_in->start;
|
|
616 p->header_in->last = p->header_in->start;
|
|
617 }
|
|
618 }
|
|
619
|
|
620 /* add one more state */
|
|
621
|
22
|
622 state = p->state->cache_state;
|
|
623
|
50
|
624 p->state = ngx_array_push(&p->states);
|
|
625 if (p->state == NULL) {
|
0
|
626 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
627 return;
|
|
628 }
|
|
629
|
22
|
630 ngx_memzero(p->state, sizeof(ngx_http_proxy_state_t));
|
|
631
|
|
632 p->state->cache_state = state;
|
|
633
|
0
|
634 p->status = 0;
|
|
635 p->status_count = 0;
|
|
636 }
|
|
637
|
|
638
|
|
639 #if 0
|
|
640
|
|
641 void ngx_http_proxy_upstream_busy_lock(ngx_http_proxy_ctx_t *p)
|
|
642 {
|
|
643 ngx_int_t rc;
|
|
644
|
|
645 rc = ngx_event_busy_lock(p->lcf->busy_lock, p->busy_lock);
|
|
646
|
|
647 if (rc == NGX_AGAIN) {
|
|
648 return;
|
|
649 }
|
|
650
|
|
651 if (rc == NGX_OK) {
|
|
652 ngx_http_proxy_connect(p);
|
|
653 return;
|
|
654 }
|
|
655
|
|
656 if (rc == NGX_ERROR) {
|
|
657 p->state->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
658 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
659 return;
|
|
660 }
|
|
661
|
|
662 /* rc == NGX_BUSY */
|
|
663
|
|
664 #if (NGX_HTTP_CACHE)
|
|
665
|
|
666 if (p->busy_lock->timer) {
|
|
667 ft_type = NGX_HTTP_PROXY_FT_MAX_WAITING;
|
|
668 } else {
|
|
669 ft_type = NGX_HTTP_PROXY_FT_BUSY_LOCK;
|
|
670 }
|
|
671
|
|
672 if (p->stale && (p->lcf->use_stale & ft_type)) {
|
|
673 ngx_http_proxy_finalize_request(p,
|
|
674 ngx_http_proxy_send_cached_response(p));
|
|
675 return;
|
|
676 }
|
|
677
|
|
678 #endif
|
|
679
|
|
680 p->state->status = NGX_HTTP_SERVICE_UNAVAILABLE;
|
|
681 ngx_http_proxy_finalize_request(p, NGX_HTTP_SERVICE_UNAVAILABLE);
|
|
682 }
|
|
683
|
|
684 #endif
|
|
685
|
|
686
|
|
687 #if 1
|
|
688
|
|
689 void ngx_http_proxy_upstream_busy_lock(ngx_http_proxy_ctx_t *p)
|
|
690 {
|
|
691 ngx_int_t rc;
|
|
692 #if (NGX_HTTP_CACHE)
|
|
693 ngx_int_t ft_type;
|
|
694 #endif
|
|
695
|
|
696 if (p->busy_lock.time == 0) {
|
|
697 p->busy_lock.event = p->request->connection->read;
|
|
698 p->busy_lock.event_handler = ngx_http_proxy_busy_lock_handler;
|
|
699 }
|
|
700
|
|
701 rc = ngx_http_busy_lock(p->lcf->busy_lock, &p->busy_lock);
|
|
702
|
|
703 if (rc == NGX_AGAIN) {
|
|
704 return;
|
|
705 }
|
|
706
|
|
707 if (rc == NGX_OK) {
|
|
708 ngx_http_proxy_connect(p);
|
|
709 return;
|
|
710 }
|
|
711
|
|
712 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
|
|
713
|
|
714 #if (NGX_HTTP_CACHE)
|
|
715
|
|
716 if (rc == NGX_DONE) {
|
|
717 ft_type = NGX_HTTP_PROXY_FT_BUSY_LOCK;
|
|
718
|
|
719 } else {
|
|
720 /* rc == NGX_ERROR */
|
|
721 ft_type = NGX_HTTP_PROXY_FT_MAX_WAITING;
|
|
722 }
|
|
723
|
|
724 if (p->stale && (p->lcf->use_stale & ft_type)) {
|
|
725 ngx_http_proxy_finalize_request(p,
|
|
726 ngx_http_proxy_send_cached_response(p));
|
|
727 return;
|
|
728 }
|
|
729
|
|
730 #endif
|
|
731
|
|
732 p->state->status = NGX_HTTP_SERVICE_UNAVAILABLE;
|
|
733 ngx_http_proxy_finalize_request(p, NGX_HTTP_SERVICE_UNAVAILABLE);
|
|
734 }
|
|
735
|
|
736 #endif
|
|
737
|
|
738
|
|
739 static void ngx_http_proxy_connect(ngx_http_proxy_ctx_t *p)
|
|
740 {
|
10
|
741 ngx_int_t rc;
|
0
|
742 ngx_connection_t *c;
|
|
743 ngx_http_request_t *r;
|
|
744 ngx_output_chain_ctx_t *output;
|
|
745 ngx_chain_writer_ctx_t *writer;
|
|
746
|
|
747 p->action = "connecting to upstream";
|
|
748
|
|
749 p->request->connection->single_connection = 0;
|
|
750
|
|
751 rc = ngx_event_connect_peer(&p->upstream->peer);
|
|
752
|
|
753 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, p->request->connection->log, 0,
|
10
|
754 "http proxy connect: %i", rc);
|
0
|
755
|
|
756 if (rc == NGX_ERROR) {
|
|
757 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
758 return;
|
|
759 }
|
|
760
|
|
761 p->state->peer =
|
28
|
762 &p->upstream->peer.peers->peer[p->upstream->peer.cur_peer].name;
|
0
|
763
|
|
764 if (rc == NGX_CONNECT_ERROR) {
|
|
765 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
|
|
766 return;
|
|
767 }
|
|
768
|
|
769 r = p->request;
|
|
770 c = p->upstream->peer.connection;
|
|
771
|
|
772 c->data = p;
|
|
773 c->write->event_handler = ngx_http_proxy_send_request_handler;
|
|
774 c->read->event_handler = ngx_http_proxy_process_upstream_status_line;
|
|
775
|
10
|
776 c->sendfile = r->connection->sendfile;
|
|
777
|
0
|
778 c->pool = r->pool;
|
|
779 c->read->log = c->write->log = c->log = r->connection->log;
|
|
780
|
|
781 /* init or reinit the ngx_output_chain() and ngx_chain_writer() contexts */
|
|
782
|
|
783 output = p->upstream->output_chain_ctx;
|
|
784 writer = output->filter_ctx;
|
|
785 writer->out = NULL;
|
|
786 writer->last = &writer->out;
|
|
787 writer->connection = c;
|
22
|
788 writer->limit = 0;
|
0
|
789
|
22
|
790 if (p->request_sent) {
|
0
|
791 ngx_http_proxy_reinit_upstream(p);
|
|
792 }
|
|
793
|
|
794 if (r->request_body->buf) {
|
28
|
795 if (r->request_body->temp_file) {
|
0
|
796
|
50
|
797 output->free = ngx_alloc_chain_link(r->pool);
|
|
798 if (output->free == NULL) {
|
0
|
799 ngx_http_proxy_finalize_request(p,
|
|
800 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
801 return;
|
|
802 }
|
|
803
|
|
804 output->free->buf = r->request_body->buf;
|
|
805 output->free->next = NULL;
|
|
806 output->allocated = 1;
|
|
807
|
|
808 r->request_body->buf->pos = r->request_body->buf->start;
|
|
809 r->request_body->buf->last = r->request_body->buf->start;
|
|
810 r->request_body->buf->tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
|
|
811
|
|
812 } else {
|
|
813 r->request_body->buf->pos = r->request_body->buf->start;
|
|
814 }
|
|
815 }
|
|
816
|
|
817 p->request_sent = 0;
|
|
818
|
|
819 if (rc == NGX_AGAIN) {
|
|
820 ngx_add_timer(c->write, p->lcf->connect_timeout);
|
|
821 return;
|
|
822 }
|
|
823
|
|
824 /* rc == NGX_OK */
|
|
825
|
4
|
826 #if 0 /* test only, see below about "post aio operation" */
|
0
|
827
|
|
828 if (c->read->ready) {
|
|
829 /* post aio operation */
|
|
830 ngx_http_proxy_process_upstream_status_line(c->read);
|
4
|
831 #if 0
|
0
|
832 return;
|
4
|
833 #endif
|
0
|
834 }
|
|
835
|
|
836 #endif
|
|
837
|
|
838 ngx_http_proxy_send_request(p);
|
|
839 }
|
|
840
|
|
841
|
|
842 static void ngx_http_proxy_send_request(ngx_http_proxy_ctx_t *p)
|
|
843 {
|
|
844 int rc;
|
|
845 ngx_connection_t *c;
|
|
846
|
|
847 c = p->upstream->peer.connection;
|
|
848
|
|
849 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
850 "http proxy send request");
|
|
851
|
18
|
852 #if (NGX_HAVE_KQUEUE)
|
0
|
853
|
4
|
854 if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT)
|
0
|
855 && !p->request_sent
|
|
856 && c->write->pending_eof)
|
|
857 {
|
|
858 ngx_log_error(NGX_LOG_ERR, c->log, c->write->kq_errno,
|
|
859 "connect() failed");
|
|
860
|
|
861 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
|
|
862 return;
|
|
863 }
|
|
864
|
|
865 #endif
|
|
866
|
|
867 p->action = "sending request to upstream";
|
|
868
|
|
869 rc = ngx_output_chain(p->upstream->output_chain_ctx,
|
|
870 p->request_sent ? NULL:
|
|
871 p->request->request_body->bufs);
|
|
872
|
22
|
873 p->request_sent = 1;
|
|
874
|
0
|
875 if (rc == NGX_ERROR) {
|
|
876 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
|
|
877 return;
|
|
878 }
|
|
879
|
|
880 if (c->write->timer_set) {
|
|
881 ngx_del_timer(c->write);
|
|
882 }
|
|
883
|
|
884 if (rc == NGX_AGAIN) {
|
|
885 ngx_add_timer(c->write, p->lcf->send_timeout);
|
|
886
|
2
|
887 if (ngx_handle_write_event(c->write, p->lcf->send_lowat) == NGX_ERROR) {
|
0
|
888 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
889 return;
|
|
890 }
|
|
891
|
|
892 return;
|
|
893 }
|
|
894
|
|
895 /* rc == NGX_OK */
|
|
896
|
|
897 if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
|
|
898 if (ngx_tcp_push(c->fd) == NGX_ERROR) {
|
|
899 ngx_log_error(NGX_LOG_CRIT, c->log,
|
|
900 ngx_socket_errno,
|
|
901 ngx_tcp_push_n " failed");
|
|
902 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
903 return;
|
|
904 }
|
|
905
|
|
906 c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
|
|
907 return;
|
|
908 }
|
|
909
|
|
910 ngx_add_timer(c->read, p->lcf->read_timeout);
|
|
911
|
4
|
912 #if 1
|
0
|
913 if (c->read->ready) {
|
|
914
|
|
915 /* post aio operation */
|
|
916
|
|
917 /*
|
|
918 * although we can post aio operation just in the end
|
|
919 * of ngx_http_proxy_connect() CHECK IT !!!
|
|
920 * it's better to do here because we postpone header buffer allocation
|
|
921 */
|
|
922
|
|
923 ngx_http_proxy_process_upstream_status_line(c->read);
|
|
924 return;
|
|
925 }
|
|
926 #endif
|
|
927
|
|
928 c->write->event_handler = ngx_http_proxy_dummy_handler;
|
|
929
|
|
930 if (ngx_handle_level_write_event(c->write) == NGX_ERROR) {
|
|
931 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
932 return;
|
|
933 }
|
|
934 }
|
|
935
|
|
936
|
|
937 static void ngx_http_proxy_send_request_handler(ngx_event_t *wev)
|
|
938 {
|
|
939 ngx_connection_t *c;
|
|
940 ngx_http_proxy_ctx_t *p;
|
|
941
|
|
942 c = wev->data;
|
|
943 p = c->data;
|
|
944
|
|
945 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
|
|
946 "http proxy send request handler");
|
|
947
|
|
948 if (wev->timedout) {
|
|
949 p->action = "sending request to upstream";
|
|
950 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT);
|
|
951 return;
|
|
952 }
|
|
953
|
|
954 if (p->request->connection->write->eof
|
|
955 && (!p->cachable || !p->request_sent))
|
|
956 {
|
|
957 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
958 return;
|
|
959 }
|
|
960
|
|
961 ngx_http_proxy_send_request(p);
|
|
962 }
|
|
963
|
|
964
|
|
965 static void ngx_http_proxy_dummy_handler(ngx_event_t *wev)
|
|
966 {
|
|
967 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http proxy dummy handler");
|
|
968 }
|
|
969
|
|
970
|
|
971 static void ngx_http_proxy_process_upstream_status_line(ngx_event_t *rev)
|
|
972 {
|
|
973 int rc;
|
|
974 ssize_t n;
|
|
975 ngx_connection_t *c;
|
|
976 ngx_http_proxy_ctx_t *p;
|
|
977
|
|
978 c = rev->data;
|
|
979 p = c->data;
|
|
980 p->action = "reading upstream status line";
|
|
981
|
|
982 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
|
983 "http proxy process status line");
|
|
984
|
|
985 if (rev->timedout) {
|
|
986 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT);
|
|
987 return;
|
|
988 }
|
|
989
|
|
990 if (p->header_in == NULL) {
|
|
991 p->header_in = ngx_create_temp_buf(p->request->pool,
|
|
992 p->lcf->header_buffer_size);
|
|
993 if (p->header_in == NULL) {
|
|
994 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
995 return;
|
|
996 }
|
|
997 p->header_in->tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
|
|
998
|
|
999 if (p->cache) {
|
|
1000 p->header_in->pos += p->cache->ctx.header_size;
|
|
1001 p->header_in->last = p->header_in->pos;
|
|
1002 }
|
|
1003 }
|
|
1004
|
|
1005 n = ngx_http_proxy_read_upstream_header(p);
|
|
1006
|
|
1007 if (n == NGX_AGAIN) {
|
|
1008 return;
|
|
1009 }
|
|
1010
|
|
1011 if (n == 0) {
|
|
1012 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
|
|
1013 "upstream prematurely closed connection");
|
|
1014 }
|
|
1015
|
|
1016 if (n == NGX_ERROR || n == 0) {
|
|
1017 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
|
|
1018 return;
|
|
1019 }
|
|
1020
|
|
1021 p->valid_header_in = 0;
|
|
1022
|
|
1023 p->upstream->peer.cached = 0;
|
|
1024
|
|
1025 rc = ngx_http_proxy_parse_status_line(p);
|
|
1026
|
|
1027 if (rc == NGX_AGAIN) {
|
50
|
1028 if (p->header_in->pos == p->header_in->end) {
|
0
|
1029 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
|
|
1030 "upstream sent too long status line");
|
|
1031 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER);
|
|
1032 }
|
|
1033 return;
|
|
1034 }
|
|
1035
|
|
1036 if (rc == NGX_HTTP_PROXY_PARSE_NO_HEADER) {
|
|
1037 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
|
|
1038 "upstream sent no valid HTTP/1.0 header");
|
|
1039
|
|
1040 if (p->accel) {
|
|
1041 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER);
|
|
1042
|
|
1043 } else {
|
|
1044 p->request->http_version = NGX_HTTP_VERSION_9;
|
|
1045 p->upstream->status = NGX_HTTP_OK;
|
|
1046 ngx_http_proxy_send_response(p);
|
|
1047 }
|
|
1048
|
|
1049 return;
|
|
1050 }
|
|
1051
|
|
1052 /* rc == NGX_OK */
|
|
1053
|
|
1054 p->upstream->status = p->status;
|
|
1055 p->state->status = p->status;
|
|
1056
|
|
1057 if (p->status == NGX_HTTP_INTERNAL_SERVER_ERROR) {
|
|
1058
|
|
1059 if (p->upstream->peer.tries > 1
|
|
1060 && (p->lcf->next_upstream & NGX_HTTP_PROXY_FT_HTTP_500))
|
|
1061 {
|
|
1062 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_HTTP_500);
|
|
1063 return;
|
|
1064 }
|
|
1065
|
|
1066 #if (NGX_HTTP_CACHE)
|
|
1067
|
|
1068 if (p->upstream->peer.tries == 0
|
|
1069 && p->stale
|
|
1070 && (p->lcf->use_stale & NGX_HTTP_PROXY_FT_HTTP_500))
|
|
1071 {
|
|
1072 ngx_http_proxy_finalize_request(p,
|
|
1073 ngx_http_proxy_send_cached_response(p));
|
|
1074
|
|
1075 return;
|
|
1076 }
|
|
1077
|
|
1078 #endif
|
|
1079 }
|
|
1080
|
|
1081 if (p->status == NGX_HTTP_NOT_FOUND
|
|
1082 && p->upstream->peer.tries > 1
|
|
1083 && p->lcf->next_upstream & NGX_HTTP_PROXY_FT_HTTP_404)
|
|
1084 {
|
|
1085 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_HTTP_404);
|
|
1086 return;
|
|
1087 }
|
|
1088
|
|
1089 /* TODO: "proxy_error_page" */
|
|
1090
|
|
1091 p->upstream->status_line.len = p->status_end - p->status_start;
|
|
1092 p->upstream->status_line.data = ngx_palloc(p->request->pool,
|
|
1093 p->upstream->status_line.len + 1);
|
|
1094 if (p->upstream->status_line.data == NULL) {
|
|
1095 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1096 return;
|
|
1097 }
|
|
1098 ngx_cpystrn(p->upstream->status_line.data, p->status_start,
|
|
1099 p->upstream->status_line.len + 1);
|
|
1100
|
|
1101 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
10
|
1102 "http proxy status %ui \"%V\"",
|
|
1103 p->upstream->status, &p->upstream->status_line);
|
0
|
1104
|
|
1105
|
|
1106 /* init or reinit the p->upstream->headers_in.headers table */
|
|
1107
|
|
1108 if (p->upstream->headers_in.headers.part.elts) {
|
|
1109 p->upstream->headers_in.headers.part.nelts = 0;
|
|
1110 p->upstream->headers_in.headers.part.next = NULL;
|
|
1111 p->upstream->headers_in.headers.last =
|
|
1112 &p->upstream->headers_in.headers.part;
|
|
1113
|
|
1114 ngx_memzero(&p->upstream->headers_in.date,
|
|
1115 sizeof(ngx_http_proxy_headers_in_t) - sizeof(ngx_list_t));
|
|
1116
|
|
1117 } else {
|
|
1118 if (ngx_list_init(&p->upstream->headers_in.headers, p->request->pool,
|
|
1119 20, sizeof(ngx_table_elt_t)) == NGX_ERROR)
|
|
1120 {
|
|
1121 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1122 return;
|
|
1123 }
|
|
1124 }
|
|
1125
|
|
1126
|
|
1127 c->read->event_handler = ngx_http_proxy_process_upstream_headers;
|
|
1128 ngx_http_proxy_process_upstream_headers(rev);
|
|
1129 }
|
|
1130
|
|
1131
|
|
1132 static void ngx_http_proxy_process_upstream_headers(ngx_event_t *rev)
|
|
1133 {
|
|
1134 int i, rc;
|
|
1135 ssize_t n;
|
|
1136 ngx_table_elt_t *h;
|
|
1137 ngx_connection_t *c;
|
|
1138 ngx_http_request_t *r;
|
|
1139 ngx_http_proxy_ctx_t *p;
|
|
1140
|
|
1141 c = rev->data;
|
|
1142 p = c->data;
|
|
1143 r = p->request;
|
|
1144 p->action = "reading upstream headers";
|
|
1145
|
|
1146 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
|
1147 "http proxy process header line");
|
|
1148
|
|
1149 if (rev->timedout) {
|
|
1150 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT);
|
|
1151 return;
|
|
1152 }
|
|
1153
|
|
1154 rc = NGX_AGAIN;
|
|
1155
|
|
1156 for ( ;; ) {
|
|
1157 if (rc == NGX_AGAIN) {
|
|
1158 n = ngx_http_proxy_read_upstream_header(p);
|
|
1159
|
|
1160 if (n == 0) {
|
|
1161 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
|
|
1162 "upstream prematurely closed connection");
|
|
1163 }
|
|
1164
|
|
1165 if (n == NGX_ERROR || n == 0) {
|
|
1166 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
|
|
1167 return;
|
|
1168 }
|
|
1169
|
|
1170 if (n == NGX_AGAIN) {
|
|
1171 return;
|
|
1172 }
|
|
1173 }
|
|
1174
|
|
1175 rc = ngx_http_parse_header_line(p->request, p->header_in);
|
|
1176
|
|
1177 if (rc == NGX_OK) {
|
|
1178
|
|
1179 /* a header line has been parsed successfully */
|
|
1180
|
50
|
1181 h = ngx_list_push(&p->upstream->headers_in.headers);
|
|
1182 if (h == NULL) {
|
0
|
1183 ngx_http_proxy_finalize_request(p,
|
|
1184 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1185 return;
|
|
1186 }
|
|
1187
|
|
1188 h->key.len = r->header_name_end - r->header_name_start;
|
|
1189 h->value.len = r->header_end - r->header_start;
|
|
1190
|
|
1191 h->key.data = ngx_palloc(p->request->pool,
|
|
1192 h->key.len + 1 + h->value.len + 1);
|
|
1193 if (h->key.data == NULL) {
|
|
1194 ngx_http_proxy_finalize_request(p,
|
|
1195 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1196 return;
|
|
1197 }
|
|
1198
|
|
1199 h->value.data = h->key.data + h->key.len + 1;
|
|
1200 ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1);
|
|
1201 ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1);
|
|
1202
|
|
1203 for (i = 0; ngx_http_proxy_headers_in[i].name.len != 0; i++) {
|
|
1204 if (ngx_http_proxy_headers_in[i].name.len != h->key.len) {
|
|
1205 continue;
|
|
1206 }
|
|
1207
|
|
1208 if (ngx_strcasecmp(ngx_http_proxy_headers_in[i].name.data,
|
|
1209 h->key.data) == 0)
|
|
1210 {
|
|
1211 *((ngx_table_elt_t **) ((char *) &p->upstream->headers_in
|
|
1212 + ngx_http_proxy_headers_in[i].offset)) = h;
|
|
1213 break;
|
|
1214 }
|
|
1215 }
|
|
1216
|
|
1217 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
10
|
1218 "http proxy header: \"%V: %V\"", &h->key, &h->value);
|
0
|
1219
|
|
1220 continue;
|
|
1221
|
|
1222 } else if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
|
|
1223
|
|
1224 /* a whole header has been parsed successfully */
|
|
1225
|
|
1226 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1227 "http proxy header done");
|
|
1228
|
|
1229 /* TODO: hook to process the upstream header */
|
|
1230
|
|
1231 #if (NGX_HTTP_CACHE)
|
|
1232
|
|
1233 if (p->cachable) {
|
|
1234 p->cachable = ngx_http_proxy_is_cachable(p);
|
|
1235 }
|
|
1236
|
|
1237 #endif
|
|
1238
|
|
1239 ngx_http_proxy_send_response(p);
|
|
1240 return;
|
|
1241
|
|
1242 } else if (rc != NGX_AGAIN) {
|
|
1243
|
|
1244 /* there was error while a header line parsing */
|
|
1245
|
|
1246 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
|
|
1247 upstream_header_errors[rc - NGX_HTTP_PARSE_HEADER_ERROR]);
|
|
1248
|
|
1249 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER);
|
|
1250 return;
|
|
1251 }
|
|
1252
|
|
1253 /* rc == NGX_AGAIN: a header line parsing is still not complete */
|
|
1254
|
|
1255 if (p->header_in->last == p->header_in->end) {
|
|
1256 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
|
|
1257 "upstream sent too big header");
|
|
1258
|
|
1259 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER);
|
|
1260 return;
|
|
1261 }
|
|
1262 }
|
|
1263 }
|
|
1264
|
|
1265
|
|
1266 static ssize_t ngx_http_proxy_read_upstream_header(ngx_http_proxy_ctx_t *p)
|
|
1267 {
|
|
1268 ssize_t n;
|
|
1269 ngx_event_t *rev;
|
|
1270
|
|
1271 rev = p->upstream->peer.connection->read;
|
|
1272
|
|
1273 n = p->header_in->last - p->header_in->pos;
|
|
1274
|
|
1275 if (n > 0) {
|
|
1276 return n;
|
|
1277 }
|
|
1278
|
|
1279 n = ngx_recv(p->upstream->peer.connection, p->header_in->last,
|
|
1280 p->header_in->end - p->header_in->last);
|
|
1281
|
|
1282 if (n == NGX_AGAIN) {
|
|
1283 #if 0
|
|
1284 ngx_add_timer(rev, p->lcf->read_timeout);
|
|
1285 #endif
|
|
1286
|
|
1287 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
|
1288 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1289 return NGX_ERROR;
|
|
1290 }
|
|
1291
|
|
1292 return NGX_AGAIN;
|
|
1293 }
|
|
1294
|
|
1295 if (n == 0) {
|
|
1296 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
|
|
1297 "upstream closed prematurely connection");
|
|
1298 }
|
|
1299
|
|
1300 if (n == 0 || n == NGX_ERROR) {
|
|
1301 return NGX_ERROR;
|
|
1302 }
|
|
1303
|
|
1304 p->header_in->last += n;
|
|
1305
|
|
1306 return n;
|
|
1307 }
|
|
1308
|
|
1309
|
|
1310 static void ngx_http_proxy_send_response(ngx_http_proxy_ctx_t *p)
|
|
1311 {
|
|
1312 int rc;
|
|
1313 ngx_event_pipe_t *ep;
|
|
1314 ngx_http_request_t *r;
|
|
1315 ngx_http_cache_header_t *header;
|
|
1316 ngx_http_core_loc_conf_t *clcf;
|
|
1317
|
|
1318 r = p->request;
|
|
1319
|
|
1320 r->headers_out.status = p->upstream->status;
|
2
|
1321 r->headers_out.status_line = p->upstream->status_line;
|
0
|
1322
|
|
1323 #if 0
|
|
1324 r->headers_out.content_length_n = -1;
|
|
1325 r->headers_out.content_length = NULL;
|
|
1326 #endif
|
|
1327
|
|
1328 /* copy an upstream header to r->headers_out */
|
|
1329
|
|
1330 if (ngx_http_proxy_copy_header(p, &p->upstream->headers_in) == NGX_ERROR) {
|
|
1331 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1332 return;
|
|
1333 }
|
|
1334
|
|
1335 /* TODO: preallocate event_pipe bufs, look "Content-Length" */
|
|
1336
|
|
1337 rc = ngx_http_send_header(r);
|
|
1338
|
50
|
1339 if (rc == NGX_ERROR || rc > NGX_OK) {
|
|
1340 ngx_http_proxy_finalize_request(p, rc);
|
|
1341 return;
|
|
1342 }
|
|
1343
|
0
|
1344 p->header_sent = 1;
|
|
1345
|
|
1346 if (p->cache && p->cache->ctx.file.fd != NGX_INVALID_FILE) {
|
|
1347 if (ngx_close_file(p->cache->ctx.file.fd) == NGX_FILE_ERROR) {
|
|
1348 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
|
|
1349 ngx_close_file_n " \"%s\" failed",
|
|
1350 p->cache->ctx.file.name.data);
|
|
1351 }
|
|
1352 }
|
|
1353
|
|
1354 if (p->cachable) {
|
|
1355 header = (ngx_http_cache_header_t *) p->header_in->start;
|
|
1356
|
|
1357 header->expires = p->cache->ctx.expires;
|
|
1358 header->last_modified = p->cache->ctx.last_modified;
|
|
1359 header->date = p->cache->ctx.date;
|
|
1360 header->length = r->headers_out.content_length_n;
|
|
1361 p->cache->ctx.length = r->headers_out.content_length_n;
|
|
1362
|
18
|
1363 header->key_len = p->cache->ctx.key0.len;
|
|
1364 ngx_memcpy(&header->key, p->cache->ctx.key0.data, header->key_len);
|
0
|
1365 header->key[header->key_len] = LF;
|
|
1366 }
|
|
1367
|
|
1368 ep = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
|
|
1369 if (ep == NULL) {
|
|
1370 ngx_http_proxy_finalize_request(p, 0);
|
|
1371 return;
|
|
1372 }
|
|
1373
|
|
1374 p->upstream->event_pipe = ep;
|
|
1375
|
|
1376 ep->input_filter = ngx_event_pipe_copy_input_filter;
|
|
1377 ep->output_filter = (ngx_event_pipe_output_filter_pt)
|
|
1378 ngx_http_output_filter;
|
|
1379 ep->output_ctx = r;
|
|
1380 ep->tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
|
|
1381 ep->bufs = p->lcf->bufs;
|
|
1382 ep->busy_size = p->lcf->busy_buffers_size;
|
|
1383 ep->upstream = p->upstream->peer.connection;
|
|
1384 ep->downstream = r->connection;
|
|
1385 ep->pool = r->pool;
|
|
1386 ep->log = r->connection->log;
|
|
1387
|
|
1388 ep->cachable = p->cachable;
|
|
1389
|
50
|
1390 ep->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));
|
|
1391 if (ep->temp_file == NULL) {
|
0
|
1392 ngx_http_proxy_finalize_request(p, 0);
|
|
1393 return;
|
|
1394 }
|
|
1395
|
|
1396 ep->temp_file->file.fd = NGX_INVALID_FILE;
|
|
1397 ep->temp_file->file.log = r->connection->log;
|
|
1398 ep->temp_file->path = p->lcf->temp_path;
|
|
1399 ep->temp_file->pool = r->pool;
|
|
1400
|
|
1401 if (p->cachable) {
|
|
1402 ep->temp_file->persistent = 1;
|
|
1403 } else {
|
|
1404 ep->temp_file->warn = "an upstream response is buffered "
|
|
1405 "to a temporary file";
|
|
1406 }
|
|
1407
|
|
1408 ep->max_temp_file_size = p->lcf->max_temp_file_size;
|
|
1409 ep->temp_file_write_size = p->lcf->temp_file_write_size;
|
|
1410
|
50
|
1411 ep->preread_bufs = ngx_alloc_chain_link(r->pool);
|
|
1412 if (ep->preread_bufs == NULL) {
|
0
|
1413 ngx_http_proxy_finalize_request(p, 0);
|
|
1414 return;
|
|
1415 }
|
|
1416 ep->preread_bufs->buf = p->header_in;
|
|
1417 ep->preread_bufs->next = NULL;
|
28
|
1418 p->header_in->recycled = 1;
|
0
|
1419
|
|
1420 ep->preread_size = p->header_in->last - p->header_in->pos;
|
|
1421
|
|
1422 if (p->cachable) {
|
|
1423 ep->buf_to_file = ngx_calloc_buf(r->pool);
|
|
1424 if (ep->buf_to_file == NULL) {
|
|
1425 ngx_http_proxy_finalize_request(p, 0);
|
|
1426 return;
|
|
1427 }
|
|
1428 ep->buf_to_file->pos = p->header_in->start;
|
|
1429 ep->buf_to_file->last = p->header_in->pos;
|
|
1430 ep->buf_to_file->temporary = 1;
|
|
1431 }
|
|
1432
|
|
1433 if (ngx_event_flags & NGX_USE_AIO_EVENT) {
|
|
1434 /* the posted aio operation can currupt a shadow buffer */
|
|
1435 ep->single_buf = 1;
|
|
1436 }
|
|
1437
|
|
1438 /* TODO: ep->free_bufs = 0 if use ngx_create_chain_of_bufs() */
|
|
1439 ep->free_bufs = 1;
|
|
1440
|
|
1441 /*
|
|
1442 * event_pipe would do p->header_in->last += ep->preread_size
|
|
1443 * as though these bytes were read.
|
|
1444 */
|
|
1445 p->header_in->last = p->header_in->pos;
|
|
1446
|
|
1447 if (p->lcf->cyclic_temp_file) {
|
|
1448
|
|
1449 /*
|
|
1450 * we need to disable the use of sendfile() if we use cyclic temp file
|
|
1451 * because the writing a new data can interfere with sendfile()
|
|
1452 * that uses the same kernel file pages (at least on FreeBSD)
|
|
1453 */
|
|
1454
|
|
1455 ep->cyclic_temp_file = 1;
|
2
|
1456 r->connection->sendfile = 0;
|
0
|
1457
|
|
1458 } else {
|
|
1459 ep->cyclic_temp_file = 0;
|
|
1460 }
|
|
1461
|
|
1462 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
1463
|
|
1464 ep->read_timeout = p->lcf->read_timeout;
|
|
1465 ep->send_timeout = clcf->send_timeout;
|
|
1466 ep->send_lowat = clcf->send_lowat;
|
|
1467
|
|
1468 p->upstream->peer.connection->read->event_handler =
|
|
1469 ngx_http_proxy_process_body;
|
|
1470 r->connection->write->event_handler = ngx_http_proxy_process_body;
|
|
1471
|
|
1472 ngx_http_proxy_process_body(p->upstream->peer.connection->read);
|
|
1473
|
|
1474 return;
|
|
1475 }
|
|
1476
|
|
1477
|
|
1478 static void ngx_http_proxy_process_body(ngx_event_t *ev)
|
|
1479 {
|
|
1480 ngx_connection_t *c;
|
|
1481 ngx_http_request_t *r;
|
|
1482 ngx_http_proxy_ctx_t *p;
|
|
1483 ngx_event_pipe_t *ep;
|
|
1484
|
|
1485 c = ev->data;
|
|
1486
|
|
1487 if (ev->write) {
|
|
1488 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
|
|
1489 "http proxy process downstream");
|
|
1490 r = c->data;
|
|
1491 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1492 p->action = "sending to client";
|
|
1493
|
|
1494 } else {
|
|
1495 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
|
|
1496 "http proxy process upstream");
|
|
1497 p = c->data;
|
|
1498 p->action = "reading upstream body";
|
|
1499 }
|
|
1500
|
|
1501 ep = p->upstream->event_pipe;
|
|
1502
|
|
1503 if (ev->timedout) {
|
|
1504 if (ev->write) {
|
|
1505 ep->downstream_error = 1;
|
|
1506 ngx_log_error(NGX_LOG_ERR, c->log, NGX_ETIMEDOUT,
|
|
1507 "client timed out");
|
|
1508
|
|
1509 } else {
|
|
1510 ep->upstream_error = 1;
|
|
1511 ngx_log_error(NGX_LOG_ERR, c->log, NGX_ETIMEDOUT,
|
|
1512 "upstream timed out");
|
|
1513 }
|
|
1514
|
|
1515 } else {
|
|
1516 if (ngx_event_pipe(ep, ev->write) == NGX_ABORT) {
|
|
1517 ngx_http_proxy_finalize_request(p, 0);
|
|
1518 return;
|
|
1519 }
|
|
1520 }
|
|
1521
|
|
1522 if (p->upstream->peer.connection) {
|
|
1523
|
|
1524 #if (NGX_HTTP_FILE_CACHE)
|
|
1525
|
|
1526 if (ep->upstream_done && p->cachable) {
|
|
1527 if (ngx_http_proxy_update_cache(p) == NGX_ERROR) {
|
|
1528 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
|
|
1529 ngx_http_proxy_finalize_request(p, 0);
|
|
1530 return;
|
|
1531 }
|
|
1532
|
|
1533 } else if (ep->upstream_eof && p->cachable) {
|
|
1534
|
|
1535 /* TODO: check length & update cache */
|
|
1536
|
|
1537 if (ngx_http_proxy_update_cache(p) == NGX_ERROR) {
|
|
1538 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
|
|
1539 ngx_http_proxy_finalize_request(p, 0);
|
|
1540 return;
|
|
1541 }
|
|
1542 }
|
|
1543
|
|
1544 #endif
|
|
1545
|
|
1546 if (ep->upstream_done || ep->upstream_eof || ep->upstream_error) {
|
|
1547 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ev->log, 0,
|
10
|
1548 "http proxy upstream exit: %p", ep->out);
|
0
|
1549 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
|
|
1550 ngx_http_proxy_finalize_request(p, 0);
|
|
1551 return;
|
|
1552 }
|
|
1553 }
|
|
1554
|
|
1555 if (ep->downstream_error) {
|
|
1556 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
|
|
1557 "http proxy downstream error");
|
|
1558 if (!p->cachable && p->upstream->peer.connection) {
|
|
1559 ngx_http_proxy_finalize_request(p, 0);
|
|
1560 }
|
|
1561 }
|
|
1562 }
|
|
1563
|
|
1564
|
10
|
1565 static void ngx_http_proxy_next_upstream(ngx_http_proxy_ctx_t *p,
|
|
1566 ngx_uint_t ft_type)
|
0
|
1567 {
|
10
|
1568 ngx_uint_t status;
|
0
|
1569
|
|
1570 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, p->request->connection->log, 0,
|
10
|
1571 "http proxy next upstream: %ui", ft_type);
|
0
|
1572
|
|
1573 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
|
|
1574
|
|
1575 if (ft_type != NGX_HTTP_PROXY_FT_HTTP_404) {
|
|
1576 ngx_event_connect_peer_failed(&p->upstream->peer);
|
|
1577 }
|
|
1578
|
|
1579 if (ft_type == NGX_HTTP_PROXY_FT_TIMEOUT) {
|
|
1580 ngx_log_error(NGX_LOG_ERR, p->request->connection->log, NGX_ETIMEDOUT,
|
|
1581 "upstream timed out");
|
|
1582 }
|
|
1583
|
|
1584 if (p->upstream->peer.cached && ft_type == NGX_HTTP_PROXY_FT_ERROR) {
|
|
1585 status = 0;
|
|
1586
|
|
1587 } else {
|
|
1588 switch(ft_type) {
|
|
1589 case NGX_HTTP_PROXY_FT_TIMEOUT:
|
|
1590 status = NGX_HTTP_GATEWAY_TIME_OUT;
|
|
1591 break;
|
|
1592
|
|
1593 case NGX_HTTP_PROXY_FT_HTTP_500:
|
|
1594 status = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
1595 break;
|
|
1596
|
|
1597 case NGX_HTTP_PROXY_FT_HTTP_404:
|
|
1598 status = NGX_HTTP_NOT_FOUND;
|
|
1599 break;
|
|
1600
|
|
1601 /*
|
|
1602 * NGX_HTTP_PROXY_FT_BUSY_LOCK and NGX_HTTP_PROXY_FT_MAX_WAITING
|
|
1603 * never reach here
|
|
1604 */
|
|
1605
|
|
1606 default:
|
|
1607 status = NGX_HTTP_BAD_GATEWAY;
|
|
1608 }
|
|
1609 }
|
|
1610
|
|
1611 if (p->upstream->peer.connection) {
|
|
1612 ngx_http_proxy_close_connection(p);
|
|
1613 }
|
|
1614
|
|
1615 if (p->request->connection->write->eof) {
|
|
1616 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
1617 return;
|
|
1618 }
|
|
1619
|
|
1620 if (status) {
|
|
1621 p->state->status = status;
|
|
1622
|
|
1623 if (p->upstream->peer.tries == 0 || !(p->lcf->next_upstream & ft_type))
|
|
1624 {
|
|
1625
|
|
1626 #if (NGX_HTTP_CACHE)
|
|
1627
|
|
1628 if (p->stale && (p->lcf->use_stale & ft_type)) {
|
|
1629 ngx_http_proxy_finalize_request(p,
|
|
1630 ngx_http_proxy_send_cached_response(p));
|
|
1631 return;
|
|
1632 }
|
|
1633
|
|
1634 #endif
|
|
1635
|
|
1636 ngx_http_proxy_finalize_request(p, status);
|
|
1637 return;
|
|
1638 }
|
|
1639 }
|
|
1640
|
|
1641 if (p->lcf->busy_lock && !p->busy_locked) {
|
|
1642 ngx_http_proxy_upstream_busy_lock(p);
|
|
1643 } else {
|
|
1644 ngx_http_proxy_connect(p);
|
|
1645 }
|
|
1646 }
|