X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_request_datashare_api.c;h=6c70f3f7f88fe8f47f288f239cbbfcf2f9d9df6f;hp=efcdb848e657fd346b637a41560480ae4bddcbe5;hb=accf1817e811a7edabd687ef6105983f39f13942;hpb=d22651c805eca6f0d508c6d5a47e8daac36841fe diff --git a/http_request_datashare_api.c b/http_request_datashare_api.c index efcdb84..6c70f3f 100644 --- a/http_request_datashare_api.c +++ b/http_request_datashare_api.c @@ -21,16 +21,26 @@ #include "php_http_request_datashare_api.h" #include "php_http_request_api.h" #include "php_http_request_object.h" +#ifdef HTTP_HAVE_PERSISTENT_HANDLES +# include "php_http_persistent_handle_api.h" +#endif -#ifndef HAVE_CURL_SHARE_STRERROR -# define curl_share_strerror(dummy) "unknown error" +#ifdef HTTP_HAVE_PERSISTENT_HANDLES +# define HTTP_CURL_SHARE_CTOR(ch) (SUCCESS == http_persistent_handle_acquire("http_request_datashare", &(ch))) +# define HTTP_CURL_SHARE_DTOR(chp) http_persistent_handle_release("http_request_datashare", (chp)) +#else +# define HTTP_CURL_SHARE_CTOR(ch) ((ch) = curl_share_init()) +# define HTTP_CURL_SHARE_DTOR(chp) curl_share_cleanup(*(chp)); *(chp) = NULL #endif static HashTable http_request_datashare_options; static http_request_datashare http_request_datashare_global; static int http_request_datashare_compare_handles(void *h1, void *h2); +static void http_request_datashare_destroy_handles(void *el); +#ifdef ZTS static void http_request_datashare_lock_func(CURL *handle, curl_lock_data data, curl_lock_access locktype, void *userptr); static void http_request_datashare_unlock_func(CURL *handle, curl_lock_data data, void *userptr); +#endif http_request_datashare *_http_request_datashare_global_get(void) { @@ -41,6 +51,16 @@ PHP_MINIT_FUNCTION(http_request_datashare) { curl_lock_data val; +#ifdef HTTP_HAVE_PERSISTENT_HANDLES + if (SUCCESS != http_persistent_handle_provide("http_request_datashare", curl_share_init, (http_persistent_handle_dtor) curl_share_cleanup)) { + return FAILURE; + } +#endif + + if (!http_request_datashare_init_ex(&http_request_datashare_global, 1)) { + return FAILURE; + } + zend_hash_init(&http_request_datashare_options, 4, NULL, NULL, 1); #define ADD_DATASHARE_OPT(name, opt) \ val = opt; \ @@ -50,8 +70,6 @@ PHP_MINIT_FUNCTION(http_request_datashare) ADD_DATASHARE_OPT("ssl", CURL_LOCK_DATA_SSL_SESSION); ADD_DATASHARE_OPT("connect", CURL_LOCK_DATA_CONNECT); - http_request_datashare_init_ex(&http_request_datashare_global, 1); - return SUCCESS; } @@ -63,9 +81,22 @@ PHP_MSHUTDOWN_FUNCTION(http_request_datashare) return SUCCESS; } +PHP_RINIT_FUNCTION(http_request_datashare) +{ + zend_llist_init(&HTTP_G->request.datashare.handles, sizeof(zval *), http_request_datashare_destroy_handles, 0); + + return SUCCESS; +} + +PHP_RSHUTDOWN_FUNCTION(http_request_datashare) +{ + zend_llist_destroy(&HTTP_G->request.datashare.handles); + + return SUCCESS; +} + PHP_HTTP_API http_request_datashare *_http_request_datashare_init_ex(http_request_datashare *share, zend_bool persistent TSRMLS_DC) { - int i; zend_bool free_share; if ((free_share = !share)) { @@ -73,15 +104,20 @@ PHP_HTTP_API http_request_datashare *_http_request_datashare_init_ex(http_reques } memset(share, 0, sizeof(http_request_datashare)); - HTTP_CHECK_CURL_INIT(share->ch, curl_share_init(), ;); - if (!share->ch) { + if (!HTTP_CURL_SHARE_CTOR(share->ch)) { if (free_share) { pefree(share, persistent); } return NULL; } - if ((share->persistent = persistent)) { + if (!(share->persistent = persistent)) { + share->handles = emalloc(sizeof(zend_llist)); + zend_llist_init(share->handles, sizeof(zval *), ZVAL_PTR_DTOR, 0); +#ifdef ZTS + } else { + int i; + share->locks = pecalloc(CURL_LOCK_DATA_LAST, sizeof(http_request_datashare_lock), 1); for (i = 0; i < CURL_LOCK_DATA_LAST; ++i) { share->locks[i].mx = tsrm_mutex_alloc(); @@ -89,10 +125,9 @@ PHP_HTTP_API http_request_datashare *_http_request_datashare_init_ex(http_reques curl_share_setopt(share->ch, CURLSHOPT_LOCKFUNC, http_request_datashare_lock_func); curl_share_setopt(share->ch, CURLSHOPT_UNLOCKFUNC, http_request_datashare_unlock_func); curl_share_setopt(share->ch, CURLSHOPT_USERDATA, share); +#endif } - zend_llist_init(&share->handles, sizeof(zval *), ZVAL_PTR_DTOR, persistent); - return share; } @@ -109,14 +144,15 @@ PHP_HTTP_API STATUS _http_request_datashare_attach(http_request_datashare *share } } + HTTP_CHECK_CURL_INIT(obj->request->ch, http_curl_init_ex(obj->request->ch, obj->request), return FAILURE); if (CURLE_OK != (rc = curl_easy_setopt(obj->request->ch, CURLOPT_SHARE, share->ch))) { - http_error_ex(HE_WARNING, HTTP_E_REQUEST, "Could not attach HttpRequest object(#%d) to the HttpRequestDataShare: %s", Z_OBJ_HANDLE_P(request), curl_share_strerror(rc)); + http_error_ex(HE_WARNING, HTTP_E_REQUEST, "Could not attach HttpRequest object(#%d) to the HttpRequestDataShare: %s", Z_OBJ_HANDLE_P(request), curl_easy_strerror(rc)); return FAILURE; } obj->share = share; ZVAL_ADDREF(request); - zend_llist_add_element(&share->handles, (void *) &request); + zend_llist_add_element(HTTP_RSHARE_HANDLES(share), (void *) &request); return SUCCESS; } @@ -134,7 +170,7 @@ PHP_HTTP_API STATUS _http_request_datashare_detach(http_request_datashare *share http_error_ex(HE_WARNING, HTTP_E_REQUEST, "Could not detach HttpRequest object(#%d) from the HttpRequestDataShare: %s", Z_OBJ_HANDLE_P(request), curl_share_strerror(rc)); } else { obj->share = NULL; - zend_llist_del_element(&share->handles, request, http_request_datashare_compare_handles); + zend_llist_del_element(HTTP_RSHARE_HANDLES(share), request, http_request_datashare_compare_handles); return SUCCESS; } return FAILURE; @@ -144,23 +180,28 @@ PHP_HTTP_API void _http_request_datashare_detach_all(http_request_datashare *sha { zval **r; - while ((r = zend_llist_get_first(&share->handles))) { + while ((r = zend_llist_get_first(HTTP_RSHARE_HANDLES(share)))) { http_request_datashare_detach(share, *r); } } PHP_HTTP_API void _http_request_datashare_dtor(http_request_datashare *share TSRMLS_DC) { - int i; - - zend_llist_destroy(&share->handles); - curl_share_cleanup(share->ch); + if (!share->persistent) { + zend_llist_destroy(share->handles); + efree(share->handles); + } + HTTP_CURL_SHARE_DTOR(&share->ch); +#ifdef ZTS if (share->persistent) { + int i; + for (i = 0; i < CURL_LOCK_DATA_LAST; ++i) { tsrm_mutex_free(share->locks[i].mx); } pefree(share->locks, 1); } +#endif } PHP_HTTP_API void _http_request_datashare_free(http_request_datashare **share TSRMLS_DC) @@ -189,6 +230,20 @@ static int http_request_datashare_compare_handles(void *h1, void *h2) return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2)); } +static void http_request_datashare_destroy_handles(void *el) +{ + zval **r = (zval **) el; + TSRMLS_FETCH(); + + { /* gcc 2.95 needs these braces */ + getObjectEx(http_request_object, obj, *r); + + curl_easy_setopt(obj->request->ch, CURLOPT_SHARE, NULL); + zval_ptr_dtor(r); + } +} + +#ifdef ZTS static void http_request_datashare_lock_func(CURL *handle, curl_lock_data data, curl_lock_access locktype, void *userptr) { http_request_datashare *share = (http_request_datashare *) userptr; @@ -206,6 +261,7 @@ static void http_request_datashare_unlock_func(CURL *handle, curl_lock_data data tsrm_mutex_unlock(share->locks[data].mx); } } +#endif #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */