rename http\Env\Request::getPost() to getForm(), because it actually retrieves form...
[m6w6/ext-http] / php_http_curl.c
index 377a0ea43d8398743de8bc9d0b29b6899df3f494..65b1746b8f7b38ac7a71f71c584d49fe108c3b44 100644 (file)
@@ -1,19 +1,26 @@
-
-#include "php_http.h"
-#include "php_http_request.h"
-#include "php_http_request_pool.h"
+/*
+    +--------------------------------------------------------------------+
+    | PECL :: http                                                       |
+    +--------------------------------------------------------------------+
+    | Redistribution and use in source and binary forms, with or without |
+    | modification, are permitted provided that the conditions mentioned |
+    | in the accompanying LICENSE file are met.                          |
+    +--------------------------------------------------------------------+
+    | Copyright (c) 2004-2011, Michael Wallner <mike@php.net>            |
+    +--------------------------------------------------------------------+
+*/
+
+#include "php_http_api.h"
+
+#if PHP_HTTP_HAVE_CURL
 
 #include <curl/curl.h>
 #define PHP_HTTP_CURL_VERSION(x, y, z) (LIBCURL_VERSION_NUM >= (((x)<<16) + ((y)<<8) + (z)))
 
-#ifdef PHP_HTTP_HAVE_EVENT
+#if PHP_HTTP_HAVE_EVENT
 #      include <event.h>
 #endif
 
