split off property proxy
[m6w6/ext-http] / php_http_curl_client_pool.c
index 61a00f17a7f6b3597c63a057d1abdc3b924a39cc..6d04b538fdf54bae1edd2b3d175a847fe7ab51ba 100644 (file)
 
 #if PHP_HTTP_HAVE_EVENT
 #      include <event.h>
+#      if !PHP_HTTP_HAVE_EVENT2 && /* just be really sure */ !(LIBEVENT_VERSION_NUMBER >= 0x02000000)
+#              define event_base_new event_init
+#              define event_assign(e, b, s, a, cb, d) do {\
+                       event_set(e, s, a, cb, d); \
+                       event_base_set(b, e); \
+               } while(0)
+#      endif
 #endif
 
 typedef struct php_http_curl_client_pool {
@@ -30,7 +37,7 @@ typedef struct php_http_curl_client_pool {
 #endif
 } php_http_curl_client_pool_t;
 
-static void *php_http_curlm_ctor(void *opaque TSRMLS_DC)
+static void *php_http_curlm_ctor(void *opaque, void *init_arg TSRMLS_DC)
 {
        return curl_multi_init();
 }
@@ -55,7 +62,7 @@ static void php_http_curl_client_pool_responsehandler(php_http_client_pool_t *po
                        zval **request;
 
                        if (CURLE_OK != msg->data.result) {
-                               php_http_curl_client_storage_t *st = get_storage(msg->easy_handle);
+                               php_http_curl_client_storage_t *st = php_http_curl_client_get_storage(msg->easy_handle);
                                php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "%s; %s (%s)", curl_easy_strerror(msg->data.result), STR_PTR(st->errorbuffer), STR_PTR(st->url));
                        }
 
@@ -166,7 +173,6 @@ static int php_http_curl_client_pool_socket_callback(CURL *easy, curl_socket_t s
                        ev = ecalloc(1, sizeof(php_http_client_pool_event_t));
                        ev->pool = pool;
                        curl_multi_assign(curl->handle, sock, ev);
-                       event_base_set(PHP_HTTP_G->curl.event_base, &ev->evnt);
                } else {
                        event_del(&ev->evnt);
                }
@@ -193,7 +199,7 @@ static int php_http_curl_client_pool_socket_callback(CURL *easy, curl_socket_t s
                                return -1;
                }
 
-               event_set(&ev->evnt, sock, events, php_http_curl_client_pool_event_callback, pool);
+               event_assign(&ev->evnt, PHP_HTTP_G->curl.event_base, sock, events, php_http_curl_client_pool_event_callback, pool);
                event_add(&ev->evnt, NULL);
        }
 
