X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_cookie.c;h=a92bb5e6d29c15ed80eed3ffcecfffb258d0a220;hp=cf5e56ceba1b55612adb73cd85f9320dbffb7300;hb=aacbe6aa75ca07853bf88d5b3647f67f107d1af7;hpb=a07b79b1871054ca17e48b69445b4dc201f24662 diff --git a/php_http_cookie.c b/php_http_cookie.c index cf5e56c..a92bb5e 100644 --- a/php_http_cookie.c +++ b/php_http_cookie.c @@ -6,15 +6,11 @@ | modification, are permitted provided that the conditions mentioned | | in the accompanying LICENSE file are met. | +--------------------------------------------------------------------+ - | Copyright (c) 2004-2010, Michael Wallner | + | Copyright (c) 2004-2011, Michael Wallner | +--------------------------------------------------------------------+ */ -/* $Id: http_cookie_api.c 298662 2010-04-27 13:42:32Z mike $ */ - -#include "php_http.h" - -#include +#include "php_http_api.h" PHP_HTTP_API php_http_cookie_list_t *php_http_cookie_list_init(php_http_cookie_list_t *list TSRMLS_DC) { @@ -79,7 +75,7 @@ PHP_HTTP_API void php_http_cookie_list_free(php_http_cookie_list_t **list) PHP_HTTP_API const char *php_http_cookie_list_get_cookie(php_http_cookie_list_t *list, const char *name, size_t name_len) { zval **cookie = NULL; - if ((SUCCESS != zend_hash_find(&list->cookies, name, name_len + 1, (void *) &cookie)) || (Z_TYPE_PP(cookie) != IS_STRING)) { + if ((SUCCESS != zend_symtable_find(&list->cookies, name, name_len + 1, (void *) &cookie)) || (Z_TYPE_PP(cookie) != IS_STRING)) { return NULL; } return Z_STRVAL_PP(cookie); @@ -90,7 +86,7 @@ PHP_HTTP_API const char *php_http_cookie_list_get_cookie(php_http_cookie_list_t PHP_HTTP_API const char *php_http_cookie_list_get_extra(php_http_cookie_list_t *list, const char *name, size_t name_len) { zval **extra = NULL; - if ((SUCCESS != zend_hash_find(&list->extras, name, name_len + 1, (void *) &extra)) || (Z_TYPE_PP(extra) != IS_STRING)) { + if ((SUCCESS != zend_symtable_find(&list->extras, name, name_len + 1, (void *) &extra)) || (Z_TYPE_PP(extra) != IS_STRING)) { return NULL; } return Z_STRVAL_PP(extra); @@ -104,7 +100,7 @@ PHP_HTTP_API void php_http_cookie_list_add_cookie(php_http_cookie_list_t *list, MAKE_STD_ZVAL(cookie_value); ZVAL_STRINGL(cookie_value, estrndup(value, value_len), value_len, 0); - zend_hash_update(&list->cookies, name, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL); + zend_symtable_update(&list->cookies, name, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL); } @@ -115,94 +111,98 @@ PHP_HTTP_API void php_http_cookie_list_add_extra(php_http_cookie_list_t *list, c MAKE_STD_ZVAL(cookie_value); ZVAL_STRINGL(cookie_value, estrndup(value, value_len), value_len, 0); - zend_hash_update(&list->extras, name, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL); + zend_symtable_update(&list->extras, name, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL); } +PHP_HTTP_API php_http_cookie_list_t *php_http_cookie_list_parse(php_http_cookie_list_t *list, const char *str, size_t len, long flags, char **allowed_extras TSRMLS_DC) +{ + php_http_params_opts_t opts; + HashTable params; + HashPosition pos1, pos2; + php_http_array_hashkey_t key = php_http_array_hashkey_init(0); + zval **param, **val, **args, **arg; -typedef struct php_http_param_parse_cb_arg { - php_http_cookie_list_t *list; - long flags; - char **allowed_extras; -} php_http_parse_param_cb_arg_t; - + php_http_params_opts_default_get(&opts); + opts.input.str = estrndup(str, len); + opts.input.len = len; + opts.param = NULL; + zend_hash_init(¶ms, 10, NULL, ZVAL_PTR_DTOR, 0); + php_http_params_parse(¶ms, &opts TSRMLS_CC); + efree(opts.input.str); -static void php_http_cookie_parse_callback(void *ptr, const char *key, int keylen, const char *val, int vallen TSRMLS_DC) -{ - php_http_parse_param_cb_arg_t *arg = (php_http_parse_param_cb_arg_t *) ptr; - -#define _KEY_IS(s) (keylen == lenof(s) && !strncasecmp(key, (s), keylen)) - if _KEY_IS("path") { - STR_SET(arg->list->path, estrndup(val, vallen)); - } else if _KEY_IS("domain") { - STR_SET(arg->list->domain, estrndup(val, vallen)); - } else if _KEY_IS("expires") { - char *date = estrndup(val, vallen); - arg->list->expires = php_parse_date(date, NULL); - efree(date); - } else if _KEY_IS("secure") { - arg->list->flags |= PHP_HTTP_COOKIE_SECURE; - } else if _KEY_IS("httpOnly") { - arg->list->flags |= PHP_HTTP_COOKIE_HTTPONLY; - } else { - /* check for extra */ - if (arg->allowed_extras) { - char **ae = arg->allowed_extras; - - for (; *ae; ++ae) { - if ((size_t) keylen == strlen(*ae) && !strncasecmp(key, *ae, keylen)) { - if (arg->flags & PHP_HTTP_COOKIE_PARSE_RAW) { - php_http_cookie_list_add_extra(arg->list, key, keylen, val, vallen); - } else { - char *dec = estrndup(val, vallen); - int declen = php_url_decode(dec, vallen); - - php_http_cookie_list_add_extra(arg->list, key, keylen, dec, declen); - efree(dec); + list = php_http_cookie_list_init(list TSRMLS_CC); + FOREACH_HASH_KEYVAL(pos1, ¶ms, key, param) { + if (Z_TYPE_PP(param) == IS_ARRAY) { + if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(param), ZEND_STRS("value"), (void *) &val)) { + Z_ADDREF_PP(val); + if (key.type == HASH_KEY_IS_STRING) { + zend_hash_update(&list->cookies, key.str, key.len, (void *) val, sizeof(zval *), NULL); + } else { + zend_hash_index_update(&list->cookies, key.num, (void *) val, sizeof(zval *), NULL); + } + } + if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(param), ZEND_STRS("arguments"), (void *) &args) && Z_TYPE_PP(args) == IS_ARRAY) { + FOREACH_KEYVAL(pos2, *args, key, arg) { + if (key.type == HASH_KEY_IS_STRING) { + zval *tmp = php_http_ztyp(IS_STRING, *arg); + char *arg_str = Z_STRVAL_P(tmp); + size_t arg_len = Z_STRLEN_P(tmp); +#define _KEY_IS(s) (key.len == sizeof(s) && !strncasecmp(key.str, (s), key.len)) + if _KEY_IS("path") { + STR_SET(list->path, estrndup(arg_str, arg_len)); + } else if _KEY_IS("domain") { + STR_SET(list->domain, estrndup(arg_str, arg_len)); + } else if _KEY_IS("expires") { + char *date = estrndup(arg_str, arg_len); + list->expires = php_parse_date(date, NULL); + efree(date); + } else if _KEY_IS("secure") { + list->flags |= PHP_HTTP_COOKIE_SECURE; + } else if _KEY_IS("httpOnly") { + list->flags |= PHP_HTTP_COOKIE_HTTPONLY; + } else { + /* check for extra */ + if (allowed_extras) { + char **ae = allowed_extras; + + for (; *ae; ++ae) { + if (!strncasecmp(key.str, *ae, key.len)) { + if (flags & PHP_HTTP_COOKIE_PARSE_RAW) { + php_http_cookie_list_add_extra(list, key.str, key.len, arg_str, arg_len); + } else { + char *dec = estrndup(arg_str, arg_len); + int declen = php_url_decode(dec, arg_len); + + php_http_cookie_list_add_extra(list, key.str, key.len, dec, declen); + efree(dec); + } + continue; + } + } + } + /* new cookie */ + if (flags & PHP_HTTP_COOKIE_PARSE_RAW) { + php_http_cookie_list_add_cookie(list, key.str, key.len, arg_str, arg_len); + } else { + char *dec = estrndup(arg_str, arg_len); + int declen = php_url_decode(dec, arg_len); + + php_http_cookie_list_add_cookie(list, key.str, key.len, dec, declen); + efree(dec); + } + } + zval_ptr_dtor(&tmp); } - return; } } } - /* new cookie */ - if (arg->flags & PHP_HTTP_COOKIE_PARSE_RAW) { - php_http_cookie_list_add_cookie(arg->list, key, keylen, val, vallen); - } else { - char *dec = estrndup(val, vallen); - int declen = php_url_decode(dec, vallen); - - php_http_cookie_list_add_cookie(arg->list, key, keylen, dec, declen); - efree(dec); - } } -} - - + zend_hash_destroy(¶ms); -PHP_HTTP_API php_http_cookie_list_t *php_http_cookie_list_parse(php_http_cookie_list_t *list, const char *string, long flags, char **allowed_extras TSRMLS_DC) -{ - int free_list = !list; - php_http_parse_param_cb_arg_t arg; - - list = php_http_cookie_list_init(list TSRMLS_CC); - - arg.list = list; - arg.flags = flags; - arg.allowed_extras = allowed_extras; - - if (SUCCESS != php_http_params_parse(string, PHP_HTTP_PARAMS_RAISE_ERROR, php_http_cookie_parse_callback, &arg TSRMLS_CC)) { - if (free_list) { - php_http_cookie_list_free(&list); - } else { - php_http_cookie_list_dtor(list); - } - list = NULL; - } - return list; } - PHP_HTTP_API void php_http_cookie_list_to_struct(php_http_cookie_list_t *list, zval *strct) { zval array, *cookies, *extras; @@ -549,7 +549,7 @@ PHP_METHOD(HttpCookie, __construct) default: { zval *cpy = php_http_ztyp(IS_STRING, zcookie); - obj->list = php_http_cookie_list_parse(obj->list, Z_STRVAL_P(cpy), flags, ae TSRMLS_CC); + obj->list = php_http_cookie_list_parse(obj->list, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy), flags, ae TSRMLS_CC); zval_ptr_dtor(&cpy); break; } @@ -669,7 +669,7 @@ PHP_METHOD(HttpCookie, getCookie) if (!obj->list) { obj->list = php_http_cookie_list_init(NULL TSRMLS_CC); } - if (SUCCESS == zend_hash_find(&obj->list->cookies, name_str, name_len + 1, (void *) &zvalue)) { + if (SUCCESS == zend_symtable_find(&obj->list->cookies, name_str, name_len + 1, (void *) &zvalue)) { RETURN_ZVAL(*zvalue, 1, 0); } } @@ -688,13 +688,13 @@ PHP_METHOD(HttpCookie, setCookie) obj->list = php_http_cookie_list_init(NULL TSRMLS_CC); } if (!value_str) { - zend_hash_del(&obj->list->cookies, name_str, name_len + 1); + zend_symtable_del(&obj->list->cookies, name_str, name_len + 1); } else { zval *zvalue; MAKE_STD_ZVAL(zvalue); ZVAL_STRINGL(zvalue, value_str, value_len, 1); - zend_hash_update(&obj->list->cookies, name_str, name_len + 1, &zvalue, sizeof(zval *), NULL); + zend_symtable_update(&obj->list->cookies, name_str, name_len + 1, &zvalue, sizeof(zval *), NULL); } } RETVAL_ZVAL(getThis(), 1, 0); @@ -714,7 +714,7 @@ PHP_METHOD(HttpCookie, addCookie) } MAKE_STD_ZVAL(zvalue); ZVAL_STRINGL(zvalue, value_str, value_len, 1); - zend_hash_add(&obj->list->cookies, name_str, name_len + 1, &zvalue, sizeof(zval *), NULL); + zend_symtable_update(&obj->list->cookies, name_str, name_len + 1, &zvalue, sizeof(zval *), NULL); } RETVAL_ZVAL(getThis(), 1, 0); } @@ -731,7 +731,7 @@ PHP_METHOD(HttpCookie, getExtra) if (!obj->list) { obj->list = php_http_cookie_list_init(NULL TSRMLS_CC); } - if (SUCCESS == zend_hash_find(&obj->list->extras, name_str, name_len + 1, (void *) &zvalue)) { + if (SUCCESS == zend_symtable_find(&obj->list->extras, name_str, name_len + 1, (void *) &zvalue)) { RETURN_ZVAL(*zvalue, 1, 0); } } @@ -750,13 +750,13 @@ PHP_METHOD(HttpCookie, setExtra) obj->list = php_http_cookie_list_init(NULL TSRMLS_CC); } if (!value_str) { - zend_hash_del(&obj->list->extras, name_str, name_len + 1); + zend_symtable_del(&obj->list->extras, name_str, name_len + 1); } else { zval *zvalue; MAKE_STD_ZVAL(zvalue); ZVAL_STRINGL(zvalue, value_str, value_len, 1); - zend_hash_update(&obj->list->extras, name_str, name_len + 1, &zvalue, sizeof(zval *), NULL); + zend_symtable_update(&obj->list->extras, name_str, name_len + 1, &zvalue, sizeof(zval *), NULL); } } RETVAL_ZVAL(getThis(), 1, 0); @@ -776,7 +776,7 @@ PHP_METHOD(HttpCookie, addExtra) } MAKE_STD_ZVAL(zvalue); ZVAL_STRINGL(zvalue, value_str, value_len, 1); - zend_hash_add(&obj->list->extras, name_str, name_len + 1, &zvalue, sizeof(zval *), NULL); + zend_symtable_update(&obj->list->extras, name_str, name_len + 1, &zvalue, sizeof(zval *), NULL); } RETVAL_ZVAL(getThis(), 1, 0); } @@ -933,7 +933,6 @@ PHP_METHOD(HttpCookie, toArray) PHP_MINIT_FUNCTION(http_cookie) { PHP_HTTP_REGISTER_CLASS(http, Cookie, http_cookie, php_http_object_class_entry, 0); - zend_class_implements(php_http_cookie_class_entry TSRMLS_CC, 1, php_http_fluently_callable_class_entry); php_http_cookie_class_entry->create_object = php_http_cookie_object_new; memcpy(&php_http_cookie_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_http_cookie_object_handlers.clone_obj = php_http_cookie_object_clone;