Mercurial > hg > nginx
comparison src/http/modules/proxy/ngx_http_proxy_handler.c @ 171:aff0e5d32af8
nginx-0.0.1-2003-11-03-20:33:31 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 03 Nov 2003 17:33:31 +0000 |
parents | c42be4185301 |
children | caa57ddf6d77 |
comparison
equal
deleted
inserted
replaced
170:c42be4185301 | 171:aff0e5d32af8 |
---|---|
4 #include <ngx_http.h> | 4 #include <ngx_http.h> |
5 #include <ngx_http_proxy_handler.h> | 5 #include <ngx_http_proxy_handler.h> |
6 | 6 |
7 | 7 |
8 static int ngx_http_proxy_handler(ngx_http_request_t *r); | 8 static int ngx_http_proxy_handler(ngx_http_request_t *r); |
9 static int ngx_http_proxy_request_upstream(ngx_http_proxy_ctx_t *p); | |
10 static void ngx_http_proxy_init_request(void *data); | |
11 static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_proxy_ctx_t *p); | |
12 static void ngx_http_proxy_send_request_handler(ngx_event_t *wev); | |
13 static void ngx_http_proxy_connect(ngx_http_proxy_ctx_t *p); | |
14 static void ngx_http_proxy_send_request(ngx_http_proxy_ctx_t *p); | |
15 static void ngx_http_proxy_process_upstream_status_line(ngx_event_t *rev); | |
16 static void ngx_http_proxy_process_upstream_headers(ngx_event_t *rev); | |
17 static ssize_t ngx_http_proxy_read_upstream_header(ngx_http_proxy_ctx_t *); | |
18 static void ngx_http_proxy_send_response(ngx_http_proxy_ctx_t *p); | |
19 static void ngx_http_proxy_process_body(ngx_event_t *ev); | |
20 static int ngx_http_proxy_update_cache(ngx_http_proxy_ctx_t *p); | |
21 | |
22 static void ngx_http_proxy_next_upstream(ngx_http_proxy_ctx_t *p, int ft_type); | |
23 static int ngx_http_proxy_log_state(ngx_http_proxy_ctx_t *p, int status); | |
24 static void ngx_http_proxy_finalize_request(ngx_http_proxy_ctx_t *p, int rc); | |
25 static void ngx_http_proxy_close_connection(ngx_connection_t *c); | |
26 | |
27 static size_t ngx_http_proxy_log_error(void *data, char *buf, size_t len); | |
28 | 9 |
29 static int ngx_http_proxy_init(ngx_cycle_t *cycle); | 10 static int ngx_http_proxy_init(ngx_cycle_t *cycle); |
30 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf); | 11 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf); |
31 static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, | 12 static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, |
32 void *parent, void *child); | 13 void *parent, void *child); |
33 | 14 |
34 static char *ngx_http_proxy_set_pass(ngx_conf_t *cf, ngx_command_t *cmd, | 15 static char *ngx_http_proxy_set_pass(ngx_conf_t *cf, ngx_command_t *cmd, |
35 void *conf); | 16 void *conf); |
36 static char *ngx_http_proxy_parse_upstream(ngx_str_t *url, | 17 static char *ngx_http_proxy_parse_upstream(ngx_str_t *url, |
37 ngx_http_proxy_upstream_t *u); | 18 ngx_http_proxy_upstream_conf_t *u); |
38 | 19 |
39 | 20 |
40 static ngx_conf_bitmask_t next_upstream_masks[] = { | 21 static ngx_conf_bitmask_t next_upstream_masks[] = { |
41 { ngx_string("error"), NGX_HTTP_PROXY_FT_ERROR }, | 22 { ngx_string("error"), NGX_HTTP_PROXY_FT_ERROR }, |
42 { ngx_string("timeout"), NGX_HTTP_PROXY_FT_TIMEOUT }, | 23 { ngx_string("timeout"), NGX_HTTP_PROXY_FT_TIMEOUT }, |
188 NULL, /* init module */ | 169 NULL, /* init module */ |
189 NULL /* init child */ | 170 NULL /* init child */ |
190 }; | 171 }; |
191 | 172 |
192 | 173 |
193 static ngx_str_t http_methods[] = { | |
194 ngx_string("GET "), | |
195 ngx_string("HEAD "), | |
196 ngx_string("POST ") | |
197 }; | |
198 | |
199 | |
200 static char *upstream_header_errors[] = { | |
201 "upstream sent invalid header", | |
202 "upstream sent too long header line" | |
203 }; | |
204 | |
205 | |
206 ngx_http_header_t ngx_http_proxy_headers_in[] = { | 174 ngx_http_header_t ngx_http_proxy_headers_in[] = { |
207 { ngx_string("Date"), offsetof(ngx_http_proxy_headers_in_t, date) }, | 175 { ngx_string("Date"), offsetof(ngx_http_proxy_headers_in_t, date) }, |
208 { ngx_string("Server"), offsetof(ngx_http_proxy_headers_in_t, server) }, | 176 { ngx_string("Server"), offsetof(ngx_http_proxy_headers_in_t, server) }, |
177 | |
178 { ngx_string("Expires"), offsetof(ngx_http_proxy_headers_in_t, expires) }, | |
179 { ngx_string("Cache-Control"), | |
180 offsetof(ngx_http_proxy_headers_in_t, cache_control) }, | |
181 { ngx_string("X-Accel-Expires"), | |
182 offsetof(ngx_http_proxy_headers_in_t, x_accel_expires) }, | |
183 | |
209 { ngx_string("Connection"), | 184 { ngx_string("Connection"), |
210 offsetof(ngx_http_proxy_headers_in_t, connection) }, | 185 offsetof(ngx_http_proxy_headers_in_t, connection) }, |
211 { ngx_string("Content-Type"), | 186 { ngx_string("Content-Type"), |
212 offsetof(ngx_http_proxy_headers_in_t, content_type) }, | 187 offsetof(ngx_http_proxy_headers_in_t, content_type) }, |
213 { ngx_string("Content-Length"), | 188 { ngx_string("Content-Length"), |
219 | 194 |
220 { ngx_null_string, 0 } | 195 { ngx_null_string, 0 } |
221 }; | 196 }; |
222 | 197 |
223 | 198 |
224 static char http_version[] = " HTTP/1.0" CRLF; | |
225 static char host_header[] = "Host: "; | |
226 static char connection_close_header[] = "Connection: close" CRLF; | |
227 | |
228 | |
229 | |
230 static int ngx_http_proxy_handler(ngx_http_request_t *r) | 199 static int ngx_http_proxy_handler(ngx_http_request_t *r) |
231 { | 200 { |
232 int rc; | 201 int rc; |
233 char *last; | 202 char *last; |
234 ngx_http_cache_ctx_t *cctx; | 203 ngx_http_cache_ctx_t *cctx; |
235 ngx_http_proxy_ctx_t *p; | 204 ngx_http_proxy_ctx_t *p; |
236 ngx_http_proxy_upstream_t *u; | |
237 | 205 |
238 ngx_http_create_ctx(r, p, ngx_http_proxy_module, | 206 ngx_http_create_ctx(r, p, ngx_http_proxy_module, |
239 sizeof(ngx_http_proxy_ctx_t), | 207 sizeof(ngx_http_proxy_ctx_t), |
240 NGX_HTTP_INTERNAL_SERVER_ERROR); | 208 NGX_HTTP_INTERNAL_SERVER_ERROR); |
241 | 209 |
250 } | 218 } |
251 | 219 |
252 rc = ngx_http_proxy_get_cached_response(p); | 220 rc = ngx_http_proxy_get_cached_response(p); |
253 | 221 |
254 if (rc == NGX_OK) { | 222 if (rc == NGX_OK) { |
255 return ngx_http_proxy_process_cached_response(p); | 223 return ngx_http_proxy_send_cached_response(p); |
256 } | 224 } |
257 | 225 |
258 if (rc == NGX_HTTP_INTERNAL_SERVER_ERROR) { | 226 if (rc == NGX_HTTP_INTERNAL_SERVER_ERROR) { |
259 return rc; | 227 return rc; |
260 } | 228 } |
265 | 233 |
266 return NGX_DONE; | 234 return NGX_DONE; |
267 } | 235 } |
268 | 236 |
269 | 237 |
270 static int ngx_http_proxy_request_upstream(ngx_http_proxy_ctx_t *p) | 238 int ngx_http_proxy_log_state(ngx_http_proxy_ctx_t *p, int status) |
271 { | |
272 ngx_http_request_t *r; | |
273 | |
274 r = p->request; | |
275 | |
276 p->upstream.peers = p->lcf->peers; | |
277 p->upstream.tries = p->lcf->peers->number; | |
278 | |
279 ngx_init_array(p->states, r->pool, p->upstream.tries, | |
280 sizeof(ngx_http_proxy_state_t), | |
281 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
282 | |
283 p->method = r->method; | |
284 | |
285 /* STUB */ p->cachable = p->lcf->cache; | |
286 | |
287 if (r->headers_in.content_length_n > 0) { | |
288 if (!(r->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)))) { | |
289 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
290 } | |
291 | |
292 r->temp_file->file.fd = NGX_INVALID_FILE; | |
293 r->temp_file->file.log = r->connection->log; | |
294 r->temp_file->path = p->lcf->temp_path; | |
295 r->temp_file->pool = r->pool; | |
296 r->temp_file->warn = "a client request body is buffered " | |
297 "to a temporary file"; | |
298 /* r->temp_file->persistent = 0; */ | |
299 | |
300 r->request_body_handler = ngx_http_proxy_init_request; | |
301 r->data = p; | |
302 | |
303 /* TODO: we ignore return value of ngx_http_read_client_request_body, | |
304 probably we should not return anything */ | |
305 ngx_http_read_client_request_body(r, p->lcf->request_buffer_size); | |
306 | |
307 return NGX_DONE; | |
308 } | |
309 | |
310 ngx_http_proxy_init_request(p); | |
311 | |
312 return NGX_DONE; | |
313 } | |
314 | |
315 | |
316 static void ngx_http_proxy_init_request(void *data) | |
317 { | |
318 ngx_http_proxy_ctx_t *p = data; | |
319 | |
320 ngx_chain_t *cl; | |
321 ngx_http_request_t *r; | |
322 ngx_output_chain_ctx_t *octx; | |
323 ngx_chain_writer_ctx_t *wctx; | |
324 | |
325 r = p->request; | |
326 | |
327 ngx_log_debug(r->connection->log, "timer_set: %d" _ | |
328 r->connection->read->timer_set); | |
329 | |
330 if (r->connection->read->timer_set) { | |
331 ngx_del_timer(r->connection->read); | |
332 } | |
333 | |
334 if (!(cl = ngx_http_proxy_create_request(p))) { | |
335 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
336 return; | |
337 } | |
338 | |
339 if (r->request_hunks) { | |
340 cl->next = r->request_hunks; | |
341 } | |
342 | |
343 r->request_hunks = cl; | |
344 | |
345 p->upstream.log = r->connection->log; | |
346 p->saved_ctx = r->connection->log->data; | |
347 p->saved_handler = r->connection->log->handler; | |
348 r->connection->log->data = p; | |
349 r->connection->log->handler = ngx_http_proxy_log_error; | |
350 p->action = "connecting to upstream"; | |
351 | |
352 if (!(octx = ngx_pcalloc(r->pool, sizeof(ngx_output_chain_ctx_t)))) { | |
353 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
354 return; | |
355 } | |
356 | |
357 p->output_chain_ctx = octx; | |
358 octx->sendfile = r->sendfile; | |
359 octx->pool = r->pool; | |
360 octx->bufs.num = 1; | |
361 octx->tag = (ngx_hunk_tag_t) &ngx_http_proxy_module; | |
362 octx->output_filter = (ngx_output_chain_filter_pt) ngx_chain_writer; | |
363 | |
364 if (!(wctx = ngx_palloc(r->pool, sizeof(ngx_chain_writer_ctx_t)))) { | |
365 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
366 return; | |
367 } | |
368 | |
369 octx->output_ctx = wctx; | |
370 wctx->pool = r->pool; | |
371 | |
372 ngx_http_proxy_connect(p); | |
373 } | |
374 | |
375 | |
376 static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_proxy_ctx_t *p) | |
377 { | |
378 int i; | |
379 size_t len; | |
380 ngx_hunk_t *h; | |
381 ngx_chain_t *chain; | |
382 ngx_table_elt_t *header; | |
383 ngx_http_request_t *r; | |
384 ngx_http_proxy_upstream_t *u; | |
385 | |
386 r = p->request; | |
387 u = p->lcf->upstream; | |
388 | |
389 len = http_methods[p->method - 1].len | |
390 + u->uri.len | |
391 + r->uri.len - u->location->len | |
392 + 1 + r->args.len /* 1 is for "?" */ | |
393 + sizeof(http_version) - 1 | |
394 + sizeof(host_header) - 1 + u->host_header.len + 2 | |
395 /* 2 is for "\r\n" */ | |
396 + sizeof(connection_close_header) - 1 | |
397 + 2; /* 2 is for "\r\n" at the header end */ | |
398 | |
399 header = (ngx_table_elt_t *) r->headers_in.headers->elts; | |
400 for (i = 0; i < r->headers_in.headers->nelts; i++) { | |
401 | |
402 if (&header[i] == r->headers_in.host) { | |
403 continue; | |
404 } | |
405 | |
406 if (&header[i] == r->headers_in.connection) { | |
407 continue; | |
408 } | |
409 | |
410 /* 2 is for ": " and 2 is for "\r\n" */ | |
411 len += header[i].key.len + 2 + header[i].value.len + 2; | |
412 } | |
413 | |
414 /* STUB */ len++; | |
415 | |
416 ngx_test_null(h, ngx_create_temp_hunk(r->pool, len), NULL); | |
417 ngx_alloc_link_and_set_hunk(chain, h, r->pool, NULL); | |
418 | |
419 | |
420 /* the request line */ | |
421 | |
422 h->last = ngx_cpymem(h->last, http_methods[p->method - 1].data, | |
423 http_methods[p->method - 1].len); | |
424 | |
425 h->last = ngx_cpymem(h->last, u->uri.data, u->uri.len); | |
426 | |
427 h->last = ngx_cpymem(h->last, | |
428 r->uri.data + u->location->len, | |
429 r->uri.len - u->location->len); | |
430 | |
431 if (r->args.len > 0) { | |
432 *(h->last++) = '?'; | |
433 h->last = ngx_cpymem(h->last, r->args.data, r->args.len); | |
434 } | |
435 | |
436 h->last = ngx_cpymem(h->last, http_version, sizeof(http_version) - 1); | |
437 | |
438 | |
439 /* "Host" header */ | |
440 | |
441 h->last = ngx_cpymem(h->last, host_header, sizeof(host_header) - 1); | |
442 h->last = ngx_cpymem(h->last, u->host_header.data, u->host_header.len); | |
443 *(h->last++) = CR; *(h->last++) = LF; | |
444 | |
445 | |
446 /* "Connection: close" header */ | |
447 | |
448 h->last = ngx_cpymem(h->last, connection_close_header, | |
449 sizeof(connection_close_header) - 1); | |
450 | |
451 | |
452 for (i = 0; i < r->headers_in.headers->nelts; i++) { | |
453 | |
454 if (&header[i] == r->headers_in.host) { | |
455 continue; | |
456 } | |
457 | |
458 if (&header[i] == r->headers_in.connection) { | |
459 continue; | |
460 } | |
461 | |
462 if (&header[i] == r->headers_in.keep_alive) { | |
463 continue; | |
464 } | |
465 | |
466 h->last = ngx_cpymem(h->last, header[i].key.data, header[i].key.len); | |
467 | |
468 *(h->last++) = ':'; *(h->last++) = ' '; | |
469 | |
470 h->last = ngx_cpymem(h->last, header[i].value.data, | |
471 header[i].value.len); | |
472 | |
473 *(h->last++) = CR; *(h->last++) = LF; | |
474 | |
475 ngx_log_debug(r->connection->log, "proxy: '%s: %s'" _ | |
476 header[i].key.data _ header[i].value.data); | |
477 } | |
478 | |
479 /* add "\r\n" at the header end */ | |
480 *(h->last++) = CR; *(h->last++) = LF; | |
481 | |
482 /* STUB */ *(h->last) = '\0'; | |
483 ngx_log_debug(r->connection->log, "PROXY:\n'%s'" _ h->pos); | |
484 | |
485 return chain; | |
486 } | |
487 | |
488 | |
489 static void ngx_http_proxy_send_request_handler(ngx_event_t *wev) | |
490 { | |
491 ngx_connection_t *c; | |
492 ngx_http_proxy_ctx_t *p; | |
493 | |
494 c = wev->data; | |
495 p = c->data; | |
496 | |
497 if (wev->timedout) { | |
498 p->action = "sending request to upstream"; | |
499 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT); | |
500 return; | |
501 } | |
502 | |
503 ngx_http_proxy_send_request(p); | |
504 } | |
505 | |
506 | |
507 static void ngx_http_proxy_dummy_handler(ngx_event_t *wev) | |
508 { | |
509 ngx_log_debug(wev->log, "dummy handler"); | |
510 } | |
511 | |
512 | |
513 static void ngx_http_proxy_connect(ngx_http_proxy_ctx_t *p) | |
514 { | |
515 int rc; | |
516 ngx_chain_t *cl; | |
517 ngx_connection_t *c; | |
518 ngx_http_request_t *r; | |
519 ngx_output_chain_ctx_t *octx; | |
520 | |
521 p->action = "connecting to upstream"; | |
522 | |
523 rc = ngx_event_connect_peer(&p->upstream); | |
524 | |
525 if (rc == NGX_ERROR) { | |
526 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
527 return; | |
528 } | |
529 | |
530 if (rc == NGX_CONNECT_ERROR) { | |
531 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR); | |
532 return; | |
533 } | |
534 | |
535 p->upstream.connection->data = p; | |
536 p->upstream.connection->write->event_handler = | |
537 ngx_http_proxy_send_request_handler; | |
538 p->upstream.connection->read->event_handler = | |
539 ngx_http_proxy_process_upstream_status_line; | |
540 | |
541 r = p->request; | |
542 c = p->upstream.connection; | |
543 c->pool = r->pool; | |
544 c->read->log = c->write->log = c->log = r->connection->log; | |
545 | |
546 octx = p->output_chain_ctx; | |
547 | |
548 if (p->upstream.tries > 1 && p->request_sent) { | |
549 ngx_http_proxy_reinit_upstream(p); | |
550 } | |
551 | |
552 /* init or reinit ngx_output_chain() context */ | |
553 | |
554 if (r->request_body_hunk) { | |
555 if (!(octx->free = ngx_alloc_chain_link(r->pool))) { | |
556 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
557 return; | |
558 } | |
559 | |
560 octx->free->hunk = r->request_body_hunk; | |
561 octx->free->next = NULL; | |
562 octx->hunks = 1; | |
563 | |
564 r->request_body_hunk->pos = r->request_body_hunk->start; | |
565 r->request_body_hunk->last = r->request_body_hunk->start; | |
566 } | |
567 | |
568 p->request_sent = 0; | |
569 | |
570 if (rc == NGX_AGAIN) { | |
571 ngx_add_timer(c->write, p->lcf->connect_timeout); | |
572 return; | |
573 } | |
574 | |
575 /* rc == NGX_OK */ | |
576 | |
577 #if 1 /* test only */ | |
578 | |
579 if (c->read->ready) { | |
580 /* post aio operation */ | |
581 ngx_http_proxy_process_upstream_status_line(c->read); | |
582 return; | |
583 } | |
584 #endif | |
585 | |
586 ngx_http_proxy_send_request(p); | |
587 } | |
588 | |
589 | |
590 static void ngx_http_proxy_send_request(ngx_http_proxy_ctx_t *p) | |
591 { | |
592 int rc; | |
593 ngx_connection_t *c; | |
594 ngx_chain_writer_ctx_t *wctx; | |
595 | |
596 c = p->upstream.connection; | |
597 | |
598 #if (HAVE_KQUEUE) | |
599 | |
600 if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) | |
601 && !p->request_sent | |
602 && c->write->kq_eof) | |
603 { | |
604 ngx_log_error(NGX_LOG_ERR, c->log, c->write->kq_errno, | |
605 "connect() failed"); | |
606 | |
607 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR); | |
608 return; | |
609 } | |
610 | |
611 #endif | |
612 | |
613 p->action = "sending request to upstream"; | |
614 | |
615 wctx = p->output_chain_ctx->output_ctx; | |
616 wctx->out = NULL; | |
617 wctx->last = &wctx->out; | |
618 wctx->connection = c; | |
619 | |
620 rc = ngx_output_chain(p->output_chain_ctx, | |
621 p->request_sent ? NULL : p->request->request_hunks); | |
622 | |
623 if (rc == NGX_ERROR) { | |
624 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR); | |
625 return; | |
626 } | |
627 | |
628 p->request_sent = 1; | |
629 | |
630 if (c->write->timer_set) { | |
631 ngx_del_timer(c->write); | |
632 } | |
633 | |
634 if (rc == NGX_AGAIN) { | |
635 ngx_add_timer(c->write, p->lcf->send_timeout); | |
636 | |
637 if (ngx_handle_write_event(c->write, /* STUB: lowat */ 0) == NGX_ERROR) | |
638 { | |
639 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
640 return; | |
641 } | |
642 | |
643 return; | |
644 } | |
645 | |
646 /* rc == NGX_OK */ | |
647 | |
648 if (c->tcp_nopush) { | |
649 if (ngx_tcp_push(c->fd) == NGX_ERROR) { | |
650 ngx_log_error(NGX_LOG_CRIT, c->log, | |
651 ngx_socket_errno, | |
652 ngx_tcp_push_n " failed"); | |
653 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
654 return; | |
655 } | |
656 | |
657 c->tcp_nopush = 0; | |
658 return; | |
659 } | |
660 | |
661 #if 0 | |
662 if (c->read->ready) { | |
663 | |
664 /* post aio operation */ | |
665 | |
666 /* | |
667 * although we can post aio operation just in the end | |
668 * of ngx_http_proxy_connect() CHECK IT !!! | |
669 * it's better to do here because we postpone header buffer allocation | |
670 */ | |
671 | |
672 ngx_http_proxy_process_upstream_status_line(c->read); | |
673 return; | |
674 } | |
675 #endif | |
676 | |
677 p->upstream.connection->write->event_handler = ngx_http_proxy_dummy_handler; | |
678 | |
679 if (ngx_handle_level_write_event(c->write) == NGX_ERROR) { | |
680 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
681 return; | |
682 } | |
683 } | |
684 | |
685 | |
686 static void ngx_http_proxy_process_upstream_status_line(ngx_event_t *rev) | |
687 { | |
688 int rc; | |
689 ssize_t n; | |
690 ngx_connection_t *c; | |
691 ngx_http_proxy_ctx_t *p; | |
692 | |
693 c = rev->data; | |
694 p = c->data; | |
695 p->action = "reading upstream status line"; | |
696 | |
697 ngx_log_debug(rev->log, "http proxy process status line"); | |
698 | |
699 if (rev->timedout) { | |
700 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT); | |
701 return; | |
702 } | |
703 | |
704 if (p->header_in == NULL) { | |
705 p->header_in = ngx_create_temp_hunk(p->request->pool, | |
706 p->lcf->header_buffer_size); | |
707 if (p->header_in == NULL) { | |
708 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
709 return; | |
710 } | |
711 p->header_in->tag = (ngx_hunk_tag_t) &ngx_http_proxy_module; | |
712 | |
713 if (p->cache) { | |
714 p->header_in->pos += p->cache->ctx.header.size; | |
715 p->header_in->last = p->header_in->pos; | |
716 } | |
717 } | |
718 | |
719 n = ngx_http_proxy_read_upstream_header(p); | |
720 | |
721 if (n == NGX_AGAIN) { | |
722 return; | |
723 } | |
724 | |
725 if (n == 0) { | |
726 ngx_log_error(NGX_LOG_ERR, rev->log, 0, | |
727 "upstream prematurely closed connection"); | |
728 } | |
729 | |
730 if (n == NGX_ERROR || n == 0) { | |
731 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR); | |
732 return; | |
733 } | |
734 | |
735 p->upstream.cached = 0; | |
736 | |
737 rc = ngx_http_proxy_parse_status_line(p); | |
738 | |
739 if (rc == NGX_AGAIN) { | |
740 if (p->header_in->pos == p->header_in->last) { | |
741 ngx_log_error(NGX_LOG_ERR, rev->log, 0, | |
742 "upstream sent too long status line"); | |
743 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER); | |
744 } | |
745 return; | |
746 } | |
747 | |
748 if (rc == NGX_HTTP_PROXY_PARSE_NO_HEADER) { | |
749 ngx_log_error(NGX_LOG_ERR, rev->log, 0, | |
750 "upstream sent no valid HTTP/1.0 header"); | |
751 | |
752 if (p->accel) { | |
753 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER); | |
754 | |
755 } else { | |
756 p->request->http_version = NGX_HTTP_VERSION_9; | |
757 p->status = NGX_HTTP_OK; | |
758 ngx_http_proxy_send_response(p); | |
759 } | |
760 | |
761 return; | |
762 } | |
763 | |
764 /* rc == NGX_OK */ | |
765 | |
766 if (p->status == NGX_HTTP_INTERNAL_SERVER_ERROR) { | |
767 | |
768 if (p->upstream.tries > 1 | |
769 && (p->lcf->next_upstream & NGX_HTTP_PROXY_FT_HTTP_500)) | |
770 { | |
771 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_HTTP_500); | |
772 return; | |
773 } | |
774 | |
775 if (p->upstream.tries == 0 | |
776 && p->stale | |
777 && (p->lcf->use_stale & NGX_HTTP_PROXY_FT_HTTP_500)) | |
778 { | |
779 /* | |
780 * TODO: use stale cached response if it exists and enabled | |
781 */ | |
782 | |
783 return; | |
784 } | |
785 } | |
786 | |
787 p->status_line.len = p->status_end - p->status_start; | |
788 p->status_line.data = ngx_palloc(p->request->pool, p->status_line.len + 1); | |
789 if (p->status_line.data == NULL) { | |
790 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
791 return; | |
792 } | |
793 ngx_cpystrn(p->status_line.data, p->status_start, p->status_line.len + 1); | |
794 | |
795 ngx_log_debug(rev->log, "http proxy status %d '%s'" _ | |
796 p->status _ p->status_line.data); | |
797 | |
798 if (p->headers_in.headers) { | |
799 p->headers_in.headers->nelts = 0; | |
800 } else { | |
801 p->headers_in.headers = ngx_create_table(p->request->pool, 20); | |
802 } | |
803 | |
804 c->read->event_handler = ngx_http_proxy_process_upstream_headers; | |
805 ngx_http_proxy_process_upstream_headers(rev); | |
806 } | |
807 | |
808 | |
809 static void ngx_http_proxy_process_upstream_headers(ngx_event_t *rev) | |
810 { | |
811 int i, rc; | |
812 ssize_t n; | |
813 ngx_table_elt_t *h; | |
814 ngx_connection_t *c; | |
815 ngx_http_request_t *r; | |
816 ngx_http_proxy_ctx_t *p; | |
817 | |
818 c = rev->data; | |
819 p = c->data; | |
820 r = p->request; | |
821 p->action = "reading upstream headers"; | |
822 | |
823 ngx_log_debug(rev->log, "http proxy process header line"); | |
824 | |
825 if (rev->timedout) { | |
826 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT); | |
827 return; | |
828 } | |
829 | |
830 rc = NGX_AGAIN; | |
831 | |
832 for ( ;; ) { | |
833 if (rc == NGX_AGAIN) { | |
834 n = ngx_http_proxy_read_upstream_header(p); | |
835 | |
836 if (n == 0) { | |
837 ngx_log_error(NGX_LOG_ERR, rev->log, 0, | |
838 "upstream prematurely closed connection"); | |
839 } | |
840 | |
841 if (n == NGX_ERROR || n == 0) { | |
842 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR); | |
843 return; | |
844 } | |
845 | |
846 if (n == NGX_AGAIN) { | |
847 return; | |
848 } | |
849 } | |
850 | |
851 rc = ngx_http_parse_header_line(p->request, p->header_in); | |
852 | |
853 if (rc == NGX_OK) { | |
854 | |
855 /* a header line has been parsed successfully */ | |
856 | |
857 h = ngx_http_add_header(&p->headers_in, ngx_http_proxy_headers_in); | |
858 if (h == NULL) { | |
859 ngx_http_proxy_finalize_request(p, | |
860 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
861 return; | |
862 } | |
863 | |
864 h->key.len = r->header_name_end - r->header_name_start; | |
865 h->value.len = r->header_end - r->header_start; | |
866 | |
867 h->key.data = ngx_palloc(p->request->pool, | |
868 h->key.len + 1 + h->value.len + 1); | |
869 if (h->key.data == NULL) { | |
870 ngx_http_proxy_finalize_request(p, | |
871 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
872 return; | |
873 } | |
874 | |
875 h->value.data = h->key.data + h->key.len + 1; | |
876 ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1); | |
877 ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1); | |
878 | |
879 for (i = 0; ngx_http_proxy_headers_in[i].name.len != 0; i++) { | |
880 if (ngx_http_proxy_headers_in[i].name.len != h->key.len) { | |
881 continue; | |
882 } | |
883 | |
884 if (ngx_strcasecmp(ngx_http_proxy_headers_in[i].name.data, | |
885 h->key.data) == 0) | |
886 { | |
887 *((ngx_table_elt_t **) ((char *) &p->headers_in | |
888 + ngx_http_proxy_headers_in[i].offset)) = h; | |
889 break; | |
890 } | |
891 } | |
892 | |
893 ngx_log_debug(c->log, "HTTP proxy header: '%s: %s'" _ | |
894 h->key.data _ h->value.data); | |
895 | |
896 continue; | |
897 | |
898 } else if (rc == NGX_HTTP_PARSE_HEADER_DONE) { | |
899 | |
900 /* a whole header has been parsed successfully */ | |
901 | |
902 ngx_log_debug(c->log, "HTTP header done"); | |
903 | |
904 ngx_http_proxy_send_response(p); | |
905 return; | |
906 | |
907 } else if (rc != NGX_AGAIN) { | |
908 | |
909 /* there was error while a header line parsing */ | |
910 | |
911 ngx_log_error(NGX_LOG_ERR, rev->log, 0, | |
912 upstream_header_errors[rc - NGX_HTTP_PARSE_HEADER_ERROR]); | |
913 | |
914 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER); | |
915 return; | |
916 } | |
917 | |
918 /* rc == NGX_AGAIN: a header line parsing is still not complete */ | |
919 | |
920 if (p->header_in->last == p->header_in->end) { | |
921 ngx_log_error(NGX_LOG_ERR, rev->log, 0, | |
922 "upstream sent too big header"); | |
923 | |
924 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER); | |
925 return; | |
926 } | |
927 } | |
928 } | |
929 | |
930 | |
931 static ssize_t ngx_http_proxy_read_upstream_header(ngx_http_proxy_ctx_t *p) | |
932 { | |
933 ssize_t n; | |
934 ngx_event_t *rev; | |
935 | |
936 rev = p->upstream.connection->read; | |
937 | |
938 n = p->header_in->last - p->header_in->pos; | |
939 | |
940 if (n > 0) { | |
941 return n; | |
942 } | |
943 | |
944 n = ngx_recv(p->upstream.connection, p->header_in->last, | |
945 p->header_in->end - p->header_in->last); | |
946 | |
947 if (n == NGX_AGAIN) { | |
948 ngx_add_timer(rev, p->lcf->read_timeout); | |
949 | |
950 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) { | |
951 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
952 return NGX_ERROR; | |
953 } | |
954 | |
955 return NGX_AGAIN; | |
956 } | |
957 | |
958 if (n == 0) { | |
959 ngx_log_error(NGX_LOG_ERR, rev->log, 0, | |
960 "upstream closed prematurely connection"); | |
961 } | |
962 | |
963 if (n == 0 || n == NGX_ERROR) { | |
964 return NGX_ERROR; | |
965 } | |
966 | |
967 p->header_in->last += n; | |
968 | |
969 return n; | |
970 } | |
971 | |
972 | |
973 static void ngx_http_proxy_send_response(ngx_http_proxy_ctx_t *p) | |
974 { | |
975 int rc, i; | |
976 ngx_table_elt_t *ho, *h; | |
977 ngx_event_pipe_t *ep; | |
978 ngx_http_request_t *r; | |
979 ngx_http_bin_cache_t *header; | |
980 ngx_http_core_loc_conf_t *clcf; | |
981 | |
982 r = p->request; | |
983 | |
984 r->headers_out.status = p->status; | |
985 | |
986 #if 0 | |
987 r->headers_out.content_length_n = -1; | |
988 r->headers_out.content_length = NULL; | |
989 #endif | |
990 | |
991 /* copy an upstream header to r->headers_out */ | |
992 | |
993 if (ngx_http_proxy_copy_header(p, &p->headers_in) == NGX_ERROR) { | |
994 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
995 return; | |
996 } | |
997 | |
998 /* TODO: preallocate event_pipe hunks, look "Content-Length" */ | |
999 | |
1000 rc = ngx_http_send_header(r); | |
1001 | |
1002 p->header_sent = 1; | |
1003 | |
1004 if (p->cache) { | |
1005 header = (ngx_http_bin_cache_t *) p->header_in->start; | |
1006 header->type = 0x42424242; /* "BBBB" */ | |
1007 header->header.length = r->headers_out.content_length_n; | |
1008 header->key_len = p->cache->ctx.key.len; | |
1009 ngx_memcpy(&header->key, p->cache->ctx.key.data, header->key_len); | |
1010 header->key[header->key_len] = LF; | |
1011 } | |
1012 | |
1013 ep = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t)); | |
1014 if (ep == NULL) { | |
1015 ngx_http_proxy_finalize_request(p, 0); | |
1016 return; | |
1017 } | |
1018 | |
1019 ep->input_filter = ngx_event_pipe_copy_input_filter; | |
1020 ep->output_filter = (ngx_event_pipe_output_filter_pt) | |
1021 ngx_http_output_filter; | |
1022 ep->output_ctx = r; | |
1023 ep->tag = (ngx_hunk_tag_t) &ngx_http_proxy_module; | |
1024 ep->bufs = p->lcf->bufs; | |
1025 ep->busy_size = p->lcf->busy_buffers_size; | |
1026 ep->upstream = p->upstream.connection; | |
1027 ep->downstream = r->connection; | |
1028 ep->pool = r->pool; | |
1029 ep->log = r->connection->log; | |
1030 | |
1031 if (!(ep->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)))) { | |
1032 ngx_http_proxy_finalize_request(p, 0); | |
1033 return; | |
1034 } | |
1035 | |
1036 ep->temp_file->file.fd = NGX_INVALID_FILE; | |
1037 ep->temp_file->file.log = r->connection->log; | |
1038 ep->temp_file->path = p->lcf->temp_path; | |
1039 ep->temp_file->pool = r->pool; | |
1040 ep->temp_file->warn = "an upstream response is buffered " | |
1041 "to a temporary file"; | |
1042 | |
1043 ep->max_temp_file_size = p->lcf->max_temp_file_size; | |
1044 ep->temp_file_write_size = p->lcf->temp_file_write_size; | |
1045 | |
1046 ep->preread_hunks = ngx_alloc_chain_link(r->pool); | |
1047 if (ep->preread_hunks == NULL) { | |
1048 ngx_http_proxy_finalize_request(p, 0); | |
1049 return; | |
1050 } | |
1051 ep->preread_hunks->hunk = p->header_in; | |
1052 ep->preread_hunks->next = NULL; | |
1053 p->header_in->type |= NGX_HUNK_PREREAD; | |
1054 | |
1055 ep->preread_size = p->header_in->last - p->header_in->pos; | |
1056 | |
1057 if (ngx_event_flags & NGX_USE_AIO_EVENT) { | |
1058 | |
1059 /* the posted aio operation can currupt shadow buf */ | |
1060 ep->single_buf = 1; | |
1061 } | |
1062 | |
1063 /* TODO: ep->free_bufs = 0 if use ngx_create_chain_of_hunks() */ | |
1064 ep->free_bufs = 1; | |
1065 | |
1066 /* | |
1067 * event_pipe would do p->header_in->last += ep->preread_size | |
1068 * as though these bytes were read. | |
1069 */ | |
1070 p->header_in->last = p->header_in->pos; | |
1071 | |
1072 ep->cachable = p->cachable; | |
1073 | |
1074 if (p->lcf->cyclic_temp_file) { | |
1075 | |
1076 /* | |
1077 * we need to disable the use of sendfile() if we use cyclic temp file | |
1078 * because the writing a new data can interfere with sendfile() | |
1079 * that uses the same kernel file pages (at least on FreeBSD) | |
1080 */ | |
1081 | |
1082 ep->cyclic_temp_file = 1; | |
1083 r->sendfile = 0; | |
1084 | |
1085 } else { | |
1086 ep->cyclic_temp_file = 0; | |
1087 r->sendfile = 1; | |
1088 } | |
1089 | |
1090 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
1091 | |
1092 ep->read_timeout = p->lcf->read_timeout; | |
1093 ep->send_timeout = clcf->send_timeout; | |
1094 ep->send_lowat = clcf->send_lowat; | |
1095 | |
1096 p->event_pipe = ep; | |
1097 | |
1098 p->upstream.connection->read->event_handler = ngx_http_proxy_process_body; | |
1099 r->connection->write->event_handler = ngx_http_proxy_process_body; | |
1100 | |
1101 ngx_http_proxy_process_body(p->upstream.connection->read); | |
1102 | |
1103 return; | |
1104 } | |
1105 | |
1106 | |
1107 static void ngx_http_proxy_process_body(ngx_event_t *ev) | |
1108 { | |
1109 ngx_connection_t *c; | |
1110 ngx_http_request_t *r; | |
1111 ngx_http_proxy_ctx_t *p; | |
1112 ngx_event_pipe_t *ep; | |
1113 | |
1114 c = ev->data; | |
1115 | |
1116 if (ev->write) { | |
1117 ngx_log_debug(ev->log, "http proxy process downstream"); | |
1118 r = c->data; | |
1119 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module); | |
1120 p->action = "sending to client"; | |
1121 | |
1122 } else { | |
1123 ngx_log_debug(ev->log, "http proxy process upstream"); | |
1124 p = c->data; | |
1125 r = p->request; | |
1126 p->action = "reading upstream body"; | |
1127 } | |
1128 | |
1129 ep = p->event_pipe; | |
1130 | |
1131 if (ev->timedout) { | |
1132 if (ev->write) { | |
1133 ep->downstream_error = 1; | |
1134 ngx_log_error(NGX_LOG_ERR, c->log, NGX_ETIMEDOUT, | |
1135 "client timed out"); | |
1136 | |
1137 } else { | |
1138 ep->upstream_error = 1; | |
1139 ngx_log_error(NGX_LOG_ERR, c->log, NGX_ETIMEDOUT, | |
1140 "upstream timed out"); | |
1141 } | |
1142 | |
1143 } else { | |
1144 if (ngx_event_pipe(ep, ev->write) == NGX_ABORT) { | |
1145 ngx_http_proxy_finalize_request(p, 0); | |
1146 return; | |
1147 } | |
1148 } | |
1149 | |
1150 if (p->upstream.connection) { | |
1151 if (ep->upstream_done) { | |
1152 if (ngx_http_proxy_update_cache(p) == NGX_ERROR) { | |
1153 ngx_http_proxy_finalize_request(p, 0); | |
1154 return; | |
1155 } | |
1156 | |
1157 } else if (ep->upstream_eof) { | |
1158 | |
1159 /* TODO: check length & update cache */ | |
1160 | |
1161 if (ngx_http_proxy_update_cache(p) == NGX_ERROR) { | |
1162 ngx_http_proxy_finalize_request(p, 0); | |
1163 return; | |
1164 } | |
1165 } | |
1166 | |
1167 if (ep->upstream_done || ep->upstream_eof || ep->upstream_error) { | |
1168 ngx_http_proxy_close_connection(p->upstream.connection); | |
1169 p->upstream.connection = NULL; | |
1170 } | |
1171 } | |
1172 | |
1173 if (ep->downstream_done) { | |
1174 ngx_log_debug(ev->log, "http proxy downstream done"); | |
1175 ngx_http_proxy_finalize_request(p, r->main ? 0 : ngx_http_send_last(r)); | |
1176 return; | |
1177 } | |
1178 | |
1179 if (ep->downstream_error) { | |
1180 if (!p->cachable && p->upstream.connection) { | |
1181 ngx_http_proxy_close_connection(p->upstream.connection); | |
1182 p->upstream.connection = NULL; | |
1183 } | |
1184 | |
1185 if (p->upstream.connection == NULL) { | |
1186 ngx_http_close_connection(c); | |
1187 } | |
1188 } | |
1189 | |
1190 return; | |
1191 } | |
1192 | |
1193 | |
1194 static int ngx_http_proxy_update_cache(ngx_http_proxy_ctx_t *p) | |
1195 { | |
1196 if (p->cache == NULL) { | |
1197 return NGX_OK; | |
1198 } | |
1199 | |
1200 return ngx_http_cache_update_file(p->request, &p->cache->ctx, | |
1201 &p->event_pipe->temp_file->file.name); | |
1202 } | |
1203 | |
1204 | |
1205 static void ngx_http_proxy_next_upstream(ngx_http_proxy_ctx_t *p, int ft_type) | |
1206 { | |
1207 int status; | |
1208 | |
1209 ngx_log_debug(p->request->connection->log, "next upstream: %d" _ ft_type); | |
1210 | |
1211 if (ft_type != NGX_HTTP_PROXY_FT_HTTP_404) { | |
1212 ngx_event_connect_peer_failed(&p->upstream); | |
1213 } | |
1214 | |
1215 if (ft_type == NGX_HTTP_PROXY_FT_TIMEOUT) { | |
1216 ngx_log_error(NGX_LOG_ERR, p->request->connection->log, NGX_ETIMEDOUT, | |
1217 "upstream timed out"); | |
1218 } | |
1219 | |
1220 if (p->upstream.cached && ft_type == NGX_HTTP_PROXY_FT_ERROR) { | |
1221 status = 0; | |
1222 | |
1223 } else { | |
1224 switch(ft_type) { | |
1225 case NGX_HTTP_PROXY_FT_TIMEOUT: | |
1226 status = NGX_HTTP_GATEWAY_TIME_OUT; | |
1227 break; | |
1228 | |
1229 case NGX_HTTP_PROXY_FT_HTTP_500: | |
1230 status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1231 break; | |
1232 | |
1233 case NGX_HTTP_PROXY_FT_HTTP_404: | |
1234 status = NGX_HTTP_NOT_FOUND; | |
1235 break; | |
1236 | |
1237 /* | |
1238 * NGX_HTTP_PROXY_FT_BUSY_LOCK and NGX_HTTP_PROXY_FT_MAX_WAITING | |
1239 * never reach here | |
1240 */ | |
1241 | |
1242 default: | |
1243 status = NGX_HTTP_BAD_GATEWAY; | |
1244 } | |
1245 } | |
1246 | |
1247 if (p->upstream.connection) { | |
1248 ngx_http_proxy_close_connection(p->upstream.connection); | |
1249 p->upstream.connection = NULL; | |
1250 } | |
1251 | |
1252 if (status) { | |
1253 if (ngx_http_proxy_log_state(p, status) == NGX_ERROR) { | |
1254 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
1255 return; | |
1256 } | |
1257 | |
1258 if (p->upstream.tries == 0 || !(p->lcf->next_upstream & ft_type)) { | |
1259 | |
1260 if (p->stale && (p->lcf->use_stale & ft_type)) { | |
1261 /* | |
1262 * TODO: use stale cached response if it exists and enabled | |
1263 */ | |
1264 } | |
1265 | |
1266 ngx_http_proxy_finalize_request(p, status); | |
1267 return; | |
1268 } | |
1269 } | |
1270 | |
1271 ngx_http_proxy_connect(p); | |
1272 } | |
1273 | |
1274 | |
1275 static int ngx_http_proxy_log_state(ngx_http_proxy_ctx_t *p, int status) | |
1276 { | 239 { |
1277 ngx_http_proxy_state_t *state; | 240 ngx_http_proxy_state_t *state; |
1278 | 241 |
1279 if (!(state = ngx_push_array(&p->states))) { | 242 if (!(state = ngx_push_array(&p->states))) { |
1280 return NGX_ERROR; | 243 return NGX_ERROR; |
1281 } | 244 } |
1282 | 245 |
1283 state->status = status; | 246 state->status = status; |
1284 state->peer = | 247 state->peer = |
1285 &p->upstream.peers->peers[p->upstream.cur_peer].addr_port_text; | 248 &p->upstream->peer.peers->peers[p->upstream->peer.cur_peer].addr_port_text; |
1286 | 249 |
1287 return NGX_OK; | 250 return NGX_OK; |
1288 } | 251 } |
1289 | 252 |
1290 | 253 |
1291 static void ngx_http_proxy_finalize_request(ngx_http_proxy_ctx_t *p, int rc) | 254 void ngx_http_proxy_finalize_request(ngx_http_proxy_ctx_t *p, int rc) |
1292 { | 255 { |
1293 ngx_log_debug(p->request->connection->log, | 256 ngx_log_debug(p->request->connection->log, |
1294 "finalize http proxy request"); | 257 "finalize http proxy request"); |
1295 | 258 |
1296 if (p->upstream.connection) { | 259 if (p->upstream->peer.connection) { |
1297 ngx_http_proxy_close_connection(p->upstream.connection); | 260 ngx_http_proxy_close_connection(p->upstream->peer.connection); |
1298 p->upstream.connection = NULL; | 261 p->upstream->peer.connection = NULL; |
1299 } | 262 } |
1300 | 263 |
1301 if (p->header_sent | 264 if (p->header_sent |
1302 && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE)) | 265 && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE)) |
1303 { | 266 { |
1309 | 272 |
1310 ngx_http_finalize_request(p->request, rc); | 273 ngx_http_finalize_request(p->request, rc); |
1311 } | 274 } |
1312 | 275 |
1313 | 276 |
1314 static void ngx_http_proxy_close_connection(ngx_connection_t *c) | 277 void ngx_http_proxy_close_connection(ngx_connection_t *c) |
1315 { | 278 { |
1316 ngx_log_debug(c->log, "proxy close connection: %d" _ c->fd); | 279 ngx_log_debug(c->log, "proxy close connection: %d" _ c->fd); |
1317 | 280 |
1318 if (c->fd == -1) { | 281 if (c->fd == -1) { |
1319 #if 0 | 282 #if 0 |
1352 | 315 |
1353 c->fd = -1; | 316 c->fd = -1; |
1354 } | 317 } |
1355 | 318 |
1356 | 319 |
1357 static size_t ngx_http_proxy_log_error(void *data, char *buf, size_t len) | 320 size_t ngx_http_proxy_log_error(void *data, char *buf, size_t len) |
1358 { | 321 { |
1359 ngx_http_proxy_ctx_t *p = data; | 322 ngx_http_proxy_ctx_t *p = data; |
1360 | 323 |
324 ngx_http_request_t *r; | |
325 ngx_peer_connection_t *peer; | |
326 | |
327 r = p->request; | |
328 peer = &p->upstream->peer; | |
329 | |
1361 return ngx_snprintf(buf, len, | 330 return ngx_snprintf(buf, len, |
1362 " while %s, client: %s, URL: %s, upstream: %s%s%s%s%s", | 331 " while %s, client: %s, URL: %s, upstream: %s%s%s%s%s", |
1363 p->action, | 332 p->action, |
1364 p->request->connection->addr_text.data, | 333 r->connection->addr_text.data, |
1365 p->request->unparsed_uri.data, | 334 r->unparsed_uri.data, |
1366 p->upstream.peers->peers[p->upstream.cur_peer].addr_port_text.data, | 335 peer->peers->peers[peer->cur_peer].addr_port_text.data, |
1367 p->lcf->upstream->uri.data, | 336 p->lcf->upstream->uri.data, |
1368 p->request->uri.data + p->lcf->upstream->location->len, | 337 r->uri.data + p->lcf->upstream->location->len, |
1369 p->request->args.len ? "?" : "", | 338 r->args.len ? "?" : "", |
1370 p->request->args.len ? p->request->args.data : ""); | 339 r->args.len ? r->args.data : ""); |
1371 } | 340 } |
1372 | 341 |
1373 | 342 |
1374 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf) | 343 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf) |
1375 { | 344 { |
1492 if (ngx_strncasecmp(value[1].data, "http://", 7) != 0) { | 461 if (ngx_strncasecmp(value[1].data, "http://", 7) != 0) { |
1493 return "invalid URL prefix"; | 462 return "invalid URL prefix"; |
1494 } | 463 } |
1495 | 464 |
1496 ngx_test_null(lcf->upstream, | 465 ngx_test_null(lcf->upstream, |
1497 ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_upstream_t)), | 466 ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_upstream_conf_t)), |
1498 NGX_CONF_ERROR); | 467 NGX_CONF_ERROR); |
1499 | 468 |
1500 lcf->upstream->url.len = value[1].len; | 469 lcf->upstream->url.len = value[1].len; |
1501 if (!(lcf->upstream->url.data = ngx_palloc(cf->pool, value[1].len + 1))) { | 470 if (!(lcf->upstream->url.data = ngx_palloc(cf->pool, value[1].len + 1))) { |
1502 return NGX_CONF_ERROR; | 471 return NGX_CONF_ERROR; |
1604 return NULL; | 573 return NULL; |
1605 } | 574 } |
1606 | 575 |
1607 | 576 |
1608 static char *ngx_http_proxy_parse_upstream(ngx_str_t *url, | 577 static char *ngx_http_proxy_parse_upstream(ngx_str_t *url, |
1609 ngx_http_proxy_upstream_t *u) | 578 ngx_http_proxy_upstream_conf_t *u) |
1610 { | 579 { |
1611 size_t i; | 580 size_t i; |
1612 | 581 |
1613 if (url->data[0] == ':' || url->data[0] == '/') { | 582 if (url->data[0] == ':' || url->data[0] == '/') { |
1614 return "invalid upstream URL"; | 583 return "invalid upstream URL"; |