- fix mem-leaks with http_curl_callback_ctx
[m6w6/ext-http] / http_request_api.c
index 4cf683475dd56658d5226dc705c9801393c0bd5a..87d40a01e49e1d47f8c6c335c66a7c25af204c39 100644 (file)
@@ -114,11 +114,6 @@ static size_t http_curl_read_callback(void *, size_t, size_t, void *);
 static int http_curl_progress_callback(void *, double, double, double, double);
 static int http_curl_debug_callback(CURL *, curl_infotype, char *, size_t, void *);
 
-typedef struct {
-       void ***tsrm_ctx;
-       void *data;
-} http_curl_callback_ctx;
-
 #define HTTP_CURL_CALLBACK_DATA(from, type, var) \
        http_curl_callback_ctx *__CTX = (http_curl_callback_ctx *) (from); \
        TSRMLS_FETCH_FROM_CTX(__CTX->tsrm_ctx); \
@@ -177,6 +172,12 @@ void *_http_request_data_copy(int type, void *data TSRMLS_DC)
                        return data;
                }
 
+               case COPY_CONTEXT:
+               {
+                       zend_llist_add_element(&HTTP_G(request).copies.contexts, &data);
+                       return data;
+               }
+               
                default:
                {
                        return data;
@@ -199,6 +200,13 @@ void _http_request_data_free_slist(void *list)
 }
 /* }}} */
 
+/* {{{ _http_request_data_free_context(http_curl_callback_ctx **) */
+void _http_request_data_free_context(void *context)
+{
+       efree(*((http_curl_callback_ctx **) context));
+}
+/* }}} */
+
 /* {{{ http_request_body *http_request_body_new() */
 PHP_HTTP_API http_request_body *_http_request_body_new(TSRMLS_D)
 {
@@ -349,7 +357,7 @@ PHP_HTTP_API STATUS _http_request_init(CURL *ch, http_request_method meth, const
        HTTP_CURL_OPT(NOSIGNAL, 1);
 #endif
 #if LIBCURL_VERSION_NUM < 0x070c00
-       HTTP_CURL_OPT(ERRORBUFFER, HTTP_G(request).curl.error);
+       HTTP_CURL_OPT(ERRORBUFFER, HTTP_G(request).error);
 #endif
 
        /* progress callback */
@@ -702,10 +710,23 @@ PHP_HTTP_API void _http_request_info(CURL *ch, HashTable *info TSRMLS_DC)
 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
 PHP_HTTP_API STATUS _http_request_ex(CURL *ch, http_request_method meth, const char *url, http_request_body *body, HashTable *options, HashTable *info, phpstr *response TSRMLS_DC)
 {
-       if (SUCCESS != http_request_init(ch, meth, url, body, options, response)) {
-               return FAILURE;
+       STATUS status;
+       zend_bool clean_curl;
+
+       if ((clean_curl = (!ch))) {
+               if (!(ch = curl_easy_init())) {
+                       http_error(E_WARNING, HTTP_E_CURL, "Could not initialize curl.");
+                       return FAILURE;
+               }
        }
-       return http_request_exec(ch, info);
+
+       status =        ((SUCCESS == http_request_init(ch, meth, url, body, options, response)) &&
+                               (SUCCESS == http_request_exec(ch, info))) ? SUCCESS : FAILURE;
+
+       if (clean_curl) {
+               curl_easy_cleanup(ch);
+       }
+       return status;
 }
 /* }}} */
 
@@ -846,7 +867,6 @@ PHP_HTTP_API STATUS _http_request_pool_attach(http_request_pool *pool, zval *req
                zval *info = GET_PROP_EX(req, request, responseInfo);
 
                if (SUCCESS != http_request_object_requesthandler(req, request, body)) {
-                       efree(body);
                        http_error_ex(E_WARNING, HTTP_E_CURL, "Could not initialize HttpRequest object for attaching to the HttpRequestPool");
                } else if (CURLM_OK != (code = curl_multi_add_handle(pool->ch, req->ch))) {
                        http_error_ex(E_WARNING, HTTP_E_CURL, "Could not attach HttpRequest object to the HttpRequestPool: %s", curl_multi_strerror(code));
@@ -857,6 +877,7 @@ PHP_HTTP_API STATUS _http_request_pool_attach(http_request_pool *pool, zval *req
                        zend_llist_add_element(&pool->bodies, &body);
                        return SUCCESS;
                }
+               efree(body);
        }
        return FAILURE;
 }
@@ -870,7 +891,7 @@ PHP_HTTP_API STATUS _http_request_pool_detach(http_request_pool *pool, zval *req
        fprintf(stderr, "Detaching request %p (pool: %p) from pool %p\n", req, req->pool, pool);
 #endif
        if (req->pool != pool) {
-               http_error_ex(E_WARNING, HTTP_E_CURL, "HttpRequest object is not attached to this HttpRequestPool (%p != %p)", req->pool, pool);
+               http_error(E_WARNING, HTTP_E_CURL, "HttpRequest object is not attached to this HttpRequestPool");
        } else {
                CURLMcode code;
 
@@ -960,7 +981,7 @@ void _http_request_pool_responsehandler(zval **req TSRMLS_DC)
 #if HTTP_DEBUG_REQPOOLS
        fprintf(stderr, "Fetching data from request %p of pool %p\n", obj, obj->pool);
 #endif
-       http_request_object_responsehandler(obj, *req, NULL);
+       http_request_object_responsehandler(obj, *req);
 }
 /* }}} */
 
@@ -1052,7 +1073,7 @@ static http_curl_callback_ctx *_http_curl_callback_data(void *data TSRMLS_DC)
        http_curl_callback_ctx *ctx = emalloc(sizeof(http_curl_callback_ctx));
        TSRMLS_SET_CTX(ctx->tsrm_ctx);
        ctx->data = data;
-       return ctx;
+       return http_request_data_copy(COPY_CONTEXT, ctx);
 }
 /* }}} */