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