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-2014, 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
28 # endif /* PHP_HTTP_HAVE_OPENSSL || PHP_HTTP_HAVE_GNUTLS */
29 # endif /* PHP_WIN32 */
30 #endif /* ZTS && PHP_HTTP_HAVE_SSL */
33 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
34 static MUTEX_T
*php_http_openssl_tsl
= NULL
;
36 static void php_http_openssl_thread_lock(int mode
, int n
, const char * file
, int line
)
38 if (mode
& CRYPTO_LOCK
) {
39 tsrm_mutex_lock(php_http_openssl_tsl
[n
]);
41 tsrm_mutex_unlock(php_http_openssl_tsl
[n
]);
45 static ulong
php_http_openssl_thread_id(void)
47 return (ulong
) tsrm_thread_id();
50 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
51 static int php_http_gnutls_mutex_create(void **m
)
53 if (*((MUTEX_T
*) m
) = tsrm_mutex_alloc()) {
60 static int php_http_gnutls_mutex_destroy(void **m
)
62 tsrm_mutex_free(*((MUTEX_T
*) m
));
66 static int php_http_gnutls_mutex_lock(void **m
)
68 return tsrm_mutex_lock(*((MUTEX_T
*) m
));
71 static int php_http_gnutls_mutex_unlock(void **m
)
73 return tsrm_mutex_unlock(*((MUTEX_T
*) m
));
76 static struct gcry_thread_cbs php_http_gnutls_tsl
= {
77 GCRY_THREAD_OPTION_USER
,
79 php_http_gnutls_mutex_create
,
80 php_http_gnutls_mutex_destroy
,
81 php_http_gnutls_mutex_lock
,
82 php_http_gnutls_mutex_unlock
87 PHP_MINIT_FUNCTION(http_curl
)
89 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
90 /* mod_ssl, libpq or ext/curl might already have set thread lock callbacks */
91 if (!CRYPTO_get_id_callback()) {
92 int i
, c
= CRYPTO_num_locks();
94 php_http_openssl_tsl
= malloc(c
* sizeof(MUTEX_T
));
96 for (i
= 0; i
< c
; ++i
) {
97 php_http_openssl_tsl
[i
] = tsrm_mutex_alloc();
100 CRYPTO_set_id_callback(php_http_openssl_thread_id
);
101 CRYPTO_set_locking_callback(php_http_openssl_thread_lock
);
104 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
105 gcry_control(GCRYCTL_SET_THREAD_CBS
, &php_http_gnutls_tsl
);
108 if (CURLE_OK
!= curl_global_init(CURL_GLOBAL_ALL
)) {
115 PHP_MSHUTDOWN_FUNCTION(http_curl
)
117 curl_global_cleanup();
118 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
119 if (php_http_openssl_tsl
) {
120 int i
, c
= CRYPTO_num_locks();
122 CRYPTO_set_id_callback(NULL
);
123 CRYPTO_set_locking_callback(NULL
);
125 for (i
= 0; i
< c
; ++i
) {
126 tsrm_mutex_free(php_http_openssl_tsl
[i
]);
129 free(php_http_openssl_tsl
);
130 php_http_openssl_tsl
= NULL
;
136 #endif /* PHP_HTTP_HAVE_CURL */
143 * vim600: noet sw=4 ts=4 fdm=marker
144 * vim<600: noet sw=4 ts=4