#include <fpm/fpm_conf.h>
#include <fpm/fpm_php.h>
#include <fpm/fpm_log.h>
+#include <fpm/zlog.h>
#ifndef PHP_WIN32
/* XXX this will need to change later when threaded fastcgi is implemented. shane */
#endif
static int request_body_fd;
+static int fpm_is_running = 0;
static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC);
static void fastcgi_ini_parser(zval *arg1, zval *arg2, zval *arg3, int callback_type, void *arg TSRMLS_DC);
zend_llist_destroy(&sorted_exts);
}
-#ifndef STDOUT_FILENO
-#define STDOUT_FILENO 1
+#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()) {
+ /* sapi has started which means everyhting must be send through fcgi */
+ if (fpm_is_running) {
fcgi_request *request = (fcgi_request*) SG(server_context);
- long ret = fcgi_write(request, FCGI_STDOUT, str, str_length);
+ ret = fcgi_write(request, FCGI_STDOUT, str, str_length);
if (ret <= 0) {
return 0;
}
return ret;
}
-#ifdef PHP_WRITE_STDOUT
- ret = write(STDOUT_FILENO, str, str_length);
- if (ret <= 0) return 0;
+ /* sapi has not started, output to stdout instead of fcgi */
+#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;
+ return fwrite(str, 1, MIN(str_length, 16384), stdout);
#endif
}
static void sapi_cgibin_flush(void *server_context)
{
- if (fcgi_is_fastcgi()) {
+ /* fpm has started, let use fcgi instead of stdout */
+ if (fpm_is_running) {
fcgi_request *request = (fcgi_request*) server_context;
if (
#ifndef PHP_WIN32
- !parent &&
+ !parent &&
#endif
- request && !fcgi_flush(request, 0)) {
+ request && !fcgi_flush(request, 0)) {
php_handle_aborted_connection();
}
return;
}
+
+ /* fpm has not started yet, let use stdout instead of fcgi */
if (fflush(stdout) == EOF) {
php_handle_aborted_connection();
}
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;
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()) {
+ /* if fpm has started, use fcgi env */
+ if (fpm_is_running) {
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 */
+
+ /* if fpm has not started yet, use std env */
return getenv(name);
}
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)
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 &&
/* 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)
{
TSRMLS_FETCH();
- if (fcgi_is_fastcgi() && CGIG(fcgi_logging)) {
+ if (CGIG(fcgi_logging)) {
fcgi_request *request;
request = (fcgi_request*) SG(server_context);
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);
}
}
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;
{
fcgi_request *request = (fcgi_request*) SG(server_context);
- if (fcgi_is_fastcgi() && request->fd >= 0) {
+ if (request->fd >= 0) {
php_end_ob_buffers(1 TSRMLS_CC);
php_header(TSRMLS_C);
return FAILURE;
}
+ fpm_is_running = 1;
+
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;