summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Stogov <dmitry@php.net>2006-05-15 14:30:31 +0000
committerDmitry Stogov <dmitry@php.net>2006-05-15 14:30:31 +0000
commitb5cd968ffc8fe71cbf8bf96b84b80883e94f1d2b (patch)
tree8f3031fad2ccc268e14ff5606549e4708bf99fef
parent7975b4eb0e263e83fea8c19fddfc5cae4a8f9fbf (diff)
downloadphp-git-b5cd968ffc8fe71cbf8bf96b84b80883e94f1d2b.tar.gz
- Removed source compatibility with libfcgi
- Optimized access to FastCGI environment using HashTable instead of linear search - Allowed PHP_FCGI_MAX_REQUESTS=0 to disable PHP die - Allowed PHP_FCGI_CHILDREN=0 to disable PHP spawn workers
-rw-r--r--sapi/cgi/cgi_main.c163
-rw-r--r--sapi/cgi/fastcgi.c159
-rw-r--r--sapi/cgi/fastcgi.h57
3 files changed, 152 insertions, 227 deletions
diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c
index 00efce21e1..9b0f12c8a9 100644
--- a/sapi/cgi/cgi_main.c
+++ b/sapi/cgi/cgi_main.c
@@ -17,6 +17,7 @@
| Zeev Suraski <zeev@zend.com> |
| FastCGI: Ben Mansell <php@slimyhorror.com> |
| Shane Caraveo <shane@caraveo.com> |
+ | Dmitry Stogov <dmitry@zend.com> |
+----------------------------------------------------------------------+
*/
@@ -81,9 +82,8 @@ int __riscosify_control = __RISCOSIFY_STRICT_UNIX_SPECS;
#if PHP_FASTCGI
#include "fastcgi.h"
-#ifdef PHP_WIN32
-extern int OS_SetImpersonate(void);
-#else
+
+#ifndef PHP_WIN32
/* XXX this will need to change later when threaded fastcgi is
implemented. shane */
struct sigaction act, old_term, old_quit, old_int;
@@ -234,9 +234,9 @@ static inline size_t sapi_cgibin_single_write(const char *str, uint str_length T
#endif
#if PHP_FASTCGI
- if (!FCGX_IsCGI()) {
- FCGX_Request *request = (FCGX_Request *) SG(server_context);
- long ret = FCGX_PutStr(str, str_length, request->out);
+ 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;
}
@@ -276,13 +276,13 @@ static int sapi_cgibin_ub_write(const char *str, uint str_length TSRMLS_DC)
static void sapi_cgibin_flush(void *server_context)
{
#if PHP_FASTCGI
- if (!FCGX_IsCGI()) {
- FCGX_Request *request = (FCGX_Request *) server_context;
+ if (fcgi_is_fastcgi()) {
+ fcgi_request *request = (fcgi_request*) server_context;
if (
#ifndef PHP_WIN32
!parent &&
#endif
- request && FCGX_FFlush(request->out) == -1) {
+ request && fcgi_flush(request, 0) == -1) {
php_handle_aborted_connection();
}
return;
@@ -349,9 +349,9 @@ 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 PHP_FASTCGI
- if (!FCGX_IsCGI()) {
- FCGX_Request *request = (FCGX_Request *) SG(server_context);
- tmp_read_bytes = FCGX_GetStr(pos, count_bytes - read_bytes, request->in);
+ if (fcgi_is_fastcgi()) {
+ fcgi_request *request = (fcgi_request*) SG(server_context);
+ tmp_read_bytes = fcgi_read(request, pos, count_bytes - read_bytes);
pos += tmp_read_bytes;
} else {
tmp_read_bytes = read(0, buffer + read_bytes, count_bytes - read_bytes);
@@ -375,9 +375,9 @@ static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC)
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 (!FCGX_IsCGI()) {
- FCGX_Request *request = (FCGX_Request *) SG(server_context);
- return FCGX_GetParam(name,request->envp);
+ if (fcgi_is_fastcgi()) {
+ fcgi_request *request = (fcgi_request*) SG(server_context);
+ return fcgi_getenv(request, name, name_len);
}
#endif
/* if cgi, or fastcgi and not found in fcgi env
@@ -387,31 +387,23 @@ static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC)
static char *_sapi_cgibin_putenv(char *name, char *value TSRMLS_DC)
{
- int len = 0;
- char *buf = NULL;
+ int name_len;
+ int len;
+ char *buf;
+
if (!name) {
return NULL;
}
- len = strlen(name) + (value ? strlen(value) : 0) + sizeof("=") + 2;
- buf = (char *) malloc(len);
- if (buf == NULL) {
- return getenv(name);
- }
- if (value) {
- snprintf(buf, len - 1, "%s=%s", name, value);
- } else {
- snprintf(buf, len - 1, "%s=", name);
- }
+ name_len = strlen(name);
+
#if PHP_FASTCGI
/* 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 (!FCGX_IsCGI()) {
- FCGX_Request *request = (FCGX_Request *) SG(server_context);
- FCGX_PutEnv(request, buf);
- free(buf);
- return sapi_cgibin_getenv(name,0 TSRMLS_CC);
+ if (fcgi_is_fastcgi()) {
+ fcgi_request *request = (fcgi_request*) SG(server_context);
+ return fcgi_putenv(request, name, name_len, value);
}
#endif
/* if cgi, or fastcgi and not found in fcgi env
@@ -419,13 +411,23 @@ static char *_sapi_cgibin_putenv(char *name, char *value TSRMLS_DC)
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);
+ }
+ if (value) {
+ len = snprintf(buf, len - 1, "%s=%s", name, value);
+ } else {
+ len = snprintf(buf, len - 1, "%s=", name);
+ }
putenv(buf);
return getenv(name);
}
static char *sapi_cgi_read_cookies(TSRMLS_D)
{
- return sapi_cgibin_getenv((char *) "HTTP_COOKIE", 0 TSRMLS_CC);
+ return sapi_cgibin_getenv((char *) "HTTP_COOKIE", sizeof("HTTP_COOKIE")-1 TSRMLS_CC);
}
#if PHP_FASTCGI
@@ -446,21 +448,21 @@ void cgi_php_import_environment_variables(zval *array_ptr TSRMLS_DC)
zval_copy_ctor(array_ptr);
return;
}
- if (!FCGX_IsCGI()) {
- FCGX_Request *request = (FCGX_Request *) SG(server_context);
- char **env, *p;
+ 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;
/* turn off magic_quotes while importing environment variables */
PG(magic_quotes_gpc) = 0;
- for (env = request->envp; env != NULL && *env != NULL; env++) {
- p = strchr(*env, '=');
- if (!p) { /* malformed entry? */
- continue;
- }
- *p = 0;
- php_register_variable(*env, p + 1, array_ptr TSRMLS_CC);
- *p = '=';
+ 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)) {
+ php_register_variable(var, *val, array_ptr TSRMLS_CC);
}
PG(magic_quotes_gpc) = magic_quotes_gpc;
}
@@ -482,13 +484,21 @@ static void sapi_cgi_register_variables(zval *track_vars_array TSRMLS_DC)
static void sapi_cgi_log_message(char *message)
{
#if PHP_FASTCGI
- if (!FCGX_IsCGI() && fcgi_logging) {
- FCGX_Request *request;
+ if (fcgi_is_fastcgi() && fcgi_logging) {
+ fcgi_request *request;
TSRMLS_FETCH();
- request = (FCGX_Request *) SG(server_context);
- if (request) {
- FCGX_FPrintF(request->err, "%s\n", message);
+ request = (fcgi_request*) SG(server_context);
+ if (request) {
+ int len = strlen(message);
+ char *buf = malloc(len+2);
+
+ memcpy(buf, message, len);
+ memcpy(buf + len, "\n", sizeof("\n"));
+ fcgi_write(request, FCGI_STDERR, buf, len+1);
+ free(buf);
+ } else {
+ fprintf(stderr, "%s\n", message);
}
/* ignore return code */
} else
@@ -663,8 +673,8 @@ static void php_cgi_usage(char *argv0)
*/
static void init_request_info(TSRMLS_D)
{
- char *env_script_filename = sapi_cgibin_getenv("SCRIPT_FILENAME", 0 TSRMLS_CC);
- char *env_path_translated = sapi_cgibin_getenv("PATH_TRANSLATED", 0 TSRMLS_CC);
+ char *env_script_filename = sapi_cgibin_getenv("SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME")-1 TSRMLS_CC);
+ char *env_path_translated = sapi_cgibin_getenv("PATH_TRANSLATED", sizeof("PATH_TRANSLATED")-1 TSRMLS_CC);
char *script_path_translated = env_script_filename;
#if !DISCARD_PATH
@@ -692,14 +702,14 @@ static void init_request_info(TSRMLS_D)
of the script will be retreived later via argc/argv */
if (script_path_translated) {
const char *auth;
- char *content_length = sapi_cgibin_getenv("CONTENT_LENGTH", 0 TSRMLS_CC);
- char *content_type = sapi_cgibin_getenv("CONTENT_TYPE", 0 TSRMLS_CC);
- char *env_path_info = sapi_cgibin_getenv("PATH_INFO", 0 TSRMLS_CC);
- char *env_script_name = sapi_cgibin_getenv("SCRIPT_NAME", 0 TSRMLS_CC);
+ char *content_length = sapi_cgibin_getenv("CONTENT_LENGTH", sizeof("CONTENT_LENGTH")-1 TSRMLS_CC);
+ char *content_type = sapi_cgibin_getenv("CONTENT_TYPE", sizeof("CONTENT_TYPE")-1 TSRMLS_CC);
+ char *env_path_info = sapi_cgibin_getenv("PATH_INFO", sizeof("PATH_INFO")-1 TSRMLS_CC);
+ char *env_script_name = sapi_cgibin_getenv("SCRIPT_NAME", sizeof("SCRIPT_NAME")-1 TSRMLS_CC);
#if ENABLE_PATHINFO_CHECK
struct stat st;
- char *env_redirect_url = sapi_cgibin_getenv("REDIRECT_URL", 0 TSRMLS_CC);
- char *env_document_root = sapi_cgibin_getenv("DOCUMENT_ROOT", 0 TSRMLS_CC);
+ char *env_redirect_url = sapi_cgibin_getenv("REDIRECT_URL", sizeof("REDIRECT_URL")-1 TSRMLS_CC);
+ char *env_document_root = sapi_cgibin_getenv("DOCUMENT_ROOT", sizeof("DOCUMENT_ROOT")-1 TSRMLS_CC);
if (fix_pathinfo) {
@@ -849,7 +859,7 @@ static void init_request_info(TSRMLS_D)
_sapi_cgibin_putenv("PATH_INFO", NULL TSRMLS_CC);
_sapi_cgibin_putenv("PATH_TRANSLATED", NULL TSRMLS_CC);
}
- SG(request_info).request_uri = sapi_cgibin_getenv("SCRIPT_NAME",0 TSRMLS_CC);
+ SG(request_info).request_uri = sapi_cgibin_getenv("SCRIPT_NAME", sizeof("SCRIPT_NAME")-1 TSRMLS_CC);
} else {
#endif
/* pre 4.3 behaviour, shouldn't be used but provides BC */
@@ -865,9 +875,9 @@ static void init_request_info(TSRMLS_D)
#if ENABLE_PATHINFO_CHECK
}
#endif
- SG(request_info).request_method = sapi_cgibin_getenv("REQUEST_METHOD", 0 TSRMLS_CC);
+ SG(request_info).request_method = sapi_cgibin_getenv("REQUEST_METHOD", sizeof("REQUEST_METHOD")-1 TSRMLS_CC);
/* FIXME - Work out proto_num here */
- SG(request_info).query_string = sapi_cgibin_getenv("QUERY_STRING", 0 TSRMLS_CC);
+ SG(request_info).query_string = sapi_cgibin_getenv("QUERY_STRING", sizeof("QUERY_STRING")-1 TSRMLS_CC);
/* some server configurations allow '..' to slip through in the
translated path. We'll just refuse to handle such a path. */
if (script_path_translated && !strstr(script_path_translated, "..")) {
@@ -877,7 +887,7 @@ static void init_request_info(TSRMLS_D)
SG(request_info).content_length = (content_length ? atoi(content_length) : 0);
/* The CGI RFC allows servers to pass on unvalidated Authorization data */
- auth = sapi_cgibin_getenv("HTTP_AUTHORIZATION",0 TSRMLS_CC);
+ auth = sapi_cgibin_getenv("HTTP_AUTHORIZATION", sizeof("HTTP_AUTHORIZATION")-1 TSRMLS_CC);
php_handle_auth_data(auth TSRMLS_CC);
}
}
@@ -984,12 +994,12 @@ int main(int argc, char *argv[])
#if PHP_FASTCGI
int max_requests = 500;
int requests = 0;
- int fastcgi = !FCGX_IsCGI();
+ int fastcgi = fcgi_is_fastcgi();
#ifndef PHP_WIN32
char *bindpath = NULL;
#endif
int fcgi_fd = 0;
- FCGX_Request request;
+ fcgi_request request;
#ifdef PHP_WIN32
long impersonate = 0;
#else
@@ -1169,14 +1179,10 @@ consult the installation file that came with this distribution, or visit \n\
/* for windows, socket listening is broken in the fastcgi library itself
so dissabling this feature on windows till time is available to fix it */
if (bindpath) {
- /* this must be done to make FCGX_OpenSocket work correctly
- bug 23664 */
- close(0);
/* Pass on the arg to the FastCGI library, with one exception.
* If just a port is specified, then we prepend a ':' onto the
* path (it's what the fastcgi library expects)
- */
-
+ */
if (strchr(bindpath, ':') == NULL && is_port_number(bindpath)) {
char *tmp;
@@ -1184,10 +1190,10 @@ consult the installation file that came with this distribution, or visit \n\
tmp[0] = ':';
memcpy(tmp + 1, bindpath, strlen(bindpath) + 1);
- fcgi_fd = FCGX_OpenSocket(tmp, 128);
+ fcgi_fd = fcgi_listen(tmp, 128);
free(tmp);
} else {
- fcgi_fd = FCGX_OpenSocket(bindpath, 128);
+ fcgi_fd = fcgi_listen(bindpath, 128);
}
if (fcgi_fd < 0) {
fprintf(stderr, "Couldn't create FastCGI listen socket on port %s\n", bindpath);
@@ -1196,14 +1202,14 @@ consult the installation file that came with this distribution, or visit \n\
#endif
return FAILURE;
}
- fastcgi = !FCGX_IsCGI();
+ fastcgi = fcgi_is_fastcgi();
}
#endif
if (fastcgi) {
/* How many times to run PHP scripts before dying */
if (getenv("PHP_FCGI_MAX_REQUESTS")) {
max_requests = atoi(getenv("PHP_FCGI_MAX_REQUESTS"));
- if (!max_requests) {
+ if (max_requests < 0) {
fprintf(stderr, "PHP_FCGI_MAX_REQUESTS is not valid\n");
return FAILURE;
}
@@ -1214,14 +1220,13 @@ consult the installation file that came with this distribution, or visit \n\
php_import_environment_variables = cgi_php_import_environment_variables;
/* library is already initialized, now init our request */
- FCGX_Init();
- FCGX_InitRequest(&request, fcgi_fd, 0);
+ fcgi_init_request(&request, fcgi_fd);
#ifndef PHP_WIN32
/* Pre-fork, if required */
if (getenv("PHP_FCGI_CHILDREN")) {
children = atoi(getenv("PHP_FCGI_CHILDREN"));
- if (!children) {
+ if (children < 0) {
fprintf(stderr, "PHP_FCGI_CHILDREN is not valid\n");
return FAILURE;
}
@@ -1323,10 +1328,10 @@ consult the installation file that came with this distribution, or visit \n\
if (cfg_get_long("fastcgi.impersonate", &impersonate) == FAILURE) {
impersonate = 0;
}
- if (impersonate) OS_SetImpersonate();
+ if (impersonate) fcgi_impersonate();
}
#endif
- while (!fastcgi || FCGX_Accept_r(&request) >= 0) {
+ while (!fastcgi || fcgi_accept_request(&request) >= 0) {
#endif
#if PHP_FASTCGI
@@ -1540,7 +1545,7 @@ consult the installation file that came with this distribution, or visit \n\
if (php_request_startup(TSRMLS_C) == FAILURE) {
#if PHP_FASTCGI
if (fastcgi) {
- FCGX_Finish_r(&request);
+ fcgi_finish_request(&request);
}
#endif
php_module_shutdown(TSRMLS_C);
@@ -1679,7 +1684,7 @@ fastcgi_request_done:
/* only fastcgi will get here */
requests++;
if (max_requests && (requests == max_requests)) {
- FCGX_Finish_r(&request);
+ fcgi_finish_request(&request);
#ifndef PHP_WIN32
if (bindpath) {
free(bindpath);
diff --git a/sapi/cgi/fastcgi.c b/sapi/cgi/fastcgi.c
index 94a2cf67ca..4a933a99f3 100644
--- a/sapi/cgi/fastcgi.c
+++ b/sapi/cgi/fastcgi.c
@@ -18,8 +18,8 @@
/* $Id$ */
-#include "fastcgi.h"
#include "php.h"
+#include "fastcgi.h"
#include <string.h>
#include <stdlib.h>
@@ -125,14 +125,15 @@ typedef union _sa_t {
} sa_t;
typedef struct _fcgi_mgmt_rec {
- char* name;
- char val;
+ char* name;
+ size_t name_len;
+ char val;
} fcgi_mgmt_rec;
static const fcgi_mgmt_rec fcgi_mgmt_vars[] = {
- {"FCGI_MAX_CONNS", 1},
- {"FCGI_MAX_REQS", 1},
- {"FCGI_MPXS_CONNS", 0}
+ {"FCGI_MAX_CONNS", sizeof("FCGI_MAX_CONNS")-1, 1},
+ {"FCGI_MAX_REQS", sizeof("FCGI_MAX_REQS")-1, 1},
+ {"FCGI_MPXS_CONNS", sizeof("FCGI_MPXS_CONNS")-1, 0}
};
@@ -140,13 +141,6 @@ static int is_initialized = 0;
static int is_fastcgi = 0;
static int in_shutdown = 0;
-static inline char* fcgi_strndup(const char *str, int len)
-{
- char *s = malloc(len+1);
- memcpy(s, str, len+1);
- return s;
-}
-
#ifdef _WIN32
static DWORD WINAPI fcgi_shutdown_thread(LPVOID arg)
@@ -396,8 +390,11 @@ static inline int fcgi_make_header(fcgi_header *hdr, fcgi_request_type type, int
return pad;
}
-static int fcgi_get_params(fcgi_request *req, unsigned char *p, unsigned char *end, int n)
+static void fcgi_get_params(fcgi_request *req, unsigned char *p, unsigned char *end)
{
+ char buf[128];
+ char *tmp = buf;
+ size_t buf_size = sizeof(buf);
int name_len, val_len;
char *s;
@@ -416,35 +413,37 @@ static int fcgi_get_params(fcgi_request *req, unsigned char *p, unsigned char *e
val_len |= (*p++ << 8);
val_len |= *p++;
}
- req->env[n] = s = malloc(name_len + val_len + 2);
- memcpy(s, p, name_len);
- p += name_len;
- s[name_len] = '=';
- memcpy(s+name_len+1, p, val_len);
- p += val_len;
- s[name_len+1+val_len] = '\0';
- n++;
- if (n > sizeof(req->env)/sizeof(req->env[0])) {
- /* TODO: to many environment variables */
- return n;
+ if (name_len+1 >= buf_size) {
+ buf_size = name_len + 64;
+ tmp = (tmp == buf ? emalloc(buf_size): erealloc(tmp, buf_size));
}
+ memcpy(tmp, p, name_len);
+ tmp[name_len] = 0;
+ s = zend_strndup((char*)p + name_len, val_len);
+ zend_hash_update(&req->env, tmp, name_len+1, &s, sizeof(char*), NULL);
+ p += name_len + val_len;
+ }
+ if (tmp != buf && tmp != NULL) {
+ efree(tmp);
}
- return n;
+}
+
+static void fcgi_free_var(char **s)
+{
+ free(*s);
}
static int fcgi_read_request(fcgi_request *req)
{
fcgi_header hdr;
int len, padding;
- int n = 1;
- char *s;
unsigned char buf[FCGI_MAX_LENGTH+8];
req->keep = 0;
req->in_len = 0;
req->out_hdr = NULL;
req->out_pos = req->out_buf;
- memset(req->env, 0, sizeof(req->env));
+ zend_hash_init(&req->env, 0, NULL, (void (*)(void *)) fcgi_free_var, 1);
if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) ||
hdr.version < FCGI_VERSION_1) {
@@ -467,6 +466,8 @@ static int fcgi_read_request(fcgi_request *req)
req->id = (hdr.requestIdB1 << 8) + hdr.requestIdB0;
if (hdr.type == FCGI_BEGIN_REQUEST && len == sizeof(fcgi_begin_request)) {
+ char *val;
+
if (safe_read(req, buf, len+padding) != len+padding) {
return 0;
}
@@ -474,13 +475,16 @@ static int fcgi_read_request(fcgi_request *req)
req->keep = (((fcgi_begin_request*)buf)->flags & FCGI_KEEP_CONN);
switch ((((fcgi_begin_request*)buf)->roleB1 << 8) + ((fcgi_begin_request*)buf)->roleB0) {
case FCGI_RESPONDER:
- req->env[0] = fcgi_strndup("FCGI_ROLE=RESPONDER", sizeof("FCGI_ROLE=RESPONDER")-1);
+ val = strdup("RESPONDER");
+ zend_hash_update(&req->env, "FCGI_ROLE", sizeof("FCGI_ROLE"), &val, sizeof(char*), NULL);
break;
case FCGI_AUTHORIZER:
- req->env[0] = fcgi_strndup("FCGI_ROLE=AUTHORIZER", sizeof("FCGI_ROLE=AUTHORIZER")-1);
+ val = strdup("AUTHORIZER");
+ zend_hash_update(&req->env, "FCGI_ROLE", sizeof("FCGI_ROLE"), &val, sizeof(char*), NULL);
break;
case FCGI_FILTER:
- req->env[0] = fcgi_strndup("FCGI_ROLE=FILTER", sizeof("FCGI_ROLE=FILTER")-1);
+ val = strdup("FILTER");
+ zend_hash_update(&req->env, "FCGI_ROLE", sizeof("FCGI_ROLE"), val, sizeof(char*), NULL);
break;
default:
return 0;
@@ -499,7 +503,7 @@ static int fcgi_read_request(fcgi_request *req)
req->keep = 0;
return 0;
}
- n = fcgi_get_params(req, buf, buf+len, n);
+ fcgi_get_params(req, buf, buf+len);
if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) ||
hdr.version < FCGI_VERSION_1) {
@@ -510,26 +514,18 @@ static int fcgi_read_request(fcgi_request *req)
padding = hdr.paddingLength;
}
} else if (hdr.type == FCGI_GET_VALUES) {
- int i, j;
- int name_len;
+ int j;
unsigned char *p = buf + sizeof(fcgi_header);
if (safe_read(req, buf, len+padding) != len+padding) {
return 0;
}
- n = fcgi_get_params(req, buf, buf+len, 0);
- for (i = 0; i < n; i++) {
- if ((s = strchr(req->env[i], '=')) != NULL) {
- *s = '\0';
- name_len = s - req->env[i];
- } else {
- name_len = strlen(req->env[i]);
- }
- for (j = 0; j < sizeof(fcgi_mgmt_vars)/sizeof(fcgi_mgmt_vars[0]); j++) {
- if (strncmp(req->env[i], fcgi_mgmt_vars[j].name, name_len) == 0) {
- sprintf((char*)p, "%c%c%s%c", name_len, 1, fcgi_mgmt_vars[j].name, fcgi_mgmt_vars[j].val);
- p += name_len+3;
- }
+ fcgi_get_params(req, buf, buf+len);
+
+ for (j = 0; j < sizeof(fcgi_mgmt_vars)/sizeof(fcgi_mgmt_vars[0]); j++) {
+ if (zend_hash_exists(&req->env, fcgi_mgmt_vars[j].name, fcgi_mgmt_vars[j].name_len+1) == 0) {
+ sprintf((char*)p, "%c%c%s%c", fcgi_mgmt_vars[j].name_len, 1, fcgi_mgmt_vars[j].name, fcgi_mgmt_vars[j].val);
+ p += fcgi_mgmt_vars[j].name_len + 3;
}
}
len = p - buf - sizeof(fcgi_header);
@@ -601,10 +597,7 @@ int fcgi_read(fcgi_request *req, char *str, int len)
static inline void fcgi_close(fcgi_request *req, int force, int destroy)
{
if (destroy) {
- char **env = req->env;
- while (*env) {
- free(*env++);
- }
+ zend_hash_destroy(&req->env);
}
if ((force || !req->keep) && req->fd >= 0) {
#ifdef _WIN32
@@ -866,62 +859,36 @@ int fcgi_finish_request(fcgi_request *req)
return 1;
}
-char* fcgi_getenv_helper(char** env, const char *name, int len)
-{
- if (name && env) {
- while (*env) {
- if ((strncmp(name, *env, len) == 0) && ((*env)[len] == '=')) {
- return *env+len+1;
- }
- env++;
- }
- }
- return NULL;
-}
-
char* fcgi_getenv(fcgi_request *req, const char* var, int var_len)
{
+ char **val;
+
if (!req) return NULL;
- return fcgi_getenv_helper(req->env, var, var_len);
-}
-void fcgi_putenv(fcgi_request *req, char* var, int var_len)
-{
- if (var && req) {
- char **env = req->env;
- char *s = strchr(var, '=');
- int len;
-
- if (!s) return ;
- len = s - var + 1;
- while (*env) {
- if ((strncmp(var, *env, len) == 0)) {
- free(*env);
- *env = fcgi_strndup(var, var_len);
- return;
- }
- env++;
- }
- *env = fcgi_strndup(var, var_len);
+ if (zend_hash_find(&req->env, (char*)var, var_len+1, (void**)&val) == SUCCESS) {
+ return *val;
}
+ return NULL;
}
-int FCGX_FPrintF(FCGX_Stream stream, const char *format, ...)
+char* fcgi_putenv(fcgi_request *req, char* var, int var_len, char* val)
{
- int result;
- va_list args;
- char buf[4096];
-
- va_start(args, format);
- result = vsnprintf(buf, sizeof(buf), format, args);
- va_end(args);
+ if (var && req) {
+ char **ret;
- fcgi_write(stream.req, stream.type, buf, result);
- return result;
+ if (val == NULL) {
+ val = "";
+ }
+ val = strdup(val);
+ if (zend_hash_update(&req->env, var, var_len+1, &val, sizeof(char*), (void**)&ret) == SUCCESS) {
+ return *ret;
+ }
+ }
+ return NULL;
}
#ifdef _WIN32
-void OS_SetImpersonate(void)
+void fcgi_impersonate(void)
{
char *os_name;
diff --git a/sapi/cgi/fastcgi.h b/sapi/cgi/fastcgi.h
index 7c910e1abb..ba648f3ea5 100644
--- a/sapi/cgi/fastcgi.h
+++ b/sapi/cgi/fastcgi.h
@@ -105,7 +105,7 @@ typedef struct _fcgi_request {
unsigned char out_buf[1024*8];
unsigned char reserved[sizeof(fcgi_end_request_rec)];
- char *env[128];
+ HashTable env;
} fcgi_request;
int fcgi_init(void);
@@ -116,63 +116,16 @@ int fcgi_accept_request(fcgi_request *req);
int fcgi_finish_request(fcgi_request *req);
char* fcgi_getenv(fcgi_request *req, const char* var, int var_len);
-void fcgi_putenv(fcgi_request *req, char* var, int var_len);
+char* fcgi_putenv(fcgi_request *req, char* var, int var_len, char* val);
int fcgi_read(fcgi_request *req, char *str, int len);
int fcgi_write(fcgi_request *req, fcgi_request_type type, const char *str, int len);
int fcgi_flush(fcgi_request *req, int close);
-/* Some defines for limited libfcgi comatibility */
-
-typedef struct _FCGX_Stream {
- fcgi_request *req;
- fcgi_request_type type;
-} FCGX_Stream;
-
-typedef struct _FCGX_Request {
- fcgi_request req;
- FCGX_Stream in;
- FCGX_Stream out;
- FCGX_Stream err;
- char **envp;
-} FCGX_Request;
-
-#define FCGX_Init()
-#define FCGX_IsCGI() (!fcgi_is_fastcgi())
-#define FCGX_OpenSocket(path, backlog) fcgi_listen(path, backlog)
-
-#define FCGX_InitRequest(r, sock, flags) \
- do { \
- fcgi_init_request(&(r)->req, sock); \
- (r)->in.req = &(r)->req; \
- (r)->out.req = &(r)->req; \
- (r)->err.req = &(r)->req; \
- (r)->in.type = FCGI_STDIN; \
- (r)->out.type = FCGI_STDOUT; \
- (r)->err.type = FCGI_STDERR; \
- (r)->envp = (r)->req.env; \
- } while (0);
-
-
-#define FCGX_Accept_r(r) fcgi_accept_request(&(r)->req)
-#define FCGX_Finish_r(r) fcgi_finish_request(&(r)->req)
-
-#define FCGX_PutStr(str, len, stream) fcgi_write((stream).req, (stream).type, str, len)
-#define FCGX_PutS(str, len, stream) fcgi_write((stream).req, (stream).type, str, len)
-#define FCGX_FFlush(stream) fcgi_flush((stream).req, 0)
-#define FCGX_GetStr(str, len, stream) fcgi_read((stream).req, str, len)
-
-#define FCGX_GetParam(var, envp) fcgi_getenv_helper(envp, var, strlen(var));
-
-#define FCGX_PutEnv(r, var) fcgi_putenv(&(r)->req, var, strlen(var));
-
-int FCGX_FPrintF(FCGX_Stream stream, const char *format, ...);
-
-/* Internal helper functions. They shouldn't be used directly. */
-
-char* fcgi_getenv_helper(char** env, const char *name, int len);
-
+#ifdef PHP_WIN32
+void fcgi_impersonate(void);
+#endif
/*
* Local variables:
* tab-width: 4