- export http_parse_cookie()
[m6w6/ext-http] / http_request_api.c
index 9ea19024c150bcf97f9d9569bba83e6541c53b5f..3408812f179373b9fcb929e28bb9080cbdf2851d 100644 (file)
@@ -202,14 +202,14 @@ static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *);
 /* }}} */
 
 /* {{{ http_request *http_request_init(http_request *) */
-PHP_HTTP_API http_request *_http_request_init_ex(http_request *request, CURL *ch, http_request_method meth, const char *url TSRMLS_DC)
+PHP_HTTP_API http_request *_http_request_init_ex(http_request *request, CURL *ch, http_request_method meth, const char *url ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
 {
        http_request *r;
        
        if (request) {
                r = request;
        } else {
-               r = emalloc(sizeof(http_request));
+               r = emalloc_rel(sizeof(http_request));
        }
        memset(r, 0, sizeof(http_request));
        
@@ -219,13 +219,10 @@ PHP_HTTP_API http_request *_http_request_init_ex(http_request *request, CURL *ch
        
        phpstr_init(&r->conv.request);
        phpstr_init_ex(&r->conv.response, HTTP_CURLBUF_SIZE, 0);
-       
-       zend_hash_init(&r->info, 0, NULL, ZVAL_PTR_DTOR, 0);
-       
        phpstr_init(&r->_cache.cookies);
        zend_hash_init(&r->_cache.options, 0, NULL, ZVAL_PTR_DTOR, 0);
        
-       TSRMLS_SET_CTX(request->tsrm_ls);
+       TSRMLS_SET_CTX(r->tsrm_ls);
        
        return r;
 }
@@ -248,14 +245,16 @@ PHP_HTTP_API void _http_request_dtor(http_request *request)
        
        http_request_reset(request);
        
-       zend_hash_destroy(&request->info);
-       
        phpstr_dtor(&request->_cache.cookies);
        zend_hash_destroy(&request->_cache.options);
        if (request->_cache.headers) {
                curl_slist_free_all(request->_cache.headers);
                request->_cache.headers = NULL;
        }
+       if (request->_progress_callback) {
+               zval_ptr_dtor(&request->_progress_callback);
+               request->_progress_callback = NULL;
+       }
 }
 /* }}} */
 
@@ -264,6 +263,7 @@ PHP_HTTP_API void _http_request_free(http_request **request)
 {
        if (*request) {
                TSRMLS_FETCH_FROM_CTX((*request)->tsrm_ls);
+               http_request_body_free(&(*request)->body);
                http_request_dtor(*request);
                efree(*request);
                *request = NULL;
@@ -279,7 +279,7 @@ PHP_HTTP_API void _http_request_reset(http_request *request)
        request->conv.last_type = 0;
        phpstr_dtor(&request->conv.request);
        phpstr_dtor(&request->conv.response);
-       http_request_body_free(&request->body);
+       http_request_body_dtor(request->body);
 }
 /* }}} */
 
@@ -293,6 +293,8 @@ PHP_HTTP_API void _http_request_defaults(http_request *request)
 #if defined(ZTS)
                HTTP_CURL_OPT(NOSIGNAL, 1);
 #endif
+               HTTP_CURL_OPT(PRIVATE, request);
+               HTTP_CURL_OPT(ERRORBUFFER, request->_error);
                HTTP_CURL_OPT(HEADER, 0);
                HTTP_CURL_OPT(FILETIME, 1);
                HTTP_CURL_OPT(AUTOREFERER, 1);
@@ -302,6 +304,7 @@ PHP_HTTP_API void _http_request_defaults(http_request *request)
                HTTP_CURL_OPT(READFUNCTION, http_curl_read_callback);
                HTTP_CURL_OPT(IOCTLFUNCTION, http_curl_ioctl_callback);
                HTTP_CURL_OPT(WRITEFUNCTION, http_curl_dummy_callback);
+               HTTP_CURL_OPT(PROGRESSFUNCTION, NULL);
                HTTP_CURL_OPT(URL, NULL);
                HTTP_CURL_OPT(NOPROGRESS, 1);
                HTTP_CURL_OPT(PROXY, NULL);
@@ -348,10 +351,26 @@ PHP_HTTP_API void _http_request_defaults(http_request *request)
                HTTP_CURL_OPT(IOCTLDATA, NULL);
                HTTP_CURL_OPT(READDATA, NULL);
                HTTP_CURL_OPT(INFILESIZE, 0);
+#if 0
+               HTTP_CURL_OPT(IGNORE_CONTENT_ENCODING, 1);
+#endif
        }
 }
 /* }}} */
 
