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