better fix for robs issue
[m6w6/ext-http] / http_message_object.c
index 235ffc4690cd7b1c89327052937e361410c7ec91..70bf7f0c06385ae58a04ea11f60c72e7d46b6a6f 100644 (file)
@@ -6,7 +6,7 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2006, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2007, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
@@ -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"
@@ -32,6 +33,7 @@
 #include "php_http_request_method_api.h"
 #include "php_http_request_api.h"
 #include "php_http_request_object.h"
+#include "php_http_headers_api.h"
 
 #if defined(HTTP_HAVE_SPL) && !defined(WONKY)
 /* SPL doesn't install its headers */
@@ -51,6 +53,11 @@ HTTP_BEGIN_ARGS(factory, 0)
        HTTP_ARG_VAL(class_name, 0)
 HTTP_END_ARGS;
 
+HTTP_BEGIN_ARGS(fromEnv, 1)
+       HTTP_ARG_VAL(type, 0)
+       HTTP_ARG_VAL(class_name, 0)
+HTTP_END_ARGS;
+
 HTTP_EMPTY_ARGS(getBody);
 HTTP_BEGIN_ARGS(setBody, 1)
        HTTP_ARG_VAL(body, 0)
@@ -75,6 +82,11 @@ HTTP_BEGIN_ARGS(setType, 1)
        HTTP_ARG_VAL(type, 0)
 HTTP_END_ARGS;
 
+HTTP_EMPTY_ARGS(getInfo);
+HTTP_BEGIN_ARGS(setInfo, 1)
+       HTTP_ARG_VAL(http_info, 0)
+HTTP_END_ARGS;
+
 HTTP_EMPTY_ARGS(getResponseCode);
 HTTP_BEGIN_ARGS(setResponseCode, 1)
        HTTP_ARG_VAL(response_code, 0)
@@ -107,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;
@@ -139,7 +152,7 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va
 #define http_message_object_get_props _http_message_object_get_props
 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC);
 
