remove superfluous buffer macros
[m6w6/ext-http] / php_http_cookie.c
index 146b08656c74ead4f50624f2bb84051db22341f4..b767bbbb7e83d974b716ff19ed435068536c9150 100644 (file)
@@ -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 <mike@php.net>            |
+    | Copyright (c) 2004-2011, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
-/* $Id: http_cookie_api.c 298662 2010-04-27 13:42:32Z mike $ */
-
-#include "php_http.h"
-
-#include <ext/date/php_date.h>
+#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)
 {
@@ -27,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);
@@ -118,91 +114,93 @@ 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);
 
-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;
-
+       if (!(flags & PHP_HTTP_COOKIE_PARSE_RAW)) {
+               Z_STRLEN_P(arg) = php_raw_url_decode(Z_STRVAL_P(arg), Z_STRLEN_P(arg));
+       }
 
-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));
+               STR_SET(list->path, estrndup(Z_STRVAL_P(arg), Z_STRLEN_P(arg)));
        } else if _KEY_IS("domain") {
-               STR_SET(arg->list->domain, estrndup(val, vallen));
+               STR_SET(list->domain, estrndup(Z_STRVAL_P(arg), Z_STRLEN_P(arg)));
        } else if _KEY_IS("expires") {
-               char *date = estrndup(val, vallen);
-               arg->list->expires = php_parse_date(date, NULL);
+               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") {
-               arg->list->flags |= PHP_HTTP_COOKIE_SECURE;
+               list->flags |= PHP_HTTP_COOKIE_SECURE;
        } else if _KEY_IS("httpOnly") {
-               arg->list->flags |= PHP_HTTP_COOKIE_HTTPONLY;
+               list->flags |= PHP_HTTP_COOKIE_HTTPONLY;
        } else {
                /* check for extra */
-               if (arg->allowed_extras) {
-                       char **ae = arg->allowed_extras;
-                       
+               if (allowed_extras) {
+                       char **ae = allowed_extras;
+
+                       php_http_array_hashkey_stringify(key);
                        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);
+                               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 {
-                                               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);
+                                               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);
                }
-               /* new cookie */
-               if (arg->flags & PHP_HTTP_COOKIE_PARSE_RAW) {
-                       php_http_cookie_list_add_cookie(arg->list, key, keylen, val, vallen);
+
+               /* cookie */
+               if (key->type == HASH_KEY_IS_LONG) {
+                       zend_hash_index_update(&list->cookies, key->num, (void *) &arg, sizeof(zval *), NULL);
                } 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_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;
+       HashTable params;
+       HashPosition pos1, pos2;
+       php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
+       zval **param, **val, **args, **arg;
 
+       php_http_params_opts_default_get(&opts);
+       opts.input.str = estrndup(str, len);
+       opts.input.len = len;
+       opts.param = NULL;
+       zend_hash_init(&params, 10, NULL, ZVAL_PTR_DTOR, 0);
+       php_http_params_parse(&params, &opts TSRMLS_CC);
+       efree(opts.input.str);
 
-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);
+       FOREACH_HASH_KEYVAL(pos1, &params, key, param) {
+               if (Z_TYPE_PP(param) == IS_ARRAY) {
+                       if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(param), ZEND_STRS("value"), (void *) &val)) {
+                               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) {
+                                       add_entry(list, allowed_extras, flags, &key, *arg);
+                               }
+                       }
                }
-               list = NULL;
        }
-       
+       zend_hash_destroy(&params);
+
        return list;
 }
 
 
-
 PHP_HTTP_API void php_http_cookie_list_to_struct(php_http_cookie_list_t *list, zval *strct)
 {
        zval array, *cookies, *extras;
@@ -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,18 +530,36 @@ 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: {
                                                        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;
                                                }
                                        }
+
+                                       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;