Add PHP_HTTP_HAVE_CLIENT, by Jan Erhardt
[m6w6/ext-http] / php_http_message.c
index 8ba4802a6de50025b89072a0fc1c82d578c55aa4..3d3822cfeeb8326e455e5aae29d8d1afa93e8b38 100644 (file)
@@ -20,7 +20,7 @@ zend_bool php_http_message_info_callback(php_http_message_t **message, HashTable
 
        /* advance message */
        if (!old || old->type || zend_hash_num_elements(&old->hdrs)) {
-               (*message) = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
+               (*message) = php_http_message_init(NULL, 0, NULL);
                (*message)->parent = old;
                if (headers) {
                        (*headers) = &((*message)->hdrs);
@@ -275,6 +275,8 @@ void php_http_message_update_headers(php_http_message_t *msg)
 
        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 (php_http_message_header(msg, ZEND_STRL("Content-Range"))) {
+               /* don't mess around with a Content-Range message */
        } else if ((size = php_http_message_body_size(msg->body))) {
                ZVAL_LONG(&h, size);
                zend_hash_str_update(&msg->hdrs, "Content-Length", lenof("Content-Length"), &h);
@@ -299,6 +301,7 @@ void php_http_message_update_headers(php_http_message_t *msg)
                }
        } else if ((cl = php_http_message_header_string(msg, ZEND_STRL("Content-Length")))) {
                if (!zend_string_equals_literal(cl, "0")) {
+                       /* body->size == 0, so get rid of old Content-Length */
                        zend_hash_str_del(&msg->hdrs, ZEND_STRL("Content-Length"));
                }
                zend_string_release(cl);
@@ -500,7 +503,7 @@ static HashTable php_http_message_object_prophandlers;
 
 static void php_http_message_object_prophandler_hash_dtor(zval *pData)
 {
-       efree(Z_PTR_P(pData));
+       pefree(Z_PTR_P(pData), 1);
 }
 
 typedef void (*php_http_message_object_prophandler_func_t)(php_http_message_object_t *o, zval *v);
@@ -604,9 +607,8 @@ static void php_http_message_object_prophandler_set_headers(php_http_message_obj
 
        if (Z_TYPE_P(value) != IS_ARRAY && Z_TYPE_P(value) != IS_OBJECT) {
                convert_to_array_ex(value);
-       } else {
-               headers = HASH_OF(value);
        }
+       headers = HASH_OF(value);
 
        zend_hash_clean(&obj->message->hdrs);
        array_copy(headers, &obj->message->hdrs);
@@ -688,7 +690,7 @@ void php_http_message_object_reverse(zval *zmsg, zval *return_value)
 
                /* add ref, because we previously have not been a parent message */
                Z_ADDREF_P(zmsg);
-               RETVAL_OBJ(&objects[last]->zo);
+               RETVAL_OBJECT(&objects[last]->zo, 1);
 
                efree(objects);
        } else {
@@ -798,7 +800,7 @@ php_http_message_object_t *php_http_message_object_new_ex(zend_class_entry *ce,
 {
        php_http_message_object_t *o;
 
-       o = ecalloc(1, sizeof(php_http_message_object_t) + (ce->default_properties_count - 1) * sizeof(zval));
+       o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce));
        zend_object_std_init(&o->zo, ce);
        object_properties_init(&o->zo, ce);
 
@@ -851,23 +853,26 @@ void php_http_message_object_free(zend_object *object)
        zend_object_std_dtor(object);
 }
 
-static zval *php_http_message_object_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *return_value)
+static zval *php_http_message_object_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *tmp)
 {
-       php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, object);
-       php_http_message_object_prophandler_t *handler;
+       zval *return_value;
        zend_string *member_name = zval_get_string(member);
+       php_http_message_object_prophandler_t *handler = php_http_message_object_get_prophandler(member_name);
 
-       PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
+       if (!handler || type == BP_VAR_R || type == BP_VAR_IS) {
+               return_value = zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, tmp);
 
-       if ((handler = php_http_message_object_get_prophandler(member_name))) {
-               if (type == BP_VAR_R) {
-                       handler->read(obj, return_value);
-               } else {
-                       php_property_proxy_t *proxy = php_property_proxy_init(object, member_name);
-                       RETVAL_OBJ(&php_property_proxy_object_new_ex(php_property_proxy_get_class_entry(), proxy)->zo);
+               if (handler) {
+                       php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, object);
+
+                       PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
+                       handler->read(obj, tmp);
+
+                       zval_ptr_dtor(return_value);
+                       ZVAL_COPY_VALUE(return_value, tmp);
                }
        } else {
-               zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, return_value);
+               return_value = php_property_proxy_zval(object, member_name);
        }
 
        zend_string_release(member_name);
@@ -1017,7 +1022,7 @@ static PHP_METHOD(HttpMessage, __construct)
                msg = php_http_message_parse(NULL, zs_msg->val, zs_msg->len, greedy);
 
                if (!msg && !EG(exception)) {
-                       php_http_throw(bad_message, "Could not parse message: %.*s", MIN(25, zs_msg->len), zs_msg->len);
+                       php_http_throw(bad_message, "Could not parse message: %.*s", MIN(25, zs_msg->len), zs_msg->val);
                }
                zend_string_release(zs_msg);
        }
@@ -1104,15 +1109,18 @@ static PHP_METHOD(HttpMessage, getHeader)
 
                if ((header = php_http_message_header(obj->message, header_str, header_len))) {
                        if (!header_ce) {
-                               RETURN_ZVAL(header, 1, 1);
+                               RETURN_ZVAL_FAST(header);
                        } else if (instanceof_function(header_ce, php_http_header_class_entry)) {
+                               php_http_object_method_t cb;
                                zval argv[2];
 
                                ZVAL_STRINGL(&argv[0], header_str, header_len);
                                ZVAL_COPY(&argv[1], header);
 
                                object_init_ex(return_value, header_ce);
-                               php_http_method_call(return_value, ZEND_STRL("__construct"), 2, argv, NULL);
+                               php_http_object_method_init(&cb, return_value, ZEND_STRL("__construct"));
+                               php_http_object_method_call(&cb, return_value, NULL, 2, argv);
+                               php_http_object_method_dtor(&cb);
 
                                zval_ptr_dtor(&argv[0]);
                                zval_ptr_dtor(&argv[1]);
@@ -1434,7 +1442,7 @@ ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpMessage, setResponseStatus)
 {
        char *status;
-       int status_len;
+       size_t status_len;
        php_http_message_object_t *obj;
 
        php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &status, &status_len), invalid_arg, return);
@@ -1771,6 +1779,7 @@ static PHP_METHOD(HttpMessage, isMultipart)
                }
 
                if (zboundary && boundary) {
+                       ZVAL_DEREF(zboundary);
                        zval_dtor(zboundary);
                        ZVAL_STR(zboundary, php_http_cs2zs(boundary, strlen(boundary)));
                }
@@ -1959,7 +1968,7 @@ PHP_MINIT_FUNCTION(http_message)
        memcpy(&php_http_message_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
        php_http_message_object_handlers.offset = XtOffsetOf(php_http_message_object_t, zo);
        php_http_message_object_handlers.clone_obj = php_http_message_object_clone;
-       php_http_message_object_handlers.dtor_obj = php_http_message_object_free;
+       php_http_message_object_handlers.free_obj = php_http_message_object_free;
        php_http_message_object_handlers.read_property = php_http_message_object_read_prop;
        php_http_message_object_handlers.write_property = php_http_message_object_write_prop;
        php_http_message_object_handlers.get_properties = php_http_message_object_get_props;