-#define OBJ_PROP_CE http_message_object_ce
+#define THIS_CE http_message_object_ce
 zend_class_entry *http_message_object_ce;
 zend_function_entry http_message_object_fe[] = {
        HTTP_MESSAGE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
@@ -151,6 +164,8 @@ zend_function_entry http_message_object_fe[] = {
        HTTP_MESSAGE_ME(addHeaders, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(getType, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(setType, ZEND_ACC_PUBLIC)
+       HTTP_MESSAGE_ME(getInfo, ZEND_ACC_PUBLIC)
+       HTTP_MESSAGE_ME(setInfo, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(getResponseCode, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(setResponseCode, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(getResponseStatus, ZEND_ACC_PUBLIC)
@@ -181,10 +196,11 @@ 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)
+       HTTP_MESSAGE_ME(fromEnv, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
        
        HTTP_MESSAGE_ME(detach, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(prepend, ZEND_ACC_PUBLIC)
@@ -214,20 +230,20 @@ PHP_MINIT_FUNCTION(http_message_object)
        http_message_object_handlers.get_properties = http_message_object_get_props;
        http_message_object_handlers.get_property_ptr_ptr = NULL;
        
-       DCL_PROP(PROTECTED, long, type, HTTP_MSG_NONE);
-       DCL_PROP(PROTECTED, string, body, "");
-       DCL_PROP(PROTECTED, string, requestMethod, "");
-       DCL_PROP(PROTECTED, string, requestUrl, "");
-       DCL_PROP(PROTECTED, string, responseStatus, "");
-       DCL_PROP(PROTECTED, long, responseCode, 0);
-       DCL_PROP_N(PROTECTED, httpVersion);
-       DCL_PROP_N(PROTECTED, headers);
-       DCL_PROP_N(PROTECTED, parentMessage);
+       zend_declare_property_long(THIS_CE, ZEND_STRS("type")-1, HTTP_MSG_NONE, ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_string(THIS_CE, ZEND_STRS("body")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_string(THIS_CE, ZEND_STRS("requestMethod")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_string(THIS_CE, ZEND_STRS("requestUrl")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_string(THIS_CE, ZEND_STRS("responseStatus")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_long(THIS_CE, ZEND_STRS("responseCode")-1, 0, ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_null(THIS_CE, ZEND_STRS("httpVersion")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_null(THIS_CE, ZEND_STRS("headers")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_null(THIS_CE, ZEND_STRS("parentMessage")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
        
 #ifndef WONKY
-       DCL_CONST(long, "TYPE_NONE", HTTP_MSG_NONE);
-       DCL_CONST(long, "TYPE_REQUEST", HTTP_MSG_REQUEST);
-       DCL_CONST(long, "TYPE_RESPONSE", HTTP_MSG_RESPONSE);
+       zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_NONE")-1, HTTP_MSG_NONE TSRMLS_CC);
+       zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_REQUEST")-1, HTTP_MSG_REQUEST TSRMLS_CC);
+       zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_RESPONSE")-1, HTTP_MSG_RESPONSE TSRMLS_CC);
 #endif
        
        HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE);
@@ -377,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);
@@ -398,11 +418,11 @@ static zval *_http_message_object_read_prop(zval *object, zval *member, int type
        http_message *msg = obj->message;
        zval *return_value;
 #ifdef WONKY
-       ulong h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member)+1);
+       ulong h = zend_hash_func(Z_STRVAL_P(member), Z_STRLEN_P(member)+1);
 #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
@@ -413,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)
@@ -490,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;
@@ -506,18 +528,16 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va
        http_message *msg = obj->message;
        zval *cpy = NULL;
 #ifdef WONKY
-       ulong h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
+       ulong h = zend_hash_func(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
 #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
@@ -526,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;
@@ -566,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;
@@ -574,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;
@@ -582,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;
@@ -590,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)
@@ -735,6 +755,30 @@ PHP_METHOD(HttpMessage, factory)
 }
 /* }}} */
 
+/* {{{ proto static HttpMessage HttpMessage::fromEnv(int type[, string class_name = "HttpMessage"])
+       Create a new HttpMessage object from environment representing either current request or response */
+PHP_METHOD(HttpMessage, fromEnv)
+{
+       char *cn = NULL;
+       int cl = 0;
+       long type;
+       http_message_object *obj = NULL;
+       zend_object_value ov;
+       
+       RETVAL_NULL();
+       SET_EH_THROW_HTTP();
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|s", &type, &cn, &cl)) {
+               if (SUCCESS == http_object_new(&ov, cn, cl, _http_message_object_new_ex, http_message_object_ce, http_message_init_env(NULL, type), &obj)) {
+                       RETVAL_OBJVAL(ov, 0);
+               }
+               if (obj && !obj->message) {
+                       obj->message = http_message_new();
+               }
+       }
+       SET_EH_NORMAL();
+}
+/* }}} */
+
 /* {{{ proto string HttpMessage::getBody()
        Get the body of the parsed HttpMessage. */
 PHP_METHOD(HttpMessage, getBody)
@@ -758,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);
        }
 }
 /* }}} */
@@ -860,6 +904,50 @@ PHP_METHOD(HttpMessage, setType)
 }
 /* }}} */
 
+/* {{{ proto string HttpMessage::getInfo(void)
+       Get the HTTP request/response line */
+PHP_METHOD(HttpMessage, getInfo)
+{
+       NO_ARGS;
+       
+       if (return_value_used) {
+               getObject(http_message_object, obj);
+               
+               switch (obj->message->type) {
+                       case HTTP_MSG_REQUEST:
+                               Z_STRLEN_P(return_value) = spprintf(&Z_STRVAL_P(return_value), 0, HTTP_INFO_REQUEST_FMT_ARGS(&obj->message->http, ""));
+                               break;
+                       case HTTP_MSG_RESPONSE:
+                               Z_STRLEN_P(return_value) = spprintf(&Z_STRVAL_P(return_value), 0, HTTP_INFO_RESPONSE_FMT_ARGS(&obj->message->http, ""));
+                               break;
+                       default:
+                               RETURN_NULL();
+                               break;
+               }
+               Z_TYPE_P(return_value) = IS_STRING;
+       }
+}
+/* }}} */
+
+/* {{{ proto bool HttpMessage::setInfo(string http_info)
+       Set type and request or response info with a standard HTTP request or response line */
+PHP_METHOD(HttpMessage, setInfo)
+{
+       char *str;
+       int len;
+       http_info inf;
+       
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len) && SUCCESS == http_info_parse_ex(str, &inf, 0)) {
+               getObject(http_message_object, obj);
+               
+               http_message_set_info(obj->message, &inf);
+               http_info_dtor(&inf);
+               RETURN_TRUE;
+       }
+       RETURN_FALSE;
+}
+/* }}} */
+
 /* {{{ proto int HttpMessage::getResponseCode()
        Get the Response Code of the Message. */
 PHP_METHOD(HttpMessage, getResponseCode)
@@ -905,7 +993,11 @@ PHP_METHOD(HttpMessage, getResponseStatus)
        if (return_value_used) {
                getObject(http_message_object, obj);
                HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
-               RETURN_STRING(obj->message->http.info.response.status, 1);
+               if (obj->message->http.info.response.status) {
+                       RETURN_STRING(obj->message->http.info.response.status, 1);
+               } else {
+                       RETURN_EMPTY_STRING();
+               }
        }
 }
 /* }}} */
@@ -923,7 +1015,7 @@ PHP_METHOD(HttpMessage, setResponseStatus)
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &status, &status_len)) {
                RETURN_FALSE;
        }
-       STR_SET(obj->message->http.info.response.status, estrdup(status));
+       STR_SET(obj->message->http.info.response.status, estrndup(status, status_len));
        RETURN_TRUE;
 }
 /* }}} */
@@ -937,7 +1029,11 @@ PHP_METHOD(HttpMessage, getRequestMethod)
        if (return_value_used) {
                getObject(http_message_object, obj);
                HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
-               RETURN_STRING(obj->message->http.info.request.method, 1);
+               if (obj->message->http.info.request.method) {
+                       RETURN_STRING(obj->message->http.info.request.method, 1);
+               } else {
+                       RETURN_EMPTY_STRING();
+               }
        }
 }
 /* }}} */
@@ -978,7 +1074,11 @@ PHP_METHOD(HttpMessage, getRequestUrl)
        if (return_value_used) {
                getObject(http_message_object, obj);
                HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
-               RETURN_STRING(obj->message->http.info.request.url, 1);
+               if (obj->message->http.info.request.url) {
+                       RETURN_STRING(obj->message->http.info.request.url, 1);
+               } else {
+                       RETURN_EMPTY_STRING();
+               }
        }
 }
 /* }}} */
@@ -1034,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;
        }
 
@@ -1141,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);
                                }
                                
@@ -1172,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
@@ -1284,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");
                }
        }
 }
@@ -1385,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;
+                       }
                }
        }
 }