followup on #44
[m6w6/ext-http] / src / php_http_message.c
index 45f584f6094e5fff1c1561cf119660f15dcabe05..cb1192e065ab30795c0bc5f15f9a49fc77414407 100644 (file)
@@ -70,6 +70,7 @@ php_http_message_t *php_http_message_init_env(php_http_message_t *message, php_h
                        if ((sval = php_http_env_get_server_var(ZEND_STRL("REQUEST_URI"), 1))) {
                                message->http.info.request.url = php_http_url_parse(Z_STRVAL_P(sval), Z_STRLEN_P(sval), ~0);
                        }
+
                        php_http_env_get_request_headers(&message->hdrs);
                        break;
 
@@ -81,11 +82,12 @@ php_http_message_t *php_http_message_init_env(php_http_message_t *message, php_h
                                }
                                message->http.info.response.status = estrdup(php_http_env_get_response_status_for_code(message->http.info.response.code));
                        }
-                       
+
                        php_http_env_get_response_headers(&message->hdrs);
                        if (php_output_get_level()) {
                                if (php_output_get_status() & PHP_OUTPUT_SENT) {
                                        php_error_docref(NULL, E_WARNING, "Could not fetch response body, output has already been sent at %s:%d", php_output_get_start_filename(), php_output_get_start_lineno());
+
                                        goto error;
                                } else if (SUCCESS != php_output_get_contents(&tval)) {
                                        php_error_docref(NULL, E_WARNING, "Could not fetch response body");
@@ -409,7 +411,7 @@ void php_http_message_serialize(php_http_message_t *message, char **string, size
 php_http_message_t *php_http_message_reverse(php_http_message_t *msg)
 {
        size_t i, c = php_http_message_count(msg);
-       
+
        if (c > 1) {
                php_http_message_t *tmp = msg, **arr;
 
@@ -452,23 +454,22 @@ php_http_message_t *php_http_message_copy_ex(php_http_message_t *from, php_http_
 {
        php_http_message_t *temp, *copy = NULL;
        php_http_info_t info;
-       
+
        if (from) {
                info.type = from->type;
                info.http = from->http;
-               
+
                copy = temp = php_http_message_init(to, 0, php_http_message_body_copy(from->body, NULL));
                php_http_message_set_info(temp, &info);
-               zend_hash_copy(&temp->hdrs, &from->hdrs, (copy_ctor_func_t) zval_add_ref);
-       
+               array_copy(&from->hdrs, &temp->hdrs);
+
                if (parents) while (from->parent) {
                        info.type = from->parent->type;
                        info.http = from->parent->http;
-               
+
                        temp->parent = php_http_message_init(NULL, 0, php_http_message_body_copy(from->parent->body, NULL));
                        php_http_message_set_info(temp->parent, &info);
                        array_copy(&from->parent->hdrs, &temp->parent->hdrs);
-               
 
                        temp = temp->parent;
                        from = from->parent;
@@ -662,7 +663,7 @@ static void php_http_message_object_prophandler_set_parent_message(php_http_mess
                php_http_message_object_t *parent_obj = PHP_HTTP_OBJ(NULL, value);
 
                if (obj->message->parent) {
-                       zend_objects_store_del(&obj->parent->zo);
+                       zend_object_release(&obj->parent->zo);
                }
                Z_ADDREF_P(value);
                obj->parent = parent_obj;
@@ -796,7 +797,7 @@ ZEND_RESULT_CODE php_http_message_object_set_body(php_http_message_object_t *msg
                body_obj->body = php_http_message_body_init(NULL, NULL);
        }
        if (msg_obj->body) {
-               zend_objects_store_del(&msg_obj->body->zo);
+               zend_object_release(&msg_obj->body->zo);
        }
        if (msg_obj->message) {
                php_http_message_body_free(&msg_obj->message->body);
@@ -857,6 +858,8 @@ void php_http_message_object_free(zend_object *object)
 {
        php_http_message_object_t *o = PHP_HTTP_OBJ(object, NULL);
 
+       PTR_FREE(o->gc);
+
        if (!Z_ISUNDEF(o->iterator)) {
                zval_ptr_dtor(&o->iterator);
                ZVAL_UNDEF(&o->iterator);
@@ -868,17 +871,11 @@ void php_http_message_object_free(zend_object *object)
                o->message = NULL;
        }
        if (o->parent) {
-               if (GC_REFCOUNT(&o->parent->zo) == 1) {
-                       zend_objects_store_del(&o->parent->zo);
-               }
-               zend_objects_store_del(&o->parent->zo);
+               zend_object_release(&o->parent->zo);
                o->parent = NULL;
        }
        if (o->body) {
-               if (GC_REFCOUNT(&o->body->zo) == 1) {
-                       zend_objects_store_del(&o->body->zo);
-               }
-               zend_objects_store_del(&o->body->zo);
+               zend_object_release(&o->body->zo);
                o->body = NULL;
        }
        zend_object_std_dtor(object);
@@ -944,7 +941,7 @@ static HashTable *php_http_message_object_get_debug_info(zval *object, int *is_t
 
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
        *is_temp = 0;
-       
+
 #define UPDATE_PROP(name_str, action_with_tmp) \
        do { \
                zend_property_info *pi; \
@@ -1013,6 +1010,32 @@ static HashTable *php_http_message_object_get_debug_info(zval *object, int *is_t
        return props;
 }
 
+static HashTable *php_http_message_object_get_gc(zval *object, zval **table, int *n)
+{
+       php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, object);
+       HashTable *props = Z_OBJPROP_P(object);
+       uint32_t count = 2 + zend_hash_num_elements(props);
+       zval *val;
+
+       *n = 0;
+       *table = obj->gc = erealloc(obj->gc, count * sizeof(zval));
+
+       if (obj->body) {
+               ZVAL_OBJ(&obj->gc[(*n)++], &obj->body->zo);
+       }
+       if (obj->parent) {
+               ZVAL_OBJ(&obj->gc[(*n)++], &obj->parent->zo);
+       }
+
+       ZEND_HASH_FOREACH_VAL(props, val)
+       {
+               ZVAL_COPY_VALUE(&obj->gc[(*n)++], val);
+       }
+       ZEND_HASH_FOREACH_END();
+
+       return NULL;
+}
+
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage___construct, 0, 0, 0)
        ZEND_ARG_INFO(0, message)
        ZEND_ARG_INFO(0, greedy)
@@ -1246,16 +1269,27 @@ static PHP_METHOD(HttpMessage, addHeader)
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "sz", &name_str, &name_len, &zvalue)) {
                php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
                char *name = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
-               zval *header;
+               zend_string *hstr, *vstr = php_http_header_value_to_string(zvalue);
+               zval tmp, *header;
 
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
 
-               Z_TRY_ADDREF_P(zvalue);
-               if ((header = php_http_message_header(obj->message, name, name_len))) {
+               if ((name_len != lenof("Set-Cookie") && strcmp(name, "Set-Cookie"))
+               &&      (hstr = php_http_message_header_string(obj->message, name, name_len))) {
+                       char *hdr_str;
+                       size_t hdr_len = spprintf(&hdr_str, 0, "%s, %s", hstr->val, vstr->val);
+
+                       ZVAL_STR(&tmp, php_http_cs2zs(hdr_str, hdr_len));
+                       zend_symtable_str_update(&obj->message->hdrs, name, name_len, &tmp);
+                       zend_string_release(hstr);
+                       zend_string_release(vstr);
+               } else if ((header = php_http_message_header(obj->message, name, name_len))) {
                        convert_to_array(header);
-                       zend_hash_next_index_insert(Z_ARRVAL_P(header), zvalue);
+                       ZVAL_STR(&tmp, vstr);
+                       zend_hash_next_index_insert(Z_ARRVAL_P(header), &tmp);
                } else {
-                       zend_symtable_str_update(&obj->message->hdrs, name, name_len, zvalue);
+                       ZVAL_STR(&tmp, vstr);
+                       zend_symtable_str_update(&obj->message->hdrs, name, name_len, &tmp);
                }
                efree(name);
        }
@@ -2009,6 +2043,7 @@ PHP_MINIT_FUNCTION(http_message)
        php_http_message_object_handlers.write_property = php_http_message_object_write_prop;
        php_http_message_object_handlers.get_debug_info = php_http_message_object_get_debug_info;
        php_http_message_object_handlers.get_property_ptr_ptr = NULL;
+       php_http_message_object_handlers.get_gc = php_http_message_object_get_gc;
 
        zend_class_implements(php_http_message_class_entry, 3, spl_ce_Countable, zend_ce_serializable, zend_ce_iterator);