X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_cookie.c;h=b767bbbb7e83d974b716ff19ed435068536c9150;hp=a92bb5e6d29c15ed80eed3ffcecfffb258d0a220;hb=25f0c16244fc5f8b2c9d9bfddab8a541d2521789;hpb=aacbe6aa75ca07853bf88d5b3647f67f107d1af7 diff --git a/php_http_cookie.c b/php_http_cookie.c index a92bb5e..b767bbb 100644 --- a/php_http_cookie.c +++ b/php_http_cookie.c @@ -23,7 +23,7 @@ PHP_HTTP_API php_http_cookie_list_t *php_http_cookie_list_init(php_http_cookie_l list->path = NULL; list->domain = NULL; - list->expires = 0; + list->expires = -1; list->flags = 0; TSRMLS_SET_CTX(list->ts); @@ -114,6 +114,58 @@ PHP_HTTP_API void php_http_cookie_list_add_extra(php_http_cookie_list_t *list, c zend_symtable_update(&list->extras, name, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL); } +#define _KEY_IS(s) (key->len == sizeof(s) && !strncasecmp(key->str, (s), key->len)) +static void add_entry(php_http_cookie_list_t *list, char **allowed_extras, long flags, php_http_array_hashkey_t *key, zval *val) +{ + zval *arg = php_http_zsep(1, IS_STRING, val); + + if (!(flags & PHP_HTTP_COOKIE_PARSE_RAW)) { + Z_STRLEN_P(arg) = php_raw_url_decode(Z_STRVAL_P(arg), Z_STRLEN_P(arg)); + } + + if _KEY_IS("path") { + STR_SET(list->path, estrndup(Z_STRVAL_P(arg), Z_STRLEN_P(arg))); + } else if _KEY_IS("domain") { + STR_SET(list->domain, estrndup(Z_STRVAL_P(arg), Z_STRLEN_P(arg))); + } else if _KEY_IS("expires") { + char *date = estrndup(Z_STRVAL_P(arg), Z_STRLEN_P(arg)); + 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; + + php_http_array_hashkey_stringify(key); + for (; *ae; ++ae) { + if (!strncasecmp(key->str, *ae, key->len)) { + if (key->type == HASH_KEY_IS_LONG) { + zend_hash_index_update(&list->extras, key->num, (void *) &arg, sizeof(zval *), NULL); + } else { + zend_hash_update(&list->extras, key->str, key->len, (void *) &arg, sizeof(zval *), NULL); + } + php_http_array_hashkey_stringfree(key); + return; + } + } + php_http_array_hashkey_stringfree(key); + } + + /* cookie */ + if (key->type == HASH_KEY_IS_LONG) { + zend_hash_index_update(&list->cookies, key->num, (void *) &arg, sizeof(zval *), NULL); + } else { + zend_hash_update(&list->cookies, key->str, key->len, (void *) &arg, sizeof(zval *), NULL); + } + return; + } + zval_ptr_dtor(&arg); +} + 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; @@ -134,65 +186,11 @@ PHP_HTTP_API php_http_cookie_list_t *php_http_cookie_list_parse(php_http_cookie_ 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); - } + add_entry(list, NULL, flags, &key, *val); } 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); - } + add_entry(list, allowed_extras, flags, &key, *arg); } } } @@ -240,44 +238,24 @@ PHP_HTTP_API php_http_cookie_list_t *php_http_cookie_list_from_struct(php_http_c zend_hash_copy(&list->extras, Z_ARRVAL_PP(tmp), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); } if (SUCCESS == zend_hash_find(ht, "flags", sizeof("flags"), (void *) &tmp)) { - switch (Z_TYPE_PP(tmp)) { - case IS_LONG: - list->flags = Z_LVAL_PP(tmp); - break; - case IS_DOUBLE: - list->flags = (long) Z_DVAL_PP(tmp); - break; - case IS_STRING: - cpy = php_http_ztyp(IS_LONG, *tmp); - list->flags = Z_LVAL_P(cpy); - zval_ptr_dtor(&cpy); - break; - default: - break; - } + cpy = php_http_ztyp(IS_LONG, *tmp); + list->flags = Z_LVAL_P(cpy); + zval_ptr_dtor(&cpy); } if (SUCCESS == zend_hash_find(ht, "expires", sizeof("expires"), (void *) &tmp)) { - switch (Z_TYPE_PP(tmp)) { - case IS_LONG: - list->expires = Z_LVAL_PP(tmp); - break; - case IS_DOUBLE: - list->expires = (long) Z_DVAL_PP(tmp); - break; - case IS_STRING: - cpy = php_http_ztyp(IS_LONG, *tmp); - if (Z_LVAL_P(cpy)) { - list->expires = Z_LVAL_P(cpy); - } else { - time_t expires = php_parse_date(Z_STRVAL_PP(tmp), NULL); - if (expires > 0) { - list->expires = expires; - } - } - zval_ptr_dtor(&cpy); - break; - default: - break; + if (Z_TYPE_PP(tmp) == IS_LONG) { + list->expires = Z_LVAL_PP(tmp); + } else { + long lval; + + cpy = php_http_ztyp(IS_STRING, *tmp); + if (IS_LONG == is_numeric_string(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy), &lval, NULL, 0)) { + list->expires = lval; + } else { + list->expires = php_parse_date(Z_STRVAL_P(cpy), NULL); + } + + zval_ptr_dtor(&cpy); } } if (SUCCESS == zend_hash_find(ht, "path", sizeof("path"), (void *) &tmp) && Z_TYPE_PP(tmp) == IS_STRING) { @@ -297,8 +275,8 @@ static inline void append_encoded(php_http_buffer_t *buf, const char *key, size_ char *enc_str[2]; int enc_len[2]; - enc_str[0] = php_url_encode(key, key_len, &enc_len[0]); - enc_str[1] = php_url_encode(val, val_len, &enc_len[1]); + enc_str[0] = php_raw_url_encode(key, key_len, &enc_len[0]); + enc_str[1] = php_raw_url_encode(val, val_len, &enc_len[1]); php_http_buffer_append(buf, enc_str[0], enc_len[0]); php_http_buffer_appends(buf, "="); @@ -322,11 +300,13 @@ PHP_HTTP_API void php_http_cookie_list_to_string(php_http_cookie_list_t *list, c php_http_buffer_init(&buf); FOREACH_HASH_KEYVAL(pos, &list->cookies, key, val) { - if (key.type == HASH_KEY_IS_STRING && key.len) { - zval *tmp = php_http_ztyp(IS_STRING, *val); - append_encoded(&buf, key.str, key.len-1, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp)); - zval_ptr_dtor(&tmp); - } + zval *tmp = php_http_ztyp(IS_STRING, *val); + + php_http_array_hashkey_stringify(&key); + append_encoded(&buf, key.str, key.len-1, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp)); + php_http_array_hashkey_stringfree(&key); + + zval_ptr_dtor(&tmp); } if (list->domain && *list->domain) { @@ -335,18 +315,20 @@ PHP_HTTP_API void php_http_cookie_list_to_string(php_http_cookie_list_t *list, c if (list->path && *list->path) { php_http_buffer_appendf(&buf, "path=%s; ", list->path); } - if (list->expires) { + if (list->expires >= 0) { char *date = php_format_date(ZEND_STRL(PHP_HTTP_DATE_FORMAT), list->expires, 0 TSRMLS_CC); php_http_buffer_appendf(&buf, "expires=%s; ", date); efree(date); } FOREACH_HASH_KEYVAL(pos, &list->extras, key, val) { - if (key.type == HASH_KEY_IS_STRING && key.len) { - zval *tmp = php_http_ztyp(IS_STRING, *val); - append_encoded(&buf, key.str, key.len-1, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp)); - zval_ptr_dtor(&tmp); - } + zval *tmp = php_http_ztyp(IS_STRING, *val); + + php_http_array_hashkey_stringify(&key); + append_encoded(&buf, key.str, key.len-1, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp)); + php_http_array_hashkey_stringfree(&key); + + zval_ptr_dtor(&tmp); } if (list->flags & PHP_HTTP_COOKIE_SECURE) { @@ -357,8 +339,8 @@ PHP_HTTP_API void php_http_cookie_list_to_string(php_http_cookie_list_t *list, c } php_http_buffer_fix(&buf); - *str = PHP_HTTP_BUFFER_VAL(&buf); - *len = PHP_HTTP_BUFFER_LEN(&buf); + *str = buf.data; + *len = buf.used; } #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpCookie, method, 0, req_args) @@ -428,8 +410,14 @@ PHP_HTTP_BEGIN_ARGS(getExtra, 1) PHP_HTTP_ARG_VAL(name, 0) PHP_HTTP_END_ARGS; -zend_class_entry *php_http_cookie_class_entry; -zend_function_entry php_http_cookie_method_entry[] = { +static zend_class_entry *php_http_cookie_class_entry; + +zend_class_entry *php_http_cookie_get_class_entry(void) +{ + return php_http_cookie_class_entry; +} + +static zend_function_entry php_http_cookie_method_entry[] = { PHP_HTTP_COOKIE_ME(__construct, ZEND_ACC_PUBLIC) PHP_HTTP_COOKIE_ME(getCookies, ZEND_ACC_PUBLIC) PHP_HTTP_COOKIE_ME(setCookies, ZEND_ACC_PUBLIC) @@ -515,14 +503,14 @@ void php_http_cookie_object_free(void *object TSRMLS_DC) PHP_METHOD(HttpCookie, __construct) { - with_error_handling(EH_THROW, php_http_exception_class_entry) { + with_error_handling(EH_THROW, php_http_exception_get_class_entry()) { zval *zcookie = NULL; long flags = 0; HashTable *allowed_extras = NULL; if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!lH", &zcookie, &flags, &allowed_extras)) { if (zcookie) { - with_error_handling(EH_THROW, php_http_exception_class_entry) { + with_error_handling(EH_THROW, php_http_exception_get_class_entry()) { char **ae = NULL; php_http_cookie_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); @@ -542,8 +530,17 @@ PHP_METHOD(HttpCookie, __construct) } switch (Z_TYPE_P(zcookie)) { - case IS_ARRAY: case IS_OBJECT: + if (instanceof_function(Z_OBJCE_P(zcookie), php_http_cookie_class_entry TSRMLS_CC)) { + php_http_cookie_object_t *zco = zend_object_store_get_object(zcookie TSRMLS_CC); + + if (zco->list) { + obj->list = php_http_cookie_list_copy(zco->list, NULL); + } + break; + } + /* no break */ + case IS_ARRAY: obj->list = php_http_cookie_list_from_struct(obj->list, zcookie TSRMLS_CC); break; default: { @@ -554,6 +551,15 @@ PHP_METHOD(HttpCookie, __construct) break; } } + + if (ae) { + char **ae_ptr; + + for (ae_ptr = ae; *ae_ptr; ++ae_ptr) { + efree(*ae_ptr); + } + efree(ae); + } } end_error_handling(); } } @@ -678,8 +684,8 @@ PHP_METHOD(HttpCookie, getCookie) PHP_METHOD(HttpCookie, setCookie) { - char *name_str, *value_str; - int name_len, value_len; + char *name_str, *value_str = NULL; + int name_len, value_len = 0; if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s!", &name_str, &name_len, &value_str, &value_len)) { php_http_cookie_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); @@ -740,8 +746,8 @@ PHP_METHOD(HttpCookie, getExtra) PHP_METHOD(HttpCookie, setExtra) { - char *name_str, *value_str; - int name_len, value_len; + char *name_str, *value_str = NULL; + int name_len, value_len = 0; if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s!", &name_str, &name_len, &value_str, &value_len)) { php_http_cookie_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); @@ -860,7 +866,7 @@ PHP_METHOD(HttpCookie, getExpires) PHP_METHOD(HttpCookie, setExpires) { - long ts = 0; + long ts = -1; if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &ts)) { php_http_cookie_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); @@ -932,7 +938,7 @@ PHP_METHOD(HttpCookie, toArray) PHP_MINIT_FUNCTION(http_cookie) { - PHP_HTTP_REGISTER_CLASS(http, Cookie, http_cookie, php_http_object_class_entry, 0); + PHP_HTTP_REGISTER_CLASS(http, Cookie, http_cookie, php_http_object_get_class_entry(), 0); 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;