comparison src/http/modules/proxy/ngx_http_proxy_upstream.c @ 188:0061d1f0908d

nginx-0.0.1-2003-11-18-11:04:34 import
author Igor Sysoev <igor@sysoev.ru>
date Tue, 18 Nov 2003 08:04:34 +0000
parents 5a9bbe99008b
children c966c09be66b
comparison
equal deleted inserted replaced
187:5a9bbe99008b 188:0061d1f0908d
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 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 *);
21 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);
22 static void ngx_http_proxy_check_broken_connection(ngx_event_t *wev);
22 static void ngx_http_proxy_process_body(ngx_event_t *ev); 23 static void ngx_http_proxy_process_body(ngx_event_t *ev);
23 static void ngx_http_proxy_next_upstream(ngx_http_proxy_ctx_t *p, int ft_type); 24 static void ngx_http_proxy_next_upstream(ngx_http_proxy_ctx_t *p, int ft_type);
24 25
25 26
26 static ngx_str_t http_methods[] = { 27 static ngx_str_t http_methods[] = {
222 223
223 if (r->connection->read->timer_set) { 224 if (r->connection->read->timer_set) {
224 ngx_del_timer(r->connection->read); 225 ngx_del_timer(r->connection->read);
225 } 226 }
226 227
228 if ((ngx_event_flags & (NGX_USE_CLEAR_EVENT|NGX_HAVE_KQUEUE_EVENT))
229 && !r->connection->write->active)
230 {
231 /* kqueue allows to detect when client closes prematurely connection */
232
233 r->connection->write->event_handler =
234 ngx_http_proxy_check_broken_connection;
235
236 if (ngx_add_event(r->connection->write, NGX_WRITE_EVENT,
237 NGX_CLEAR_EVENT) == NGX_ERROR)
238 {
239 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
240 return;
241 }
242 }
243
244
227 if (!(cl = ngx_http_proxy_create_request(p))) { 245 if (!(cl = ngx_http_proxy_create_request(p))) {
228 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 246 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
229 return; 247 return;
230 } 248 }
231 249
422 return; 440 return;
423 } 441 }
424 442
425 /* rc == NGX_OK */ 443 /* rc == NGX_OK */
426 444
427 #if 1 /* test only */ 445 #if 1 /* test only, see below about "post aio operation" */
428 446
429 if (c->read->ready) { 447 if (c->read->ready) {
430 /* post aio operation */ 448 /* post aio operation */
431 ngx_http_proxy_process_upstream_status_line(c->read); 449 ngx_http_proxy_process_upstream_status_line(c->read);
432 return; 450 return;
544 562
545 if (wev->timedout) { 563 if (wev->timedout) {
546 p->action = "sending request to upstream"; 564 p->action = "sending request to upstream";
547 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT); 565 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT);
548 return; 566 return;
567 }
568
569 if (p->request->connection->write->eof) {
570 ngx_http_proxy_close_connection(p);
571 ngx_http_close_connection(p->request->connection);
549 } 572 }
550 573
551 ngx_http_proxy_send_request(p); 574 ngx_http_proxy_send_request(p);
552 } 575 }
553 576
937 if (!(ep->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)))) { 960 if (!(ep->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)))) {
938 ngx_http_proxy_finalize_request(p, 0); 961 ngx_http_proxy_finalize_request(p, 0);
939 return; 962 return;
940 } 963 }
941 964
965 ep->cachable = p->cachable;
966
942 ep->temp_file->file.fd = NGX_INVALID_FILE; 967 ep->temp_file->file.fd = NGX_INVALID_FILE;
943 ep->temp_file->file.log = r->connection->log; 968 ep->temp_file->file.log = r->connection->log;
944 ep->temp_file->path = p->lcf->temp_path; 969 ep->temp_file->path = p->lcf->temp_path;
945 ep->temp_file->pool = r->pool; 970 ep->temp_file->pool = r->pool;
946 ep->temp_file->warn = "an upstream response is buffered " 971 if (!p->cachable) {
947 "to a temporary file"; 972 ep->temp_file->warn = "an upstream response is buffered "
973 "to a temporary file";
974 }
948 975
949 ep->max_temp_file_size = p->lcf->max_temp_file_size; 976 ep->max_temp_file_size = p->lcf->max_temp_file_size;
950 ep->temp_file_write_size = p->lcf->temp_file_write_size; 977 ep->temp_file_write_size = p->lcf->temp_file_write_size;
951 978
952 ep->preread_hunks = ngx_alloc_chain_link(r->pool); 979 ep->preread_hunks = ngx_alloc_chain_link(r->pool);
981 * event_pipe would do p->header_in->last += ep->preread_size 1008 * event_pipe would do p->header_in->last += ep->preread_size
982 * as though these bytes were read. 1009 * as though these bytes were read.
983 */ 1010 */
984 p->header_in->last = p->header_in->pos; 1011 p->header_in->last = p->header_in->pos;
985 1012
986 ep->cachable = p->cachable;
987
988 if (p->lcf->cyclic_temp_file) { 1013 if (p->lcf->cyclic_temp_file) {
989 1014
990 /* 1015 /*
991 * we need to disable the use of sendfile() if we use cyclic temp file 1016 * we need to disable the use of sendfile() if we use cyclic temp file
992 * because the writing a new data can interfere with sendfile() 1017 * because the writing a new data can interfere with sendfile()
1012 r->connection->write->event_handler = ngx_http_proxy_process_body; 1037 r->connection->write->event_handler = ngx_http_proxy_process_body;
1013 1038
1014 ngx_http_proxy_process_body(p->upstream->peer.connection->read); 1039 ngx_http_proxy_process_body(p->upstream->peer.connection->read);
1015 1040
1016 return; 1041 return;
1042 }
1043
1044
1045 static void ngx_http_proxy_check_broken_connection(ngx_event_t *wev)
1046 {
1047 ngx_connection_t *c;
1048 ngx_http_request_t *r;
1049 ngx_http_proxy_ctx_t *p;
1050
1051 ngx_log_debug(wev->log, "http proxy check client");
1052
1053 c = wev->data;
1054 r = c->data;
1055 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
1056
1057 #if (HAVE_KQUEUE)
1058 if (wev->kq_eof) {
1059 wev->eof = 1;
1060
1061 if (wev->kq_errno) {
1062 wev->error = 1;
1063 }
1064
1065 if (!p->cachable && p->upstream->peer.connection) {
1066 ngx_log_error(NGX_LOG_INFO, wev->log, wev->kq_errno,
1067 "client closed prematurely connection, "
1068 "so upstream connection is closed too");
1069 ngx_http_proxy_close_connection(p);
1070
1071 } else {
1072 ngx_log_error(NGX_LOG_INFO, wev->log, wev->kq_errno,
1073 "client closed prematurely connection");
1074 }
1075
1076 if (p->upstream->peer.connection == NULL) {
1077 ngx_http_close_connection(c);
1078 }
1079 }
1080 #endif
1017 } 1081 }
1018 1082
1019 1083
1020 static void ngx_http_proxy_process_body(ngx_event_t *ev) 1084 static void ngx_http_proxy_process_body(ngx_event_t *ev)
1021 { 1085 {
1095 if (!p->cachable && p->upstream->peer.connection) { 1159 if (!p->cachable && p->upstream->peer.connection) {
1096 ngx_http_proxy_close_connection(p); 1160 ngx_http_proxy_close_connection(p);
1097 } 1161 }
1098 1162
1099 if (p->upstream->peer.connection == NULL) { 1163 if (p->upstream->peer.connection == NULL) {
1100 ngx_http_close_connection(c); 1164 ngx_http_close_connection(r->connection);
1101 } 1165 }
1102 } 1166 }
1103
1104 return;
1105 } 1167 }
1106 1168
1107 1169
1108 static void ngx_http_proxy_next_upstream(ngx_http_proxy_ctx_t *p, int ft_type) 1170 static void ngx_http_proxy_next_upstream(ngx_http_proxy_ctx_t *p, int ft_type)
1109 { 1171 {