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