switch to new serialization mechanism
[m6w6/ext-http] / src / php_http_header.c
index 91204c13fa7169f1ab25de7c9dc8dbbea6840e16..47db160191d5cd8e52b6f50c707cac5080cf6606 100644 (file)
@@ -100,6 +100,7 @@ void php_http_header_to_callback_ex(const char *key, zval *val, zend_bool crlf,
        zval *aval;
        zend_string *str;
 
+       ZVAL_DEREF(val);
        switch (Z_TYPE_P(val)) {
        case IS_ARRAY:
                ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(val), aval)
@@ -182,17 +183,17 @@ PHP_METHOD(HttpHeader, __construct)
 
        if (name_str && name_len) {
                char *pretty_str = estrndup(name_str, name_len);
-               zend_update_property_stringl(php_http_header_class_entry, getThis(), ZEND_STRL("name"), php_http_pretty_key(pretty_str, name_len, 1, 1), name_len);
+               zend_update_property_stringl(php_http_header_class_entry, Z_OBJ_P(ZEND_THIS), ZEND_STRL("name"), php_http_pretty_key(pretty_str, name_len, 1, 1), name_len);
                efree(pretty_str);
        }
        if (value_str && value_len) {
-               zend_update_property_stringl(php_http_header_class_entry, getThis(), ZEND_STRL("value"), value_str, value_len);
+               zend_update_property_stringl(php_http_header_class_entry, Z_OBJ_P(ZEND_THIS), ZEND_STRL("value"), value_str, value_len);
        }
 }
 
-ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeader_serialize, 0, 0, 0)
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeader_tostring, 0, 0, 0)
 ZEND_END_ARG_INFO();
-PHP_METHOD(HttpHeader, serialize)
+PHP_METHOD(HttpHeader, __toString)
 {
        if (SUCCESS == zend_parse_parameters_none()) {
                php_http_buffer_t buf;
@@ -200,11 +201,11 @@ PHP_METHOD(HttpHeader, serialize)
                zval name_tmp, value_tmp;
 
                php_http_buffer_init(&buf);
-               zs = zval_get_string(zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("name"), 0, &name_tmp));
+               zs = zval_get_string(zend_read_property(php_http_header_class_entry, Z_OBJ_P(ZEND_THIS), ZEND_STRL("name"), 0, &name_tmp));
                php_http_buffer_appendz(&buf, zs);
                zend_string_release(zs);
 
-               zs = zval_get_string(zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("value"), 0, &value_tmp));
+               zs = zval_get_string(zend_read_property(php_http_header_class_entry, Z_OBJ_P(ZEND_THIS), ZEND_STRL("value"), 0, &value_tmp));
                if (zs->len) {
                        php_http_buffer_appends(&buf, ": ");
                        php_http_buffer_appendz(&buf, zs);
@@ -218,42 +219,44 @@ PHP_METHOD(HttpHeader, serialize)
        RETURN_EMPTY_STRING();
 }
 
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeader_serialize, 0, 0, 0)
+ZEND_END_ARG_INFO();
+PHP_METHOD(HttpHeader, __serialize)
+{
+       zval *val, tmp;
+
+       if (SUCCESS != zend_parse_parameters_none()) {
+               RETURN_THROWS();
+       }
+       array_init(return_value);
+
+       val = zend_read_property(php_http_header_class_entry, Z_OBJ_P(ZEND_THIS), ZEND_STRL("name"), 0, &tmp);
+       zend_hash_next_index_insert(Z_ARRVAL_P(return_value), val);
+       val = zend_read_property(php_http_header_class_entry, Z_OBJ_P(ZEND_THIS), ZEND_STRL("value"), 0, &tmp);
+       zend_hash_next_index_insert(Z_ARRVAL_P(return_value), val);
+}
+
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeader_unserialize, 0, 0, 1)
        ZEND_ARG_INFO(0, serialized)
 ZEND_END_ARG_INFO();
-PHP_METHOD(HttpHeader, unserialize)
+PHP_METHOD(HttpHeader, __unserialize)
 {
-       char *serialized_str;
-       size_t serialized_len;
-
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &serialized_str, &serialized_len)) {
-               HashTable ht;
-
-               zend_hash_init(&ht, 1, NULL, ZVAL_PTR_DTOR, 0);
-               if (SUCCESS == php_http_header_parse(serialized_str, serialized_len, &ht, NULL, NULL)) {
-                       if (zend_hash_num_elements(&ht)) {
-                               zend_string *zs, *key;
-                               zend_ulong idx;
-
-                               zend_hash_internal_pointer_reset(&ht);
-                               switch (zend_hash_get_current_key(&ht, &key, &idx)) {
-                                       case HASH_KEY_IS_STRING:
-                                               zend_update_property_str(php_http_header_class_entry, getThis(), ZEND_STRL("name"), key);
-                                               break;
-                                       case HASH_KEY_IS_LONG:
-                                               zend_update_property_long(php_http_header_class_entry, getThis(), ZEND_STRL("name"), idx);
-                                               break;
-                                       default:
-                                               break;
-                               }
-                               zs = zval_get_string(zend_hash_get_current_data(&ht));
-                               zend_update_property_str(php_http_header_class_entry, getThis(), ZEND_STRL("value"), zs);
-                               zend_string_release(zs);
-                       }
-               }
-               zend_hash_destroy(&ht);
+       zval *serialized;
+       HashTable *data;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
+               RETURN_THROWS();
        }
 
