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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 #if PHP_HTTP_HAVE_CURL
17 #if defined(ZTS) && defined(PHP_HTTP_HAVE_SSL)
19 # define PHP_HTTP_NEED_OPENSSL_TSL
20 # include <openssl/crypto.h>
21 # else /* !PHP_WIN32 */
22 # if defined(PHP_HTTP_HAVE_OPENSSL)
23 # define PHP_HTTP_NEED_OPENSSL_TSL
24 # include <openssl/crypto.h>
25 # elif defined(PHP_HTTP_HAVE_GNUTLS)
26 # define PHP_HTTP_NEED_GNUTLS_TSL
30 "libcurl was compiled with SSL support, but configure could not determine which" \
31 "library was used; thus no SSL crypto locking callbacks will be set, which may " \
32 "cause random crashes on SSL requests"
33 # endif /* PHP_HTTP_HAVE_OPENSSL || PHP_HTTP_HAVE_GNUTLS */
34 # endif /* PHP_WIN32 */
35 #endif /* ZTS && PHP_HTTP_HAVE_SSL */
38 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
39 static MUTEX_T
*php_http_openssl_tsl
= NULL
;
41 static void php_http_openssl_thread_lock(int mode
, int n
, const char * file
, int line
)
43 if (mode
& CRYPTO_LOCK
) {
44 tsrm_mutex_lock(php_http_openssl_tsl
[n
]);
46 tsrm_mutex_unlock(php_http_openssl_tsl
[n
]);
50 static ulong
php_http_openssl_thread_id(void)
52 return (ulong
) tsrm_thread_id();
55 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
56 static int php_http_gnutls_mutex_create(void **m
)
58 if (*((MUTEX_T
*) m
) = tsrm_mutex_alloc()) {
65 static int php_http_gnutls_mutex_destroy(void **m
)
67 tsrm_mutex_free(*((MUTEX_T
*) m
));
71 static int php_http_gnutls_mutex_lock(void **m
)
73 return tsrm_mutex_lock(*((MUTEX_T
*) m
));
76 static int php_http_gnutls_mutex_unlock(void **m
)
78 return tsrm_mutex_unlock(*((MUTEX_T
*) m
));
81 static struct gcry_thread_cbs php_http_gnutls_tsl
= {
82 GCRY_THREAD_OPTION_USER
,
84 php_http_gnutls_mutex_create
,
85 php_http_gnutls_mutex_destroy
,
86 php_http_gnutls_mutex_lock
,
87 php_http_gnutls_mutex_unlock
92 PHP_MINIT_FUNCTION(http_curl
)
94 php_http_client_factory_driver_t driver
= {
95 php_http_curl_client_get_ops(),
96 php_http_curl_client_pool_get_ops(),
97 php_http_curl_client_datashare_get_ops()
100 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
101 /* mod_ssl, libpq or ext/curl might already have set thread lock callbacks */
102 if (!CRYPTO_get_id_callback()) {
103 int i
, c
= CRYPTO_num_locks();
105 php_http_openssl_tsl
= malloc(c
* sizeof(MUTEX_T
));
107 for (i
= 0; i
< c
; ++i
) {
108 php_http_openssl_tsl
[i
] = tsrm_mutex_alloc();
111 CRYPTO_set_id_callback(php_http_openssl_thread_id
);
112 CRYPTO_set_locking_callback(php_http_openssl_thread_lock
);
115 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
116 gcry_control(GCRYCTL_SET_THREAD_CBS
, &php_http_gnutls_tsl
);
119 if (CURLE_OK
!= curl_global_init(CURL_GLOBAL_ALL
)) {
123 if (SUCCESS
!= php_http_client_factory_add_driver(ZEND_STRL("curl"), &driver
)) {
130 PHP_MSHUTDOWN_FUNCTION(http_curl
)
132 curl_global_cleanup();
133 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
134 if (php_http_openssl_tsl
) {
135 int i
, c
= CRYPTO_num_locks();
137 CRYPTO_set_id_callback(NULL
);
138 CRYPTO_set_locking_callback(NULL
);
140 for (i
= 0; i
< c
; ++i
) {
141 tsrm_mutex_free(php_http_openssl_tsl
[i
]);
144 free(php_http_openssl_tsl
);
145 php_http_openssl_tsl
= NULL
;
151 #endif /* PHP_HTTP_HAVE_CURL */
158 * vim600: noet sw=4 ts=4 fdm=marker
159 * vim<600: noet sw=4 ts=4