comparison src/http/ngx_http_upstream.c @ 578:f3a9e57d2e17

Merge with current.
author Maxim Dounin <mdounin@mdounin.ru>
date Thu, 11 Mar 2010 21:27:17 +0300
parents 7fa8dc2315bd
children 8246d8a2c2be
comparison
equal deleted inserted replaced
539:5f4de8cf0d9d 578:f3a9e57d2e17
364 364
365 if (u && u->cleanup) { 365 if (u && u->cleanup) {
366 r->main->count++; 366 r->main->count++;
367 ngx_http_upstream_cleanup(r); 367 ngx_http_upstream_cleanup(r);
368 *u->cleanup = NULL; 368 *u->cleanup = NULL;
369 u->cleanup = NULL;
369 } 370 }
370 371
371 u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t)); 372 u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t));
372 if (u == NULL) { 373 if (u == NULL) {
373 return NGX_ERROR; 374 return NGX_ERROR;
477 if (u->create_request(r) != NGX_OK) { 478 if (u->create_request(r) != NGX_OK) {
478 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 479 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
479 return; 480 return;
480 } 481 }
481 482
483 u->peer.local = u->conf->local;
484
482 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); 485 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
483 486
484 u->output.alignment = clcf->directio_alignment; 487 u->output.alignment = clcf->directio_alignment;
485 u->output.pool = r->pool; 488 u->output.pool = r->pool;
486 u->output.bufs.num = 1; 489 u->output.bufs.num = 1;
530 if (u->resolved->sockaddr) { 533 if (u->resolved->sockaddr) {
531 534
532 if (ngx_http_upstream_create_round_robin_peer(r, u->resolved) 535 if (ngx_http_upstream_create_round_robin_peer(r, u->resolved)
533 != NGX_OK) 536 != NGX_OK)
534 { 537 {
535 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 538 ngx_http_upstream_finalize_request(r, u,
539 NGX_HTTP_INTERNAL_SERVER_ERROR);
536 return; 540 return;
537 } 541 }
538 542
539 ngx_http_upstream_connect(r, u); 543 ngx_http_upstream_connect(r, u);
540 544
562 566
563 temp.name = *host; 567 temp.name = *host;
564 568
565 ctx = ngx_resolve_start(clcf->resolver, &temp); 569 ctx = ngx_resolve_start(clcf->resolver, &temp);
566 if (ctx == NULL) { 570 if (ctx == NULL) {
567 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 571 ngx_http_upstream_finalize_request(r, u,
572 NGX_HTTP_INTERNAL_SERVER_ERROR);
568 return; 573 return;
569 } 574 }
570 575
571 if (ctx == NGX_NO_RESOLVER) { 576 if (ctx == NGX_NO_RESOLVER) {
572 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 577 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
573 "no resolver defined to resolve %V", host); 578 "no resolver defined to resolve %V", host);
574 579
575 ngx_http_finalize_request(r, NGX_HTTP_BAD_GATEWAY); 580 ngx_http_upstream_finalize_request(r, u, NGX_HTTP_BAD_GATEWAY);
576 return; 581 return;
577 } 582 }
578 583
579 ctx->name = *host; 584 ctx->name = *host;
580 ctx->type = NGX_RESOLVE_A; 585 ctx->type = NGX_RESOLVE_A;
584 589
585 u->resolved->ctx = ctx; 590 u->resolved->ctx = ctx;
586 591
587 if (ngx_resolve_name(ctx) != NGX_OK) { 592 if (ngx_resolve_name(ctx) != NGX_OK) {
588 u->resolved->ctx = NULL; 593 u->resolved->ctx = NULL;
589 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 594 ngx_http_upstream_finalize_request(r, u,
595 NGX_HTTP_INTERNAL_SERVER_ERROR);
590 return; 596 return;
591 } 597 }
592 598
593 return; 599 return;
594 } 600 }
595 601
596 found: 602 found:
597 603
598 if (uscf->peer.init(r, uscf) != NGX_OK) { 604 if (uscf->peer.init(r, uscf) != NGX_OK) {
599 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 605 ngx_http_upstream_finalize_request(r, u,
606 NGX_HTTP_INTERNAL_SERVER_ERROR);
600 return; 607 return;
601 } 608 }
602 609
603 ngx_http_upstream_connect(r, u); 610 ngx_http_upstream_connect(r, u);
604 } 611 }
743 ngx_int_t rc; 750 ngx_int_t rc;
744 ngx_http_cache_t *c; 751 ngx_http_cache_t *c;
745 752
746 r->cached = 1; 753 r->cached = 1;
747 c = r->cache; 754 c = r->cache;
755
756 if (c->header_start == c->body_start) {
757 r->http_version = NGX_HTTP_VERSION_9;
758 return ngx_http_cache_send(r);
759 }
748 760
749 /* TODO: cache stack */ 761 /* TODO: cache stack */
750 762
751 u->buffer = *c->buf; 763 u->buffer = *c->buf;
752 u->buffer.pos += c->header_start; 764 u->buffer.pos += c->header_start;
787 799
788 static void 800 static void
789 ngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx) 801 ngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx)
790 { 802 {
791 ngx_http_request_t *r; 803 ngx_http_request_t *r;
804 ngx_http_upstream_t *u;
792 ngx_http_upstream_resolved_t *ur; 805 ngx_http_upstream_resolved_t *ur;
793 806
794 r = ctx->data; 807 r = ctx->data;
795 808
796 r->upstream->resolved->ctx = NULL; 809 u = r->upstream;
810 ur = u->resolved;
797 811
798 if (ctx->state) { 812 if (ctx->state) {
799 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 813 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
800 "%V could not be resolved (%i: %s)", 814 "%V could not be resolved (%i: %s)",
801 &ctx->name, ctx->state, 815 &ctx->name, ctx->state,
802 ngx_resolver_strerror(ctx->state)); 816 ngx_resolver_strerror(ctx->state));
803 817
804 ngx_resolve_name_done(ctx); 818 ngx_http_upstream_finalize_request(r, u, NGX_HTTP_BAD_GATEWAY);
805 ngx_http_finalize_request(r, NGX_HTTP_BAD_GATEWAY); 819 return;
806 return; 820 }
807 } 821
808
809 ur = r->upstream->resolved;
810 ur->naddrs = ctx->naddrs; 822 ur->naddrs = ctx->naddrs;
811 ur->addrs = ctx->addrs; 823 ur->addrs = ctx->addrs;
812 824
813 #if (NGX_DEBUG) 825 #if (NGX_DEBUG)
814 { 826 {
825 } 837 }
826 } 838 }
827 #endif 839 #endif
828 840
829 if (ngx_http_upstream_create_round_robin_peer(r, ur) != NGX_OK) { 841 if (ngx_http_upstream_create_round_robin_peer(r, ur) != NGX_OK) {
830 ngx_resolve_name_done(ctx); 842 ngx_http_upstream_finalize_request(r, u,
831 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 843 NGX_HTTP_INTERNAL_SERVER_ERROR);
832 return; 844 return;
833 } 845 }
834 846
835 ngx_resolve_name_done(ctx); 847 ngx_resolve_name_done(ctx);
836 848 ur->ctx = NULL;
837 ngx_http_upstream_connect(r, r->upstream); 849
850 ngx_http_upstream_connect(r, u);
838 } 851 }
839 852
840 853
841 static void 854 static void
842 ngx_http_upstream_handler(ngx_event_t *ev) 855 ngx_http_upstream_handler(ngx_event_t *ev)
916 if (!u->cacheable) { 929 if (!u->cacheable) {
917 ngx_http_upstream_finalize_request(r, u, 930 ngx_http_upstream_finalize_request(r, u,
918 NGX_HTTP_CLIENT_CLOSED_REQUEST); 931 NGX_HTTP_CLIENT_CLOSED_REQUEST);
919 } 932 }
920 933
921 return;
922 }
923
924 if (u->peer.connection == NULL) {
925 return; 934 return;
926 } 935 }
927 936
928 #if (NGX_HAVE_KQUEUE) 937 #if (NGX_HAVE_KQUEUE)
929 938
954 "prematurely connection"); 963 "prematurely connection");
955 964
956 if (u->peer.connection == NULL) { 965 if (u->peer.connection == NULL) {
957 ngx_http_upstream_finalize_request(r, u, 966 ngx_http_upstream_finalize_request(r, u,
958 NGX_HTTP_CLIENT_CLOSED_REQUEST); 967 NGX_HTTP_CLIENT_CLOSED_REQUEST);
959 return;
960 } 968 }
961 969
962 return; 970 return;
963 } 971 }
964 972
1017 "client closed prematurely connection"); 1025 "client closed prematurely connection");
1018 1026
1019 if (u->peer.connection == NULL) { 1027 if (u->peer.connection == NULL) {
1020 ngx_http_upstream_finalize_request(r, u, 1028 ngx_http_upstream_finalize_request(r, u,
1021 NGX_HTTP_CLIENT_CLOSED_REQUEST); 1029 NGX_HTTP_CLIENT_CLOSED_REQUEST);
1022 return;
1023 } 1030 }
1024 } 1031 }
1025 1032
1026 1033
1027 static void 1034 static void
1540 return; 1547 return;
1541 } 1548 }
1542 1549
1543 /* rc == NGX_OK */ 1550 /* rc == NGX_OK */
1544 1551
1545 if (u->headers_in.status_n >= NGX_HTTP_BAD_REQUEST) { 1552 if (u->headers_in.status_n > NGX_HTTP_SPECIAL_RESPONSE) {
1546 1553
1547 if (r->subrequest_in_memory) { 1554 if (r->subrequest_in_memory) {
1548 u->buffer.last = u->buffer.pos; 1555 u->buffer.last = u->buffer.pos;
1549 } 1556 }
1550 1557
1795 } 1802 }
1796 1803
1797 uri = &u->headers_in.x_accel_redirect->value; 1804 uri = &u->headers_in.x_accel_redirect->value;
1798 args.len = 0; 1805 args.len = 0;
1799 args.data = NULL; 1806 args.data = NULL;
1800 flags = 0; 1807 flags = NGX_HTTP_LOG_UNSAFE;
1801 1808
1802 if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) { 1809 if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) {
1803 ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND); 1810 ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);
1804 return NGX_DONE; 1811 return NGX_DONE;
1805 } 1812 }
2096 2103
2097 if (valid) { 2104 if (valid) {
2098 r->cache->last_modified = r->headers_out.last_modified_time; 2105 r->cache->last_modified = r->headers_out.last_modified_time;
2099 r->cache->date = now; 2106 r->cache->date = now;
2100 r->cache->body_start = (u_short) (u->buffer.pos - u->buffer.start); 2107 r->cache->body_start = (u_short) (u->buffer.pos - u->buffer.start);
2101
2102 if (r->headers_out.content_length_n != -1) {
2103 r->cache->length = r->cache->body_start
2104 + r->headers_out.content_length_n;
2105 }
2106 2108
2107 ngx_http_file_cache_set_header(r, u->buffer.start); 2109 ngx_http_file_cache_set_header(r, u->buffer.start);
2108 2110
2109 } else { 2111 } else {
2110 u->cacheable = 0; 2112 u->cacheable = 0;
2835 2837
2836 u = r->upstream; 2838 u = r->upstream;
2837 2839
2838 if (u->resolved && u->resolved->ctx) { 2840 if (u->resolved && u->resolved->ctx) {
2839 ngx_resolve_name_done(u->resolved->ctx); 2841 ngx_resolve_name_done(u->resolved->ctx);
2842 u->resolved->ctx = NULL;
2840 } 2843 }
2841 2844
2842 ngx_http_upstream_finalize_request(r, u, NGX_DONE); 2845 ngx_http_upstream_finalize_request(r, u, NGX_DONE);
2843 } 2846 }
2844 2847
2852 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 2855 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2853 "finalize http upstream request: %i", rc); 2856 "finalize http upstream request: %i", rc);
2854 2857
2855 if (u->cleanup) { 2858 if (u->cleanup) {
2856 *u->cleanup = NULL; 2859 *u->cleanup = NULL;
2860 u->cleanup = NULL;
2861 }
2862
2863 if (u->resolved && u->resolved->ctx) {
2864 ngx_resolve_name_done(u->resolved->ctx);
2865 u->resolved->ctx = NULL;
2857 } 2866 }
2858 2867
2859 if (u->state && u->state->response_sec) { 2868 if (u->state && u->state->response_sec) {
2860 tp = ngx_timeofday(); 2869 tp = ngx_timeofday();
2861 u->state->response_sec = tp->sec - u->state->response_sec; 2870 u->state->response_sec = tp->sec - u->state->response_sec;
3033 } 3042 }
3034 3043
3035 n = 0; 3044 n = 0;
3036 3045
3037 for (p += 8; p < last; p++) { 3046 for (p += 8; p < last; p++) {
3038 if (*p == ';' || *p == ' ') { 3047 if (*p == ',' || *p == ';' || *p == ' ') {
3039 break; 3048 break;
3040 } 3049 }
3041 3050
3042 if (*p >= '0' && *p <= '9') { 3051 if (*p >= '0' && *p <= '9') {
3043 n = n * 10 + *p - '0'; 3052 n = n * 10 + *p - '0';
4194 4203
4195 return uscf; 4204 return uscf;
4196 } 4205 }
4197 4206
4198 4207
4208 char *
4209 ngx_http_upstream_bind_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
4210 void *conf)
4211 {
4212 char *p = conf;
4213
4214 ngx_int_t rc;
4215 ngx_str_t *value;
4216 ngx_addr_t **paddr;
4217
4218 paddr = (ngx_addr_t **) (p + cmd->offset);
4219
4220 *paddr = ngx_palloc(cf->pool, sizeof(ngx_addr_t));
4221 if (*paddr == NULL) {
4222 return NGX_CONF_ERROR;
4223 }
4224
4225 value = cf->args->elts;
4226
4227 rc = ngx_parse_addr(cf->pool, *paddr, value[1].data, value[1].len);
4228
4229 switch (rc) {
4230 case NGX_OK:
4231 (*paddr)->name = value[1];
4232 return NGX_CONF_OK;
4233
4234 case NGX_DECLINED:
4235 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4236 "invalid address \"%V\"", &value[1]);
4237 default:
4238 return NGX_CONF_ERROR;
4239 }
4240 }
4241
4242
4199 ngx_int_t 4243 ngx_int_t
4200 ngx_http_upstream_hide_headers_hash(ngx_conf_t *cf, 4244 ngx_http_upstream_hide_headers_hash(ngx_conf_t *cf,
4201 ngx_http_upstream_conf_t *conf, ngx_http_upstream_conf_t *prev, 4245 ngx_http_upstream_conf_t *conf, ngx_http_upstream_conf_t *prev,
4202 ngx_str_t *default_hide_headers, ngx_hash_init_t *hash) 4246 ngx_str_t *default_hide_headers, ngx_hash_init_t *hash)
4203 { 4247 {