better fix for robs issue
[m6w6/ext-http] / http_message_object.c
index c5a219f49a303144e1c8f9b333d8a07271f6615b..70bf7f0c06385ae58a04ea11f60c72e7d46b6a6f 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "zend_interfaces.h"
 #include "ext/standard/url.h"
+#include "php_variables.h"
 
 #include "php_http_api.h"
 #include "php_http_send_api.h"
@@ -118,6 +119,7 @@ HTTP_END_ARGS;
 
 HTTP_EMPTY_ARGS(getParentMessage);
 HTTP_EMPTY_ARGS(send);
+HTTP_EMPTY_ARGS(__toString);
 HTTP_BEGIN_ARGS(toString, 0)
        HTTP_ARG_VAL(include_parent, 0)
 HTTP_END_ARGS;
@@ -194,7 +196,7 @@ zend_function_entry http_message_object_fe[] = {
        HTTP_MESSAGE_ME(key, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(next, ZEND_ACC_PUBLIC)
 
-       ZEND_MALIAS(HttpMessage, __toString, toString, HTTP_ARGS(HttpMessage, toString), ZEND_ACC_PUBLIC)
+       ZEND_MALIAS(HttpMessage, __toString, toString, HTTP_ARGS(HttpMessage, __toString), ZEND_ACC_PUBLIC)
 
        HTTP_MESSAGE_ME(factory, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
        ZEND_MALIAS(HttpMessage, fromString, factory, HTTP_ARGS(HttpMessage, factory), ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
@@ -391,6 +393,10 @@ void _http_message_object_free(zend_object *object TSRMLS_DC)
 {
        http_message_object *o = (http_message_object *) object;
 
+       if (o->iterator) {
+               zval_ptr_dtor(&o->iterator);
+               o->iterator = NULL;
+       }
        if (o->message) {
                http_message_dtor(o->message);
                efree(o->message);
@@ -416,7 +422,7 @@ static zval *_http_message_object_read_prop(zval *object, zval *member, int type
 #else
        zend_property_info *pinfo = zend_get_property_info(obj->zo.ce, member, 1 TSRMLS_CC);
        
-       if (!pinfo || ACC_PROP_PUBLIC(pinfo->flags)) {
+       if (!pinfo) {
                return zend_get_std_object_handlers()->read_property(object, member, type TSRMLS_CC);
        }
 #endif
@@ -427,8 +433,13 @@ static zval *_http_message_object_read_prop(zval *object, zval *member, int type
        }
        
        ALLOC_ZVAL(return_value);
+#ifdef Z_SET_REFCOUNT
+       Z_SET_REFCOUNT_P(return_value, 0);
+       Z_UNSET_ISREF_P(return_value);
+#else
        return_value->refcount = 0;
        return_value->is_ref = 0;
+#endif
 
 #ifdef WONKY
        switch (h)
@@ -504,11 +515,8 @@ static zval *_http_message_object_read_prop(zval *object, zval *member, int type
                        break;
                
                default:
-#ifdef WONKY
+                       FREE_ZVAL(return_value);
                        return zend_get_std_object_handlers()->read_property(object, member, type TSRMLS_CC);
-#else
-                       RETVAL_NULL();
-#endif
        }
 
        return return_value;
@@ -524,14 +532,12 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va
 #else
        zend_property_info *pinfo = zend_get_property_info(obj->zo.ce, member, 1 TSRMLS_CC);
        
-       if (!pinfo || ACC_PROP_PUBLIC(pinfo->flags)) {
+       if (!pinfo) {
                zend_get_std_object_handlers()->write_property(object, member, value TSRMLS_CC);
                return;
        }
 #endif
        
-       cpy = zval_copy(Z_TYPE_P(value), value);
-       
 #ifdef WONKY
        switch (h)
 #else
@@ -540,26 +546,26 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va
        {
                case HTTP_MSG_PROPHASH_TYPE:
                case HTTP_MSG_CHILD_PROPHASH_TYPE:
-                       convert_to_long(cpy);
+                       cpy = http_zsep(IS_LONG, value);
                        http_message_set_type(msg, Z_LVAL_P(cpy));
                        break;
 
                case HTTP_MSG_PROPHASH_HTTP_VERSION:
                case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION:
-                       convert_to_double(cpy);
+                       cpy = http_zsep(IS_DOUBLE, value);
                        msg->http.version = Z_DVAL_P(cpy);
                        break;
 
                case HTTP_MSG_PROPHASH_BODY:
                case HTTP_MSG_CHILD_PROPHASH_BODY:
-                       convert_to_string(cpy);
+                       cpy = http_zsep(IS_STRING, value);
                        phpstr_dtor(PHPSTR(msg));
                        phpstr_from_string_ex(PHPSTR(msg), Z_STRVAL_P(cpy), Z_STRLEN_P(cpy));
                        break;
 
                case HTTP_MSG_PROPHASH_HEADERS:
                case HTTP_MSG_CHILD_PROPHASH_HEADERS:
-                       convert_to_array(cpy);
+                       cpy = http_zsep(IS_ARRAY, value);
                        zend_hash_clean(&msg->hdrs);
                        zend_hash_copy(&msg->hdrs, Z_ARRVAL_P(cpy), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
                        break;
@@ -580,7 +586,7 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va
                case HTTP_MSG_PROPHASH_REQUEST_METHOD:
                case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD:
                        if (HTTP_MSG_TYPE(REQUEST, msg)) {
-                               convert_to_string(cpy);
+                               cpy = http_zsep(IS_STRING, value);
                                STR_SET(msg->http.info.request.method, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
                        }
                        break;
@@ -588,7 +594,7 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va
                case HTTP_MSG_PROPHASH_REQUEST_URL:
                case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL:
                        if (HTTP_MSG_TYPE(REQUEST, msg)) {
-                               convert_to_string(cpy);
+                               cpy = http_zsep(IS_STRING, value);
                                STR_SET(msg->http.info.request.url, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
                        }
                        break;
@@ -596,7 +602,7 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va
                case HTTP_MSG_PROPHASH_RESPONSE_CODE:
                case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE:
                        if (HTTP_MSG_TYPE(RESPONSE, msg)) {
-                               convert_to_long(cpy);
+                               cpy = http_zsep(IS_LONG, value);
                                msg->http.info.response.code = Z_LVAL_P(cpy);
                        }
                        break;
@@ -604,18 +610,18 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va
                case HTTP_MSG_PROPHASH_RESPONSE_STATUS:
                case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS:
                        if (HTTP_MSG_TYPE(RESPONSE, msg)) {
-                               convert_to_string(cpy);
+                               cpy = http_zsep(IS_STRING, value);
                                STR_SET(msg->http.info.response.status, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
                        }
                        break;
                
                default:
-#ifdef WONKY
                        zend_get_std_object_handlers()->write_property(object, member, value TSRMLS_CC);
-#endif
                        break;
        }
-       zval_free(&cpy);
+       if (cpy) {
+               zval_ptr_dtor(&cpy);
+       }
 }
 
 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
@@ -796,7 +802,7 @@ PHP_METHOD(HttpMessage, setBody)
        
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &body, &len)) {
                phpstr_dtor(PHPSTR(obj->message));
-               phpstr_from_string_ex(PHPSTR(obj->message), body, len);         
+               phpstr_from_string_ex(PHPSTR(obj->message), body, len);
        }
 }
 /* }}} */
@@ -1128,9 +1134,9 @@ PHP_METHOD(HttpMessage, setHttpVersion)
        }
 
        convert_to_double(zv);
-       sprintf(v, "%1.1lf", Z_DVAL_P(zv));
+       snprintf(v, sizeof(v), "%1.1f", Z_DVAL_P(zv));
        if (strcmp(v, "1.0") && strcmp(v, "1.1")) {
-               http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid HTTP protocol version (1.0 or 1.1): %s", v);
+               http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid HTTP protocol version (1.0 or 1.1): %g", Z_DVAL_P(zv));
                RETURN_FALSE;
        }
 
@@ -1235,21 +1241,24 @@ PHP_METHOD(HttpMessage, toMessageTypeObject)
 #ifdef HTTP_HAVE_CURL
                                int method;
                                char *url;
-                               zval body, *array, *headers, *host = http_message_header(obj->message, "Host");
-                               php_url hurl, *purl = php_url_parse(obj->message->http.info.request.url);
+                               zval post, body, *array, *headers, *host = http_message_header(obj->message, "Host");
+                               php_url hurl, *purl = php_url_parse(STR_PTR(obj->message->http.info.request.url));
                                
                                MAKE_STD_ZVAL(array);
                                array_init(array);
                                
                                memset(&hurl, 0, sizeof(php_url));
-                               hurl.host = host ? Z_STRVAL_P(host) : NULL;
-                               zval_ptr_dtor(&host);
+                               if (host) {
+                                       hurl.host = Z_STRVAL_P(host);
+                                       zval_ptr_dtor(&host);
+                               }
                                http_build_url(HTTP_URL_REPLACE, purl, &hurl, NULL, &url, NULL);
                                php_url_free(purl);
                                add_assoc_string(array, "url", url, 0);
                                
-                               if (    (method = http_request_method_exists(1, 0, obj->message->http.info.request.method)) ||
-                                               (method = http_request_method_register(obj->message->http.info.request.method, strlen(obj->message->http.info.request.method)))) {
+                               if (    obj->message->http.info.request.method &&
+                                                       ((method = http_request_method_exists(1, 0, obj->message->http.info.request.method)) ||
+                                                       (method = http_request_method_register(obj->message->http.info.request.method, strlen(obj->message->http.info.request.method))))) {
                                        add_assoc_long(array, "method", method);
                                }
                                
@@ -1266,9 +1275,22 @@ PHP_METHOD(HttpMessage, toMessageTypeObject)
                                zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setoptions", NULL, array);
                                zval_ptr_dtor(&array);
                                
-                               INIT_PZVAL(&body);
-                               ZVAL_STRINGL(&body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message), 0);
-                               zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setrawpostdata", NULL, &body);
+                               if (PHPSTR_VAL(obj->message) && PHPSTR_LEN(obj->message)) {
+                                       phpstr_fix(PHPSTR(obj->message));
+                                       INIT_PZVAL(&body);
+                                       ZVAL_STRINGL(&body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message), 0);
+                                       if (method != HTTP_POST) {
+                                               zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setbody", NULL, &body);
+                                       } else {
+                                               INIT_PZVAL(&post);
+                                               array_init(&post);
+                                               
+                                               zval_copy_ctor(&body);
+                                               sapi_module.treat_data(PARSE_STRING, Z_STRVAL(body), &post TSRMLS_CC);
+                                               zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setpostfields", NULL, &post);
+                                               zval_dtor(&post);
+                                       }
+                               }
 #else
                                http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot transform HttpMessage to HttpRequest (missing curl support)");
 #endif
@@ -1378,8 +1400,8 @@ PHP_METHOD(HttpMessage, unserialize)
                if ((msg = http_message_parse_ex(obj->message, serialized, (size_t) length))) {
                        obj->message = msg;
                } else {
-                       http_error(HE_ERROR, HTTP_E_RUNTIME, "Could not unserialize HttpMessage");
                        http_message_init(obj->message);
+                       http_error(HE_ERROR, HTTP_E_RUNTIME, "Could not unserialize HttpMessage");
                }
        }
 }
@@ -1479,16 +1501,18 @@ PHP_METHOD(HttpMessage, next)
 {
        NO_ARGS {
                getObject(http_message_object, obj);
-               getObjectEx(http_message_object, itr, obj->iterator);
-               
-               if (itr && itr->parent.handle) {
-                       zval *old = obj->iterator;
-                       MAKE_STD_ZVAL(obj->iterator);
-                       ZVAL_OBJVAL(obj->iterator, itr->parent, 1);
-                       zval_ptr_dtor(&old);
-               } else {
-                       zval_ptr_dtor(&obj->iterator);
-                       obj->iterator = NULL;
+               if (obj->iterator) {
+                       getObjectEx(http_message_object, itr, obj->iterator);
+                       
+                       if (itr && itr->parent.handle) {
+                               zval *old = obj->iterator;
+                               MAKE_STD_ZVAL(obj->iterator);
+                               ZVAL_OBJVAL(obj->iterator, itr->parent, 1);
+                               zval_ptr_dtor(&old);
+                       } else {
+                               zval_ptr_dtor(&obj->iterator);
+                               obj->iterator = NULL;
+                       }
                }
        }
 }