2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #define HTTP_WANT_CURL
18 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
20 #include "php_http_api.h"
21 #include "php_http_request_datashare_api.h"
22 #include "php_http_request_api.h"
23 #include "php_http_request_object.h"
24 #ifdef HTTP_HAVE_PERSISTENT_HANDLES
25 # include "php_http_persistent_handle_api.h"
28 #ifndef HAVE_CURL_SHARE_STRERROR
29 # define curl_share_strerror(dummy) "unknown error"
31 #ifndef HAVE_CURL_EASY_STRERROR
32 # define curl_easy_strerror(dummy) "unknown error"
35 #ifdef HTTP_HAVE_PERSISTENT_HANDLES
36 # define HTTP_CURL_SHARE_CTOR(ch) (SUCCESS == http_persistent_handle_acquire("http_request_datashare", &(ch)))
37 # define HTTP_CURL_SHARE_DTOR(chp) http_persistent_handle_release("http_request_datashare", (chp))
39 # define HTTP_CURL_SHARE_CTOR(ch) ((ch) = curl_share_init())
40 # define HTTP_CURL_SHARE_DTOR(chp) curl_share_cleanup(*(chp)); *(chp) = NULL
43 static HashTable http_request_datashare_options
;
44 static http_request_datashare http_request_datashare_global
;
45 static int http_request_datashare_compare_handles(void *h1
, void *h2
);
46 static void http_request_datashare_destroy_handles(void *el
);
48 static void http_request_datashare_lock_func(CURL
*handle
, curl_lock_data data
, curl_lock_access locktype
, void *userptr
);
49 static void http_request_datashare_unlock_func(CURL
*handle
, curl_lock_data data
, void *userptr
);
52 http_request_datashare
*_http_request_datashare_global_get(void)
54 return &http_request_datashare_global
;
57 PHP_MINIT_FUNCTION(http_request_datashare
)
61 #ifdef HTTP_HAVE_PERSISTENT_HANDLES
62 if (SUCCESS
!= http_persistent_handle_provide("http_request_datashare", curl_share_init
, (http_persistent_handle_dtor
) curl_share_cleanup
)) {
67 if (!http_request_datashare_init_ex(&http_request_datashare_global
, 1)) {
71 zend_hash_init(&http_request_datashare_options
, 4, NULL
, NULL
, 1);
72 #define ADD_DATASHARE_OPT(name, opt) \
74 zend_hash_add(&http_request_datashare_options, name, sizeof(name), &val, sizeof(curl_lock_data), NULL)
75 ADD_DATASHARE_OPT("cookie", CURL_LOCK_DATA_COOKIE
);
76 ADD_DATASHARE_OPT("dns", CURL_LOCK_DATA_DNS
);
77 ADD_DATASHARE_OPT("ssl", CURL_LOCK_DATA_SSL_SESSION
);
78 ADD_DATASHARE_OPT("connect", CURL_LOCK_DATA_CONNECT
);
83 PHP_MSHUTDOWN_FUNCTION(http_request_datashare
)
85 http_request_datashare_dtor(&http_request_datashare_global
);
86 zend_hash_destroy(&http_request_datashare_options
);
91 PHP_RINIT_FUNCTION(http_request_datashare
)
93 zend_llist_init(&HTTP_G
->request
.datashare
.handles
, sizeof(zval
*), http_request_datashare_destroy_handles
, 0);
98 PHP_RSHUTDOWN_FUNCTION(http_request_datashare
)
100 zend_llist_destroy(&HTTP_G
->request
.datashare
.handles
);
105 PHP_HTTP_API http_request_datashare
*_http_request_datashare_init_ex(http_request_datashare
*share
, zend_bool persistent TSRMLS_DC
)
107 zend_bool free_share
;
109 if ((free_share
= !share
)) {
110 share
= pemalloc(sizeof(http_request_datashare
), persistent
);
112 memset(share
, 0, sizeof(http_request_datashare
));
114 if (!HTTP_CURL_SHARE_CTOR(share
->ch
)) {
116 pefree(share
, persistent
);
121 if (!(share
->persistent
= persistent
)) {
122 share
->handles
= emalloc(sizeof(zend_llist
));
123 zend_llist_init(share
->handles
, sizeof(zval
*), ZVAL_PTR_DTOR
, 0);
128 share
->locks
= pecalloc(CURL_LOCK_DATA_LAST
, sizeof(http_request_datashare_lock
), 1);
129 for (i
= 0; i
< CURL_LOCK_DATA_LAST
; ++i
) {
130 share
->locks
[i
].mx
= tsrm_mutex_alloc();
132 curl_share_setopt(share
->ch
, CURLSHOPT_LOCKFUNC
, http_request_datashare_lock_func
);
133 curl_share_setopt(share
->ch
, CURLSHOPT_UNLOCKFUNC
, http_request_datashare_unlock_func
);
134 curl_share_setopt(share
->ch
, CURLSHOPT_USERDATA
, share
);
141 PHP_HTTP_API STATUS
_http_request_datashare_attach(http_request_datashare
*share
, zval
*request TSRMLS_DC
)
144 getObjectEx(http_request_object
, obj
, request
);
147 if (obj
->share
== share
) {
149 } else if (SUCCESS
!= http_request_datashare_detach(obj
->share
, request
)) {
154 HTTP_CHECK_CURL_INIT(obj
->request
->ch
, http_curl_init_ex(obj
->request
->ch
, obj
->request
), return FAILURE
);
155 if (CURLE_OK
!= (rc
= curl_easy_setopt(obj
->request
->ch
, CURLOPT_SHARE
, share
->ch
))) {
156 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
));
161 ZVAL_ADDREF(request
);
162 zend_llist_add_element(HTTP_RSHARE_HANDLES(share
), (void *) &request
);
167 PHP_HTTP_API STATUS
_http_request_datashare_detach(http_request_datashare
*share
, zval
*request TSRMLS_DC
)
170 getObjectEx(http_request_object
, obj
, request
);
173 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "HttpRequest object(#%d) is not attached to any HttpRequestDataShare", Z_OBJ_HANDLE_P(request
));
174 } else if (obj
->share
!= share
) {
175 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "HttpRequest object(#%d) is not attached to this HttpRequestDataShare", Z_OBJ_HANDLE_P(request
));
176 } else if (CURLE_OK
!= (rc
= curl_easy_setopt(obj
->request
->ch
, CURLOPT_SHARE
, NULL
))) {
177 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
));
180 zend_llist_del_element(HTTP_RSHARE_HANDLES(share
), request
, http_request_datashare_compare_handles
);
186 PHP_HTTP_API
void _http_request_datashare_detach_all(http_request_datashare
*share TSRMLS_DC
)
190 while ((r
= zend_llist_get_first(HTTP_RSHARE_HANDLES(share
)))) {
191 http_request_datashare_detach(share
, *r
);
195 PHP_HTTP_API
void _http_request_datashare_dtor(http_request_datashare
*share TSRMLS_DC
)
197 if (!share
->persistent
) {
198 zend_llist_destroy(share
->handles
);
199 efree(share
->handles
);
201 HTTP_CURL_SHARE_DTOR(&share
->ch
);
203 if (share
->persistent
) {
206 for (i
= 0; i
< CURL_LOCK_DATA_LAST
; ++i
) {
207 tsrm_mutex_free(share
->locks
[i
].mx
);
209 pefree(share
->locks
, 1);
214 PHP_HTTP_API
void _http_request_datashare_free(http_request_datashare
**share TSRMLS_DC
)
216 http_request_datashare_dtor(*share
);
217 pefree(*share
, (*share
)->persistent
);
221 PHP_HTTP_API STATUS
_http_request_datashare_set(http_request_datashare
*share
, const char *option
, size_t option_len
, zend_bool enable TSRMLS_DC
)
226 if (SUCCESS
== zend_hash_find(&http_request_datashare_options
, (char *) option
, option_len
+ 1, (void *) &opt
)) {
227 if (CURLSHE_OK
== (rc
= curl_share_setopt(share
->ch
, enable
? CURLSHOPT_SHARE
: CURLSHOPT_UNSHARE
, *opt
))) {
230 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "Could not %s sharing of %s data: %s", enable
? "enable" : "disable", option
, curl_share_strerror(rc
));
235 static int http_request_datashare_compare_handles(void *h1
, void *h2
)
237 return (Z_OBJ_HANDLE_PP((zval
**) h1
) == Z_OBJ_HANDLE_P((zval
*) h2
));
240 static void http_request_datashare_destroy_handles(void *el
)
242 zval
**r
= (zval
**) el
;
245 { /* gcc 2.95 needs these braces */
246 getObjectEx(http_request_object
, obj
, *r
);
248 curl_easy_setopt(obj
->request
->ch
, CURLOPT_SHARE
, NULL
);
254 static void http_request_datashare_lock_func(CURL
*handle
, curl_lock_data data
, curl_lock_access locktype
, void *userptr
)
256 http_request_datashare
*share
= (http_request_datashare
*) userptr
;
258 /* TSRM can't distinguish shared/exclusive locks */
259 tsrm_mutex_lock(share
->locks
[data
].mx
);
260 share
->locks
[data
].ch
= handle
;
263 static void http_request_datashare_unlock_func(CURL
*handle
, curl_lock_data data
, void *userptr
)
265 http_request_datashare
*share
= (http_request_datashare
*) userptr
;
267 if (share
->locks
[data
].ch
== handle
) {
268 tsrm_mutex_unlock(share
->locks
[data
].mx
);
273 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
281 * vim600: noet sw=4 ts=4 fdm=marker
282 * vim<600: noet sw=4 ts=4