cvs: pecl /htscanner htscanner.c php_htscanner.h /htscanner/docs htscanner.ini

From: Date: Thu, 07 Dec 2006 19:41:11 +0000
Subject: cvs: pecl /htscanner htscanner.c php_htscanner.h /htscanner/docs htscanner.ini
Groups: php.pecl.cvs 
Request: Send a blank email to pecl-cvs+get-6771@lists.php.net to get a copy of this message
pajoye Thu Dec 7 19:41:11 2006 UTC Modified files: /pecl/htscanner htscanner.c php_htscanner.h /pecl/htscanner/docs htscanner.ini Log: - implement cache for the ini entries found in the htaccess files - add ini setting default_ttl - add a note about VCWD_GETCWD not working in rinit. cwd is not initialized at this stage but later.

http://cvs.php.net/viewvc.cgi/pecl/htscanner/htscanner.c?r1=1.4&r2=1.5&diff_format=u Index: pecl/htscanner/htscanner.c diff -u pecl/htscanner/htscanner.c:1.4 pecl/htscanner/htscanner.c:1.5 --- pecl/htscanner/htscanner.c:1.4 Thu Nov 30 14:59:34 2006 +++ pecl/htscanner/htscanner.c Thu Dec 7 19:41:10 2006 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: htscanner.c,v 1.4 2006/11/30 14:59:34 pajoye Exp $ */ +/* $Id: htscanner.c,v 1.5 2006/12/07 19:41:10 pajoye Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -30,12 +30,15 @@ #include "php_htscanner.h" #include "ext/standard/file.h" #include "ext/standard/php_string.h" +#include "main/php_config.h" ZEND_DECLARE_MODULE_GLOBALS(htscanner) #define FILE_BUFFER 1000 +#define HTSCANNER_DEBUG 0 +#define HTSCANNER_ENABLE_CACHE 1 -#ifdef HTSCANNER_DEBUG +#if HTSCANNER_DEBUG /* {{{ debug */ static void debug(char *format, ...) { char output_buf[512]; @@ -77,7 +80,7 @@ /* {{{ value_hnd * Parse an option and try to set the option */ -static void value_hnd(char *string, int flag, int status TSRMLS_DC) { +static void value_hnd(char *string, int flag, int status, HashTable *ini_entries TSRMLS_DC) { char *name = string; char *value; @@ -94,7 +97,6 @@ /* strip any leading whitespaces or tabs from the value */ len = strlen(value); PHP_HTSCANNER_LTRIM(value); -debug("value len: %i\n", len); if (len > 2 && value[len - 2] == '\r') { value[len - 2] = 0; } else { @@ -149,8 +151,12 @@ if (zend_alter_ini_entry(name, strlen(name) + 1, value, strlen(value), status, PHP_INI_STAGE_RUNTIME) == FAILURE) { zend_error(E_ERROR, "Adding option (Name: %s Value: %s) failed!\n", name, value); + return; } - +#if HTSCANNER_ENABLE_CACHE + debug("in:%s||%s||\n", name, value); + zend_hash_update(ini_entries, name, strlen(name), value, strlen(value), NULL); +#endif } } /* }}} */ @@ -158,7 +164,7 @@ /* {{{ parse_config_file * Parse the configuration file */ -static void parse_config_file(char *file TSRMLS_DC) { +static void parse_config_file(char *file, HashTable *ini_entries TSRMLS_DC) { php_stream *stream; stream = php_stream_open_wrapper(file, "r", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); @@ -170,9 +176,9 @@ PHP_HTSCANNER_LTRIM(pos); if (strncmp(pos, "php_value", sizeof("php_value") - 1) == 0) { - value_hnd(pos + sizeof("php_value"), 0, PHP_INI_PERDIR TSRMLS_CC); + value_hnd(pos + sizeof("php_value"), 0, PHP_INI_PERDIR, ini_entries TSRMLS_CC); } else if (strncmp(pos, "php_flag", sizeof("php_flag") - 1) == 0) { - value_hnd(pos + sizeof("php_flag"), 1, PHP_INI_PERDIR TSRMLS_CC); + value_hnd(pos + sizeof("php_flag"), 1, PHP_INI_PERDIR, ini_entries TSRMLS_CC); } } php_stream_close(stream); @@ -196,6 +202,8 @@ return Z_STRVAL_PP(data); } } + } else { + return HTG(default_docroot); } return NULL; } @@ -240,15 +248,49 @@ PHP_INI_BEGIN() STD_PHP_INI_ENTRY("htscanner.config_file", ".htaccess", PHP_INI_SYSTEM, OnUpdateString, config_file, zend_htscanner_globals, htscanner_globals) STD_PHP_INI_ENTRY("htscanner.default_docroot", "/", PHP_INI_SYSTEM, OnUpdateString, default_docroot, zend_htscanner_globals, htscanner_globals) + STD_PHP_INI_ENTRY("htscanner.default_ttl", "300", PHP_INI_SYSTEM, OnUpdateLong, default_ttl, zend_htscanner_globals, htscanner_globals) PHP_INI_END() /* }}} */ -/* {{{ php_htscanner_init_globals - */ -static void php_htscanner_init_globals(zend_htscanner_globals *htscanner_globals) +#if HTSCANNER_ENABLE_CACHE +htscannerMutexDeclare(ini_entries_cache_mutex); +static htscanner_cache *ini_entries_cache = NULL; + +static void ini_cache_entry_dtor(void *data) /* {{{ */ +{ + HashTable *entries = (HashTable *)data; + if (entries) { + zend_hash_destroy(entries); + } +} +/* }}} */ + +static int php_htscanner_create_cache() /* {{{ */ +{ + if (ini_entries_cache) { + /* Already set up */ + return SUCCESS; + } + + htscannerMutexSetup(ini_entries_cache_mutex); + + ini_entries_cache = pemalloc(sizeof(htscanner_cache), 1); + if (!ini_entries_cache) { + return FAILURE; + } + + ini_entries_cache->paths = malloc(sizeof(HashTable)); + zend_hash_init(ini_entries_cache->paths, 0, NULL, ini_cache_entry_dtor, 1); + return SUCCESS; +} +/* }}} */ +#endif + +static void php_htscanner_init_globals(zend_htscanner_globals *htscanner_globals) /* {{{ */ { htscanner_globals->config_file = NULL; htscanner_globals->default_docroot = NULL; + htscanner_globals->default_ttl = 5*60; } /* }}} */ @@ -262,6 +304,16 @@ PHP_MSHUTDOWN_FUNCTION(htscanner) /* {{{ */ { +#if HTSCANNER_ENABLE_CACHE + if (ini_entries_cache) { + htscannerMutexLock(ini_entries_cache_mutex); + if (ini_entries_cache->paths) { + zend_hash_destroy(ini_entries_cache->paths); + free(ini_entries_cache->paths); + } + htscannerMutexShutdown(ini_entries_cache_mutex); + } +#endif UNREGISTER_INI_ENTRIES(); return SUCCESS; } @@ -272,33 +324,83 @@ char *doc_root; char cwd[MAXPATHLEN + 1]; int cwd_len, doc_root_len; + HashTable *ini_entries; + time_t t; + htscanner_cache_entry entry; + htscanner_cache_entry *entry_fetched; doc_root = get_doc_root(TSRMLS_C); if (doc_root == NULL) { - doc_root = HTG(default_docroot); + return FAILURE; } doc_root_len = strlen(doc_root); + /* + VCWD_GETCWD cannot be used in rinit, cwd is not yet + initialized. Get it from path_translated. + */ if (!SG(request_info).path_translated) { return FAILURE; } + strcpy(cwd, SG(request_info).path_translated); cwd_len = php_dirname(cwd, strlen(cwd)); - + if (cwd[cwd_len] != PHP_DIR_SEPARATOR) { cwd[cwd_len] = PHP_DIR_SEPARATOR; cwd_len++; } cwd[cwd_len] = '\0'; + +#if HTSCANNER_ENABLE_CACHE + if (!ini_entries_cache) { + if (php_htscanner_create_cache() != SUCCESS) { + return FAILURE; + } + } + + t = sapi_get_request_time(TSRMLS_C); + + htscannerMutexLock(ini_entries_cache_mutex); + if (zend_hash_find(ini_entries_cache->paths, cwd, cwd_len, (void**)&entry_fetched) == SUCCESS) { + /* fetch cache and assign */ + if ((t - entry_fetched->created_on) < HTG(default_ttl)) { + char *value, *name; + HashPosition pos; + uint len; + ulong num; + + zend_hash_internal_pointer_reset_ex(entry_fetched->ini_entries, &pos); + + while (SUCCESS == zend_hash_get_current_data_ex(entry_fetched->ini_entries, (void**)&value, &pos)) { + zend_hash_get_current_key_ex(entry_fetched->ini_entries, &name, &len, &num, 0, &pos); + if (zend_alter_ini_entry(name, strlen(name) + 1, value, strlen(value), + PHP_INI_PERDIR, PHP_INI_STAGE_RUNTIME) == FAILURE) { + zend_error(E_ERROR, "Adding option (Name: %s Value: %s) failed!\n", name, value); + return FAILURE; + } + zend_hash_move_forward_ex(entry_fetched->ini_entries, &pos); + } + htscannerMutexUnlock(ini_entries_cache_mutex); + return SUCCESS; + } + } + + /* parse, insert, assign */ + entry.created_on = t; + ini_entries = malloc(sizeof(HashTable)); + entry.ini_entries = ini_entries; + zend_hash_init(ini_entries, 0, NULL, NULL, 1); +#endif + debug("\nStarting request %s (%i)... \n", cwd, cwd_len); debug("\ndoc root %s (%i)... \n", doc_root, doc_root_len); debug("\ncwd %s (%i)... \n", cwd, cwd_len); if (cwd != NULL && doc_root != NULL) { - size_t i, ht_len, doc_root_len, tmp; + size_t i, ht_len, tmp; ht_len = strlen(HTG(config_file)); - doc_root_len = strlen(doc_root); for (i = doc_root_len - 1; i < cwd_len; i++) { if (cwd[i] == PHP_DIR_SEPARATOR) { @@ -311,18 +413,23 @@ file[i + 1] = '\0'; strcat(file, HTG(config_file)); debug("Going to parse: %s\n", file); - parse_config_file(file TSRMLS_CC); + parse_config_file(file, ini_entries TSRMLS_CC); debug("Done parsing: %s (%d = %d)\n", file, strlen(file), tmp); } } } - debug("Finished iterating ... \n"); +#if HTSCANNER_ENABLE_CACHE + zend_hash_add(ini_entries_cache->paths, cwd, cwd_len, &entry, + sizeof(htscanner_cache_entry), NULL); + htscannerMutexUnlock(ini_entries_cache_mutex); +#endif return SUCCESS; } /* }}} */ PHP_RSHUTDOWN_FUNCTION(htscanner) /* {{{ */ { + debug("rshutdown\n"); return SUCCESS; } /* }}} */ @@ -332,7 +439,7 @@ php_info_print_table_start(); php_info_print_table_row(2, "htscanner support", "enabled"); - php_info_print_table_row(2, "Extension Version","$Id: htscanner.c,v 1.4 2006/11/30 14:59:34 pajoye Exp $"); + php_info_print_table_row(2, "Extension Version","$Id: htscanner.c,v 1.5 2006/12/07 19:41:10 pajoye Exp $"); php_info_print_table_row(2, "htaccess version", "0.5.0-dev"); php_info_print_table_end(); http://cvs.php.net/viewvc.cgi/pecl/htscanner/php_htscanner.h?r1=1.1&r2=1.2&diff_format=u Index: pecl/htscanner/php_htscanner.h diff -u pecl/htscanner/php_htscanner.h:1.1 pecl/htscanner/php_htscanner.h:1.2 --- pecl/htscanner/php_htscanner.h:1.1 Thu Nov 30 01:54:09 2006 +++ pecl/htscanner/php_htscanner.h Thu Dec 7 19:41:10 2006 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_htscanner.h,v 1.1 2006/11/30 01:54:09 pajoye Exp $ */ +/* $Id: php_htscanner.h,v 1.2 2006/12/07 19:41:10 pajoye Exp $ */ #ifndef PHP_HTSCANNER_H #define PHP_HTSCANNER_H @@ -34,7 +34,7 @@ #include "TSRM.h" #endif -#define HTSCANNER_VERSION "0.4" +#define HTSCANNER_VERSION "0.6.0" PHP_MINIT_FUNCTION(htscanner); PHP_MSHUTDOWN_FUNCTION(htscanner); @@ -42,20 +42,38 @@ PHP_RSHUTDOWN_FUNCTION(htscanner); PHP_MINFO_FUNCTION(htscanner); +typedef struct _ze_htscanner_cache_entry { + time_t created_on; + HashTable *ini_entries; +} htscanner_cache_entry; + +typedef struct _ze_htscanner_cache { + HashTable *paths; +} htscanner_cache; + ZEND_BEGIN_MODULE_GLOBALS(htscanner) char *config_file; char *default_docroot; + unsigned long default_ttl; ZEND_END_MODULE_GLOBALS(htscanner) #ifdef ZTS #define HTG(v) TSRMG(htscanner_globals_id, zend_htscanner_globals *, v) +#define htscannerMutexDeclare(x) MUTEX_T x +#define htscannerMutexSetup(x) x = tsrm_mutex_alloc() +#define htscannerMutexShutdown(x) tsrm_mutex_free(x) +#define htscannerMutexLock(x) tsrm_mutex_lock(x) +#define htscannerUnlock(x) tsrm_mutex_unlock(x) #else #define HTG(v) (htscanner_globals.v) +#define htscannerMutexDeclare(x) +#define htscannerMutexSetup(x) +#define htscannerMutexShutdown(x) +#define htscannerMutexLock(x) +#define htscannerMutexUnlock(x) #endif #endif /* PHP_HTSCANNER_H */ - - /* * Local variables: * tab-width: 4 http://cvs.php.net/viewvc.cgi/pecl/htscanner/docs/htscanner.ini?r1=1.1&r2=1.2&diff_format=u Index: pecl/htscanner/docs/htscanner.ini diff -u pecl/htscanner/docs/htscanner.ini:1.1 pecl/htscanner/docs/htscanner.ini:1.2 --- pecl/htscanner/docs/htscanner.ini:1.1 Thu Nov 30 01:54:09 2006 +++ pecl/htscanner/docs/htscanner.ini Thu Dec 7 19:41:10 2006 @@ -6,3 +6,4 @@ ; The fallback docroot when htscanner can't determine the current docroot default_docroot="/" +default.ttl=300
« previous php.pecl.cvs (#6771) next »