+       ZVAL_NULL(return_value);
+       serialized = zend_hash_index_find(data, 0);
+       if (serialized) {
+               zend_update_property(php_http_header_class_entry, Z_OBJ_P(ZEND_THIS), ZEND_STRL("name"), serialized);
+       }
+       serialized = zend_hash_index_find(data, 1);
+       if (serialized) {
+               zend_update_property(php_http_header_class_entry, Z_OBJ_P(ZEND_THIS), ZEND_STRL("value"), serialized);
+       }
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeader_match, 0, 0, 1)
@@ -272,7 +275,7 @@ PHP_METHOD(HttpHeader, match)
                return;
        }
 
-       zs = zval_get_string(zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("value"), 0, &value_tmp));
+       zs = zval_get_string(zend_read_property(php_http_header_class_entry, Z_OBJ_P(ZEND_THIS), ZEND_STRL("value"), 0, &value_tmp));
        RETVAL_BOOL(php_http_match(zs->val, val_str, flags));
        zend_string_release(zs);
 }
@@ -298,7 +301,7 @@ PHP_METHOD(HttpHeader, negotiate)
                array_init(rs_array);
        }
 
-       zs = zval_get_string(zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("name"), 0, &name_tmp));
+       zs = zval_get_string(zend_read_property(php_http_header_class_entry, Z_OBJ_P(ZEND_THIS), ZEND_STRL("name"), 0, &name_tmp));
        if (zend_string_equals_literal(zs, "Accept")) {
                sep_str = "/";
                sep_len = 1;
@@ -308,7 +311,7 @@ PHP_METHOD(HttpHeader, negotiate)
        }
        zend_string_release(zs);
 
-       zs = zval_get_string(zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("value"), 0, &value_tmp));
+       zs = zval_get_string(zend_read_property(php_http_header_class_entry, Z_OBJ_P(ZEND_THIS), ZEND_STRL("value"), 0, &value_tmp));
        if ((rs = php_http_negotiate(zs->val, zs->len, supported, sep_str, sep_len))) {
                PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(rs, supported, rs_array);
        } else {
@@ -332,7 +335,7 @@ PHP_METHOD(HttpHeader, getParams)
        object_init_ex(&zparams_obj, php_http_params_get_class_entry());
        
        zargs = (zval *) ecalloc(ZEND_NUM_ARGS()+1, sizeof(zval));
-       ZVAL_COPY_VALUE(&zargs[0], zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("value"), 0, &value_tmp));
+       ZVAL_COPY_VALUE(&zargs[0], zend_read_property(php_http_header_class_entry, Z_OBJ_P(ZEND_THIS), ZEND_STRL("value"), 0, &value_tmp));
        if (ZEND_NUM_ARGS()) {
                zend_get_parameters_array(ZEND_NUM_ARGS(), ZEND_NUM_ARGS(), &zargs[1]);
        }
@@ -380,7 +383,7 @@ PHP_METHOD(HttpHeader, parse)
 
                                        object_init_ex(&zho, ce);
                                        Z_TRY_ADDREF_P(val);
-                                       zend_call_method_with_2_params(&zho, ce, NULL, "__construct", NULL, &zkey, val);
+                                       zend_call_method_with_2_params(Z_OBJ(zho), ce, NULL, "__construct", NULL, &zkey, val);
                                        zval_ptr_dtor(val);
                                        zval_ptr_dtor(&zkey);
 
@@ -397,11 +400,11 @@ PHP_METHOD(HttpHeader, parse)
 }
 
 static zend_function_entry php_http_header_methods[] = {
-       PHP_ME(HttpHeader, __construct,   ai_HttpHeader___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
-       PHP_ME(HttpHeader, serialize,     ai_HttpHeader_serialize, ZEND_ACC_PUBLIC)
-       ZEND_MALIAS(HttpHeader, __toString, serialize, ai_HttpHeader_serialize, ZEND_ACC_PUBLIC)
-       ZEND_MALIAS(HttpHeader, toString, serialize, ai_HttpHeader_serialize, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpHeader, unserialize,   ai_HttpHeader_unserialize, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpHeader, __construct,   ai_HttpHeader___construct, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpHeader, __serialize,     ai_HttpHeader_serialize, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpHeader, __toString,      ai_HttpHeader_tostring, ZEND_ACC_PUBLIC)
+       ZEND_MALIAS(HttpHeader, toString, __toString, ai_HttpHeader_tostring, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpHeader, __unserialize,   ai_HttpHeader_unserialize, ZEND_ACC_PUBLIC)
        PHP_ME(HttpHeader, match,         ai_HttpHeader_match, ZEND_ACC_PUBLIC)
        PHP_ME(HttpHeader, negotiate,     ai_HttpHeader_negotiate, ZEND_ACC_PUBLIC)
        PHP_ME(HttpHeader, getParams,     ai_HttpHeader_getParams, ZEND_ACC_PUBLIC)
@@ -415,7 +418,6 @@ PHP_MINIT_FUNCTION(http_header)
 
        INIT_NS_CLASS_ENTRY(ce, "http", "Header", php_http_header_methods);
        php_http_header_class_entry = zend_register_internal_class(&ce);
-       zend_class_implements(php_http_header_class_entry, 1, zend_ce_serializable);
        zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_LOOSE"), PHP_HTTP_MATCH_LOOSE);
        zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_CASE"), PHP_HTTP_MATCH_CASE);
        zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_WORD"), PHP_HTTP_MATCH_WORD);