comparison src/http/modules/proxy/ngx_http_proxy_upstream.c @ 172:caa57ddf6d77

nginx-0.0.1-2003-11-04-01:20:44 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 03 Nov 2003 22:20:44 +0000
parents aff0e5d32af8
children ea464a6c0581
comparison
equal deleted inserted replaced
171:aff0e5d32af8 172:caa57ddf6d77
15 static void ngx_http_proxy_send_request(ngx_http_proxy_ctx_t *p); 15 static void ngx_http_proxy_send_request(ngx_http_proxy_ctx_t *p);
16 static void ngx_http_proxy_send_request_handler(ngx_event_t *wev); 16 static void ngx_http_proxy_send_request_handler(ngx_event_t *wev);
17 static void ngx_http_proxy_dummy_handler(ngx_event_t *wev); 17 static void ngx_http_proxy_dummy_handler(ngx_event_t *wev);
18 static void ngx_http_proxy_process_upstream_status_line(ngx_event_t *rev); 18 static void ngx_http_proxy_process_upstream_status_line(ngx_event_t *rev);
19 static void ngx_http_proxy_process_upstream_headers(ngx_event_t *rev); 19 static void ngx_http_proxy_process_upstream_headers(ngx_event_t *rev);
20 static void ngx_http_proxy_process_upstream_header(ngx_http_proxy_ctx_t *p);
21 static ssize_t ngx_http_proxy_read_upstream_header(ngx_http_proxy_ctx_t *); 20 static ssize_t ngx_http_proxy_read_upstream_header(ngx_http_proxy_ctx_t *);
22 static void ngx_http_proxy_send_response(ngx_http_proxy_ctx_t *p); 21 static void ngx_http_proxy_send_response(ngx_http_proxy_ctx_t *p);
23 static void ngx_http_proxy_process_body(ngx_event_t *ev); 22 static void ngx_http_proxy_process_body(ngx_event_t *ev);
24 static void ngx_http_proxy_next_upstream(ngx_http_proxy_ctx_t *p, int ft_type); 23 static void ngx_http_proxy_next_upstream(ngx_http_proxy_ctx_t *p, int ft_type);
25 24
57 p->upstream = u; 56 p->upstream = u;
58 57
59 u->peer.peers = p->lcf->peers; 58 u->peer.peers = p->lcf->peers;
60 u->peer.tries = p->lcf->peers->number; 59 u->peer.tries = p->lcf->peers->number;
61 60
62 ngx_init_array(p->states, r->pool, u->peer.tries,
63 sizeof(ngx_http_proxy_state_t),
64 NGX_HTTP_INTERNAL_SERVER_ERROR);
65
66 u->method = r->method; 61 u->method = r->method;
67
68 /* STUB */ p->cachable = p->lcf->cache;
69 62
70 if (r->headers_in.content_length_n > 0) { 63 if (r->headers_in.content_length_n > 0) {
71 if (!(r->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)))) { 64 if (!(r->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)))) {
72 return NGX_HTTP_INTERNAL_SERVER_ERROR; 65 return NGX_HTTP_INTERNAL_SERVER_ERROR;
73 } 66 }
311 304
312 305
313 static void ngx_http_proxy_connect(ngx_http_proxy_ctx_t *p) 306 static void ngx_http_proxy_connect(ngx_http_proxy_ctx_t *p)
314 { 307 {
315 int rc; 308 int rc;
316 ngx_chain_t *cl;
317 ngx_connection_t *c; 309 ngx_connection_t *c;
318 ngx_http_request_t *r; 310 ngx_http_request_t *r;
319 ngx_output_chain_ctx_t *octx; 311 ngx_output_chain_ctx_t *octx;
320 312
321 p->action = "connecting to upstream"; 313 p->action = "connecting to upstream";
324 316
325 if (rc == NGX_ERROR) { 317 if (rc == NGX_ERROR) {
326 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); 318 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
327 return; 319 return;
328 } 320 }
321
322 p->state->peer =
323 &p->upstream->peer.peers->peers[p->upstream->peer.cur_peer].addr_port_text;
329 324
330 if (rc == NGX_CONNECT_ERROR) { 325 if (rc == NGX_CONNECT_ERROR) {
331 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR); 326 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
332 return; 327 return;
333 } 328 }
364 r->request_body_hunk->pos = r->request_body_hunk->start; 359 r->request_body_hunk->pos = r->request_body_hunk->start;
365 r->request_body_hunk->last = r->request_body_hunk->start; 360 r->request_body_hunk->last = r->request_body_hunk->start;
366 } 361 }
367 362
368 p->request_sent = 0; 363 p->request_sent = 0;
364
365 if (!(p->state = ngx_push_array(&p->states))) {
366 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
367 return;
368 }
369 369
370 if (rc == NGX_AGAIN) { 370 if (rc == NGX_AGAIN) {
371 ngx_add_timer(c->write, p->lcf->connect_timeout); 371 ngx_add_timer(c->write, p->lcf->connect_timeout);
372 return; 372 return;
373 } 373 }
729 729
730 /* a whole header has been parsed successfully */ 730 /* a whole header has been parsed successfully */
731 731
732 ngx_log_debug(c->log, "HTTP header done"); 732 ngx_log_debug(c->log, "HTTP header done");
733 733
734 ngx_http_proxy_process_upstream_header(p); 734 /* TODO: hook to process the upstream header */
735
736 if (p->cachable) {
737 p->cachable = ngx_http_proxy_is_cachable(p);
738 }
739
735 ngx_http_proxy_send_response(p); 740 ngx_http_proxy_send_response(p);
736 return; 741 return;
737 742
738 } else if (rc != NGX_AGAIN) { 743 } else if (rc != NGX_AGAIN) {
739 744
757 } 762 }
758 } 763 }
759 } 764 }
760 765
761 766
762 static void ngx_http_proxy_process_upstream_header(ngx_http_proxy_ctx_t *p)
763 {
764 time_t expires;
765
766 expires = ngx_http_parse_time(p->upstream->headers_in.expires->value.data,
767 p->upstream->headers_in.expires->value.len);
768
769 p->cache->ctx.header.expires = expires;
770 }
771
772
773 static ssize_t ngx_http_proxy_read_upstream_header(ngx_http_proxy_ctx_t *p) 767 static ssize_t ngx_http_proxy_read_upstream_header(ngx_http_proxy_ctx_t *p)
774 { 768 {
775 ssize_t n; 769 ssize_t n;
776 ngx_event_t *rev; 770 ngx_event_t *rev;
777 771
812 } 806 }
813 807
814 808
815 static void ngx_http_proxy_send_response(ngx_http_proxy_ctx_t *p) 809 static void ngx_http_proxy_send_response(ngx_http_proxy_ctx_t *p)
816 { 810 {
817 int rc, i; 811 int rc;
818 ngx_table_elt_t *ho, *h;
819 ngx_event_pipe_t *ep; 812 ngx_event_pipe_t *ep;
820 ngx_http_request_t *r; 813 ngx_http_request_t *r;
821 ngx_http_cache_file_t *header; 814 ngx_http_cache_file_t *header;
822 ngx_http_core_loc_conf_t *clcf; 815 ngx_http_core_loc_conf_t *clcf;
823 816
993 return; 986 return;
994 } 987 }
995 } 988 }
996 989
997 if (p->upstream->peer.connection) { 990 if (p->upstream->peer.connection) {
998 if (ep->upstream_done) { 991 if (ep->upstream_done && p->cachable) {
999 if (ngx_http_proxy_update_cache(p) == NGX_ERROR) { 992 if (ngx_http_proxy_update_cache(p) == NGX_ERROR) {
1000 ngx_http_proxy_finalize_request(p, 0); 993 ngx_http_proxy_finalize_request(p, 0);
1001 return; 994 return;
1002 } 995 }
1003 996
1004 } else if (ep->upstream_eof) { 997 } else if (ep->upstream_eof && p->cachable) {
1005 998
1006 /* TODO: check length & update cache */ 999 /* TODO: check length & update cache */
1007 1000
1008 if (ngx_http_proxy_update_cache(p) == NGX_ERROR) { 1001 if (ngx_http_proxy_update_cache(p) == NGX_ERROR) {
1009 ngx_http_proxy_finalize_request(p, 0); 1002 ngx_http_proxy_finalize_request(p, 0);
1084 ngx_http_proxy_close_connection(p->upstream->peer.connection); 1077 ngx_http_proxy_close_connection(p->upstream->peer.connection);
1085 p->upstream->peer.connection = NULL; 1078 p->upstream->peer.connection = NULL;
1086 } 1079 }
1087 1080
1088 if (status) { 1081 if (status) {
1089 if (ngx_http_proxy_log_state(p, status) == NGX_ERROR) { 1082 p->state->status = status;
1090 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
1091 return;
1092 }
1093 1083
1094 if (p->upstream->peer.tries == 0 || !(p->lcf->next_upstream & ft_type)) 1084 if (p->upstream->peer.tries == 0 || !(p->lcf->next_upstream & ft_type))
1095 { 1085 {
1096 if (p->stale && (p->lcf->use_stale & ft_type)) { 1086 if (p->stale && (p->lcf->use_stale & ft_type)) {
1097 ngx_http_proxy_finalize_request(p, 1087 ngx_http_proxy_finalize_request(p,