Skip to content

Commit ba8e317

Browse files
committed
Optimized request startup sequence for php.ini without per dir and per host configurations
1 parent 8401580 commit ba8e317

File tree

3 files changed

+60
-32
lines changed

3 files changed

+60
-32
lines changed

main/php_ini.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ typedef struct _php_extension_lists {
5050
static int is_special_section = 0;
5151
static HashTable *active_ini_hash;
5252
static HashTable configuration_hash;
53+
static int has_per_dir_config = 0;
54+
static int has_per_host_config = 0;
5355
PHPAPI char *php_ini_opened_path=NULL;
5456
static php_extension_lists extension_lists;
5557
PHPAPI char *php_ini_scanned_files=NULL;
@@ -264,13 +266,16 @@ static void php_ini_parser_cb(zval *arg1, zval *arg2, zval *arg3, int callback_t
264266
key = key + sizeof("PATH") - 1;
265267
key_len = Z_STRLEN_P(arg1) - sizeof("PATH") + 1;
266268
is_special_section = 1;
269+
has_per_dir_config = 1;
267270

268271
/* HOST sections */
269272
} else if (!strncasecmp(Z_STRVAL_P(arg1), "HOST", sizeof("HOST") - 1)) {
270273
key = Z_STRVAL_P(arg1);
271274
key = key + sizeof("HOST") - 1;
272275
key_len = Z_STRLEN_P(arg1) - sizeof("HOST") + 1;
273276
is_special_section = 1;
277+
has_per_host_config = 1;
278+
274279
} else {
275280
is_special_section = 0;
276281
}
@@ -737,6 +742,14 @@ PHPAPI void php_ini_activate_config(HashTable *source_hash, int modify_type, int
737742
}
738743
/* }}} */
739744

745+
/* {{{ php_ini_has_per_dir_config
746+
*/
747+
PHPAPI int php_ini_has_per_dir_config(void)
748+
{
749+
return has_per_dir_config;
750+
}
751+
/* }}} */
752+
740753
/* {{{ php_ini_activate_per_dir_config
741754
*/
742755
PHPAPI void php_ini_activate_per_dir_config(char *path, uint path_len TSRMLS_DC)
@@ -745,7 +758,7 @@ PHPAPI void php_ini_activate_per_dir_config(char *path, uint path_len TSRMLS_DC)
745758
char *ptr;
746759

747760
/* Walk through each directory in path and apply any found per-dir-system-configuration from configuration_hash */
748-
if (path && path_len) {
761+
if (has_per_dir_config && path && path_len) {
749762
ptr = path + 1;
750763
while ((ptr = strchr(ptr, DEFAULT_SLASH)) != NULL) {
751764
*ptr = 0;
@@ -760,13 +773,21 @@ PHPAPI void php_ini_activate_per_dir_config(char *path, uint path_len TSRMLS_DC)
760773
}
761774
/* }}} */
762775

776+
/* {{{ php_ini_has_per_host_config
777+
*/
778+
PHPAPI int php_ini_has_per_host_config(void)
779+
{
780+
return has_per_host_config;
781+
}
782+
/* }}} */
783+
763784
/* {{{ php_ini_activate_per_host_config
764785
*/
765786
PHPAPI void php_ini_activate_per_host_config(char *host, uint host_len TSRMLS_DC)
766787
{
767788
zval *tmp;
768789

769-
if (host && host_len) {
790+
if (has_per_host_config && host && host_len) {
770791
/* Search for source array matching the host from configuration_hash */
771792
if (zend_hash_find(&configuration_hash, host, host_len, (void **) &tmp) == SUCCESS) {
772793
php_ini_activate_config(Z_ARRVAL_P(tmp), PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE TSRMLS_CC);

main/php_ini.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ PHPAPI int cfg_get_double(char *varname, double *result);
3434
PHPAPI int cfg_get_string(char *varname, char **result);
3535
PHPAPI int php_parse_user_ini_file(char *dirname, char *ini_filename, HashTable *target_hash TSRMLS_DC);
3636
PHPAPI void php_ini_activate_config(HashTable *source_hash, int modify_type, int stage TSRMLS_DC);
37+
PHPAPI int php_ini_has_per_dir_config(void);
38+
PHPAPI int php_ini_has_per_host_config(void);
3739
PHPAPI void php_ini_activate_per_dir_config(char *path, uint path_len TSRMLS_DC);
3840
PHPAPI void php_ini_activate_per_host_config(char *host, uint host_len TSRMLS_DC);
3941
PHPAPI HashTable* php_ini_get_configuration_hash(void);

sapi/cgi/cgi_main.c

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -710,42 +710,47 @@ static int sapi_cgi_activate(TSRMLS_D)
710710
return FAILURE;
711711
}
712712

713-
doc_root = sapi_cgibin_getenv("DOCUMENT_ROOT", sizeof("DOCUMENT_ROOT") - 1 TSRMLS_CC);
714-
server_name = sapi_cgibin_getenv("SERVER_NAME", sizeof("SERVER_NAME") - 1 TSRMLS_CC);
715-
716-
/* DOCUMENT_ROOT and SERVER_NAME should also be defined at this stage..but better check it anyway */
717-
if (!doc_root || !server_name) {
718-
return FAILURE;
719-
}
720-
doc_root_len = strlen(doc_root);
721-
if (doc_root[doc_root_len - 1] == '/') {
722-
--doc_root_len;
723-
}
724-
725-
/* Prepare search path */
726-
path_len = strlen(SG(request_info).path_translated);
727-
path = zend_strndup(SG(request_info).path_translated, path_len);
728-
php_dirname(path, path_len);
729-
path_len = strlen(path);
730-
731-
/* Make sure we have trailing slash! */
732-
if (!IS_SLASH(path[path_len])) {
733-
path[path_len++] = DEFAULT_SLASH;
713+
if (php_ini_has_per_host_config()) {
714+
/* Activate per-host-system-configuration defined in php.ini and stored into configuration_hash during startup */
715+
server_name = sapi_cgibin_getenv("SERVER_NAME", sizeof("SERVER_NAME") - 1 TSRMLS_CC);
716+
/* SERVER_NAME should also be defined at this stage..but better check it anyway */
717+
if (server_name) {
718+
php_ini_activate_per_host_config(server_name, strlen(server_name) + 1 TSRMLS_CC);
719+
}
734720
}
735-
path[path_len] = 0;
736721

737-
/* Activate per-dir-system-configuration defined in php.ini and stored into configuration_hash during startup */
738-
php_ini_activate_per_dir_config(path, path_len TSRMLS_CC); /* Note: for global settings sake we check from root to path */
722+
if (php_ini_has_per_dir_config() ||
723+
(PG(user_ini_filename) && *PG(user_ini_filename))) {
724+
/* Prepare search path */
725+
path_len = strlen(SG(request_info).path_translated);
726+
path = estrndup(SG(request_info).path_translated, path_len);
727+
path_len = zend_dirname(path, path_len);
739728

740-
/* Activate per-host-system-configuration defined in php.ini and stored into configuration_hash during startup */
741-
php_ini_activate_per_host_config(server_name, strlen(server_name) + 1 TSRMLS_CC);
729+
/* Make sure we have trailing slash! */
730+
if (!IS_SLASH(path[path_len])) {
731+
path[path_len++] = DEFAULT_SLASH;
732+
}
733+
path[path_len] = 0;
734+
735+
/* Activate per-dir-system-configuration defined in php.ini and stored into configuration_hash during startup */
736+
php_ini_activate_per_dir_config(path, path_len TSRMLS_CC); /* Note: for global settings sake we check from root to path */
737+
738+
/* Load and activate user ini files in path starting from DOCUMENT_ROOT */
739+
if (PG(user_ini_filename) && *PG(user_ini_filename)) {
740+
doc_root = sapi_cgibin_getenv("DOCUMENT_ROOT", sizeof("DOCUMENT_ROOT") - 1 TSRMLS_CC);
741+
/* DOCUMENT_ROOT should also be defined at this stage..but better check it anyway */
742+
if (doc_root) {
743+
doc_root_len = strlen(doc_root);
744+
if (doc_root[doc_root_len - 1] == '/') {
745+
--doc_root_len;
746+
}
747+
php_cgi_ini_activate_user_config(path, path_len, doc_root_len - 1 TSRMLS_CC);
748+
}
749+
}
742750

743-
/* Load and activate user ini files in path starting from DOCUMENT_ROOT */
744-
if (strlen(PG(user_ini_filename))) {
745-
php_cgi_ini_activate_user_config(path, path_len, doc_root_len - 1 TSRMLS_CC);
751+
efree(path);
746752
}
747753

748-
free(path);
749754
return SUCCESS;
750755
}
751756

0 commit comments

Comments
 (0)