mirror of
https://github.com/php/php-src.git
synced 2025-08-16 05:58:45 +02:00
simplify handling of variables by maintaining two strings which
are simply appended instead of traversing the hash table on each URL/form. also fix an unconditional segfault in rshutdown due to efree'ing a static char *. remove remove_var, add reset_vars. move the function declarations into the right header file.
This commit is contained in:
parent
ebfff2df7e
commit
9743860d35
6 changed files with 52 additions and 77 deletions
|
@ -1379,7 +1379,9 @@ PHP_FUNCTION(session_unset)
|
|||
static void php_rinit_session_globals(TSRMLS_D)
|
||||
{
|
||||
zend_hash_init(&PS(vars), 0, NULL, NULL, 0);
|
||||
php_url_scanner_remove_var(PS(session_name), strlen(PS(session_name)) TSRMLS_CC); /* save even if we haven't registered */
|
||||
#if I_KNOW_WHAT_THE_PURPOSE_OF_THIS_IS
|
||||
php_url_scanner_reset_vars(TSRMLS_C); /* save even if we haven't registered */
|
||||
#endif
|
||||
PS(id) = NULL;
|
||||
PS(session_status) = php_session_none;
|
||||
PS(mod_data) = NULL;
|
||||
|
|
|
@ -811,7 +811,7 @@ function_entry basic_functions[] = {
|
|||
PHP_FE(aggregation_info, first_arg_force_ref)
|
||||
|
||||
PHP_FE(output_add_rewrite_var, NULL)
|
||||
PHP_FE(output_remove_rewrite_var, NULL)
|
||||
PHP_FE(output_reset_rewrite_vars, NULL)
|
||||
|
||||
{NULL, NULL, NULL}
|
||||
};
|
||||
|
|
|
@ -25,11 +25,8 @@ PHP_MSHUTDOWN_FUNCTION(url_scanner_ex);
|
|||
PHP_RINIT_FUNCTION(url_scanner_ex);
|
||||
PHP_RSHUTDOWN_FUNCTION(url_scanner_ex);
|
||||
|
||||
PHP_FUNCTION(output_add_rewrite_var);
|
||||
PHP_FUNCTION(output_remove_rewrite_var);
|
||||
|
||||
int php_url_scanner_add_var(char *name, int name_len, char *value, int value_len, int urlencode TSRMLS_DC);
|
||||
int php_url_scanner_remove_var(char *name, int name_len TSRMLS_DC);
|
||||
int php_url_scanner_reset_vars(TSRMLS_D);
|
||||
|
||||
int php_url_scanner_ex_activate(TSRMLS_D);
|
||||
int php_url_scanner_ex_deactivate(TSRMLS_D);
|
||||
|
@ -46,8 +43,8 @@ typedef struct {
|
|||
/* The result buffer */
|
||||
smart_str result;
|
||||
|
||||
/* The data which is appended to each relative URL */
|
||||
HashTable *rewrite_vars;
|
||||
/* The data which is appended to each relative URL/FORM */
|
||||
smart_str form_app, url_app;
|
||||
|
||||
int active;
|
||||
|
||||
|
|
|
@ -96,15 +96,11 @@ alpha = [a-zA-Z];
|
|||
#define YYLIMIT q
|
||||
#define YYMARKER r
|
||||
|
||||
static inline void append_modified_url(smart_str *url, smart_str *dest, HashTable *rewrite_vars, const char *separator)
|
||||
static inline void append_modified_url(smart_str *url, smart_str *dest, smart_str *url_app, const char *separator)
|
||||
{
|
||||
register const char *p, *q;
|
||||
const char *bash = NULL;
|
||||
const char *sep = "?";
|
||||
char *string_key;
|
||||
url_adapt_var_t *value;
|
||||
int num_key;
|
||||
|
||||
|
||||
q = (p = url->c) + url->len;
|
||||
|
||||
|
@ -128,16 +124,8 @@ done:
|
|||
else
|
||||
smart_str_append(dest, url);
|
||||
|
||||
zend_hash_internal_pointer_reset(rewrite_vars);
|
||||
while (zend_hash_get_current_key(rewrite_vars, &string_key, &num_key, 0) != HASH_KEY_NON_EXISTANT) {
|
||||
zend_hash_get_current_data(rewrite_vars, &value);
|
||||
smart_str_appends(dest, sep);
|
||||
smart_str_append(dest, &(value->var));
|
||||
smart_str_appendc(dest, '=');
|
||||
smart_str_append(dest, &(value->val));
|
||||
zend_hash_move_forward(rewrite_vars);
|
||||
sep = separator; /* switch from ? to now! */
|
||||
}
|
||||
smart_str_appends(dest, sep);
|
||||
smart_str_append(dest, url_app);
|
||||
|
||||
if (bash)
|
||||
smart_str_appendl(dest, bash, q - bash);
|
||||
|
@ -160,7 +148,7 @@ static inline void tag_arg(url_adapt_state_ex_t *ctx, char quotes, char type TSR
|
|||
if (quotes)
|
||||
smart_str_appendc(&ctx->result, type);
|
||||
if (f) {
|
||||
append_modified_url(&ctx->val, &ctx->result, ctx->rewrite_vars, PG(arg_separator).output);
|
||||
append_modified_url(&ctx->val, &ctx->result, &ctx->url_app, PG(arg_separator).output);
|
||||
} else {
|
||||
smart_str_append(&ctx->result, &ctx->val);
|
||||
}
|
||||
|
@ -194,21 +182,10 @@ static inline void passthru(STD_PARA)
|
|||
|
||||
static inline void handle_form(STD_PARA)
|
||||
{
|
||||
if (ctx->tag.len == 4 && strncasecmp(ctx->tag.c, "form", 4) == 0) {
|
||||
char *string_key;
|
||||
url_adapt_var_t *value;
|
||||
int num_key;
|
||||
|
||||
zend_hash_internal_pointer_reset(ctx->rewrite_vars);
|
||||
while (zend_hash_get_current_key(ctx->rewrite_vars, &string_key, &num_key, 0) != HASH_KEY_NON_EXISTANT) {
|
||||
zend_hash_get_current_data(ctx->rewrite_vars, &value);
|
||||
smart_str_appends(&ctx->result, "<input type=\"hidden\" name=\"");
|
||||
smart_str_append(&ctx->result, &(value->var));
|
||||
smart_str_appends(&ctx->result, "\" value=\"");
|
||||
smart_str_append(&ctx->result, &(value->val));
|
||||
smart_str_appends(&ctx->result, "\" />");
|
||||
zend_hash_move_forward(ctx->rewrite_vars);
|
||||
}
|
||||
if (ctx->form_app.len > 0
|
||||
&& ctx->tag.len == 4
|
||||
&& strncasecmp(ctx->tag.c, "form", 4) == 0) {
|
||||
smart_str_append(&ctx->result, &ctx->form_app);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -389,56 +366,60 @@ int php_url_scanner_ex_deactivate(TSRMLS_D)
|
|||
|
||||
static void php_url_scanner_output_handler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
|
||||
{
|
||||
if (BG(url_adapt_state_ex).rewrite_vars && zend_hash_num_elements(BG(url_adapt_state_ex).rewrite_vars)) {
|
||||
if (BG(url_adapt_state_ex).url_app.len != 0) {
|
||||
*handled_output = url_adapt_ext(output, output_len, handled_output_len, (zend_bool) (mode&PHP_OUTPUT_HANDLER_END ? 1 : 0) TSRMLS_CC);
|
||||
} else {
|
||||
*handled_output = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void php_url_scanner_var_dtor(url_adapt_var_t *var)
|
||||
{
|
||||
smart_str_free(&(var->var));
|
||||
smart_str_free(&(var->val));
|
||||
}
|
||||
|
||||
int php_url_scanner_add_var(char *name, int name_len, char *value, int value_len, int urlencode TSRMLS_DC)
|
||||
{
|
||||
url_adapt_var_t var;
|
||||
char *encoded;
|
||||
int encoded_len;
|
||||
|
||||
smart_str val;
|
||||
|
||||
if (! BG(url_adapt_state_ex).active) {
|
||||
int chunk_size = 4096; /* XXX where should we get chunk_size from? */
|
||||
|
||||
php_url_scanner_ex_activate(TSRMLS_C);
|
||||
php_start_ob_buffer(NULL, chunk_size, 1 TSRMLS_CC);
|
||||
php_ob_set_internal_handler(php_url_scanner_output_handler, chunk_size, "URL-Rewriter", 1 TSRMLS_CC);
|
||||
php_ob_set_internal_handler(php_url_scanner_output_handler, chunk_size, estrdup("URL-Rewriter"), 1 TSRMLS_CC);
|
||||
BG(url_adapt_state_ex).active = 1;
|
||||
}
|
||||
|
||||
if (! BG(url_adapt_state_ex).rewrite_vars) {
|
||||
BG(url_adapt_state_ex).rewrite_vars = emalloc(sizeof(HashTable));
|
||||
zend_hash_init(BG(url_adapt_state_ex).rewrite_vars, 0, NULL, (void (*)(void *)) php_url_scanner_var_dtor, 0);
|
||||
}
|
||||
|
||||
smart_str_appendl(&(var.var), name, name_len);
|
||||
if (BG(url_adapt_state_ex).url_app.len != 0) {
|
||||
smart_str_appends(&BG(url_adapt_state_ex).url_app, PG(arg_separator).output);
|
||||
}
|
||||
|
||||
if (urlencode) {
|
||||
encoded = php_url_encode(value, value_len, &encoded_len);
|
||||
smart_str_setl(&(var.val), encoded, encoded_len);
|
||||
smart_str_setl(&val, encoded, encoded_len);
|
||||
} else {
|
||||
smart_str_appendl(&(var.val), value, value_len);
|
||||
smart_str_setl(&val, value, value_len);
|
||||
}
|
||||
|
||||
smart_str_appendl(&BG(url_adapt_state_ex).url_app, name, name_len);
|
||||
smart_str_appendc(&BG(url_adapt_state_ex).url_app, '=');
|
||||
smart_str_append(&BG(url_adapt_state_ex).url_app, &val);
|
||||
|
||||
return zend_hash_add(BG(url_adapt_state_ex).rewrite_vars, name, name_len, &var, sizeof(url_adapt_var_t), NULL);
|
||||
smart_str_appends(&BG(url_adapt_state_ex).form_app, "<input type=\"hidden\" name=\"");
|
||||
smart_str_appendl(&BG(url_adapt_state_ex).form_app, name, name_len);
|
||||
smart_str_appends(&BG(url_adapt_state_ex).form_app, "\" value=\"");
|
||||
smart_str_append(&BG(url_adapt_state_ex).form_app, &val);
|
||||
smart_str_appends(&BG(url_adapt_state_ex).form_app, "\" />");
|
||||
|
||||
if (urlencode)
|
||||
efree(encoded);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
int php_url_scanner_remove_var(char *name, int name_len TSRMLS_DC)
|
||||
int php_url_scanner_reset_vars(TSRMLS_D)
|
||||
{
|
||||
if (BG(url_adapt_state_ex).rewrite_vars) {
|
||||
return zend_hash_del(BG(url_adapt_state_ex).rewrite_vars, name, name_len);
|
||||
}
|
||||
BG(url_adapt_state_ex).form_app.len = 0;
|
||||
BG(url_adapt_state_ex).url_app.len = 0;
|
||||
|
||||
return FAILURE;
|
||||
}
|
||||
|
@ -447,6 +428,9 @@ PHP_MINIT_FUNCTION(url_scanner)
|
|||
{
|
||||
BG(url_adapt_state_ex).tags = NULL;
|
||||
|
||||
BG(url_adapt_state_ex).form_app.c = BG(url_adapt_state_ex).url_app.c = 0;
|
||||
BG(url_adapt_state_ex).form_app.len = BG(url_adapt_state_ex).url_app.len = 0;
|
||||
|
||||
REGISTER_INI_ENTRIES();
|
||||
return SUCCESS;
|
||||
}
|
||||
|
@ -463,8 +447,10 @@ PHP_MSHUTDOWN_FUNCTION(url_scanner)
|
|||
PHP_RINIT_FUNCTION(url_scanner)
|
||||
{
|
||||
BG(url_adapt_state_ex).active = 0;
|
||||
BG(url_adapt_state_ex).rewrite_vars = NULL;
|
||||
|
||||
smart_str_free(&BG(url_adapt_state_ex).form_app);
|
||||
smart_str_free(&BG(url_adapt_state_ex).url_app);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
@ -475,11 +461,5 @@ PHP_RSHUTDOWN_FUNCTION(url_scanner)
|
|||
BG(url_adapt_state_ex).active = 0;
|
||||
}
|
||||
|
||||
if (BG(url_adapt_state_ex).rewrite_vars) {
|
||||
zend_hash_destroy(BG(url_adapt_state_ex).rewrite_vars);
|
||||
efree(BG(url_adapt_state_ex).rewrite_vars);
|
||||
BG(url_adapt_state_ex).rewrite_vars = NULL;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
|
|
@ -795,16 +795,9 @@ PHPAPI int php_get_output_start_lineno(TSRMLS_D)
|
|||
return OG(output_start_lineno);
|
||||
}
|
||||
|
||||
PHP_FUNCTION(output_remove_rewrite_var)
|
||||
PHP_FUNCTION(output_reset_rewrite_vars)
|
||||
{
|
||||
char *name;
|
||||
int name_len;
|
||||
|
||||
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) {
|
||||
RETURN_FALSE;
|
||||
}
|
||||
|
||||
if (php_url_scanner_remove_var(name, name_len TSRMLS_CC) == SUCCESS) {
|
||||
if (php_url_scanner_reset_vars(TSRMLS_C) == SUCCESS) {
|
||||
RETURN_TRUE;
|
||||
} else {
|
||||
RETURN_FALSE;
|
||||
|
|
|
@ -94,5 +94,8 @@ ZEND_API extern php_output_globals output_globals;
|
|||
#define PHP_OUTPUT_HANDLER_INTERNAL 0
|
||||
#define PHP_OUTPUT_HANDLER_USER 1
|
||||
|
||||
PHP_FUNCTION(output_add_rewrite_var);
|
||||
PHP_FUNCTION(output_reset_rewrite_vars);
|
||||
|
||||
|
||||
#endif /* PHP_OUTPUT_H */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue