merged response-streams branch
[m6w6/ext-http] / php_http_message.c
index 912de7014b8fd87b3cbaa32894b6de483fcc927c..d063b21a40bf5f5ce6465e8b52e0480de83e41b7 100644 (file)
@@ -12,7 +12,6 @@
 
 #include "php_http_api.h"
 
-static zval *message_header_strval(zval **header TSRMLS_DC);
 static void message_headers(php_http_message_t *msg, php_http_buffer_t *str);
 
 PHP_HTTP_API zend_bool php_http_message_info_callback(php_http_message_t **message, HashTable **headers, php_http_info_t *info TSRMLS_DC)
@@ -20,7 +19,7 @@ PHP_HTTP_API zend_bool php_http_message_info_callback(php_http_message_t **messa
        php_http_message_t *old = *message;
 
        /* advance message */
-       if (!old || old->type || zend_hash_num_elements(&old->hdrs) || PHP_HTTP_BUFFER_LEN(old)) {
+       if (!old || old->type || zend_hash_num_elements(&old->hdrs)) {
                (*message) = php_http_message_init(NULL, 0 TSRMLS_CC);
                (*message)->parent = old;
                if (headers) {
@@ -148,7 +147,7 @@ PHP_HTTP_API php_http_message_t *php_http_message_parse(php_http_message_t *msg,
        return msg;
 }
 
-PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_str, size_t key_len, int join)
+PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, const char *key_str, size_t key_len, int join)
 {
        zval *ret = NULL, **header;
        char *key = php_http_pretty_key(estrndup(key_str, key_len), key_len, 1, 1);
@@ -157,7 +156,7 @@ PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_st
                if (join && Z_TYPE_PP(header) == IS_ARRAY) {
                        TSRMLS_FETCH_FROM_CTX(msg->ts);
 
-                       ret = message_header_strval(header TSRMLS_CC);
+                       ret = php_http_header_value_to_string(*header TSRMLS_CC);
                } else {
                        Z_ADDREF_PP(header);
                        ret = *header;
@@ -277,9 +276,10 @@ PHP_HTTP_API void php_http_message_update_headers(php_http_message_t *msg)
 {
        zval *h;
        size_t size;
-       TSRMLS_FETCH_FROM_CTX(msg->ts);
 
-       if ((size = php_http_message_body_size(&msg->body))) {
+       if (php_http_message_body_stream(&msg->body)->readfilters.head) {
+               /* if a read stream filter is attached to the body the caller must also care for the headers */
+       } else if ((size = php_http_message_body_size(&msg->body))) {
                MAKE_STD_ZVAL(h);
                ZVAL_LONG(h, size);
                zend_hash_update(&msg->hdrs, "Content-Length", sizeof("Content-Length"), &h, sizeof(zval *), NULL);
@@ -304,40 +304,8 @@ PHP_HTTP_API void php_http_message_update_headers(php_http_message_t *msg)
        }
 }
 
-static zval *message_header_strval(zval **header TSRMLS_DC)
-{
-       zval *ret;
-
-       if (Z_TYPE_PP(header) == IS_BOOL) {
-               MAKE_STD_ZVAL(ret);
-               ZVAL_STRING(ret, Z_BVAL_PP(header) ? "true" : "false", 1);
-       } else if (Z_TYPE_PP(header) == IS_ARRAY) {
-               zval **val;
-               HashPosition pos;
-               php_http_buffer_t str;
-
-               php_http_buffer_init(&str);
-               MAKE_STD_ZVAL(ret);
-               FOREACH_VAL(pos, *header, val) {
-                       zval *strval = message_header_strval(val TSRMLS_CC);
-
-                       php_http_buffer_appendf(&str, PHP_HTTP_BUFFER_LEN(&str) ? ", %s":"%s", Z_STRVAL_P(strval));
-                       zval_ptr_dtor(&strval);
-               }
-               php_http_buffer_fix(&str);
-               ZVAL_STRINGL(ret, PHP_HTTP_BUFFER_VAL(&str), PHP_HTTP_BUFFER_LEN(&str), 0);
-       } else  {
-               ret = php_http_zsep(1, IS_STRING, *header);
-       }
-
-       return ret;
-}
-
 static void message_headers(php_http_message_t *msg, php_http_buffer_t *str)
 {
-       php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
-       HashPosition pos1;
-       zval **header;
        TSRMLS_FETCH_FROM_CTX(msg->ts);
 
        switch (msg->type) {
@@ -354,41 +322,7 @@ static void message_headers(php_http_message_t *msg, php_http_buffer_t *str)
        }
 
        php_http_message_update_headers(msg);
-
-       FOREACH_HASH_KEYVAL(pos1, &msg->hdrs, key, header) {
-               if (key.type == HASH_KEY_IS_STRING) {
-                       if (key.len == sizeof("Set-Cookie") && !strcasecmp(key.str, "Set-Cookie") && Z_TYPE_PP(header) == IS_ARRAY) {
-                               HashPosition pos2;
-                               zval **single_header;
-
-                               FOREACH_VAL(pos2, *header, single_header) {
-                                       if (Z_TYPE_PP(single_header) == IS_ARRAY) {
-                                               php_http_cookie_list_t *cookie = php_http_cookie_list_from_struct(NULL, *single_header TSRMLS_CC);
-
-                                               if (cookie) {
-                                                       char *buf;
-                                                       size_t len;
-
-                                                       php_http_cookie_list_to_string(cookie, &buf, &len);
-                                                       php_http_buffer_appendf(str, "Set-Cookie: %s" PHP_HTTP_CRLF, buf);
-                                                       php_http_cookie_list_free(&cookie);
-                                                       efree(buf);
-                                               }
-                                       } else {
-                                               zval *strval = message_header_strval(single_header TSRMLS_CC);
-
-                                               php_http_buffer_appendf(str, "Set-Cookie: %s" PHP_HTTP_CRLF, Z_STRVAL_P(strval));
-                                               zval_ptr_dtor(&strval);
-                                       }
-                               }
-                       } else {
-                               zval *strval = message_header_strval(header TSRMLS_CC);
-
-                               php_http_buffer_appendf(str, "%s: %s" PHP_HTTP_CRLF, key.str, Z_STRVAL_P(strval));
-                               zval_ptr_dtor(&strval);
-                       }
-               }
-       }
+       php_http_headers_to_string(str, &msg->hdrs TSRMLS_CC);
 }
 
 PHP_HTTP_API void php_http_message_to_callback(php_http_message_t *msg, php_http_pass_callback_t cb, void *cb_arg)
@@ -397,7 +331,7 @@ PHP_HTTP_API void php_http_message_to_callback(php_http_message_t *msg, php_http
 
        php_http_buffer_init_ex(&str, 0x1000, 0);
        message_headers(msg, &str);
-       cb(cb_arg, PHP_HTTP_BUFFER_VAL(&str), PHP_HTTP_BUFFER_LEN(&str));
+       cb(cb_arg, str.data, str.used);
        php_http_buffer_dtor(&str);
 
        if (php_http_message_body_size(&msg->body)) {
@@ -873,11 +807,14 @@ static void php_http_message_object_prophandler_get_parent_message(php_http_mess
 }
 static void php_http_message_object_prophandler_set_parent_message(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
        if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), php_http_message_class_entry TSRMLS_CC)) {
+               php_http_message_object_t *parent_obj = zend_object_store_get_object(value TSRMLS_CC);
+
                if (obj->message->parent) {
                        zend_objects_store_del_ref_by_handle(obj->parent.handle TSRMLS_CC);
                }
                Z_OBJ_ADDREF_P(value);
                obj->parent = Z_OBJVAL_P(value);
+               obj->message->parent = parent_obj->message;
        }
 }
 
@@ -897,7 +834,7 @@ PHP_MINIT_FUNCTION(http_message)
        zend_hash_init(&php_http_message_object_prophandlers, 9, NULL, NULL, 1);
        zend_declare_property_long(php_http_message_class_entry, ZEND_STRL("type"), PHP_HTTP_NONE, ZEND_ACC_PROTECTED TSRMLS_CC);
        php_http_message_object_add_prophandler(ZEND_STRL("type"), php_http_message_object_prophandler_get_type, php_http_message_object_prophandler_set_type);
-       zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("body"), "", ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("body"), ZEND_ACC_PROTECTED TSRMLS_CC);
        php_http_message_object_add_prophandler(ZEND_STRL("body"), php_http_message_object_prophandler_get_body, php_http_message_object_prophandler_set_body);
        zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED TSRMLS_CC);
        php_http_message_object_add_prophandler(ZEND_STRL("requestMethod"), php_http_message_object_prophandler_get_request_method, php_http_message_object_prophandler_set_request_method);
@@ -1096,11 +1033,14 @@ zend_object_value php_http_message_object_new_ex(zend_class_entry *ce, php_http_
        }
 
        if (msg) {
+               php_http_message_body_object_t *body_obj;
+
                o->message = msg;
                if (msg->parent) {
                        o->parent = php_http_message_object_new_ex(ce, msg->parent, NULL TSRMLS_CC);
                }
-               o->body = php_http_message_body_object_new_ex(php_http_message_body_get_class_entry(), php_http_message_body_copy(&msg->body, NULL, 0), NULL TSRMLS_CC);
+               o->body = php_http_message_body_object_new_ex(php_http_message_body_get_class_entry(), &msg->body, &body_obj TSRMLS_CC);
+               body_obj->shared = 1;
        }
 
        ov.handle = zend_objects_store_put((zend_object *) o, NULL, php_http_message_object_free, NULL TSRMLS_CC);
@@ -1340,8 +1280,12 @@ PHP_METHOD(HttpMessage, getBody)
                        if (!obj->message) {
                                obj->message = php_http_message_init(NULL, 0 TSRMLS_CC);
                        }
-
-                       if (obj->body.handle || SUCCESS == php_http_new(&obj->body, php_http_message_body_get_class_entry(), (php_http_new_t) php_http_message_body_object_new_ex, NULL, (void *) php_http_message_body_copy(&obj->message->body, NULL, 0), NULL TSRMLS_CC)) {
+                       if (!obj->body.handle) {
+                               php_http_message_body_object_t *body_obj;
+                               obj->body = php_http_message_body_object_new_ex(php_http_message_body_get_class_entry(), &obj->message->body, &body_obj TSRMLS_CC);
+                               body_obj->shared = 1;
+                       }
+                       if (obj->body.handle) {
                                RETVAL_OBJVAL(obj->body, 1);
                        }
                }
@@ -1368,10 +1312,10 @@ PHP_METHOD(HttpMessage, addBody)
        zval *new_body;
 
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &new_body, php_http_message_body_get_class_entry())) {
-               php_http_message_body_object_t *old_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+               php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
                php_http_message_body_object_t *new_obj = zend_object_store_get_object(new_body TSRMLS_CC);
 
-               php_http_message_body_to_callback(old_obj->body, (php_http_pass_callback_t) php_http_message_body_append, new_obj->body, 0, 0);
+               php_http_message_body_to_callback(new_obj->body, (php_http_pass_callback_t) php_http_message_body_append, &obj->message->body, 0, 0);
        }
        RETVAL_ZVAL(getThis(), 1, 0);
 }
@@ -1474,6 +1418,7 @@ PHP_METHOD(HttpMessage, addHeader)
                if ((header = php_http_message_header(obj->message, name, name_len, 0))) {
                        convert_to_array(header);
                        zend_hash_next_index_insert(Z_ARRVAL_P(header), &zvalue, sizeof(void *), NULL);
+                       zval_ptr_dtor(&header);
                } else {
                        zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL);
                }
@@ -1906,7 +1851,7 @@ PHP_METHOD(HttpMessage, detach)
                                obj->message = php_http_message_init(NULL, 0 TSRMLS_CC);
                        }
 
-                       RETVAL_OBJVAL(php_http_message_object_new_ex(obj->zo.ce, php_http_message_copy(obj->message, NULL), NULL TSRMLS_CC), 0);
+                       RETVAL_OBJVAL(php_http_message_object_new_ex(obj->zo.ce, php_http_message_copy_ex(obj->message, NULL, 0), NULL TSRMLS_CC), 0);
                }
        } end_error_handling();
 }