@@ -206,19 +212,18 @@ static void php_http_curl_client_pool_timer_callback(CURLM *multi, long timeout_
        php_http_curl_client_pool_t *curl = pool->ctx;
 
 #if DBG_EVENTS
-       fprintf(stderr, "%ld", timeout_ms);
+       fprintf(stderr, "\ntimer <- timeout_ms: %ld\n", timeout_ms);
 #endif
        if (curl->useevents) {
 
                if (timeout_ms < 0) {
-                       php_http_curl_client_pool_timeout_callback(CURL_SOCKET_TIMEOUT, CURL_CSELECT_IN|CURL_CSELECT_OUT, pool);
+                       php_http_curl_client_pool_timeout_callback(CURL_SOCKET_TIMEOUT, EV_READ|EV_WRITE, pool);
                } else if (timeout_ms > 0 || !event_initialized(curl->timeout) || !event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
                        struct timeval timeout;
                        TSRMLS_FETCH_FROM_CTX(pool->ts);
 
                        if (!event_initialized(curl->timeout)) {
-                               event_set(curl->timeout, -1, 0, php_http_curl_client_pool_timeout_callback, pool);
-                               event_base_set(PHP_HTTP_G->curl.event_base, curl->timeout);
+                               event_assign(curl->timeout, PHP_HTTP_G->curl.event_base, CURL_SOCKET_TIMEOUT, 0, php_http_curl_client_pool_timeout_callback, pool);
                        } else if (event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
                                event_del(curl->timeout);
                        }
@@ -241,7 +246,7 @@ static php_http_client_pool_t *php_http_curl_client_pool_init(php_http_client_po
        php_http_curl_client_pool_t *curl;
        TSRMLS_FETCH_FROM_CTX(h->ts);
 
-       if (!handle && !(handle = php_http_resource_factory_handle_ctor(h->rf TSRMLS_CC))) {
+       if (!handle && !(handle = php_resource_factory_handle_ctor(h->rf, NULL TSRMLS_CC))) {
                php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT_POOL, "could not initialize curl pool handle");
                return NULL;
        }
@@ -268,7 +273,7 @@ static void php_http_curl_client_pool_dtor(php_http_client_pool_t *h)
        curl->unfinished = 0;
        php_http_client_pool_reset(h);
 
-       php_http_resource_factory_handle_dtor(h->rf, curl->handle TSRMLS_CC);
+       php_resource_factory_handle_dtor(h->rf, curl->handle TSRMLS_CC);
 
        efree(curl);
        h->ctx = NULL;
@@ -281,6 +286,11 @@ static STATUS php_http_curl_client_pool_attach(php_http_client_pool_t *h, php_ht
        CURLMcode rs;
        TSRMLS_FETCH_FROM_CTX(h->ts);
 
+       if (r->ops != php_http_curl_client_get_ops()) {
+               php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT_POOL, "Cannot attach a non-curl client to this pool");
+               return FAILURE;
+       }
+
        if (SUCCESS != php_http_curl_client_prepare(r, m)) {
                return FAILURE;
        }
@@ -298,10 +308,15 @@ static STATUS php_http_curl_client_pool_detach(php_http_client_pool_t *h, php_ht
 {
        php_http_curl_client_pool_t *curl = h->ctx;
        php_http_curl_client_t *recurl = r->ctx;
-       CURLMcode rs = curl_multi_remove_handle(curl->handle, recurl->handle);
+       CURLMcode rs;
        TSRMLS_FETCH_FROM_CTX(h->ts);
 
-       if (CURLM_OK == rs) {
+       if (r->ops != php_http_curl_client_get_ops()) {
+               php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT_POOL, "Cannot attach a non-curl client to this pool");
+               return FAILURE;
+       }
+
+       if (CURLM_OK == (rs = curl_multi_remove_handle(curl->handle, recurl->handle))) {
                return SUCCESS;
        } else {
                php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT_POOL, "Could not detach request from pool: %s", curl_multi_strerror(rs));
@@ -379,11 +394,7 @@ static int php_http_curl_client_pool_once(php_http_client_pool_t *h)
        return curl->unfinished;
 
 }
-#if PHP_HTTP_HAVE_EVENT
-static void dolog(int i, const char *m) {
-       fprintf(stderr, "%d: %s\n", i, m);
-}
-#endif
+
 static STATUS php_http_curl_client_pool_exec(php_http_client_pool_t *h)
 {
        TSRMLS_FETCH_FROM_CTX(h->ts);
@@ -392,12 +403,18 @@ static STATUS php_http_curl_client_pool_exec(php_http_client_pool_t *h)
        php_http_curl_client_pool_t *curl = h->ctx;
 
        if (curl->useevents) {
-               event_set_log_callback(dolog);
+               php_http_curl_client_pool_timeout_callback(CURL_SOCKET_TIMEOUT, EV_READ|EV_WRITE, h);
                do {
+                       int ev_rc = event_base_dispatch(PHP_HTTP_G->curl.event_base);
+
 #if DBG_EVENTS
-                       fprintf(stderr, "X");
+                       fprintf(stderr, "%c", "X.0"[ev_rc+1]);
 #endif
-                       event_base_dispatch(PHP_HTTP_G->curl.event_base);
+
+                       if (ev_rc < 0) {
+                               php_http_error(HE_ERROR, PHP_HTTP_E_RUNTIME, "Error in event_base_dispatch()");
+                               return FAILURE;
+                       }
                } while (curl->unfinished);
        } else
 #endif
@@ -454,17 +471,12 @@ static STATUS php_http_curl_client_pool_setopt(php_http_client_pool_t *h, php_ht
        return SUCCESS;
 }
 
-static php_http_resource_factory_ops_t php_http_curlm_resource_factory_ops = {
+static php_resource_factory_ops_t php_http_curlm_resource_factory_ops = {
        php_http_curlm_ctor,
        NULL,
        php_http_curlm_dtor
 };
 
-static zend_class_entry *get_class_entry(void)
-{
-       return php_http_curl_client_pool_class_entry;
-}
-
 static php_http_client_pool_ops_t php_http_curl_client_pool_ops = {
        &php_http_curlm_resource_factory_ops,
        php_http_curl_client_pool_init,
@@ -478,7 +490,7 @@ static php_http_client_pool_ops_t php_http_curl_client_pool_ops = {
        php_http_curl_client_pool_detach,
        php_http_curl_client_pool_setopt,
        (php_http_new_t) php_http_curl_client_pool_object_new_ex,
-       get_class_entry
+       php_http_curl_client_pool_get_class_entry
 };
 
 PHP_HTTP_API php_http_client_pool_ops_t *php_http_curl_client_pool_get_ops(void)
@@ -492,8 +504,14 @@ PHP_HTTP_API php_http_client_pool_ops_t *php_http_curl_client_pool_get_ops(void)
 #define PHP_HTTP_CURL_ALIAS(method, func)              PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpClientCURL, method))
 #define PHP_HTTP_CURL_MALIAS(me, al, vis)              ZEND_FENTRY(me, ZEND_MN(HttpClientCURL_##al), PHP_HTTP_ARGS(HttpClientCURL, al), vis)
 
-zend_class_entry *php_http_curl_client_pool_class_entry;
-zend_function_entry php_http_curl_client_pool_method_entry[] = {
+static zend_class_entry *php_http_curl_client_pool_class_entry;
+
+zend_class_entry *php_http_curl_client_pool_get_class_entry(void)
+{
+       return php_http_curl_client_pool_class_entry;
+}
+
+static zend_function_entry php_http_curl_client_pool_method_entry[] = {
        EMPTY_FUNCTION_ENTRY
 };
 
@@ -528,7 +546,7 @@ zend_object_value php_http_curl_client_pool_object_new_ex(zend_class_entry *ce,
 
 PHP_MINIT_FUNCTION(http_curl_client_pool)
 {
-       if (SUCCESS != php_http_persistent_handle_provide(ZEND_STRL("http_client_pool.curl"), &php_http_curlm_resource_factory_ops, NULL, NULL)) {
+       if (SUCCESS != php_persistent_handle_provide(ZEND_STRL("http_client_pool.curl"), &php_http_curlm_resource_factory_ops, NULL, NULL)) {
                return FAILURE;
        }
 
@@ -538,16 +556,26 @@ PHP_MINIT_FUNCTION(http_curl_client_pool)
        return SUCCESS;
 }
 
+#if PHP_HTTP_HAVE_EVENT
 PHP_RINIT_FUNCTION(http_curl_client_pool)
 {
-#if PHP_HTTP_HAVE_EVENT
-       if (!PHP_HTTP_G->curl.event_base && !(PHP_HTTP_G->curl.event_base = event_init())) {
+       if (!PHP_HTTP_G->curl.event_base && !(PHP_HTTP_G->curl.event_base = event_base_new())) {
                return FAILURE;
        }
+       return SUCCESS;
+}
 #endif
 
+#if PHP_HTTP_HAVE_EVENT
+PHP_RSHUTDOWN_FUNCTION(http_curl_client_pool)
+{
+       if (PHP_HTTP_G->curl.event_base) {
+               event_base_free(PHP_HTTP_G->curl.event_base);
+               PHP_HTTP_G->curl.event_base = NULL;
+       }
        return SUCCESS;
 }
+#endif
 
 #endif /* PHP_HTTP_HAVE_CURL */