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