Skip to content

Commit bfbfd15

Browse files
committed
improved performance of FastCGI request parsing
1 parent f3028b9 commit bfbfd15

File tree

4 files changed

+295
-116
lines changed

4 files changed

+295
-116
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
. ZEND_FETCH_*_R operations simplified and can't be used with EXT_TYPE_UNUSED
2626
flag any more. Thit is very rare and useless case. ZEND_FREE might be
2727
required after them instead.
28+
. improved performance of FastCGI request parsing
2829
- Added concept of interned strings. All strings constants known at compile
2930
time are allocated in a single copy and never changed. (Dmitry)
3031
- Added an optimization which saves memory and emalloc/efree calls for empty

sapi/cgi/cgi_main.c

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,17 @@ static char *sapi_cgi_read_cookies(TSRMLS_D)
587587
return sapi_cgibin_getenv((char *) "HTTP_COOKIE", sizeof("HTTP_COOKIE")-1 TSRMLS_CC);
588588
}
589589

590+
static void cgi_php_load_env_var(char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg)
591+
{
592+
zval *array_ptr = (zval*)arg;
593+
int filter_arg = (array_ptr == PG(http_globals)[TRACK_VARS_ENV])?PARSE_ENV:PARSE_SERVER;
594+
unsigned int new_val_len;
595+
596+
if (sapi_module.input_filter(filter_arg, var, &val, strlen(val), &new_val_len TSRMLS_CC)) {
597+
php_register_variable_safe(var, val, new_val_len, array_ptr TSRMLS_CC);
598+
}
599+
}
600+
590601
void cgi_php_import_environment_variables(zval *array_ptr TSRMLS_DC)
591602
{
592603
if (PG(http_globals)[TRACK_VARS_ENV] &&
@@ -616,26 +627,11 @@ void cgi_php_import_environment_variables(zval *array_ptr TSRMLS_DC)
616627

617628
if (fcgi_is_fastcgi()) {
618629
fcgi_request *request = (fcgi_request*) SG(server_context);
619-
HashPosition pos;
620630
int magic_quotes_gpc = PG(magic_quotes_gpc);
621-
char *var, **val;
622-
uint var_len;
623-
ulong idx;
624-
int filter_arg = (array_ptr == PG(http_globals)[TRACK_VARS_ENV])?PARSE_ENV:PARSE_SERVER;
625631

626632
/* turn off magic_quotes while importing environment variables */
627633
PG(magic_quotes_gpc) = 0;
628-
for (zend_hash_internal_pointer_reset_ex(&request->env, &pos);
629-
zend_hash_get_current_key_ex(&request->env, &var, &var_len, &idx, 0, &pos) == HASH_KEY_IS_STRING &&
630-
zend_hash_get_current_data_ex(&request->env, (void **) &val, &pos) == SUCCESS;
631-
zend_hash_move_forward_ex(&request->env, &pos)
632-
) {
633-
unsigned int new_val_len;
634-
635-
if (sapi_module.input_filter(filter_arg, var, val, strlen(*val), &new_val_len TSRMLS_CC)) {
636-
php_register_variable_safe(var, *val, new_val_len, array_ptr TSRMLS_CC);
637-
}
638-
}
634+
fcgi_loadenv(request, cgi_php_load_env_var, array_ptr);
639635
PG(magic_quotes_gpc) = magic_quotes_gpc;
640636
}
641637
}
@@ -1100,8 +1096,10 @@ static void init_request_info(TSRMLS_D)
11001096
char *env_path_info = sapi_cgibin_getenv("PATH_INFO", sizeof("PATH_INFO")-1 TSRMLS_CC);
11011097
char *env_script_name = sapi_cgibin_getenv("SCRIPT_NAME", sizeof("SCRIPT_NAME")-1 TSRMLS_CC);
11021098

1099+
#ifdef PHP_WIN32
11031100
/* Hack for buggy IIS that sets incorrect PATH_INFO */
11041101
char *env_server_software = sapi_cgibin_getenv("SERVER_SOFTWARE", sizeof("SERVER_SOFTWARE")-1 TSRMLS_CC);
1102+
11051103
if (env_server_software &&
11061104
env_script_name &&
11071105
env_path_info &&
@@ -1115,6 +1113,7 @@ static void init_request_info(TSRMLS_D)
11151113
}
11161114
env_path_info = _sapi_cgibin_putenv("PATH_INFO", env_path_info TSRMLS_CC);
11171115
}
1116+
#endif
11181117

11191118
if (CGIG(fix_pathinfo)) {
11201119
struct stat st;
@@ -1488,7 +1487,7 @@ int main(int argc, char *argv[])
14881487
int fastcgi = fcgi_is_fastcgi();
14891488
char *bindpath = NULL;
14901489
int fcgi_fd = 0;
1491-
fcgi_request request;
1490+
fcgi_request *request = NULL;
14921491
int repeats = 1;
14931492
int benchmark = 0;
14941493
#if HAVE_GETTIMEOFDAY
@@ -1689,7 +1688,7 @@ consult the installation file that came with this distribution, or visit \n\
16891688
php_import_environment_variables = cgi_php_import_environment_variables;
16901689

16911690
/* library is already initialized, now init our request */
1692-
fcgi_init_request(&request, fcgi_fd);
1691+
request = fcgi_init_request(fcgi_fd);
16931692

16941693
#ifndef PHP_WIN32
16951694
/* Pre-fork, if required */
@@ -1810,6 +1809,9 @@ consult the installation file that came with this distribution, or visit \n\
18101809
break;
18111810
case 'h':
18121811
case '?':
1812+
if (request) {
1813+
fcgi_destroy_request(request);
1814+
}
18131815
fcgi_shutdown();
18141816
no_headers = 1;
18151817
SG(headers_sent) = 1;
@@ -1831,8 +1833,8 @@ consult the installation file that came with this distribution, or visit \n\
18311833
fcgi_impersonate();
18321834
}
18331835
#endif
1834-
while (!fastcgi || fcgi_accept_request(&request) >= 0) {
1835-
SG(server_context) = (void *) &request;
1836+
while (!fastcgi || fcgi_accept_request(request) >= 0) {
1837+
SG(server_context) = fastcgi ? (void *) request : (void *) 1;
18361838
init_request_info(TSRMLS_C);
18371839
CG(interactive) = 0;
18381840

@@ -2026,7 +2028,7 @@ consult the installation file that came with this distribution, or visit \n\
20262028
* get path_translated */
20272029
if (php_request_startup(TSRMLS_C) == FAILURE) {
20282030
if (fastcgi) {
2029-
fcgi_finish_request(&request, 1);
2031+
fcgi_finish_request(request, 1);
20302032
}
20312033
SG(server_context) = NULL;
20322034
php_module_shutdown(TSRMLS_C);
@@ -2232,7 +2234,7 @@ consult the installation file that came with this distribution, or visit \n\
22322234
/* only fastcgi will get here */
22332235
requests++;
22342236
if (max_requests && (requests == max_requests)) {
2235-
fcgi_finish_request(&request, 1);
2237+
fcgi_finish_request(request, 1);
22362238
if (bindpath) {
22372239
free(bindpath);
22382240
}
@@ -2244,6 +2246,9 @@ consult the installation file that came with this distribution, or visit \n\
22442246
}
22452247
/* end of fastcgi loop */
22462248
}
2249+
if (request) {
2250+
fcgi_destroy_request(request);
2251+
}
22472252
fcgi_shutdown();
22482253

22492254
if (cgi_sapi_module.php_ini_path_override) {

0 commit comments

Comments
 (0)