- add HttpRequest::flushCookies() (libcurl>=7.17.1)
[m6w6/ext-http] / http_message_object.c
index c3f05dee89b368cc7ed43aa4a6b7723c4b4dec4a..b6d8de21a0d8cbdb293229e8a965cc1b20d7430f 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"
@@ -150,7 +151,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)
@@ -228,20 +229,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);
@@ -412,7 +413,7 @@ 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);
        
@@ -520,7 +521,7 @@ 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);
        
@@ -1235,21 +1236,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 +1270,20 @@ 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)) {
+                                       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);
+                                       }
+                               }
 #else
                                http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot transform HttpMessage to HttpRequest (missing curl support)");
 #endif
@@ -1479,16 +1494,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;
+                       }
                }
        }
 }