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