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