+PHP_HTTP_API void _http_request_set_progress_callback(http_request *request, zval *cb)
+{
+       TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
+       
+       if (request->_progress_callback) {
+               zval_ptr_dtor(&request->_progress_callback);
+       }
+       if (cb) {
+               ZVAL_ADDREF(cb);
+       }
+       request->_progress_callback = cb;
+}
+
 /* {{{ STATUS http_request_prepare(http_request *, HashTable *) */
 PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *options)
 {
@@ -367,13 +386,13 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
        /* set options */
        HTTP_CURL_OPT(DEBUGDATA, request);
        HTTP_CURL_OPT(URL, request->url);
-       HTTP_CURL_OPT(PRIVATE, request->url);
 
-       /* progress callback * /
+       /* 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);
-               HTTP_CURL_OPT(PROGRESSDATA,  http_request_callback_data(zoption));
+               http_request_set_progress_callback(request, zoption);
        }
 
        /* proxy */
@@ -657,7 +676,7 @@ PHP_HTTP_API void _http_request_exec(http_request *request)
        TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
        
        if (CURLE_OK != (result = curl_easy_perform(request->ch))) {
-               http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s (%s)", curl_easy_strerror(result), request->url);
+               http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(result), request->_error, request->url);
        }
 }
 /* }}} */
@@ -666,7 +685,7 @@ PHP_HTTP_API void _http_request_exec(http_request *request)
 PHP_HTTP_API void _http_request_info(http_request *request, HashTable *info)
 {
        zval array;
-       INIT_ZARR(array, &request->info);
+       INIT_ZARR(array, info);
 
        HTTP_CURL_INFO(EFFECTIVE_URL);
        HTTP_CURL_INFO(RESPONSE_CODE);
@@ -698,10 +717,6 @@ PHP_HTTP_API void _http_request_info(http_request *request, HashTable *info)
 #if LIBCURL_VERSION_NUM >= 0x070e01
        HTTP_CURL_INFO_EX(COOKIELIST, cookies);
 #endif
-       
-       if (info) {
-               zend_hash_copy(info, &request->info, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
-       }
 }
 /* }}} */
 
@@ -718,11 +733,13 @@ 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 *data, double dltotal, double dlnow, double ultotal, double ulnow)
+/* {{{ 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;
-       HTTP_REQUEST_CALLBACK_DATA(data, zval *, func);
+       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];
@@ -738,7 +755,11 @@ static int http_curl_progress_callback(void *data, double dltotal, double dlnow,
        ZVAL_DOUBLE(params_pass[2], ultotal);
        ZVAL_DOUBLE(params_pass[3], ulnow);
 
-       return call_user_function(EG(function_table), NULL, func, &retval, 4, params_pass TSRMLS_CC);
+       INIT_PZVAL(&retval);
+       ZVAL_NULL(&retval);
+       rc = call_user_function(EG(function_table), NULL, request->_progress_callback, &retval, 4, params_pass TSRMLS_CC);
+       zval_dtor(&retval);
+       return rc;
 }
 /* }}} */
 
@@ -814,9 +835,17 @@ static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size
 static inline zval *_http_request_option_ex(http_request *r, HashTable *options, char *key, size_t keylen, int type TSRMLS_DC)
 {
        zval **zoption;
+#ifdef ZEND_ENGINE_2
        ulong h = zend_get_hash_value(key, keylen);
-
-       if (!options || (SUCCESS != zend_hash_quick_find(options, key, keylen, h, (void **) &zoption))) {
+#endif
+       
+       if (!options || 
+#ifdef ZEND_ENGINE_2
+                       (SUCCESS != zend_hash_quick_find(options, key, keylen, h, (void **) &zoption))
+#else
+                       (SUCCESS != zend_hash_find(options, key, keylen, (void **) &zoption))
+#endif
+       ) {
                return NULL;
        }
 
@@ -835,9 +864,14 @@ static inline zval *_http_request_option_ex(http_request *r, HashTable *options,
        }
        
        ZVAL_ADDREF(*zoption);
+#ifdef ZEND_ENGINE_2
        _zend_hash_quick_add_or_update(&r->_cache.options, key, keylen, h, zoption, sizeof(zval *), NULL, 
                zend_hash_quick_exists(&r->_cache.options, key, keylen, h)?HASH_UPDATE:HASH_ADD ZEND_FILE_LINE_CC);
-
+#else
+       zend_hash_add_or_update(&r->_cache.options, key, keylen, zoption, sizeof(zval *), NULL,
+               zend_hash_exists(&r->_cache.options, key, keylen)?HASH_UPDATE:HASH_ADD);
+#endif
+       
        return *zoption;
 }
 /* }}} */