comparison src/http/modules/proxy/ngx_http_proxy_handler.c @ 136:da00cde00e8a

nginx-0.0.1-2003-10-02-09:39:37 import
author Igor Sysoev <igor@sysoev.ru>
date Thu, 02 Oct 2003 05:39:37 +0000
parents e29909bd9b8a
children 2a615b036870
comparison
equal deleted inserted replaced
135:e29909bd9b8a 136:da00cde00e8a
7 #include <ngx_http_proxy_handler.h> 7 #include <ngx_http_proxy_handler.h>
8 8
9 9
10 10
11 static void ngx_http_proxy_send_request(ngx_event_t *wev); 11 static void ngx_http_proxy_send_request(ngx_event_t *wev);
12 static void ngx_http_proxy_close_connection(ngx_connection_t *c);
13 static ngx_chain_t *ngx_http_proxy_copy_request_hunks(ngx_http_proxy_ctx_t *p);
12 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf); 14 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
13 15
14 16
15 static ngx_command_t ngx_http_proxy_commands[] = { 17 static ngx_command_t ngx_http_proxy_commands[] = {
16 ngx_null_command 18 ngx_null_command
64 #endif 66 #endif
65 67
66 p->action = "connecting to upstream"; 68 p->action = "connecting to upstream";
67 p->request = r; 69 p->request = r;
68 p->upstream.peers = p->lcf->peers; 70 p->upstream.peers = p->lcf->peers;
69 71 p->upstream.tries = p->lcf->peers->number;
70 /* TODO: change log->data, how to restore log->data ? */ 72
73 /* TODO: log->data would be changed, how to restore log->data ? */
71 p->upstream.log = r->connection->log; 74 p->upstream.log = r->connection->log;
72 75
73 do { 76 for ( ;; ) {
74 rc = ngx_event_connect_peer(&p->upstream); 77 rc = ngx_event_connect_peer(&p->upstream);
75 78
76 if (rc == NGX_ERROR) { 79 if (rc == NGX_ERROR) {
77 return NGX_HTTP_INTERNAL_SERVER_ERROR; 80 return NGX_HTTP_INTERNAL_SERVER_ERROR;
81 }
82
83 if (rc == NGX_CONNECT_ERROR) {
84 ngx_event_connect_peer_failed(&p->upstream);
85
86 if (p->upstream.tries == 0) {
87 return NGX_HTTP_BAD_GATEWAY;
88 }
89 }
90
91 p->upstream.connection->data = p;
92 p->upstream.connection->write->event_handler =
93 ngx_http_proxy_send_request;
94 p->upstream.connection->read->event_handler = /* STUB */ NULL;
95
96 if (p->upstream.tries > 1) {
97 ngx_test_null(p->work_request_hunks,
98 ngx_http_proxy_copy_request_hunks(p),
99 NGX_HTTP_INTERNAL_SERVER_ERROR);
100 } else {
101 p->work_request_hunks = p->request_hunks;
78 } 102 }
79 103
80 if (rc == NGX_OK) { 104 if (rc == NGX_OK) {
81 ngx_http_proxy_send_request(p->upstream.connection->write); 105 ngx_http_proxy_send_request(p->upstream.connection->write);
82 return NGX_OK; 106 return NGX_OK;
83 } 107 }
84 108
85 if (rc == NGX_AGAIN) { 109 /* rc == NGX_AGAIN */
86 /* TODO */ return NGX_OK; 110
87 } 111 /* timer */
88 112
89 /* rc == NGX_CONNECT_FAILED */ 113 /* TODO */ return NGX_OK;
90 114 }
91 ngx_event_connect_peer_failed(&p->upstream);
92
93 } while (p->upstream.tries);
94
95 return NGX_HTTP_BAD_GATEWAY;
96 } 115 }
97 116
98 117
99 static void ngx_http_proxy_send_request(ngx_event_t *wev) 118 static void ngx_http_proxy_send_request(ngx_event_t *wev)
100 { 119 {
110 chain = ngx_write_chain(c, p->request_hunks); 129 chain = ngx_write_chain(c, p->request_hunks);
111 130
112 if (chain == (ngx_chain_t *) -1) { 131 if (chain == (ngx_chain_t *) -1) {
113 ngx_http_proxy_close_connection(c); 132 ngx_http_proxy_close_connection(c);
114 133
115 do { 134 for ( ;; ) {
116 rc = ngx_event_connect_peer(&p->upstream); 135 rc = ngx_event_connect_peer(&p->upstream);
117
118 if (rc == NGX_OK) {
119
120 /* copy chain and hunks p->request_hunks
121 from p->initial_request_hunks */
122
123 p->request_hunks = NULL;
124 if (ngx_chain_add_copy(r->pool, p->request_hunks,
125 p->initial_request_hunks) == NGX_ERROR)
126 {
127 return NGX_HTTP_INTERNAL_SERVER_ERROR;
128 }
129
130 for (ce = p->request_hunks; ce; ce = ce->next) {
131 ce->hunk->pos = ce->hunk->start;
132 }
133
134
135 c = p->connection;
136 wev = c->write;
137
138 break;
139 }
140 136
141 if (rc == NGX_ERROR) { 137 if (rc == NGX_ERROR) {
142 ngx_http_finalize_request(p->request, 138 ngx_http_finalize_request(p->request,
143 NGX_HTTP_INTERNAL_SERVER_ERROR); 139 NGX_HTTP_INTERNAL_SERVER_ERROR);
144 return; 140 return;
145 } 141 }
146 142
147 if (rc == NGX_AGAIN) { 143 if (rc == NGX_CONNECT_ERROR) {
148 return; 144 ngx_event_connect_peer_failed(&p->upstream);
149 } 145
150 146 if (p->upstream.tries == 0) {
151 /* rc == NGX_CONNECT_FAILED */ 147 return;
152 148 }
153 ngx_event_connect_peer_failed(&p->upstream); 149 }
154 150
155 } while (p->upstream.tries); 151 if (p->upstream.tries > 1) {
156 152 ngx_test_null(p->work_request_hunks,
157 return; 153 ngx_http_proxy_copy_request_hunks(p),
154 /* void */);
155 } else {
156 p->work_request_hunks = p->request_hunks;
157 }
158
159 if (rc == NGX_OK) {
160 c = p->connection;
161 wev = c->write;
162
163 break;
164 }
165
166 /* rc == NGX_AGAIN */
167 return;
168
169 }
158 170
159 } else { 171 } else {
160 p->request_hunks = chain; 172 p->work_request_hunks = chain;
161 173
162 ngx_del_timer(wev); 174 ngx_del_timer(wev);
163 175
164 if (chain) { 176 if (chain) {
165 ngx_add_timer(wev, p->lcf->send_timeout); 177 ngx_add_timer(wev, p->lcf->send_timeout);
171 } 183 }
172 184
173 return; 185 return;
174 } 186 }
175 } 187 }
188 }
189
190 static void ngx_http_proxy_close_connection(ngx_connection_t *c)
191 {
192 return;
193 }
194
195 static ngx_chain_t *ngx_http_proxy_copy_request_hunks(ngx_http_proxy_ctx_t *p)
196 {
197 ngx_chain_t *ce, *te, *fe, **le;
198
199 #if (NGX_SUPPRESS_WARN)
200 le = NULL;
201 #endif
202
203 ngx_test_null(fe, ngx_alloc_chain_entry(p->request->pool), NULL);
204
205 te = fe;
206
207 for (ce = p->request_hunks; ce; ce = ce->next) {
208 te->hunk = ce->hunk;
209 *le = te;
210 le = &te->next;
211 ce->hunk->pos = ce->hunk->start;
212
213 ngx_test_null(te, ngx_alloc_chain_entry(p->request->pool), NULL);
214 }
215
216 *le = NULL;
217
218 return fe;
176 } 219 }
177 220
178 221
179 static size_t ngx_http_proxy_log_error(void *data, char *buf, size_t len) 222 static size_t ngx_http_proxy_log_error(void *data, char *buf, size_t len)
180 { 223 {