-#include <ext/spl/spl_iterators.h>
-#include <Zend/zend_interfaces.h>
-
-
 typedef struct php_http_curl_request {
        CURL *handle;
 
@@ -47,26 +54,15 @@ typedef struct php_http_curl_request_pool {
 
        int unfinished;  /* int because of curl_multi_perform() */
 
-#ifdef PHP_HTTP_HAVE_EVENT
+#if PHP_HTTP_HAVE_EVENT
        struct event *timeout;
        unsigned useevents:1;
        unsigned runsocket:1;
 #endif
 } php_http_curl_request_pool_t;
 
-#ifdef ZTS
-typedef struct php_http_curl_request_datashare_lock {
-       CURL *ch;
-       MUTEX_T mx;
-} php_http_curl_request_datashare_lock_t;
-#endif
-
 typedef struct php_http_curl_request_datashare {
        CURLSH *handle;
-
-#ifdef ZTS
-       php_http_curl_request_datashare_lock_t *locks;
-#endif
 } php_http_curl_request_datashare_t;
 
 #define PHP_HTTP_CURL_OPT_STRING(OPTION, ldiff, obdc) \
@@ -131,6 +127,7 @@ static void *php_http_curl_copy(void *opaque, void *handle TSRMLS_DC)
        void *ch;
 
        if ((ch = curl_easy_duphandle(handle))) {
+               curl_easy_reset(ch);
                get_storage(ch);
                return ch;
        }
@@ -395,7 +392,7 @@ static void php_http_curl_request_pool_responsehandler(php_http_request_pool_t *
 }
 
 
-#ifdef PHP_HTTP_HAVE_EVENT
+#if PHP_HTTP_HAVE_EVENT
 
 typedef struct php_http_request_pool_event {
        struct event evnt;
@@ -484,7 +481,7 @@ static int php_http_curl_request_pool_socket_callback(CURL *easy, curl_socket_t
                        ev = ecalloc(1, sizeof(php_http_request_pool_event_t));
                        ev->pool = pool;
                        curl_multi_assign(curl->handle, sock, ev);
-                       event_base_set(PHP_HTTP_G->request_pool.event_base, &ev->evnt);
+                       event_base_set(PHP_HTTP_G->curl.event_base, &ev->evnt);
                } else {
                        event_del(&ev->evnt);
                }
@@ -502,6 +499,7 @@ static int php_http_curl_request_pool_socket_callback(CURL *easy, curl_socket_t
 
                        case CURL_POLL_REMOVE:
                                efree(ev);
+                               /* no break */
                        case CURL_POLL_NONE:
                                return 0;
 
@@ -535,7 +533,7 @@ static void php_http_curl_request_pool_timer_callback(CURLM *multi, long timeout
 
                        if (!event_initialized(curl->timeout)) {
                                event_set(curl->timeout, -1, 0, php_http_curl_request_pool_timeout_callback, pool);
-                               event_base_set(PHP_HTTP_G->request_pool.event_base, curl->timeout);
+                               event_base_set(PHP_HTTP_G->curl.event_base, curl->timeout);
                        } else if (event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
                                event_del(curl->timeout);
                        }
@@ -1140,53 +1138,6 @@ static STATUS get_info(CURL *ch, HashTable *info)
 }
 
 
-#ifdef ZTS
-static void *php_http_curl_request_datashare_locks_init(void)
-{
-       int i;
-       php_http_curl_request_datashare_lock_t *locks = pecalloc(CURL_LOCK_DATA_LAST, sizeof(*locks), 1);
-
-       if (locks) {
-               for (i = 0; i < CURL_LOCK_DATA_LAST; ++i) {
-                       locks[i].mx = tsrm_mutex_alloc();
-               }
-       }
-
-       return locks;
-}
-
-static void php_http_curl_request_datashare_locks_dtor(void *l)
-{
-       int i;
-       php_http_curl_request_datashare_lock_t *locks = l;
-
-       for (i = 0; i < CURL_LOCK_DATA_LAST; ++i) {
-               tsrm_mutex_free(locks[i].mx);
-       }
-       pefree(locks, 1);
-}
-
-static void php_http_curl_request_datashare_lock_func(CURL *handle, curl_lock_data data, curl_lock_access locktype, void *userptr)
-{
-       php_http_curl_request_datashare_lock_t *locks = userptr;
-
-       /* TSRM can't distinguish shared/exclusive locks */
-       tsrm_mutex_lock(locks[data].mx);
-       locks[data].ch = handle;
-}
-
-static void php_http_curl_request_datashare_unlock_func(CURL *handle, curl_lock_data data, void *userptr)
-{
-       php_http_curl_request_datashare_lock_t *locks = userptr;
-
-       if (locks[data].ch == handle) {
-               tsrm_mutex_unlock(locks[data].mx);
-       }
-}
-#endif
-
-
-
 /* request datashare handler ops */
 
 static php_http_request_datashare_t *php_http_curl_request_datashare_init(php_http_request_datashare_t *h, void *handle)
@@ -1199,18 +1150,8 @@ static php_http_request_datashare_t *php_http_curl_request_datashare_init(php_ht
                return NULL;
        }
 
-       curl = pecalloc(1, sizeof(*curl), h->persistent);
+       curl = ecalloc(1, sizeof(*curl));
        curl->handle = handle;
-#ifdef ZTS
-       if (h->persistent) {
-               curl->locks = php_http_curl_request_datashare_locks_init();
-               if (curl->locks) {
-                       curl_share_setopt(curl->handle, CURLSHOPT_LOCKFUNC, php_http_curl_request_datashare_lock_func);
-                       curl_share_setopt(curl->handle, CURLSHOPT_UNLOCKFUNC, php_http_curl_request_datashare_unlock_func);
-                       curl_share_setopt(curl->handle, CURLSHOPT_USERDATA, curl->locks);
-               }
-       }
-#endif
        h->ctx = curl;
 
        return h;
@@ -1223,13 +1164,7 @@ static void php_http_curl_request_datashare_dtor(php_http_request_datashare_t *h
 
        php_http_resource_factory_handle_dtor(h->rf, curl->handle TSRMLS_CC);
 
-#ifdef ZTS
-       if (h->persistent) {
-               php_http_curl_request_datashare_locks_dtor(curl->locks);
-       }
-#endif
-
-       pefree(curl, h->persistent);
+       efree(curl);
        h->ctx = NULL;
 }
 
@@ -1277,7 +1212,7 @@ static STATUS php_http_curl_request_datashare_setopt(php_http_request_datashare_
                        break;
 
                case PHP_HTTP_REQUEST_DATASHARE_OPT_RESOLVER:
-                       if (CURLSHE_OK != (rc = curl_share_setopt(curl->handle, *((zend_bool *) arg) ? CURLSHOPT_SHARE : CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE))) {
+                       if (CURLSHE_OK != (rc = curl_share_setopt(curl->handle, *((zend_bool *) arg) ? CURLSHOPT_SHARE : CURLSHOPT_UNSHARE, CURL_LOCK_DATA_DNS))) {
                                TSRMLS_FETCH_FROM_CTX(h->ts);
 
                                php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not %s sharing of resolver data: %s",  *((zend_bool *) arg) ? "enable" : "disable", curl_share_strerror(rc));
@@ -1340,7 +1275,7 @@ static void php_http_curl_request_pool_dtor(php_http_request_pool_t *h)
        php_http_curl_request_pool_t *curl = h->ctx;
        TSRMLS_FETCH_FROM_CTX(h->ts);
 
-#ifdef PHP_HTTP_HAVE_EVENT
+#if PHP_HTTP_HAVE_EVENT
        if (curl->timeout) {
                efree(curl->timeout);
                curl->timeout = NULL;
@@ -1403,7 +1338,7 @@ static STATUS php_http_curl_request_pool_wait(php_http_request_pool_t *h, struct
        struct timeval timeout;
        php_http_curl_request_pool_t *curl = h->ctx;
 
-#ifdef PHP_HTTP_HAVE_EVENT
+#if PHP_HTTP_HAVE_EVENT
        if (curl->useevents) {
                TSRMLS_FETCH_FROM_CTX(h->ts);
 
@@ -1444,10 +1379,10 @@ static STATUS php_http_curl_request_pool_wait(php_http_request_pool_t *h, struct
 static int php_http_curl_request_pool_once(php_http_request_pool_t *h)
 {
        php_http_curl_request_pool_t *curl = h->ctx;
-       TSRMLS_FETCH_FROM_CTX(h->ts);
 
-#ifdef PHP_HTTP_HAVE_EVENT
+#if PHP_HTTP_HAVE_EVENT
        if (curl->useevents) {
+               TSRMLS_FETCH_FROM_CTX(h->ts);
                php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "not implemented");
                return FAILURE;
        }
@@ -1460,7 +1395,7 @@ static int php_http_curl_request_pool_once(php_http_request_pool_t *h)
        return curl->unfinished;
 
 }
-#ifdef PHP_HTTP_HAVE_EVENT
+#if PHP_HTTP_HAVE_EVENT
 static void dolog(int i, const char *m) {
        fprintf(stderr, "%d: %s\n", i, m);
 }
@@ -1469,7 +1404,7 @@ static STATUS php_http_curl_request_pool_exec(php_http_request_pool_t *h)
 {
        TSRMLS_FETCH_FROM_CTX(h->ts);
 
-#ifdef PHP_HTTP_HAVE_EVENT
+#if PHP_HTTP_HAVE_EVENT
        php_http_curl_request_pool_t *curl = h->ctx;
 
        if (curl->useevents) {
@@ -1478,7 +1413,7 @@ static STATUS php_http_curl_request_pool_exec(php_http_request_pool_t *h)
 #if DBG_EVENTS
                        fprintf(stderr, "X");
 #endif
-                       event_base_dispatch(PHP_HTTP_G->request_pool.event_base);
+                       event_base_dispatch(PHP_HTTP_G->curl.event_base);
                } while (curl->unfinished);
        } else
 #endif
@@ -2140,11 +2075,23 @@ PHP_MSHUTDOWN_FUNCTION(http_curl)
 
 PHP_RINIT_FUNCTION(http_curl)
 {
-#ifdef PHP_HTTP_HAVE_EVENT
-       if (!PHP_HTTP_G->request_pool.event_base && !(PHP_HTTP_G->request_pool.event_base = event_init())) {
+#if PHP_HTTP_HAVE_EVENT
+       if (!PHP_HTTP_G->curl.event_base && !(PHP_HTTP_G->curl.event_base = event_init())) {
                return FAILURE;
        }
 #endif
 
        return SUCCESS;
 }
+
+#endif /* PHP_HTTP_HAVE_CURL */
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: noet sw=4 ts=4 fdm=marker
+ * vim<600: noet sw=4 ts=4
+ */
+