- fix access of private properties
authorMichael Wallner <mike@php.net>
Sat, 31 Dec 2005 10:37:54 +0000 (10:37 +0000)
committerMichael Wallner <mike@php.net>
Sat, 31 Dec 2005 10:37:54 +0000 (10:37 +0000)
- fix progress callback
- change onprogress callback param to be an array instead of 4 single params
- add progress callback if not set and inherited request object defines onProgress()

http_request_api.c
http_request_object.c
php_http_std_defs.h

index f7d308997f653d71275938f494cbb57bbd758d23..8dc72dd6a01a6e7babc29037b6daaa01732fb504 100644 (file)
@@ -366,10 +366,16 @@ PHP_HTTP_API void _http_request_set_progress_callback(http_request *request, zva
        if (request->_progress_callback) {
                zval_ptr_dtor(&request->_progress_callback);
        }
-       if (cb) {
+       if ((request->_progress_callback = cb)) {
                ZVAL_ADDREF(cb);
+               HTTP_CURL_OPT(NOPROGRESS, 0);
+               HTTP_CURL_OPT(PROGRESSDATA, request);
+               HTTP_CURL_OPT(PROGRESSFUNCTION, http_curl_progress_callback);
+       } else {
+               HTTP_CURL_OPT(NOPROGRESS, 1);
+               HTTP_CURL_OPT(PROGRESSDATA, NULL);
+               HTTP_CURL_OPT(PROGRESSFUNCTION, NULL);
        }
-       request->_progress_callback = cb;
 }
 
 /* {{{ STATUS http_request_prepare(http_request *, HashTable *) */
@@ -389,10 +395,7 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
        HTTP_CURL_OPT(URL, request->url);
 
        /* progress callback */
-       if ((zoption = http_request_option(request, options, "onprogress", 0))) {
-               HTTP_CURL_OPT(NOPROGRESS, 0);
-               HTTP_CURL_OPT(PROGRESSDATA, request);
-               HTTP_CURL_OPT(PROGRESSFUNCTION, http_curl_progress_callback);
+       if ((zoption = http_request_option(request, options, "onprogress", -1))) {
                http_request_set_progress_callback(request, zoption);
        }
 
@@ -713,30 +716,26 @@ static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *ct
 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
 static int http_curl_progress_callback(void *ctx, double dltotal, double dlnow, double ultotal, double ulnow)
 {
-       int rc;
-       zval *params_pass[4], params_local[4], retval;
+       zval *param, retval;
        http_request *request = (http_request *) ctx;
        TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
 
-       params_pass[0] = &params_local[0];
-       params_pass[1] = &params_local[1];
-       params_pass[2] = &params_local[2];
-       params_pass[3] = &params_local[3];
-
-       INIT_PZVAL(params_pass[0]);
-       INIT_PZVAL(params_pass[1]);
-       INIT_PZVAL(params_pass[2]);
-       INIT_PZVAL(params_pass[3]);
-       ZVAL_DOUBLE(params_pass[0], dltotal);
-       ZVAL_DOUBLE(params_pass[1], dlnow);
-       ZVAL_DOUBLE(params_pass[2], ultotal);
-       ZVAL_DOUBLE(params_pass[3], ulnow);
-
        INIT_PZVAL(&retval);
        ZVAL_NULL(&retval);
-       rc = call_user_function(EG(function_table), NULL, request->_progress_callback, &retval, 4, params_pass TSRMLS_CC);
+
+       MAKE_STD_ZVAL(param);
+       array_init(param);
+       add_assoc_double(param, "dltotal", dltotal);
+       add_assoc_double(param, "dlnow", dlnow);
+       add_assoc_double(param, "ultotal", ultotal);
+       add_assoc_double(param, "ulnow", ulnow);
+       
+       call_user_function(EG(function_table), NULL, request->_progress_callback, &retval, 1, &param TSRMLS_CC);
+       
+       zval_ptr_dtor(&param);
        zval_dtor(&retval);
-       return rc;
+       
+       return 0;
 }
 /* }}} */
 
index 7567b96d342edeaa482423b378369dca9ad9d9a7..6307539058e7c76879d78104b3b85dfe0f987994 100644 (file)
@@ -215,6 +215,7 @@ HTTP_END_ARGS;
 #define http_request_object_declare_default_properties() _http_request_object_declare_default_properties(TSRMLS_C)
 static inline void _http_request_object_declare_default_properties(TSRMLS_D);
 
+#define OBJ_PROP_CE http_request_object_ce
 zend_class_entry *http_request_object_ce;
 zend_function_entry http_request_object_fe[] = {
        HTTP_REQUEST_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
@@ -518,6 +519,23 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
                }
                
                http_request_prepare(obj->request, Z_ARRVAL_P(options));
+               
+               /* check if there's a onProgress method and add it as progress callback if one isn't already set */
+               if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onprogress", sizeof("onprogress"))) {
+                       zval **entry, *pcb;
+                       
+                       if (    (Z_TYPE_P(options) != IS_ARRAY)
+                               ||      (SUCCESS != zend_hash_find(Z_ARRVAL_P(options), "onprogress", sizeof("onprogress"), (void **) &entry)
+                               ||      (!zval_is_true(*entry)))) {
+                               MAKE_STD_ZVAL(pcb);
+                               array_init(pcb);
+                               ZVAL_ADDREF(getThis());
+                               add_next_index_zval(pcb, getThis());
+                               add_next_index_stringl(pcb, "onprogress", lenof("onprogress"), 1);
+                               http_request_set_progress_callback(obj->request, pcb);
+                               zval_ptr_dtor(&pcb);
+                       }
+               }
        }
 
        return status;
