0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10 #include <ngx_event_connect.h>
|
|
11 #include <ngx_event_pipe.h>
|
|
12 #include <ngx_http.h>
|
|
13 #include <ngx_http_proxy_handler.h>
|
|
14
|
|
15
|
|
16 static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_proxy_ctx_t *p);
|
|
17 static void ngx_http_proxy_init_upstream(void *data);
|
|
18 static void ngx_http_proxy_reinit_upstream(ngx_http_proxy_ctx_t *p);
|
|
19 static void ngx_http_proxy_connect(ngx_http_proxy_ctx_t *p);
|
|
20 static void ngx_http_proxy_send_request(ngx_http_proxy_ctx_t *p);
|
|
21 static void ngx_http_proxy_send_request_handler(ngx_event_t *wev);
|
|
22 static void ngx_http_proxy_dummy_handler(ngx_event_t *wev);
|
|
23 static void ngx_http_proxy_process_upstream_status_line(ngx_event_t *rev);
|
|
24 static void ngx_http_proxy_process_upstream_headers(ngx_event_t *rev);
|
|
25 static ssize_t ngx_http_proxy_read_upstream_header(ngx_http_proxy_ctx_t *);
|
|
26 static void ngx_http_proxy_send_response(ngx_http_proxy_ctx_t *p);
|
|
27 static void ngx_http_proxy_process_body(ngx_event_t *ev);
|
|
28 static void ngx_http_proxy_next_upstream(ngx_http_proxy_ctx_t *p, int ft_type);
|
|
29
|
|
30
|
|
31 static ngx_str_t http_methods[] = {
|
|
32 ngx_string("GET "),
|
|
33 ngx_string("HEAD "),
|
|
34 ngx_string("POST ")
|
|
35 };
|
|
36
|
|
37
|
|
38 static char *upstream_header_errors[] = {
|
|
39 "upstream sent invalid header",
|
|
40 "upstream sent too long header line"
|
|
41 };
|
|
42
|
|
43
|
|
44 static char http_version[] = " HTTP/1.0" CRLF;
|
|
45 static char host_header[] = "Host: ";
|
|
46 static char x_real_ip_header[] = "X-Real-IP: ";
|
|
47 static char x_forwarded_for_header[] = "X-Forwarded-For: ";
|
|
48 static char connection_close_header[] = "Connection: close" CRLF;
|
|
49
|
|
50
|
|
51 int ngx_http_proxy_request_upstream(ngx_http_proxy_ctx_t *p)
|
|
52 {
|
|
53 int rc;
|
|
54 ngx_temp_file_t *tf;
|
|
55 ngx_http_request_t *r;
|
|
56 ngx_http_request_body_t *rb;
|
|
57 ngx_http_proxy_upstream_t *u;
|
|
58
|
|
59 r = p->request;
|
|
60
|
|
61 if (!(u = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_upstream_t)))) {
|
|
62 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
63 }
|
|
64
|
|
65 p->upstream = u;
|
|
66
|
|
67 u->peer.log_error = NGX_ERROR_ERR;
|
|
68 u->peer.peers = p->lcf->peers;
|
|
69 u->peer.tries = p->lcf->peers->number;
|
|
70 #if (NGX_THREADS)
|
|
71 u->peer.lock = &r->connection->lock;
|
|
72 #endif
|
|
73
|
|
74 u->method = r->method;
|
|
75
|
|
76 if (!(rb = ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_t)))) {
|
|
77 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
78 }
|
|
79 r->request_body = rb;
|
|
80
|
|
81 if (r->headers_in.content_length_n <= 0) {
|
|
82 ngx_http_proxy_init_upstream(p);
|
|
83 return NGX_DONE;
|
|
84 }
|
|
85
|
|
86 if (!(tf = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)))) {
|
|
87 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
88 }
|
|
89
|
|
90 tf->file.fd = NGX_INVALID_FILE;
|
|
91 tf->file.log = r->connection->log;
|
|
92 tf->path = p->lcf->temp_path;
|
|
93 tf->pool = r->pool;
|
|
94 tf->warn = "a client request body is buffered to a temporary file";
|
|
95 /* tf->persistent = 0; */
|
|
96
|
|
97 rb->handler = ngx_http_proxy_init_upstream;
|
|
98 rb->data = p;
|
|
99 /* rb->bufs = NULL; */
|
|
100 /* rb->buf = NULL; */
|
|
101 /* rb->rest = 0; */
|
|
102
|
|
103 rb->temp_file = tf;
|
|
104
|
|
105 rc = ngx_http_read_client_request_body(r);
|
|
106
|
|
107 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
|
|
108 return rc;
|
|
109 }
|
|
110
|
|
111 return NGX_DONE;
|
|
112 }
|
|
113
|
|
114
|
|
115 static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_proxy_ctx_t *p)
|
|
116 {
|
|
117 size_t len;
|
2
|
118 ngx_int_t escape;
|
0
|
119 ngx_uint_t i;
|
|
120 ngx_buf_t *b;
|
|
121 ngx_chain_t *chain;
|
|
122 ngx_list_part_t *part;
|
|
123 ngx_table_elt_t *header;
|
|
124 ngx_http_request_t *r;
|
|
125 ngx_http_proxy_upstream_conf_t *uc;
|
|
126
|
|
127 r = p->request;
|
|
128 uc = p->lcf->upstream;
|
|
129
|
|
130 if (p->upstream->method) {
|
|
131 len = http_methods[p->upstream->method - 1].len;
|
|
132
|
|
133 } else {
|
|
134 len = r->method_name.len;
|
|
135 }
|
|
136
|
2
|
137 if (r->quoted_uri) {
|
|
138 escape = 2 * ngx_escape_uri(NULL, r->uri.data + uc->location->len,
|
|
139 r->uri.len - uc->location->len);
|
|
140 } else {
|
|
141 escape = 0;
|
|
142 }
|
|
143
|
0
|
144 len += uc->uri.len
|
2
|
145 + r->uri.len - uc->location->len + escape
|
0
|
146 + 1 + r->args.len /* 1 is for "?" */
|
|
147 + sizeof(http_version) - 1
|
|
148 + sizeof(connection_close_header) - 1
|
|
149 + 2; /* 2 is for "\r\n" at the header end */
|
|
150
|
|
151 if (p->lcf->preserve_host && r->headers_in.host) {
|
|
152 len += sizeof(host_header) - 1
|
|
153 + r->headers_in.host_name_len
|
|
154 + 1 /* 1 is for ":" */
|
|
155 + uc->port_text.len
|
|
156 + 2; /* 2 is for "\r\n" */
|
|
157 } else { /* 2 is for "\r\n" */
|
|
158 len += sizeof(host_header) - 1 + uc->host_header.len + 2;
|
|
159 }
|
|
160
|
|
161
|
|
162 if (p->lcf->set_x_real_ip) { /* 2 is for "\r\n" */
|
|
163 len += sizeof(x_real_ip_header) - 1 + INET_ADDRSTRLEN - 1 + 2;
|
|
164 }
|
|
165
|
|
166
|
|
167 if (p->lcf->add_x_forwarded_for) {
|
|
168 if (r->headers_in.x_forwarded_for) {
|
|
169 len += sizeof(x_forwarded_for_header) - 1
|
|
170 + r->headers_in.x_forwarded_for->value.len
|
|
171 + 2 /* 2 is ofr ", " */
|
|
172 + INET_ADDRSTRLEN - 1
|
|
173 + 2; /* 2 is for "\r\n" */
|
|
174 } else {
|
|
175 len += sizeof(x_forwarded_for_header) - 1 + INET_ADDRSTRLEN - 1 + 2;
|
|
176 /* 2 is for "\r\n" */
|
|
177 }
|
|
178 }
|
|
179
|
|
180
|
|
181 part = &r->headers_in.headers.part;
|
|
182 header = part->elts;
|
|
183
|
|
184 for (i = 0; /* void */; i++) {
|
|
185
|
|
186 if (i >= part->nelts) {
|
|
187 if (part->next == NULL) {
|
|
188 break;
|
|
189 }
|
|
190
|
|
191 part = part->next;
|
|
192 header = part->elts;
|
|
193 i = 0;
|
|
194 }
|
|
195
|
|
196 if (&header[i] == r->headers_in.host) {
|
|
197 continue;
|
|
198 }
|
|
199
|
|
200 if (&header[i] == r->headers_in.connection) {
|
|
201 continue;
|
|
202 }
|
|
203
|
|
204 /* 2 is for ": " and 2 is for "\r\n" */
|
|
205 len += header[i].key.len + 2 + header[i].value.len + 2;
|
|
206 }
|
|
207
|
|
208 #if (NGX_DEBUG)
|
|
209 len++;
|
|
210 #endif
|
|
211
|
|
212 ngx_test_null(b, ngx_create_temp_buf(r->pool, len), NULL);
|
|
213 ngx_alloc_link_and_set_buf(chain, b, r->pool, NULL);
|
|
214
|
|
215
|
|
216 /* the request line */
|
|
217
|
|
218 if (p->upstream->method) {
|
|
219 b->last = ngx_cpymem(b->last,
|
|
220 http_methods[p->upstream->method - 1].data,
|
|
221 http_methods[p->upstream->method - 1].len);
|
|
222 } else {
|
|
223 b->last = ngx_cpymem(b->last, r->method_name.data, r->method_name.len);
|
|
224 }
|
|
225
|
|
226 b->last = ngx_cpymem(b->last, uc->uri.data, uc->uri.len);
|
|
227
|
2
|
228 if (escape) {
|
|
229 ngx_escape_uri(b->last, r->uri.data + uc->location->len,
|
|
230 r->uri.len - uc->location->len);
|
|
231 b->last += r->uri.len - uc->location->len + escape;
|
|
232
|
|
233 } else {
|
|
234 b->last = ngx_cpymem(b->last,
|
|
235 r->uri.data + uc->location->len,
|
|
236 r->uri.len - uc->location->len);
|
|
237 }
|
0
|
238
|
|
239 if (r->args.len > 0) {
|
|
240 *(b->last++) = '?';
|
|
241 b->last = ngx_cpymem(b->last, r->args.data, r->args.len);
|
|
242 }
|
|
243
|
|
244 b->last = ngx_cpymem(b->last, http_version, sizeof(http_version) - 1);
|
|
245
|
|
246
|
|
247 /* the "Connection: close" header */
|
|
248
|
|
249 b->last = ngx_cpymem(b->last, connection_close_header,
|
|
250 sizeof(connection_close_header) - 1);
|
|
251
|
|
252
|
|
253 /* the "Host" header */
|
|
254
|
|
255 b->last = ngx_cpymem(b->last, host_header, sizeof(host_header) - 1);
|
|
256
|
|
257 if (p->lcf->preserve_host && r->headers_in.host) {
|
|
258 b->last = ngx_cpymem(b->last, r->headers_in.host->value.data,
|
|
259 r->headers_in.host_name_len);
|
|
260
|
|
261 if (!uc->default_port) {
|
|
262 *(b->last++) = ':';
|
|
263 b->last = ngx_cpymem(b->last, uc->port_text.data,
|
|
264 uc->port_text.len);
|
|
265 }
|
|
266
|
|
267 } else {
|
|
268 b->last = ngx_cpymem(b->last, uc->host_header.data,
|
|
269 uc->host_header.len);
|
|
270 }
|
|
271 *(b->last++) = CR; *(b->last++) = LF;
|
|
272
|
|
273
|
|
274 /* the "X-Real-IP" header */
|
|
275
|
|
276 if (p->lcf->set_x_real_ip) {
|
|
277 b->last = ngx_cpymem(b->last, x_real_ip_header,
|
|
278 sizeof(x_real_ip_header) - 1);
|
|
279 b->last = ngx_cpymem(b->last, r->connection->addr_text.data,
|
|
280 r->connection->addr_text.len);
|
|
281 *(b->last++) = CR; *(b->last++) = LF;
|
|
282 }
|
|
283
|
|
284
|
|
285 /* the "X-Forwarded-For" header */
|
|
286
|
|
287 if (p->lcf->add_x_forwarded_for) {
|
|
288 if (r->headers_in.x_forwarded_for) {
|
|
289 b->last = ngx_cpymem(b->last, x_forwarded_for_header,
|
|
290 sizeof(x_forwarded_for_header) - 1);
|
|
291
|
|
292 b->last = ngx_cpymem(b->last,
|
|
293 r->headers_in.x_forwarded_for->value.data,
|
|
294 r->headers_in.x_forwarded_for->value.len);
|
|
295
|
|
296 *(b->last++) = ','; *(b->last++) = ' ';
|
|
297
|
|
298 } else {
|
|
299 b->last = ngx_cpymem(b->last, x_forwarded_for_header,
|
|
300 sizeof(x_forwarded_for_header) - 1);
|
|
301 }
|
|
302
|
|
303 b->last = ngx_cpymem(b->last, r->connection->addr_text.data,
|
|
304 r->connection->addr_text.len);
|
|
305 *(b->last++) = CR; *(b->last++) = LF;
|
|
306 }
|
|
307
|
|
308
|
|
309 part = &r->headers_in.headers.part;
|
|
310 header = part->elts;
|
|
311
|
|
312 for (i = 0; /* void */; i++) {
|
|
313
|
|
314 if (i >= part->nelts) {
|
|
315 if (part->next == NULL) {
|
|
316 break;
|
|
317 }
|
|
318
|
|
319 part = part->next;
|
|
320 header = part->elts;
|
|
321 i = 0;
|
|
322 }
|
|
323
|
|
324 if (&header[i] == r->headers_in.host) {
|
|
325 continue;
|
|
326 }
|
|
327
|
|
328 if (&header[i] == r->headers_in.connection) {
|
|
329 continue;
|
|
330 }
|
|
331
|
|
332 if (&header[i] == r->headers_in.keep_alive) {
|
|
333 continue;
|
|
334 }
|
|
335
|
|
336 if (&header[i] == r->headers_in.x_forwarded_for
|
|
337 && p->lcf->add_x_forwarded_for)
|
|
338 {
|
|
339 continue;
|
|
340 }
|
|
341
|
|
342 b->last = ngx_cpymem(b->last, header[i].key.data, header[i].key.len);
|
|
343
|
|
344 *(b->last++) = ':'; *(b->last++) = ' ';
|
|
345
|
|
346 b->last = ngx_cpymem(b->last, header[i].value.data,
|
|
347 header[i].value.len);
|
|
348
|
|
349 *(b->last++) = CR; *(b->last++) = LF;
|
|
350
|
|
351 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
352 "http proxy header: \"%s: %s\"",
|
|
353 header[i].key.data, header[i].value.data);
|
|
354 }
|
|
355
|
|
356 /* add "\r\n" at the header end */
|
|
357 *(b->last++) = CR; *(b->last++) = LF;
|
|
358
|
|
359 #if (NGX_DEBUG)
|
|
360 *(b->last) = '\0';
|
|
361 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
362 "http proxy header:\n\"%s\"", b->pos);
|
|
363 #endif
|
|
364
|
|
365 return chain;
|
|
366 }
|
|
367
|
|
368
|
|
369 static void ngx_http_proxy_init_upstream(void *data)
|
|
370 {
|
|
371 ngx_http_proxy_ctx_t *p = data;
|
|
372
|
|
373 ngx_chain_t *cl;
|
|
374 ngx_http_request_t *r;
|
|
375 ngx_output_chain_ctx_t *output;
|
|
376 ngx_chain_writer_ctx_t *writer;
|
|
377 ngx_http_proxy_log_ctx_t *ctx;
|
|
378
|
|
379 r = p->request;
|
|
380
|
|
381 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
382 "http proxy init upstream, client timer: %d",
|
|
383 r->connection->read->timer_set);
|
|
384
|
|
385 if (r->connection->read->timer_set) {
|
|
386 ngx_del_timer(r->connection->read);
|
|
387 }
|
|
388
|
|
389 r->connection->read->event_handler = ngx_http_proxy_check_broken_connection;
|
|
390
|
|
391 if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
|
|
392
|
|
393 r->connection->write->event_handler =
|
|
394 ngx_http_proxy_check_broken_connection;
|
|
395
|
|
396 if (!r->connection->write->active) {
|
|
397 if (ngx_add_event(r->connection->write, NGX_WRITE_EVENT,
|
|
398 NGX_CLEAR_EVENT) == NGX_ERROR)
|
|
399 {
|
|
400 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
401 return;
|
|
402 }
|
|
403 }
|
|
404 }
|
|
405
|
|
406
|
|
407 if (!(cl = ngx_http_proxy_create_request(p))) {
|
|
408 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
409 return;
|
|
410 }
|
|
411
|
|
412 if (r->request_body->bufs) {
|
|
413 cl->next = r->request_body->bufs;
|
|
414 }
|
|
415
|
|
416 r->request_body->bufs = cl;
|
|
417
|
|
418 if (!(ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_log_ctx_t)))) {
|
|
419 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
420 return;
|
|
421 }
|
|
422 ctx->connection = r->connection->number;
|
|
423 ctx->proxy = p;
|
|
424
|
|
425 p->upstream->peer.log = r->connection->log;
|
|
426 p->saved_ctx = r->connection->log->data;
|
|
427 p->saved_handler = r->connection->log->handler;
|
|
428 r->connection->log->data = ctx;
|
|
429 r->connection->log->handler = ngx_http_proxy_log_error;
|
|
430 p->action = "connecting to upstream";
|
|
431
|
|
432 if (!(output = ngx_pcalloc(r->pool, sizeof(ngx_output_chain_ctx_t)))) {
|
|
433 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
434 return;
|
|
435 }
|
|
436
|
|
437 p->upstream->output_chain_ctx = output;
|
|
438
|
2
|
439 output->sendfile = r->connection->sendfile;
|
0
|
440 output->pool = r->pool;
|
|
441 output->bufs.num = 1;
|
|
442 output->tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
|
|
443 output->output_filter = (ngx_output_chain_filter_pt) ngx_chain_writer;
|
|
444
|
|
445 if (!(writer = ngx_palloc(r->pool, sizeof(ngx_chain_writer_ctx_t)))) {
|
|
446 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
447 return;
|
|
448 }
|
|
449
|
|
450 output->filter_ctx = writer;
|
|
451 writer->pool = r->pool;
|
|
452
|
|
453 #if 0
|
|
454 if (p->lcf->busy_lock && p->busy_lock == NULL) {
|
|
455 #else
|
|
456 if (p->lcf->busy_lock && !p->busy_locked) {
|
|
457 #endif
|
|
458 ngx_http_proxy_upstream_busy_lock(p);
|
|
459 } else {
|
|
460 ngx_http_proxy_connect(p);
|
|
461 }
|
|
462 }
|
|
463
|
|
464
|
|
465 static void ngx_http_proxy_reinit_upstream(ngx_http_proxy_ctx_t *p)
|
|
466 {
|
|
467 ngx_chain_t *cl;
|
|
468 ngx_output_chain_ctx_t *output;
|
|
469
|
|
470 /* reinit the request chain */
|
|
471
|
|
472 for (cl = p->request->request_body->bufs; cl; cl = cl->next) {
|
|
473 cl->buf->pos = cl->buf->start;
|
|
474 cl->buf->file_pos = 0;
|
|
475 }
|
|
476
|
|
477 /* reinit the ngx_output_chain() context */
|
|
478
|
|
479 output = p->upstream->output_chain_ctx;
|
|
480
|
|
481 output->buf = NULL;
|
|
482 output->in = NULL;
|
|
483 output->free = NULL;
|
|
484 output->busy = NULL;
|
|
485
|
|
486 /* reinit r->header_in buffer */
|
|
487
|
|
488 if (p->header_in) {
|
|
489 if (p->cache) {
|
|
490 p->header_in->pos = p->header_in->start + p->cache->ctx.header_size;
|
|
491 p->header_in->last = p->header_in->pos;
|
|
492
|
|
493 } else {
|
|
494 p->header_in->pos = p->header_in->start;
|
|
495 p->header_in->last = p->header_in->start;
|
|
496 }
|
|
497 }
|
|
498
|
|
499 /* add one more state */
|
|
500
|
|
501 if (!(p->state = ngx_push_array(&p->states))) {
|
|
502 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
503 return;
|
|
504 }
|
|
505
|
|
506 p->status = 0;
|
|
507 p->status_count = 0;
|
|
508 }
|
|
509
|
|
510
|
|
511 #if 0
|
|
512
|
|
513 void ngx_http_proxy_upstream_busy_lock(ngx_http_proxy_ctx_t *p)
|
|
514 {
|
|
515 ngx_int_t rc;
|
|
516
|
|
517 rc = ngx_event_busy_lock(p->lcf->busy_lock, p->busy_lock);
|
|
518
|
|
519 if (rc == NGX_AGAIN) {
|
|
520 return;
|
|
521 }
|
|
522
|
|
523 if (rc == NGX_OK) {
|
|
524 ngx_http_proxy_connect(p);
|
|
525 return;
|
|
526 }
|
|
527
|
|
528 if (rc == NGX_ERROR) {
|
|
529 p->state->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
530 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
531 return;
|
|
532 }
|
|
533
|
|
534 /* rc == NGX_BUSY */
|
|
535
|
|
536 #if (NGX_HTTP_CACHE)
|
|
537
|
|
538 if (p->busy_lock->timer) {
|
|
539 ft_type = NGX_HTTP_PROXY_FT_MAX_WAITING;
|
|
540 } else {
|
|
541 ft_type = NGX_HTTP_PROXY_FT_BUSY_LOCK;
|
|
542 }
|
|
543
|
|
544 if (p->stale && (p->lcf->use_stale & ft_type)) {
|
|
545 ngx_http_proxy_finalize_request(p,
|
|
546 ngx_http_proxy_send_cached_response(p));
|
|
547 return;
|
|
548 }
|
|
549
|
|
550 #endif
|
|
551
|
|
552 p->state->status = NGX_HTTP_SERVICE_UNAVAILABLE;
|
|
553 ngx_http_proxy_finalize_request(p, NGX_HTTP_SERVICE_UNAVAILABLE);
|
|
554 }
|
|
555
|
|
556 #endif
|
|
557
|
|
558
|
|
559 #if 1
|
|
560
|
|
561 void ngx_http_proxy_upstream_busy_lock(ngx_http_proxy_ctx_t *p)
|
|
562 {
|
|
563 ngx_int_t rc;
|
|
564 #if (NGX_HTTP_CACHE)
|
|
565 ngx_int_t ft_type;
|
|
566 #endif
|
|
567
|
|
568 if (p->busy_lock.time == 0) {
|
|
569 p->busy_lock.event = p->request->connection->read;
|
|
570 p->busy_lock.event_handler = ngx_http_proxy_busy_lock_handler;
|
|
571 }
|
|
572
|
|
573 rc = ngx_http_busy_lock(p->lcf->busy_lock, &p->busy_lock);
|
|
574
|
|
575 if (rc == NGX_AGAIN) {
|
|
576 return;
|
|
577 }
|
|
578
|
|
579 if (rc == NGX_OK) {
|
|
580 ngx_http_proxy_connect(p);
|
|
581 return;
|
|
582 }
|
|
583
|
|
584 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
|
|
585
|
|
586 #if (NGX_HTTP_CACHE)
|
|
587
|
|
588 if (rc == NGX_DONE) {
|
|
589 ft_type = NGX_HTTP_PROXY_FT_BUSY_LOCK;
|
|
590
|
|
591 } else {
|
|
592 /* rc == NGX_ERROR */
|
|
593 ft_type = NGX_HTTP_PROXY_FT_MAX_WAITING;
|
|
594 }
|
|
595
|
|
596 if (p->stale && (p->lcf->use_stale & ft_type)) {
|
|
597 ngx_http_proxy_finalize_request(p,
|
|
598 ngx_http_proxy_send_cached_response(p));
|
|
599 return;
|
|
600 }
|
|
601
|
|
602 #endif
|
|
603
|
|
604 p->state->status = NGX_HTTP_SERVICE_UNAVAILABLE;
|
|
605 ngx_http_proxy_finalize_request(p, NGX_HTTP_SERVICE_UNAVAILABLE);
|
|
606 }
|
|
607
|
|
608 #endif
|
|
609
|
|
610
|
|
611 static void ngx_http_proxy_connect(ngx_http_proxy_ctx_t *p)
|
|
612 {
|
|
613 int rc;
|
|
614 ngx_connection_t *c;
|
|
615 ngx_http_request_t *r;
|
|
616 ngx_output_chain_ctx_t *output;
|
|
617 ngx_chain_writer_ctx_t *writer;
|
|
618
|
|
619 p->action = "connecting to upstream";
|
|
620
|
|
621 p->request->connection->single_connection = 0;
|
|
622
|
|
623 rc = ngx_event_connect_peer(&p->upstream->peer);
|
|
624
|
|
625 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, p->request->connection->log, 0,
|
|
626 "http proxy connect: %d", rc);
|
|
627
|
|
628 if (rc == NGX_ERROR) {
|
|
629 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
630 return;
|
|
631 }
|
|
632
|
|
633 p->state->peer =
|
|
634 &p->upstream->peer.peers->peers[p->upstream->peer.cur_peer].addr_port_text;
|
|
635
|
|
636 if (rc == NGX_CONNECT_ERROR) {
|
|
637 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
|
|
638 return;
|
|
639 }
|
|
640
|
|
641 r = p->request;
|
|
642 c = p->upstream->peer.connection;
|
|
643
|
|
644 c->data = p;
|
|
645 c->write->event_handler = ngx_http_proxy_send_request_handler;
|
|
646 c->read->event_handler = ngx_http_proxy_process_upstream_status_line;
|
|
647
|
|
648 c->pool = r->pool;
|
|
649 c->read->log = c->write->log = c->log = r->connection->log;
|
|
650
|
|
651 /* init or reinit the ngx_output_chain() and ngx_chain_writer() contexts */
|
|
652
|
|
653 output = p->upstream->output_chain_ctx;
|
|
654 writer = output->filter_ctx;
|
|
655 writer->out = NULL;
|
|
656 writer->last = &writer->out;
|
|
657 writer->connection = c;
|
|
658 writer->limit = OFF_T_MAX_VALUE;
|
|
659
|
|
660 if (p->upstream->peer.tries > 1 && p->request_sent) {
|
|
661 ngx_http_proxy_reinit_upstream(p);
|
|
662 }
|
|
663
|
|
664 if (r->request_body->buf) {
|
|
665 if (r->request_body->temp_file->file.fd != NGX_INVALID_FILE) {
|
|
666
|
|
667 if (!(output->free = ngx_alloc_chain_link(r->pool))) {
|
|
668 ngx_http_proxy_finalize_request(p,
|
|
669 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
670 return;
|
|
671 }
|
|
672
|
|
673 output->free->buf = r->request_body->buf;
|
|
674 output->free->next = NULL;
|
|
675 output->allocated = 1;
|
|
676
|
|
677 r->request_body->buf->pos = r->request_body->buf->start;
|
|
678 r->request_body->buf->last = r->request_body->buf->start;
|
|
679 r->request_body->buf->tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
|
|
680
|
|
681 } else {
|
|
682 r->request_body->buf->pos = r->request_body->buf->start;
|
|
683 }
|
|
684 }
|
|
685
|
|
686 p->request_sent = 0;
|
|
687
|
|
688 if (rc == NGX_AGAIN) {
|
|
689 ngx_add_timer(c->write, p->lcf->connect_timeout);
|
|
690 return;
|
|
691 }
|
|
692
|
|
693 /* rc == NGX_OK */
|
|
694
|
|
695 #if 1 /* test only, see below about "post aio operation" */
|
|
696
|
|
697 if (c->read->ready) {
|
|
698 /* post aio operation */
|
|
699 ngx_http_proxy_process_upstream_status_line(c->read);
|
|
700 return;
|
|
701 }
|
|
702
|
|
703 #endif
|
|
704
|
|
705 ngx_http_proxy_send_request(p);
|
|
706 }
|
|
707
|
|
708
|
|
709 static void ngx_http_proxy_send_request(ngx_http_proxy_ctx_t *p)
|
|
710 {
|
|
711 int rc;
|
|
712 ngx_connection_t *c;
|
|
713
|
|
714 c = p->upstream->peer.connection;
|
|
715
|
|
716 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
717 "http proxy send request");
|
|
718
|
|
719 #if (HAVE_KQUEUE)
|
|
720
|
|
721 if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)
|
|
722 && !p->request_sent
|
|
723 && c->write->pending_eof)
|
|
724 {
|
|
725 ngx_log_error(NGX_LOG_ERR, c->log, c->write->kq_errno,
|
|
726 "connect() failed");
|
|
727
|
|
728 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
|
|
729 return;
|
|
730 }
|
|
731
|
|
732 #endif
|
|
733
|
|
734 p->action = "sending request to upstream";
|
|
735
|
|
736 rc = ngx_output_chain(p->upstream->output_chain_ctx,
|
|
737 p->request_sent ? NULL:
|
|
738 p->request->request_body->bufs);
|
|
739
|
|
740 if (rc == NGX_ERROR) {
|
|
741 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
|
|
742 return;
|
|
743 }
|
|
744
|
|
745 p->request_sent = 1;
|
|
746
|
|
747 if (c->write->timer_set) {
|
|
748 ngx_del_timer(c->write);
|
|
749 }
|
|
750
|
|
751 if (rc == NGX_AGAIN) {
|
|
752 ngx_add_timer(c->write, p->lcf->send_timeout);
|
|
753
|
2
|
754 if (ngx_handle_write_event(c->write, p->lcf->send_lowat) == NGX_ERROR) {
|
0
|
755 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
756 return;
|
|
757 }
|
|
758
|
|
759 return;
|
|
760 }
|
|
761
|
|
762 /* rc == NGX_OK */
|
|
763
|
|
764 if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
|
|
765 if (ngx_tcp_push(c->fd) == NGX_ERROR) {
|
|
766 ngx_log_error(NGX_LOG_CRIT, c->log,
|
|
767 ngx_socket_errno,
|
|
768 ngx_tcp_push_n " failed");
|
|
769 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
770 return;
|
|
771 }
|
|
772
|
|
773 c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
|
|
774 return;
|
|
775 }
|
|
776
|
|
777 ngx_add_timer(c->read, p->lcf->read_timeout);
|
|
778
|
|
779 #if 0
|
|
780 if (c->read->ready) {
|
|
781
|
|
782 /* post aio operation */
|
|
783
|
|
784 /*
|
|
785 * although we can post aio operation just in the end
|
|
786 * of ngx_http_proxy_connect() CHECK IT !!!
|
|
787 * it's better to do here because we postpone header buffer allocation
|
|
788 */
|
|
789
|
|
790 ngx_http_proxy_process_upstream_status_line(c->read);
|
|
791 return;
|
|
792 }
|
|
793 #endif
|
|
794
|
|
795 c->write->event_handler = ngx_http_proxy_dummy_handler;
|
|
796
|
|
797 if (ngx_handle_level_write_event(c->write) == NGX_ERROR) {
|
|
798 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
799 return;
|
|
800 }
|
|
801 }
|
|
802
|
|
803
|
|
804 static void ngx_http_proxy_send_request_handler(ngx_event_t *wev)
|
|
805 {
|
|
806 ngx_connection_t *c;
|
|
807 ngx_http_proxy_ctx_t *p;
|
|
808
|
|
809 c = wev->data;
|
|
810 p = c->data;
|
|
811
|
|
812 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
|
|
813 "http proxy send request handler");
|
|
814
|
|
815 if (wev->timedout) {
|
|
816 p->action = "sending request to upstream";
|
|
817 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT);
|
|
818 return;
|
|
819 }
|
|
820
|
|
821 if (p->request->connection->write->eof
|
|
822 && (!p->cachable || !p->request_sent))
|
|
823 {
|
|
824 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
825 return;
|
|
826 }
|
|
827
|
|
828 ngx_http_proxy_send_request(p);
|
|
829 }
|
|
830
|
|
831
|
|
832 static void ngx_http_proxy_dummy_handler(ngx_event_t *wev)
|
|
833 {
|
|
834 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http proxy dummy handler");
|
|
835 }
|
|
836
|
|
837
|
|
838 static void ngx_http_proxy_process_upstream_status_line(ngx_event_t *rev)
|
|
839 {
|
|
840 int rc;
|
|
841 ssize_t n;
|
|
842 ngx_connection_t *c;
|
|
843 ngx_http_proxy_ctx_t *p;
|
|
844
|
|
845 c = rev->data;
|
|
846 p = c->data;
|
|
847 p->action = "reading upstream status line";
|
|
848
|
|
849 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
|
850 "http proxy process status line");
|
|
851
|
|
852 if (rev->timedout) {
|
|
853 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT);
|
|
854 return;
|
|
855 }
|
|
856
|
|
857 if (p->header_in == NULL) {
|
|
858 p->header_in = ngx_create_temp_buf(p->request->pool,
|
|
859 p->lcf->header_buffer_size);
|
|
860 if (p->header_in == NULL) {
|
|
861 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
862 return;
|
|
863 }
|
|
864 p->header_in->tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
|
|
865
|
|
866 if (p->cache) {
|
|
867 p->header_in->pos += p->cache->ctx.header_size;
|
|
868 p->header_in->last = p->header_in->pos;
|
|
869 }
|
|
870 }
|
|
871
|
|
872 n = ngx_http_proxy_read_upstream_header(p);
|
|
873
|
|
874 if (n == NGX_AGAIN) {
|
|
875 return;
|
|
876 }
|
|
877
|
|
878 if (n == 0) {
|
|
879 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
|
|
880 "upstream prematurely closed connection");
|
|
881 }
|
|
882
|
|
883 if (n == NGX_ERROR || n == 0) {
|
|
884 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
|
|
885 return;
|
|
886 }
|
|
887
|
|
888 p->valid_header_in = 0;
|
|
889
|
|
890 p->upstream->peer.cached = 0;
|
|
891
|
|
892 rc = ngx_http_proxy_parse_status_line(p);
|
|
893
|
|
894 if (rc == NGX_AGAIN) {
|
|
895 if (p->header_in->pos == p->header_in->last) {
|
|
896 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
|
|
897 "upstream sent too long status line");
|
|
898 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER);
|
|
899 }
|
|
900 return;
|
|
901 }
|
|
902
|
|
903 if (rc == NGX_HTTP_PROXY_PARSE_NO_HEADER) {
|
|
904 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
|
|
905 "upstream sent no valid HTTP/1.0 header");
|
|
906
|
|
907 if (p->accel) {
|
|
908 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER);
|
|
909
|
|
910 } else {
|
|
911 p->request->http_version = NGX_HTTP_VERSION_9;
|
|
912 p->upstream->status = NGX_HTTP_OK;
|
|
913 ngx_http_proxy_send_response(p);
|
|
914 }
|
|
915
|
|
916 return;
|
|
917 }
|
|
918
|
|
919 /* rc == NGX_OK */
|
|
920
|
|
921 p->upstream->status = p->status;
|
|
922 p->state->status = p->status;
|
|
923
|
|
924 if (p->status == NGX_HTTP_INTERNAL_SERVER_ERROR) {
|
|
925
|
|
926 if (p->upstream->peer.tries > 1
|
|
927 && (p->lcf->next_upstream & NGX_HTTP_PROXY_FT_HTTP_500))
|
|
928 {
|
|
929 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_HTTP_500);
|
|
930 return;
|
|
931 }
|
|
932
|
|
933 #if (NGX_HTTP_CACHE)
|
|
934
|
|
935 if (p->upstream->peer.tries == 0
|
|
936 && p->stale
|
|
937 && (p->lcf->use_stale & NGX_HTTP_PROXY_FT_HTTP_500))
|
|
938 {
|
|
939 ngx_http_proxy_finalize_request(p,
|
|
940 ngx_http_proxy_send_cached_response(p));
|
|
941
|
|
942 return;
|
|
943 }
|
|
944
|
|
945 #endif
|
|
946 }
|
|
947
|
|
948 if (p->status == NGX_HTTP_NOT_FOUND
|
|
949 && p->upstream->peer.tries > 1
|
|
950 && p->lcf->next_upstream & NGX_HTTP_PROXY_FT_HTTP_404)
|
|
951 {
|
|
952 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_HTTP_404);
|
|
953 return;
|
|
954 }
|
|
955
|
|
956 /* TODO: "proxy_error_page" */
|
|
957
|
|
958 p->upstream->status_line.len = p->status_end - p->status_start;
|
|
959 p->upstream->status_line.data = ngx_palloc(p->request->pool,
|
|
960 p->upstream->status_line.len + 1);
|
|
961 if (p->upstream->status_line.data == NULL) {
|
|
962 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
963 return;
|
|
964 }
|
|
965 ngx_cpystrn(p->upstream->status_line.data, p->status_start,
|
|
966 p->upstream->status_line.len + 1);
|
|
967
|
|
968 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
|
969 "http proxy status %d \"%s\"",
|
|
970 p->upstream->status, p->upstream->status_line.data);
|
|
971
|
|
972
|
|
973 /* init or reinit the p->upstream->headers_in.headers table */
|
|
974
|
|
975 if (p->upstream->headers_in.headers.part.elts) {
|
|
976 p->upstream->headers_in.headers.part.nelts = 0;
|
|
977 p->upstream->headers_in.headers.part.next = NULL;
|
|
978 p->upstream->headers_in.headers.last =
|
|
979 &p->upstream->headers_in.headers.part;
|
|
980
|
|
981 ngx_memzero(&p->upstream->headers_in.date,
|
|
982 sizeof(ngx_http_proxy_headers_in_t) - sizeof(ngx_list_t));
|
|
983
|
|
984 } else {
|
|
985 if (ngx_list_init(&p->upstream->headers_in.headers, p->request->pool,
|
|
986 20, sizeof(ngx_table_elt_t)) == NGX_ERROR)
|
|
987 {
|
|
988 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
989 return;
|
|
990 }
|
|
991 }
|
|
992
|
|
993
|
|
994 c->read->event_handler = ngx_http_proxy_process_upstream_headers;
|
|
995 ngx_http_proxy_process_upstream_headers(rev);
|
|
996 }
|
|
997
|
|
998
|
|
999 static void ngx_http_proxy_process_upstream_headers(ngx_event_t *rev)
|
|
1000 {
|
|
1001 int i, rc;
|
|
1002 ssize_t n;
|
|
1003 ngx_table_elt_t *h;
|
|
1004 ngx_connection_t *c;
|
|
1005 ngx_http_request_t *r;
|
|
1006 ngx_http_proxy_ctx_t *p;
|
|
1007
|
|
1008 c = rev->data;
|
|
1009 p = c->data;
|
|
1010 r = p->request;
|
|
1011 p->action = "reading upstream headers";
|
|
1012
|
|
1013 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
|
1014 "http proxy process header line");
|
|
1015
|
|
1016 if (rev->timedout) {
|
|
1017 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT);
|
|
1018 return;
|
|
1019 }
|
|
1020
|
|
1021 rc = NGX_AGAIN;
|
|
1022
|
|
1023 for ( ;; ) {
|
|
1024 if (rc == NGX_AGAIN) {
|
|
1025 n = ngx_http_proxy_read_upstream_header(p);
|
|
1026
|
|
1027 if (n == 0) {
|
|
1028 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
|
|
1029 "upstream prematurely closed connection");
|
|
1030 }
|
|
1031
|
|
1032 if (n == NGX_ERROR || n == 0) {
|
|
1033 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
|
|
1034 return;
|
|
1035 }
|
|
1036
|
|
1037 if (n == NGX_AGAIN) {
|
|
1038 return;
|
|
1039 }
|
|
1040 }
|
|
1041
|
|
1042 rc = ngx_http_parse_header_line(p->request, p->header_in);
|
|
1043
|
|
1044 if (rc == NGX_OK) {
|
|
1045
|
|
1046 /* a header line has been parsed successfully */
|
|
1047
|
|
1048 if (!(h = ngx_list_push(&p->upstream->headers_in.headers))) {
|
|
1049 ngx_http_proxy_finalize_request(p,
|
|
1050 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1051 return;
|
|
1052 }
|
|
1053
|
|
1054 h->key.len = r->header_name_end - r->header_name_start;
|
|
1055 h->value.len = r->header_end - r->header_start;
|
|
1056
|
|
1057 h->key.data = ngx_palloc(p->request->pool,
|
|
1058 h->key.len + 1 + h->value.len + 1);
|
|
1059 if (h->key.data == NULL) {
|
|
1060 ngx_http_proxy_finalize_request(p,
|
|
1061 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1062 return;
|
|
1063 }
|
|
1064
|
|
1065 h->value.data = h->key.data + h->key.len + 1;
|
|
1066 ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1);
|
|
1067 ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1);
|
|
1068
|
|
1069 for (i = 0; ngx_http_proxy_headers_in[i].name.len != 0; i++) {
|
|
1070 if (ngx_http_proxy_headers_in[i].name.len != h->key.len) {
|
|
1071 continue;
|
|
1072 }
|
|
1073
|
|
1074 if (ngx_strcasecmp(ngx_http_proxy_headers_in[i].name.data,
|
|
1075 h->key.data) == 0)
|
|
1076 {
|
|
1077 *((ngx_table_elt_t **) ((char *) &p->upstream->headers_in
|
|
1078 + ngx_http_proxy_headers_in[i].offset)) = h;
|
|
1079 break;
|
|
1080 }
|
|
1081 }
|
|
1082
|
|
1083 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1084 "http proxy header: \"%s: %s\"",
|
|
1085 h->key.data, h->value.data);
|
|
1086
|
|
1087 continue;
|
|
1088
|
|
1089 } else if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
|
|
1090
|
|
1091 /* a whole header has been parsed successfully */
|
|
1092
|
|
1093 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1094 "http proxy header done");
|
|
1095
|
|
1096 /* TODO: hook to process the upstream header */
|
|
1097
|
|
1098 #if (NGX_HTTP_CACHE)
|
|
1099
|
|
1100 if (p->cachable) {
|
|
1101 p->cachable = ngx_http_proxy_is_cachable(p);
|
|
1102 }
|
|
1103
|
|
1104 #endif
|
|
1105
|
|
1106 ngx_http_proxy_send_response(p);
|
|
1107 return;
|
|
1108
|
|
1109 } else if (rc != NGX_AGAIN) {
|
|
1110
|
|
1111 /* there was error while a header line parsing */
|
|
1112
|
|
1113 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
|
|
1114 upstream_header_errors[rc - NGX_HTTP_PARSE_HEADER_ERROR]);
|
|
1115
|
|
1116 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER);
|
|
1117 return;
|
|
1118 }
|
|
1119
|
|
1120 /* rc == NGX_AGAIN: a header line parsing is still not complete */
|
|
1121
|
|
1122 if (p->header_in->last == p->header_in->end) {
|
|
1123 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
|
|
1124 "upstream sent too big header");
|
|
1125
|
|
1126 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER);
|
|
1127 return;
|
|
1128 }
|
|
1129 }
|
|
1130 }
|
|
1131
|
|
1132
|
|
1133 static ssize_t ngx_http_proxy_read_upstream_header(ngx_http_proxy_ctx_t *p)
|
|
1134 {
|
|
1135 ssize_t n;
|
|
1136 ngx_event_t *rev;
|
|
1137
|
|
1138 rev = p->upstream->peer.connection->read;
|
|
1139
|
|
1140 n = p->header_in->last - p->header_in->pos;
|
|
1141
|
|
1142 if (n > 0) {
|
|
1143 return n;
|
|
1144 }
|
|
1145
|
|
1146 n = ngx_recv(p->upstream->peer.connection, p->header_in->last,
|
|
1147 p->header_in->end - p->header_in->last);
|
|
1148
|
|
1149 if (n == NGX_AGAIN) {
|
|
1150 #if 0
|
|
1151 ngx_add_timer(rev, p->lcf->read_timeout);
|
|
1152 #endif
|
|
1153
|
|
1154 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
|
1155 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1156 return NGX_ERROR;
|
|
1157 }
|
|
1158
|
|
1159 return NGX_AGAIN;
|
|
1160 }
|
|
1161
|
|
1162 if (n == 0) {
|
|
1163 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
|
|
1164 "upstream closed prematurely connection");
|
|
1165 }
|
|
1166
|
|
1167 if (n == 0 || n == NGX_ERROR) {
|
|
1168 return NGX_ERROR;
|
|
1169 }
|
|
1170
|
|
1171 p->header_in->last += n;
|
|
1172
|
|
1173 return n;
|
|
1174 }
|
|
1175
|
|
1176
|
|
1177 static void ngx_http_proxy_send_response(ngx_http_proxy_ctx_t *p)
|
|
1178 {
|
|
1179 int rc;
|
|
1180 ngx_event_pipe_t *ep;
|
|
1181 ngx_http_request_t *r;
|
|
1182 ngx_http_cache_header_t *header;
|
|
1183 ngx_http_core_loc_conf_t *clcf;
|
|
1184
|
|
1185 r = p->request;
|
|
1186
|
|
1187 r->headers_out.status = p->upstream->status;
|
2
|
1188 r->headers_out.status_line = p->upstream->status_line;
|
0
|
1189
|
|
1190 #if 0
|
|
1191 r->headers_out.content_length_n = -1;
|
|
1192 r->headers_out.content_length = NULL;
|
|
1193 #endif
|
|
1194
|
|
1195 /* copy an upstream header to r->headers_out */
|
|
1196
|
|
1197 if (ngx_http_proxy_copy_header(p, &p->upstream->headers_in) == NGX_ERROR) {
|
|
1198 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1199 return;
|
|
1200 }
|
|
1201
|
|
1202 /* TODO: preallocate event_pipe bufs, look "Content-Length" */
|
|
1203
|
|
1204 rc = ngx_http_send_header(r);
|
|
1205
|
|
1206 p->header_sent = 1;
|
|
1207
|
|
1208 if (p->cache && p->cache->ctx.file.fd != NGX_INVALID_FILE) {
|
|
1209 if (ngx_close_file(p->cache->ctx.file.fd) == NGX_FILE_ERROR) {
|
|
1210 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
|
|
1211 ngx_close_file_n " \"%s\" failed",
|
|
1212 p->cache->ctx.file.name.data);
|
|
1213 }
|
|
1214 }
|
|
1215
|
|
1216 if (p->cachable) {
|
|
1217 header = (ngx_http_cache_header_t *) p->header_in->start;
|
|
1218
|
|
1219 header->expires = p->cache->ctx.expires;
|
|
1220 header->last_modified = p->cache->ctx.last_modified;
|
|
1221 header->date = p->cache->ctx.date;
|
|
1222 header->length = r->headers_out.content_length_n;
|
|
1223 p->cache->ctx.length = r->headers_out.content_length_n;
|
|
1224
|
|
1225 header->key_len = p->cache->ctx.key.len;
|
|
1226 ngx_memcpy(&header->key, p->cache->ctx.key.data, header->key_len);
|
|
1227 header->key[header->key_len] = LF;
|
|
1228 }
|
|
1229
|
|
1230 ep = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
|
|
1231 if (ep == NULL) {
|
|
1232 ngx_http_proxy_finalize_request(p, 0);
|
|
1233 return;
|
|
1234 }
|
|
1235
|
|
1236 p->upstream->event_pipe = ep;
|
|
1237
|
|
1238 ep->input_filter = ngx_event_pipe_copy_input_filter;
|
|
1239 ep->output_filter = (ngx_event_pipe_output_filter_pt)
|
|
1240 ngx_http_output_filter;
|
|
1241 ep->output_ctx = r;
|
|
1242 ep->tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
|
|
1243 ep->bufs = p->lcf->bufs;
|
|
1244 ep->busy_size = p->lcf->busy_buffers_size;
|
|
1245 ep->upstream = p->upstream->peer.connection;
|
|
1246 ep->downstream = r->connection;
|
|
1247 ep->pool = r->pool;
|
|
1248 ep->log = r->connection->log;
|
|
1249
|
|
1250 ep->cachable = p->cachable;
|
|
1251
|
|
1252 if (!(ep->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)))) {
|
|
1253 ngx_http_proxy_finalize_request(p, 0);
|
|
1254 return;
|
|
1255 }
|
|
1256
|
|
1257 ep->temp_file->file.fd = NGX_INVALID_FILE;
|
|
1258 ep->temp_file->file.log = r->connection->log;
|
|
1259 ep->temp_file->path = p->lcf->temp_path;
|
|
1260 ep->temp_file->pool = r->pool;
|
|
1261
|
|
1262 if (p->cachable) {
|
|
1263 ep->temp_file->persistent = 1;
|
|
1264 } else {
|
|
1265 ep->temp_file->warn = "an upstream response is buffered "
|
|
1266 "to a temporary file";
|
|
1267 }
|
|
1268
|
|
1269 ep->max_temp_file_size = p->lcf->max_temp_file_size;
|
|
1270 ep->temp_file_write_size = p->lcf->temp_file_write_size;
|
|
1271
|
|
1272 if (!(ep->preread_bufs = ngx_alloc_chain_link(r->pool))) {
|
|
1273 ngx_http_proxy_finalize_request(p, 0);
|
|
1274 return;
|
|
1275 }
|
|
1276 ep->preread_bufs->buf = p->header_in;
|
|
1277 ep->preread_bufs->next = NULL;
|
|
1278
|
|
1279 ep->preread_size = p->header_in->last - p->header_in->pos;
|
|
1280
|
|
1281 if (p->cachable) {
|
|
1282 ep->buf_to_file = ngx_calloc_buf(r->pool);
|
|
1283 if (ep->buf_to_file == NULL) {
|
|
1284 ngx_http_proxy_finalize_request(p, 0);
|
|
1285 return;
|
|
1286 }
|
|
1287 ep->buf_to_file->pos = p->header_in->start;
|
|
1288 ep->buf_to_file->last = p->header_in->pos;
|
|
1289 ep->buf_to_file->temporary = 1;
|
|
1290 }
|
|
1291
|
|
1292 if (ngx_event_flags & NGX_USE_AIO_EVENT) {
|
|
1293 /* the posted aio operation can currupt a shadow buffer */
|
|
1294 ep->single_buf = 1;
|
|
1295 }
|
|
1296
|
|
1297 /* TODO: ep->free_bufs = 0 if use ngx_create_chain_of_bufs() */
|
|
1298 ep->free_bufs = 1;
|
|
1299
|
|
1300 /*
|
|
1301 * event_pipe would do p->header_in->last += ep->preread_size
|
|
1302 * as though these bytes were read.
|
|
1303 */
|
|
1304 p->header_in->last = p->header_in->pos;
|
|
1305
|
|
1306 if (p->lcf->cyclic_temp_file) {
|
|
1307
|
|
1308 /*
|
|
1309 * we need to disable the use of sendfile() if we use cyclic temp file
|
|
1310 * because the writing a new data can interfere with sendfile()
|
|
1311 * that uses the same kernel file pages (at least on FreeBSD)
|
|
1312 */
|
|
1313
|
|
1314 ep->cyclic_temp_file = 1;
|
2
|
1315 r->connection->sendfile = 0;
|
0
|
1316
|
|
1317 } else {
|
|
1318 ep->cyclic_temp_file = 0;
|
|
1319 }
|
|
1320
|
|
1321 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
1322
|
|
1323 ep->read_timeout = p->lcf->read_timeout;
|
|
1324 ep->send_timeout = clcf->send_timeout;
|
|
1325 ep->send_lowat = clcf->send_lowat;
|
|
1326
|
|
1327 p->upstream->peer.connection->read->event_handler =
|
|
1328 ngx_http_proxy_process_body;
|
|
1329 r->connection->write->event_handler = ngx_http_proxy_process_body;
|
|
1330
|
|
1331 ngx_http_proxy_process_body(p->upstream->peer.connection->read);
|
|
1332
|
|
1333 return;
|
|
1334 }
|
|
1335
|
|
1336
|
|
1337 static void ngx_http_proxy_process_body(ngx_event_t *ev)
|
|
1338 {
|
|
1339 ngx_connection_t *c;
|
|
1340 ngx_http_request_t *r;
|
|
1341 ngx_http_proxy_ctx_t *p;
|
|
1342 ngx_event_pipe_t *ep;
|
|
1343
|
|
1344 c = ev->data;
|
|
1345
|
|
1346 if (ev->write) {
|
|
1347 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
|
|
1348 "http proxy process downstream");
|
|
1349 r = c->data;
|
|
1350 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1351 p->action = "sending to client";
|
|
1352
|
|
1353 } else {
|
|
1354 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
|
|
1355 "http proxy process upstream");
|
|
1356 p = c->data;
|
|
1357 r = p->request;
|
|
1358 p->action = "reading upstream body";
|
|
1359 }
|
|
1360
|
|
1361 ep = p->upstream->event_pipe;
|
|
1362
|
|
1363 if (ev->timedout) {
|
|
1364 if (ev->write) {
|
|
1365 ep->downstream_error = 1;
|
|
1366 ngx_log_error(NGX_LOG_ERR, c->log, NGX_ETIMEDOUT,
|
|
1367 "client timed out");
|
|
1368
|
|
1369 } else {
|
|
1370 ep->upstream_error = 1;
|
|
1371 ngx_log_error(NGX_LOG_ERR, c->log, NGX_ETIMEDOUT,
|
|
1372 "upstream timed out");
|
|
1373 }
|
|
1374
|
|
1375 } else {
|
|
1376 if (ngx_event_pipe(ep, ev->write) == NGX_ABORT) {
|
|
1377 ngx_http_proxy_finalize_request(p, 0);
|
|
1378 return;
|
|
1379 }
|
|
1380 }
|
|
1381
|
|
1382 if (p->upstream->peer.connection) {
|
|
1383
|
|
1384 #if (NGX_HTTP_FILE_CACHE)
|
|
1385
|
|
1386 if (ep->upstream_done && p->cachable) {
|
|
1387 if (ngx_http_proxy_update_cache(p) == NGX_ERROR) {
|
|
1388 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
|
|
1389 ngx_http_proxy_finalize_request(p, 0);
|
|
1390 return;
|
|
1391 }
|
|
1392
|
|
1393 } else if (ep->upstream_eof && p->cachable) {
|
|
1394
|
|
1395 /* TODO: check length & update cache */
|
|
1396
|
|
1397 if (ngx_http_proxy_update_cache(p) == NGX_ERROR) {
|
|
1398 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
|
|
1399 ngx_http_proxy_finalize_request(p, 0);
|
|
1400 return;
|
|
1401 }
|
|
1402 }
|
|
1403
|
|
1404 #endif
|
|
1405
|
|
1406 if (ep->upstream_done || ep->upstream_eof || ep->upstream_error) {
|
|
1407 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ev->log, 0,
|
|
1408 "http proxy upstream exit: " PTR_FMT, ep->out);
|
|
1409 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
|
|
1410 ngx_http_proxy_finalize_request(p, 0);
|
|
1411 return;
|
|
1412 }
|
|
1413 }
|
|
1414
|
|
1415 if (ep->downstream_error) {
|
|
1416 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
|
|
1417 "http proxy downstream error");
|
|
1418 if (!p->cachable && p->upstream->peer.connection) {
|
|
1419 ngx_http_proxy_finalize_request(p, 0);
|
|
1420 }
|
|
1421 }
|
|
1422 }
|
|
1423
|
|
1424
|
|
1425 static void ngx_http_proxy_next_upstream(ngx_http_proxy_ctx_t *p, int ft_type)
|
|
1426 {
|
|
1427 int status;
|
|
1428
|
|
1429 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, p->request->connection->log, 0,
|
|
1430 "http proxy next upstream: %d", ft_type);
|
|
1431
|
|
1432 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
|
|
1433
|
|
1434 if (ft_type != NGX_HTTP_PROXY_FT_HTTP_404) {
|
|
1435 ngx_event_connect_peer_failed(&p->upstream->peer);
|
|
1436 }
|
|
1437
|
|
1438 if (ft_type == NGX_HTTP_PROXY_FT_TIMEOUT) {
|
|
1439 ngx_log_error(NGX_LOG_ERR, p->request->connection->log, NGX_ETIMEDOUT,
|
|
1440 "upstream timed out");
|
|
1441 }
|
|
1442
|
|
1443 if (p->upstream->peer.cached && ft_type == NGX_HTTP_PROXY_FT_ERROR) {
|
|
1444 status = 0;
|
|
1445
|
|
1446 } else {
|
|
1447 switch(ft_type) {
|
|
1448 case NGX_HTTP_PROXY_FT_TIMEOUT:
|
|
1449 status = NGX_HTTP_GATEWAY_TIME_OUT;
|
|
1450 break;
|
|
1451
|
|
1452 case NGX_HTTP_PROXY_FT_HTTP_500:
|
|
1453 status = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
1454 break;
|
|
1455
|
|
1456 case NGX_HTTP_PROXY_FT_HTTP_404:
|
|
1457 status = NGX_HTTP_NOT_FOUND;
|
|
1458 break;
|
|
1459
|
|
1460 /*
|
|
1461 * NGX_HTTP_PROXY_FT_BUSY_LOCK and NGX_HTTP_PROXY_FT_MAX_WAITING
|
|
1462 * never reach here
|
|
1463 */
|
|
1464
|
|
1465 default:
|
|
1466 status = NGX_HTTP_BAD_GATEWAY;
|
|
1467 }
|
|
1468 }
|
|
1469
|
|
1470 if (p->upstream->peer.connection) {
|
|
1471 ngx_http_proxy_close_connection(p);
|
|
1472 }
|
|
1473
|
|
1474 if (p->request->connection->write->eof) {
|
|
1475 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
1476 return;
|
|
1477 }
|
|
1478
|
|
1479 if (status) {
|
|
1480 p->state->status = status;
|
|
1481
|
|
1482 if (p->upstream->peer.tries == 0 || !(p->lcf->next_upstream & ft_type))
|
|
1483 {
|
|
1484
|
|
1485 #if (NGX_HTTP_CACHE)
|
|
1486
|
|
1487 if (p->stale && (p->lcf->use_stale & ft_type)) {
|
|
1488 ngx_http_proxy_finalize_request(p,
|
|
1489 ngx_http_proxy_send_cached_response(p));
|
|
1490 return;
|
|
1491 }
|
|
1492
|
|
1493 #endif
|
|
1494
|
|
1495 ngx_http_proxy_finalize_request(p, status);
|
|
1496 return;
|
|
1497 }
|
|
1498 }
|
|
1499
|
|
1500 if (p->lcf->busy_lock && !p->busy_locked) {
|
|
1501 ngx_http_proxy_upstream_busy_lock(p);
|
|
1502 } else {
|
|
1503 ngx_http_proxy_connect(p);
|
|
1504 }
|
|
1505 }
|