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