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"
25 #ifndef HAVE_CURL_SHARE_STRERROR
26 # define curl_share_strerror(dummy) "unknown error"
29 static HashTable http_request_datashare_options
;
30 static http_request_datashare http_request_datashare_global
;
31 static int http_request_datashare_compare_handles(void *h1
, void *h2
);
32 static void http_request_datashare_lock_func(CURL
*handle
, curl_lock_data data
, curl_lock_access locktype
, void *userptr
);
33 static void http_request_datashare_unlock_func(CURL
*handle
, curl_lock_data data
, void *userptr
);
35 PHP_MINIT_FUNCTION(http_request_datashare
)
39 zend_hash_init(&http_request_datashare_options
, 4, NULL
, NULL
, 1);
40 #define ADD_DATASHARE_OPT(name, opt) \
42 zend_hash_add(&http_request_datashare_options, name, sizeof(name), &val, sizeof(curl_lock_data), NULL)
43 ADD_DATASHARE_OPT("cookie", CURL_LOCK_DATA_COOKIE
);
44 ADD_DATASHARE_OPT("dns", CURL_LOCK_DATA_DNS
);
45 ADD_DATASHARE_OPT("ssl", CURL_LOCK_DATA_SSL_SESSION
);
46 ADD_DATASHARE_OPT("connect", CURL_LOCK_DATA_CONNECT
);
48 http_request_datashare_init_ex(&http_request_datashare_global
, 1);
53 PHP_MSHUTDOWN_FUNCTION(http_request_datashare
)
55 http_request_datashare_dtor(&http_request_datashare_global
);
56 zend_hash_destroy(&http_request_datashare_options
);
61 PHP_HTTP_API http_request_datashare
*_http_request_datashare_init_ex(http_request_datashare
*share
, zend_bool persistent TSRMLS_DC
)
66 if ((free_share
= !share
)) {
67 share
= pemalloc(sizeof(http_request_datashare
), persistent
);
69 memset(share
, 0, sizeof(http_request_datashare
));
71 HTTP_CHECK_CURL_INIT(share
->ch
, curl_share_init(), ;);
74 pefree(share
, persistent
);
79 if ((share
->persistent
= persistent
)) {
80 share
->locks
= pecalloc(CURL_LOCK_DATA_LAST
, sizeof(http_request_datashare_lock
), 1);
81 for (i
= 0; i
< CURL_LOCK_DATA_LAST
; ++i
) {
82 share
->locks
[i
].mx
= tsrm_mutex_alloc();
84 curl_share_setopt(share
->ch
, CURLSHOPT_LOCKFUNC
, http_request_datashare_lock_func
);
85 curl_share_setopt(share
->ch
, CURLSHOPT_UNLOCKFUNC
, http_request_datashare_unlock_func
);
86 curl_share_setopt(share
->ch
, CURLSHOPT_USERDATA
, share
);
89 zend_llist_init(&share
->handles
, sizeof(zval
*), ZVAL_PTR_DTOR
, persistent
);
94 PHP_HTTP_API STATUS
_http_request_datashare_attach(http_request_datashare
*share
, zval
*request TSRMLS_DC
)
97 getObjectEx(http_request_object
, obj
, request
);
100 if (obj
->share
== share
) {
102 } else if (SUCCESS
!= http_request_datashare_detach(obj
->share
, request
)) {
107 if (CURLE_OK
!= (rc
= curl_easy_setopt(obj
->request
->ch
, CURLOPT_SHARE
, share
->ch
))) {
108 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
));
113 ZVAL_ADDREF(request
);
114 zend_llist_add_element(&share
->handles
, (void *) &request
);
119 PHP_HTTP_API STATUS
_http_request_datashare_detach(http_request_datashare
*share
, zval
*request TSRMLS_DC
)
122 getObjectEx(http_request_object
, obj
, request
);
125 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "HttpRequest object(#%d) is not attached to any HttpRequestDataShare", Z_OBJ_HANDLE_P(request
));
126 } else if (obj
->share
!= share
) {
127 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "HttpRequest object(#%d) is not attached to this HttpRequestDataShare", Z_OBJ_HANDLE_P(request
));
128 } else if (CURLE_OK
!= (rc
= curl_easy_setopt(obj
->request
->ch
, CURLOPT_SHARE
, NULL
))) {
129 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
));
132 zend_llist_del_element(&share
->handles
, request
, http_request_datashare_compare_handles
);
138 PHP_HTTP_API
void _http_request_datashare_detach_all(http_request_datashare
*share TSRMLS_DC
)
142 while ((r
= zend_llist_get_first(&share
->handles
))) {
143 http_request_datashare_detach(share
, *r
);
147 PHP_HTTP_API
void _http_request_datashare_dtor(http_request_datashare
*share TSRMLS_DC
)
151 zend_llist_destroy(&share
->handles
);
152 curl_share_cleanup(share
->ch
);
153 if (share
->persistent
) {
154 for (i
= 0; i
< CURL_LOCK_DATA_LAST
; ++i
) {
155 tsrm_mutex_free(share
->locks
[i
].mx
);
157 pefree(share
->locks
, 1);
161 PHP_HTTP_API
void _http_request_datashare_free(http_request_datashare
**share TSRMLS_DC
)
163 http_request_datashare_dtor(*share
);
164 pefree(*share
, (*share
)->persistent
);
168 PHP_HTTP_API STATUS
_http_request_datashare_set(http_request_datashare
*share
, const char *option
, size_t option_len
, zend_bool enable TSRMLS_DC
)
173 if (SUCCESS
== zend_hash_find(&http_request_datashare_options
, (char *) option
, option_len
+ 1, (void *) &opt
)) {
174 if (CURLSHE_OK
== (rc
= curl_share_setopt(share
->ch
, enable
? CURLSHOPT_SHARE
: CURLSHOPT_UNSHARE
, *opt
))) {
177 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "Could not %s sharing of %s data: %s", enable
? "enable" : "disable", option
, curl_share_strerror(rc
));
182 static int http_request_datashare_compare_handles(void *h1
, void *h2
)
184 return (Z_OBJ_HANDLE_PP((zval
**) h1
) == Z_OBJ_HANDLE_P((zval
*) h2
));
187 static void http_request_datashare_lock_func(CURL
*handle
, curl_lock_data data
, curl_lock_access locktype
, void *userptr
)
189 http_request_datashare
*share
= (http_request_datashare
*) userptr
;
191 /* TSRM can't distinguish shared/exclusive locks */
192 tsrm_mutex_lock(share
->locks
[data
].mx
);
193 share
->locks
[data
].ch
= handle
;
196 static void http_request_datashare_unlock_func(CURL
*handle
, curl_lock_data data
, void *userptr
)
198 http_request_datashare
*share
= (http_request_datashare
*) userptr
;
200 if (share
->locks
[data
].ch
== handle
) {
201 tsrm_mutex_unlock(share
->locks
[data
].mx
);
205 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
213 * vim600: noet sw=4 ts=4 fdm=marker
214 * vim<600: noet sw=4 ts=4