Mercurial > hg > nginx
comparison src/http/modules/ngx_http_fastcgi_module.c @ 2390:27ccc4b54fa6
fastcgi_pass variables support
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 10 Dec 2008 14:22:07 +0000 |
parents | 8c17cfe63d2c |
children | d88e757cc7d8 |
comparison
equal
deleted
inserted
replaced
2389:ad08367a257b | 2390:27ccc4b54fa6 |
---|---|
17 ngx_array_t *flushes; | 17 ngx_array_t *flushes; |
18 ngx_array_t *params_len; | 18 ngx_array_t *params_len; |
19 ngx_array_t *params; | 19 ngx_array_t *params; |
20 ngx_array_t *params_source; | 20 ngx_array_t *params_source; |
21 ngx_array_t *catch_stderr; | 21 ngx_array_t *catch_stderr; |
22 | |
23 ngx_array_t *fastcgi_lengths; | |
24 ngx_array_t *fastcgi_values; | |
22 } ngx_http_fastcgi_loc_conf_t; | 25 } ngx_http_fastcgi_loc_conf_t; |
23 | 26 |
24 | 27 |
25 typedef enum { | 28 typedef enum { |
26 ngx_http_fastcgi_st_version = 0, | 29 ngx_http_fastcgi_st_version = 0, |
101 ngx_http_fastcgi_begin_request_t br; | 104 ngx_http_fastcgi_begin_request_t br; |
102 ngx_http_fastcgi_header_small_t h1; | 105 ngx_http_fastcgi_header_small_t h1; |
103 } ngx_http_fastcgi_request_start_t; | 106 } ngx_http_fastcgi_request_start_t; |
104 | 107 |
105 | 108 |
109 static ngx_int_t ngx_http_fastcgi_eval(ngx_http_request_t *r, | |
110 ngx_http_fastcgi_loc_conf_t *flcf); | |
106 static ngx_int_t ngx_http_fastcgi_create_request(ngx_http_request_t *r); | 111 static ngx_int_t ngx_http_fastcgi_create_request(ngx_http_request_t *r); |
107 static ngx_int_t ngx_http_fastcgi_reinit_request(ngx_http_request_t *r); | 112 static ngx_int_t ngx_http_fastcgi_reinit_request(ngx_http_request_t *r); |
108 static ngx_int_t ngx_http_fastcgi_process_header(ngx_http_request_t *r); | 113 static ngx_int_t ngx_http_fastcgi_process_header(ngx_http_request_t *r); |
109 static ngx_int_t ngx_http_fastcgi_input_filter(ngx_event_pipe_t *p, | 114 static ngx_int_t ngx_http_fastcgi_input_filter(ngx_event_pipe_t *p, |
110 ngx_buf_t *buf); | 115 ngx_buf_t *buf); |
412 "ngx_http_fastcgi_module does not support " | 417 "ngx_http_fastcgi_module does not support " |
413 "subrequest in memory"); | 418 "subrequest in memory"); |
414 return NGX_HTTP_INTERNAL_SERVER_ERROR; | 419 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
415 } | 420 } |
416 | 421 |
417 flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module); | |
418 | |
419 u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t)); | 422 u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t)); |
420 if (u == NULL) { | 423 if (u == NULL) { |
421 return NGX_HTTP_INTERNAL_SERVER_ERROR; | 424 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
422 } | 425 } |
423 | 426 |
424 u->schema = flcf->upstream.schema; | 427 r->upstream = u; |
428 | |
429 flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module); | |
430 | |
431 if (flcf->fastcgi_lengths) { | |
432 if (ngx_http_fastcgi_eval(r, flcf) != NGX_OK) { | |
433 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
434 } | |
435 } | |
436 | |
437 u->schema.len = sizeof("fastcgi://") - 1; | |
438 u->schema.data = (u_char *) "fastcgi://"; | |
425 | 439 |
426 u->peer.log = r->connection->log; | 440 u->peer.log = r->connection->log; |
427 u->peer.log_error = NGX_ERROR_ERR; | 441 u->peer.log_error = NGX_ERROR_ERR; |
428 #if (NGX_THREADS) | 442 #if (NGX_THREADS) |
429 u->peer.lock = &r->connection->lock; | 443 u->peer.lock = &r->connection->lock; |
447 } | 461 } |
448 | 462 |
449 u->pipe->input_filter = ngx_http_fastcgi_input_filter; | 463 u->pipe->input_filter = ngx_http_fastcgi_input_filter; |
450 u->pipe->input_ctx = r; | 464 u->pipe->input_ctx = r; |
451 | 465 |
452 r->upstream = u; | |
453 | |
454 rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init); | 466 rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init); |
455 | 467 |
456 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { | 468 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { |
457 return rc; | 469 return rc; |
458 } | 470 } |
459 | 471 |
460 return NGX_DONE; | 472 return NGX_DONE; |
473 } | |
474 | |
475 | |
476 static ngx_int_t | |
477 ngx_http_fastcgi_eval(ngx_http_request_t *r, ngx_http_fastcgi_loc_conf_t *flcf) | |
478 { | |
479 ngx_url_t u; | |
480 | |
481 ngx_memzero(&u, sizeof(ngx_url_t)); | |
482 | |
483 if (ngx_http_script_run(r, &u.url, flcf->fastcgi_lengths->elts, 0, | |
484 flcf->fastcgi_values->elts) | |
485 == NULL) | |
486 { | |
487 return NGX_ERROR; | |
488 } | |
489 | |
490 u.no_resolve = 1; | |
491 | |
492 if (ngx_parse_url(r->pool, &u) != NGX_OK) { | |
493 if (u.err) { | |
494 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
495 "%s in upstream \"%V\"", u.err, &u.url); | |
496 } | |
497 | |
498 return NGX_ERROR; | |
499 } | |
500 | |
501 r->upstream->resolved = ngx_pcalloc(r->pool, | |
502 sizeof(ngx_http_upstream_resolved_t)); | |
503 if (r->upstream->resolved == NULL) { | |
504 return NGX_ERROR; | |
505 } | |
506 | |
507 r->upstream->resolved->host = u.host; | |
508 r->upstream->resolved->port = u.port; | |
509 | |
510 return NGX_OK; | |
461 } | 511 } |
462 | 512 |
463 | 513 |
464 static ngx_int_t | 514 static ngx_int_t |
465 ngx_http_fastcgi_create_request(ngx_http_request_t *r) | 515 ngx_http_fastcgi_create_request(ngx_http_request_t *r) |
1862 if (conf->upstream.upstream == NULL) { | 1912 if (conf->upstream.upstream == NULL) { |
1863 conf->upstream.upstream = prev->upstream.upstream; | 1913 conf->upstream.upstream = prev->upstream.upstream; |
1864 conf->upstream.schema = prev->upstream.schema; | 1914 conf->upstream.schema = prev->upstream.schema; |
1865 } | 1915 } |
1866 | 1916 |
1917 if (conf->fastcgi_lengths == NULL) { | |
1918 conf->fastcgi_lengths = prev->fastcgi_lengths; | |
1919 conf->fastcgi_values = prev->fastcgi_values; | |
1920 } | |
1921 | |
1867 if (conf->params_source == NULL) { | 1922 if (conf->params_source == NULL) { |
1868 conf->flushes = prev->flushes; | 1923 conf->flushes = prev->flushes; |
1869 conf->params_len = prev->params_len; | 1924 conf->params_len = prev->params_len; |
1870 conf->params = prev->params; | 1925 conf->params = prev->params; |
1871 conf->params_source = prev->params_source; | 1926 conf->params_source = prev->params_source; |
2042 | 2097 |
2043 | 2098 |
2044 static char * | 2099 static char * |
2045 ngx_http_fastcgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | 2100 ngx_http_fastcgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
2046 { | 2101 { |
2047 ngx_http_fastcgi_loc_conf_t *lcf = conf; | 2102 ngx_http_fastcgi_loc_conf_t *flcf = conf; |
2048 | 2103 |
2049 ngx_url_t u; | 2104 ngx_url_t u; |
2050 ngx_str_t *value; | 2105 ngx_str_t *value, *url; |
2051 ngx_http_core_loc_conf_t *clcf; | 2106 ngx_uint_t n; |
2052 | 2107 ngx_http_core_loc_conf_t *clcf; |
2053 if (lcf->upstream.schema.len) { | 2108 ngx_http_script_compile_t sc; |
2109 | |
2110 if (flcf->upstream.schema.len) { | |
2054 return "is duplicate"; | 2111 return "is duplicate"; |
2055 } | 2112 } |
2056 | 2113 |
2114 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | |
2115 clcf->handler = ngx_http_fastcgi_handler; | |
2116 | |
2117 flcf->upstream.schema.len = sizeof("fastcgi://") - 1; | |
2118 flcf->upstream.schema.data = (u_char *) "fastcgi://"; | |
2119 | |
2057 value = cf->args->elts; | 2120 value = cf->args->elts; |
2121 | |
2122 url = &value[1]; | |
2123 | |
2124 n = ngx_http_script_variables_count(url); | |
2125 | |
2126 if (n) { | |
2127 | |
2128 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
2129 | |
2130 sc.cf = cf; | |
2131 sc.source = url; | |
2132 sc.lengths = &flcf->fastcgi_lengths; | |
2133 sc.values = &flcf->fastcgi_values; | |
2134 sc.variables = n; | |
2135 sc.complete_lengths = 1; | |
2136 sc.complete_values = 1; | |
2137 | |
2138 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
2139 return NGX_CONF_ERROR; | |
2140 } | |
2141 | |
2142 return NGX_CONF_OK; | |
2143 } | |
2058 | 2144 |
2059 ngx_memzero(&u, sizeof(ngx_url_t)); | 2145 ngx_memzero(&u, sizeof(ngx_url_t)); |
2060 | 2146 |
2061 u.url = value[1]; | 2147 u.url = value[1]; |
2062 u.no_resolve = 1; | 2148 u.no_resolve = 1; |
2063 | 2149 |
2064 lcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0); | 2150 flcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0); |
2065 if (lcf->upstream.upstream == NULL) { | 2151 if (flcf->upstream.upstream == NULL) { |
2066 return NGX_CONF_ERROR; | 2152 return NGX_CONF_ERROR; |
2067 } | 2153 } |
2068 | |
2069 lcf->upstream.schema.len = sizeof("fastcgi://") - 1; | |
2070 lcf->upstream.schema.data = (u_char *) "fastcgi://"; | |
2071 | |
2072 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | |
2073 | |
2074 clcf->handler = ngx_http_fastcgi_handler; | |
2075 | 2154 |
2076 if (clcf->name.data[clcf->name.len - 1] == '/') { | 2155 if (clcf->name.data[clcf->name.len - 1] == '/') { |
2077 clcf->auto_redirect = 1; | 2156 clcf->auto_redirect = 1; |
2078 } | 2157 } |
2079 | 2158 |