index 61a55639238c32c975b32b03e6f4e219974bf85c..8824e709353776dcf5bd2ada7821dd685a6afe72 100644 (file)
@@ -248,12 +248,12 @@ typedef int STATUS;
 #      define DCL_PROP_Z(a, n, v)                              zend_declare_property(ce, (#n), sizeof(#n)-1, (v), (ZEND_ACC_ ##a) TSRMLS_CC)
 #      define DCL_PROP_N(a, n)                                 zend_declare_property_null(ce, (#n), sizeof(#n)-1, (ZEND_ACC_ ##a) TSRMLS_CC)
 #      define UPD_PROP(o, t, n, v)                             UPD_PROP_EX(o, getThis(), t, n, v)
-#      define UPD_PROP_EX(o, this, t, n, v)    zend_update_property_ ##t(o->zo.ce, this, (#n), sizeof(#n)-1, (v) TSRMLS_CC)
-#      define UPD_STRL(o, n, v, l)                             zend_update_property_stringl(o->zo.ce, getThis(), (#n), sizeof(#n)-1, (v), (l) TSRMLS_CC)
+#      define UPD_PROP_EX(o, this, t, n, v)    zend_update_property_ ##t(OBJ_PROP_CE, this, (#n), sizeof(#n)-1, (v) TSRMLS_CC)
+#      define UPD_STRL(o, n, v, l)                             zend_update_property_stringl(OBJ_PROP_CE, getThis(), (#n), sizeof(#n)-1, (v), (l) TSRMLS_CC)
 #      define SET_PROP(o, n, z)                                SET_PROP_EX(o, getThis(), n, z)
-#      define SET_PROP_EX(o, this, n, z)               zend_update_property(o->zo.ce, this, (#n), sizeof(#n)-1, (z) TSRMLS_CC)
+#      define SET_PROP_EX(o, this, n, z)               zend_update_property(OBJ_PROP_CE, this, (#n), sizeof(#n)-1, (z) TSRMLS_CC)
 #      define GET_PROP(o, n)                                   GET_PROP_EX(o, getThis(), n)
-#      define GET_PROP_EX(o, this, n)                  zend_read_property(o->zo.ce, this, (#n), sizeof(#n)-1, 0 TSRMLS_CC)
+#      define GET_PROP_EX(o, this, n)                  zend_read_property(OBJ_PROP_CE, this, (#n), sizeof(#n)-1, 0 TSRMLS_CC)
 
 #      define DCL_CONST(t, n, v) zend_declare_class_constant_ ##t(ce, (n), sizeof(n)-1, (v) TSRMLS_CC)