comparison src/http/modules/proxy/ngx_http_proxy_handler.c @ 162:96993d4d5067

nginx-0.0.1-2003-10-28-00:01:00 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 27 Oct 2003 21:01:00 +0000
parents 88abd07d9f62
children fb61ba77beba
comparison
equal deleted inserted replaced
161:88abd07d9f62 162:96993d4d5067
173 173
174 174
175 175
176 static int ngx_http_proxy_handler(ngx_http_request_t *r) 176 static int ngx_http_proxy_handler(ngx_http_request_t *r)
177 { 177 {
178 int rc;
179 ngx_http_proxy_ctx_t *p; 178 ngx_http_proxy_ctx_t *p;
180 179
181 ngx_http_create_ctx(r, p, ngx_http_proxy_module, 180 ngx_http_create_ctx(r, p, ngx_http_proxy_module,
182 sizeof(ngx_http_proxy_ctx_t), 181 sizeof(ngx_http_proxy_ctx_t),
183 NGX_HTTP_INTERNAL_SERVER_ERROR); 182 NGX_HTTP_INTERNAL_SERVER_ERROR);
206 /* STUB */ r->temp_file->persistent = 1; 205 /* STUB */ r->temp_file->persistent = 1;
207 206
208 r->request_body_handler = ngx_http_proxy_init_request; 207 r->request_body_handler = ngx_http_proxy_init_request;
209 r->data = p; 208 r->data = p;
210 209
211 rc = ngx_http_read_client_request_body(r, p->lcf->request_buffer_size); 210 ngx_http_read_client_request_body(r, p->lcf->request_buffer_size);
212 211
213 if (rc != NGX_OK) { 212 return NGX_DONE;
214 return rc;
215 }
216 } 213 }
217 214
218 ngx_http_proxy_init_request(p); 215 ngx_http_proxy_init_request(p);
219 216
220 return NGX_DONE; 217 return NGX_DONE;
225 { 222 {
226 ngx_http_proxy_ctx_t *p = data; 223 ngx_http_proxy_ctx_t *p = data;
227 224
228 ngx_chain_t *cl; 225 ngx_chain_t *cl;
229 ngx_http_request_t *r; 226 ngx_http_request_t *r;
230 ngx_output_chain_ctx_t *ctx; 227 ngx_output_chain_ctx_t *out_ctx;
228 ngx_chain_write_ctx_t *write_ctx;
231 229
232 230
233 r = p->request; 231 r = p->request;
234 232
233 ngx_log_debug(r->connection->log, "timer_set: %d" _
234 r->connection->read->timer_set);
235
236 if (r->connection->read->timer_set) {
237 ngx_del_timer(r->connection->read);
238 }
239
240 ngx_is_null(cl, ngx_http_proxy_create_request(p)) {
241 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
242 return;
243 }
244
245 #if 0
235 cl = ngx_http_proxy_create_request(p); 246 cl = ngx_http_proxy_create_request(p);
236 if (cl == NULL) { 247 if (cl == NULL) {
237 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 248 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
238 return; 249 return;
239 } 250 }
251 #endif
240 252
241 if (r->request_hunks) { 253 if (r->request_hunks) {
242 cl->next = r->request_hunks; 254 cl->next = r->request_hunks;
243 } 255 }
244 256
249 p->saved_handler = r->connection->log->handler; 261 p->saved_handler = r->connection->log->handler;
250 r->connection->log->data = p; 262 r->connection->log->data = p;
251 r->connection->log->handler = ngx_http_proxy_log_error; 263 r->connection->log->handler = ngx_http_proxy_log_error;
252 p->action = "connecting to upstream"; 264 p->action = "connecting to upstream";
253 265
254 ctx = ngx_pcalloc(r->pool, sizeof(ngx_output_chain_ctx_t)); 266 out_ctx = ngx_pcalloc(r->pool, sizeof(ngx_output_chain_ctx_t));
255 if (ctx == NULL) { 267 if (out_ctx == NULL) {
256 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); 268 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
257 } 269 }
258 270
259 p->output_chain_ctx = ctx; 271 p->output_chain_ctx = out_ctx;
260 272
261 if (r->request_body_hunk) { 273 if (r->request_body_hunk) {
262 ctx->free = ngx_alloc_chain_link(r->pool); 274 out_ctx->free = ngx_alloc_chain_link(r->pool);
263 if (ctx->free == NULL) { 275 if (out_ctx->free == NULL) {
264 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); 276 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
265 } 277 }
266 ctx->free->hunk = r->request_body_hunk; 278 out_ctx->free->hunk = r->request_body_hunk;
267 ctx->free->next = NULL; 279 out_ctx->free->next = NULL;
268 } 280 }
269 281
270 ctx->sendfile = r->sendfile; 282 out_ctx->sendfile = r->sendfile;
271 ctx->copy_chain = 1; 283 out_ctx->pool = r->pool;
272 ctx->pool = r->pool; 284 out_ctx->bufs.num = 1;
273 ctx->bufs.num = 1; 285 out_ctx->tag = (ngx_hunk_tag_t) &ngx_http_proxy_module;
274 ctx->tag = (ngx_hunk_tag_t) &ngx_http_proxy_module; 286 out_ctx->output_filter = (ngx_output_chain_filter_pt) ngx_chain_write;
275 ctx->output_filter = (ngx_output_chain_filter_pt) ngx_write_chain; 287
288 write_ctx = ngx_pcalloc(r->pool, sizeof(ngx_chain_write_ctx_t));
289 if (write_ctx == NULL) {
290 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
291 }
292
293 out_ctx->output_ctx = write_ctx;
294 write_ctx->pool = r->pool;
295 write_ctx->last = &write_ctx->out;
276 296
277 ngx_http_proxy_send_request(p); 297 ngx_http_proxy_send_request(p);
278 } 298 }
279 299
280 300
416 static void ngx_http_proxy_send_request(ngx_http_proxy_ctx_t *p) 436 static void ngx_http_proxy_send_request(ngx_http_proxy_ctx_t *p)
417 { 437 {
418 int rc; 438 int rc;
419 ngx_chain_t *cl; 439 ngx_chain_t *cl;
420 ngx_connection_t *c; 440 ngx_connection_t *c;
441 ngx_chain_write_ctx_t *ctx;
421 442
422 c = p->upstream.connection; 443 c = p->upstream.connection;
423 444
424 for ( ;; ) { 445 for ( ;; ) {
425 446
426 if (c) { 447 if (c) {
427 p->output_chain_ctx->output_ctx = c; 448 p->action = "sending request to upstream";
449 ctx = p->output_chain_ctx->output_ctx;
450 ctx->connection = c;
428 rc = ngx_output_chain(p->output_chain_ctx, 451 rc = ngx_output_chain(p->output_chain_ctx,
429 p->request->request_hunks); 452 !p->request_sent ? p->request->request_hunks:
453 NULL);
430 454
431 if (rc != NGX_ERROR) { 455 if (rc != NGX_ERROR) {
432 p->request_sent = 1; 456 p->request_sent = 1;
433 457
434 if (c->write->timer_set) { 458 if (c->write->timer_set) {
436 } 460 }
437 461
438 if (rc == NGX_AGAIN) { 462 if (rc == NGX_AGAIN) {
439 ngx_add_timer(c->write, p->lcf->send_timeout); 463 ngx_add_timer(c->write, p->lcf->send_timeout);
440 464
465 if (ngx_handle_write_event(c->write, /* STUB: lowat */ 0)
466 == NGX_ERROR)
467 {
468 ngx_http_proxy_finalize_request(p,
469 NGX_HTTP_INTERNAL_SERVER_ERROR);
470 return;
471 }
472
441 } else { 473 } else {
442 /* TODO: del event */ 474
475 /* rc == NGX_OK */
476
477 if (ngx_handle_level_write_event(c->write) == NGX_ERROR) {
478 ngx_http_proxy_finalize_request(p,
479 NGX_HTTP_INTERNAL_SERVER_ERROR);
480 return;
481 }
443 482
444 if (c->tcp_nopush) { 483 if (c->tcp_nopush) {
445 if (ngx_tcp_push(c->fd) == NGX_ERROR) { 484 if (ngx_tcp_push(c->fd) == NGX_ERROR) {
446 ngx_log_error(NGX_LOG_CRIT, c->log, 485 ngx_log_error(NGX_LOG_CRIT, c->log,
447 ngx_socket_errno, 486 ngx_socket_errno,
460 ngx_event_connect_peer_failed(&p->upstream); 499 ngx_event_connect_peer_failed(&p->upstream);
461 ngx_http_proxy_close_connection(c); 500 ngx_http_proxy_close_connection(c);
462 } 501 }
463 502
464 for ( ;; ) { 503 for ( ;; ) {
504 p->action = "connecting to upstream";
505
465 rc = ngx_event_connect_peer(&p->upstream); 506 rc = ngx_event_connect_peer(&p->upstream);
466 507
467 if (rc == NGX_ERROR) { 508 if (rc == NGX_ERROR) {
468 ngx_http_proxy_finalize_request(p, 509 ngx_http_proxy_finalize_request(p,
469 NGX_HTTP_INTERNAL_SERVER_ERROR); 510 NGX_HTTP_INTERNAL_SERVER_ERROR);
1243 return; 1284 return;
1244 } 1285 }
1245 1286
1246 static void ngx_http_proxy_finalize_request(ngx_http_proxy_ctx_t *p, int rc) 1287 static void ngx_http_proxy_finalize_request(ngx_http_proxy_ctx_t *p, int rc)
1247 { 1288 {
1289 ngx_log_debug(p->request->connection->log,
1290 "finalize http proxy request");
1291
1248 if (p->upstream.connection) { 1292 if (p->upstream.connection) {
1249 ngx_http_proxy_close_connection(p->upstream.connection); 1293 ngx_http_proxy_close_connection(p->upstream.connection);
1250 p->upstream.connection = NULL; 1294 p->upstream.connection = NULL;
1251 } 1295 }
1252 1296