From 0b71f4f58ba6fd4bb680f715286867e4aea336d8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Loyet?= Date: Mon, 18 Jul 2011 00:09:30 +0000 Subject: [PATCH] - remove is_fastcgi because FPM is always fastcgi --- sapi/fpm/fpm/fastcgi.c | 36 +------ sapi/fpm/fpm/fastcgi.h | 2 - sapi/fpm/fpm/fpm_main.c | 209 ++++++++++++---------------------------- 3 files changed, 66 insertions(+), 181 deletions(-) diff --git a/sapi/fpm/fpm/fastcgi.c b/sapi/fpm/fpm/fastcgi.c index 86e82ae98a..1a50274812 100644 --- a/sapi/fpm/fpm/fastcgi.c +++ b/sapi/fpm/fpm/fastcgi.c @@ -141,7 +141,6 @@ typedef union _sa_t { static HashTable fcgi_mgmt_vars; static int is_initialized = 0; -static int is_fastcgi = 0; static int in_shutdown = 0; static in_addr_t *allowed_clients = NULL; @@ -203,9 +202,7 @@ int fcgi_init(void) return 0; } # endif - if ((GetStdHandle(STD_OUTPUT_HANDLE) == INVALID_HANDLE_VALUE) && - (GetStdHandle(STD_ERROR_HANDLE) == INVALID_HANDLE_VALUE) && - (GetStdHandle(STD_INPUT_HANDLE) != INVALID_HANDLE_VALUE)) { + { char *str; DWORD pipe_mode = PIPE_READMODE_BYTE | PIPE_WAIT; HANDLE pipe = GetStdHandle(STD_INPUT_HANDLE); @@ -224,36 +221,14 @@ int fcgi_init(void) if (str != NULL) { fcgi_accept_mutex = (HANDLE) atoi(str); } - return is_fastcgi = 1; - } else { - return is_fastcgi = 0; + return 1; } #else - errno = 0; - if (getpeername(0, (struct sockaddr *)&sa, &len) != 0 && errno == ENOTCONN) { - fcgi_setup_signals(); - return is_fastcgi = 1; - } else { - return is_fastcgi = 0; - } + fcgi_setup_signals(); + return 1; #endif } - return is_fastcgi; -} - - -int fcgi_is_fastcgi(void) -{ - if (!is_initialized) { - return fcgi_init(); - } else { - return is_fastcgi; - } -} - -void fcgi_set_is_fastcgi(int new_value) -{ - is_fastcgi = new_value; + return 1; } void fcgi_set_in_shutdown(int new_value) @@ -266,7 +241,6 @@ void fcgi_shutdown(void) if (is_initialized) { zend_hash_destroy(&fcgi_mgmt_vars); } - is_fastcgi = 0; if (allowed_clients) { free(allowed_clients); } diff --git a/sapi/fpm/fpm/fastcgi.h b/sapi/fpm/fpm/fastcgi.h index 4129def050..caac528547 100644 --- a/sapi/fpm/fpm/fastcgi.h +++ b/sapi/fpm/fpm/fastcgi.h @@ -114,12 +114,10 @@ typedef struct _fcgi_request { int fcgi_init(void); void fcgi_shutdown(void); -int fcgi_is_fastcgi(void); void fcgi_init_request(fcgi_request *req, int listen_socket); int fcgi_accept_request(fcgi_request *req); int fcgi_finish_request(fcgi_request *req, int force_close); -void fcgi_set_is_fastcgi(int new_value); void fcgi_set_in_shutdown(int); void fcgi_set_allowed_clients(char *); void fcgi_close(fcgi_request *req, int force, int destroy); diff --git a/sapi/fpm/fpm/fpm_main.c b/sapi/fpm/fpm/fpm_main.c index 4611981578..0648b0c73b 100644 --- a/sapi/fpm/fpm/fpm_main.c +++ b/sapi/fpm/fpm/fpm_main.c @@ -106,6 +106,7 @@ int __riscosify_control = __RISCOSIFY_STRICT_UNIX_SPECS; #include #include #include +#include #ifndef PHP_WIN32 /* XXX this will need to change later when threaded fastcgi is implemented. shane */ @@ -260,35 +261,16 @@ static void print_extensions(TSRMLS_D) zend_llist_destroy(&sorted_exts); } -#ifndef STDOUT_FILENO -#define STDOUT_FILENO 1 -#endif - static inline size_t sapi_cgibin_single_write(const char *str, uint str_length TSRMLS_DC) { -#ifdef PHP_WRITE_STDOUT long ret; -#else - size_t ret; -#endif - if (fcgi_is_fastcgi()) { - fcgi_request *request = (fcgi_request*) SG(server_context); - long ret = fcgi_write(request, FCGI_STDOUT, str, str_length); - if (ret <= 0) { - return 0; - } - return ret; + fcgi_request *request = (fcgi_request*) SG(server_context); + ret = fcgi_write(request, FCGI_STDOUT, str, str_length); + if (ret <= 0) { + return 0; } - -#ifdef PHP_WRITE_STDOUT - ret = write(STDOUT_FILENO, str, str_length); - if (ret <= 0) return 0; - return ret; -#else - ret = fwrite(str, 1, MIN(str_length, 16384), stdout); return ret; -#endif } static int sapi_cgibin_ub_write(const char *str, uint str_length TSRMLS_DC) @@ -313,18 +295,12 @@ static int sapi_cgibin_ub_write(const char *str, uint str_length TSRMLS_DC) static void sapi_cgibin_flush(void *server_context) { - if (fcgi_is_fastcgi()) { - fcgi_request *request = (fcgi_request*) server_context; - if ( + fcgi_request *request = (fcgi_request*) server_context; + if ( #ifndef PHP_WIN32 - !parent && + !parent && #endif - request && !fcgi_flush(request, 0)) { - php_handle_aborted_connection(); - } - return; - } - if (fflush(stdout) == EOF) { + request && !fcgi_flush(request, 0)) { php_handle_aborted_connection(); } } @@ -490,31 +466,27 @@ static int sapi_cgi_read_post(char *buffer, uint count_bytes TSRMLS_DC) count_bytes = MIN(count_bytes, (uint) SG(request_info).content_length - SG(read_post_bytes)); while (read_bytes < count_bytes) { - if (fcgi_is_fastcgi()) { - fcgi_request *request = (fcgi_request*) SG(server_context); - if (request_body_fd == -1) { - char *request_body_filename = sapi_cgibin_getenv((char *) "REQUEST_BODY_FILE", - sizeof("REQUEST_BODY_FILE") - 1 TSRMLS_CC); - - if (request_body_filename && *request_body_filename) { - request_body_fd = open(request_body_filename, O_RDONLY); - - if (0 > request_body_fd) { - php_error(E_WARNING, "REQUEST_BODY_FILE: open('%s') failed: %s (%d)", - request_body_filename, strerror(errno), errno); - return 0; - } + fcgi_request *request = (fcgi_request*) SG(server_context); + if (request_body_fd == -1) { + char *request_body_filename = sapi_cgibin_getenv((char *) "REQUEST_BODY_FILE", + sizeof("REQUEST_BODY_FILE") - 1 TSRMLS_CC); + + if (request_body_filename && *request_body_filename) { + request_body_fd = open(request_body_filename, O_RDONLY); + + if (0 > request_body_fd) { + php_error(E_WARNING, "REQUEST_BODY_FILE: open('%s') failed: %s (%d)", + request_body_filename, strerror(errno), errno); + return 0; } } + } - /* If REQUEST_BODY_FILE variable not available - read post body from fastcgi stream */ - if (request_body_fd < 0) { - tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes); - } else { - tmp_read_bytes = read(request_body_fd, buffer + read_bytes, count_bytes - read_bytes); - } + /* If REQUEST_BODY_FILE variable not available - read post body from fastcgi stream */ + if (request_body_fd < 0) { + tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes); } else { - tmp_read_bytes = read(STDIN_FILENO, buffer + read_bytes, count_bytes - read_bytes); + tmp_read_bytes = read(request_body_fd, buffer + read_bytes, count_bytes - read_bytes); } if (tmp_read_bytes <= 0) { break; @@ -526,77 +498,21 @@ static int sapi_cgi_read_post(char *buffer, uint count_bytes TSRMLS_DC) static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC) { - /* when php is started by mod_fastcgi, no regular environment - * is provided to PHP. It is always sent to PHP at the start - * of a request. So we have to do our own lookup to get env - * vars. This could probably be faster somehow. */ - if (fcgi_is_fastcgi()) { - fcgi_request *request = (fcgi_request*) SG(server_context); - return fcgi_getenv(request, name, name_len); - } - /* if cgi, or fastcgi and not found in fcgi env - check the regular environment */ - return getenv(name); + fcgi_request *request = (fcgi_request*) SG(server_context); + return fcgi_getenv(request, name, name_len); } static char *_sapi_cgibin_putenv(char *name, char *value TSRMLS_DC) { int name_len; -#if !HAVE_SETENV || !HAVE_UNSETENV - int len; - char *buf; -#endif if (!name) { return NULL; } name_len = strlen(name); - /* when php is started by mod_fastcgi, no regular environment - * is provided to PHP. It is always sent to PHP at the start - * of a request. So we have to do our own lookup to get env - * vars. This could probably be faster somehow. */ - if (fcgi_is_fastcgi()) { - fcgi_request *request = (fcgi_request*) SG(server_context); - return fcgi_putenv(request, name, name_len, value); - } - -#if HAVE_SETENV - if (value) { - setenv(name, value, 1); - } -#endif -#if HAVE_UNSETENV - if (!value) { - unsetenv(name); - } -#endif - -#if !HAVE_SETENV || !HAVE_UNSETENV - /* if cgi, or fastcgi and not found in fcgi env - check the regular environment - this leaks, but it's only cgi anyway, we'll fix - it for 5.0 - */ - len = name_len + (value ? strlen(value) : 0) + sizeof("=") + 2; - buf = (char *) malloc(len); - if (buf == NULL) { - return getenv(name); - } -#endif -#if !HAVE_SETENV - if (value) { - len = slprintf(buf, len - 1, "%s=%s", name, value); - putenv(buf); - } -#endif -#if !HAVE_UNSETENV - if (!value) { - len = slprintf(buf, len - 1, "%s=", name); - putenv(buf); - } -#endif - return getenv(name); + fcgi_request *request = (fcgi_request*) SG(server_context); + return fcgi_putenv(request, name, name_len, value); } static char *sapi_cgi_read_cookies(TSRMLS_D) @@ -606,6 +522,15 @@ static char *sapi_cgi_read_cookies(TSRMLS_D) void cgi_php_import_environment_variables(zval *array_ptr TSRMLS_DC) { + fcgi_request *request; + HashPosition pos; + int magic_quotes_gpc;; + char *var, **val; + uint var_len; + ulong idx; + int filter_arg; + + if (PG(http_globals)[TRACK_VARS_ENV] && array_ptr != PG(http_globals)[TRACK_VARS_ENV] && Z_TYPE_P(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY && @@ -631,30 +556,24 @@ void cgi_php_import_environment_variables(zval *array_ptr TSRMLS_DC) /* call php's original import as a catch-all */ php_php_import_environment_variables(array_ptr TSRMLS_CC); - if (fcgi_is_fastcgi()) { - fcgi_request *request = (fcgi_request*) SG(server_context); - HashPosition pos; - int magic_quotes_gpc = PG(magic_quotes_gpc); - char *var, **val; - uint var_len; - ulong idx; - int filter_arg = (array_ptr == PG(http_globals)[TRACK_VARS_ENV])?PARSE_ENV:PARSE_SERVER; - - /* turn off magic_quotes while importing environment variables */ - PG(magic_quotes_gpc) = 0; - for (zend_hash_internal_pointer_reset_ex(request->env, &pos); - zend_hash_get_current_key_ex(request->env, &var, &var_len, &idx, 0, &pos) == HASH_KEY_IS_STRING && - zend_hash_get_current_data_ex(request->env, (void **) &val, &pos) == SUCCESS; - zend_hash_move_forward_ex(request->env, &pos) - ) { - unsigned int new_val_len; + request = (fcgi_request*) SG(server_context); + magic_quotes_gpc = PG(magic_quotes_gpc); + filter_arg = (array_ptr == PG(http_globals)[TRACK_VARS_ENV])?PARSE_ENV:PARSE_SERVER; - if (sapi_module.input_filter(filter_arg, var, val, strlen(*val), &new_val_len TSRMLS_CC)) { - php_register_variable_safe(var, *val, new_val_len, array_ptr TSRMLS_CC); - } + /* turn off magic_quotes while importing environment variables */ + PG(magic_quotes_gpc) = 0; + for (zend_hash_internal_pointer_reset_ex(request->env, &pos); + zend_hash_get_current_key_ex(request->env, &var, &var_len, &idx, 0, &pos) == HASH_KEY_IS_STRING && + zend_hash_get_current_data_ex(request->env, (void **) &val, &pos) == SUCCESS; + zend_hash_move_forward_ex(request->env, &pos) + ) { + unsigned int new_val_len; + + if (sapi_module.input_filter(filter_arg, var, val, strlen(*val), &new_val_len TSRMLS_CC)) { + php_register_variable_safe(var, *val, new_val_len, array_ptr TSRMLS_CC); } - PG(magic_quotes_gpc) = magic_quotes_gpc; } + PG(magic_quotes_gpc) = magic_quotes_gpc; } static void sapi_cgi_register_variables(zval *track_vars_array TSRMLS_DC) @@ -700,7 +619,7 @@ static void sapi_cgi_register_variables(zval *track_vars_array TSRMLS_DC) static void sapi_cgi_log_message(char *message TSRMLS_DC) { - if (fcgi_is_fastcgi() && CGIG(fcgi_logging)) { + if (CGIG(fcgi_logging)) { fcgi_request *request; request = (fcgi_request*) SG(server_context); @@ -714,10 +633,9 @@ static void sapi_cgi_log_message(char *message TSRMLS_DC) free(buf); } else { fprintf(stderr, "%s\n", message); + //FIXME zlog(ZLOG_NOTICE, "PHP message: %s", message); } /* ignore return code */ - } else { - fprintf(stderr, "%s\n", message); } } @@ -875,16 +793,12 @@ static int sapi_cgi_deactivate(TSRMLS_D) 2. When the first call occurs and the request is not set up, flush fails on FastCGI. */ if (SG(sapi_started)) { - if (fcgi_is_fastcgi()) { - if ( + if ( #ifndef PHP_WIN32 - !parent && + !parent && #endif - !fcgi_finish_request((fcgi_request*)SG(server_context), 0)) { - php_handle_aborted_connection(); - } - } else { - sapi_cgibin_flush(SG(server_context)); + !fcgi_finish_request((fcgi_request*)SG(server_context), 0)) { + php_handle_aborted_connection(); } } return SUCCESS; @@ -1528,7 +1442,7 @@ PHP_FUNCTION(fastcgi_finish_request) /* {{{ */ { fcgi_request *request = (fcgi_request*) SG(server_context); - if (fcgi_is_fastcgi() && request->fd >= 0) { + if (request->fd >= 0) { php_output_end_all(TSRMLS_C); php_header(TSRMLS_C); @@ -1829,7 +1743,6 @@ consult the installation file that came with this distribution, or visit \n\ fcgi_fd = fpm_run(&max_requests); parent = 0; - fcgi_set_is_fastcgi(1); /* make php call us to get _ENV vars */ php_php_import_environment_variables = php_import_environment_variables; -- 2.40.0