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_LIBCURL
17 #if ZTS && PHP_HTTP_HAVE_LIBCURL_SSL
20 # define PHP_HTTP_NEED_OPENSSL_TSL 1
21 # include <openssl/crypto.h>
22 # else /* !PHP_WIN32 */
23 # if PHP_HTTP_HAVE_LIBCURL_OPENSSL
24 # define PHP_HTTP_NEED_OPENSSL_TSL 1
25 # include <openssl/crypto.h>
26 # elif PHP_HTTP_HAVE_LIBCURL_GNUTLS
27 # define PHP_HTTP_NEED_GNUTLS_TSL 1
29 # endif /* PHP_HTTP_HAVE_LIBCURL_OPENSSL || PHP_HTTP_HAVE_LIBCURL_GNUTLS */
30 # endif /* PHP_WIN32 */
31 #endif /* ZTS && PHP_HTTP_HAVE_LIBCURL_SSL */
34 #if PHP_HTTP_NEED_OPENSSL_TSL
35 static MUTEX_T
*php_http_openssl_tsl
= NULL
;
37 static void php_http_openssl_thread_lock(int mode
, int n
, const char * file
, int line
)
39 if (mode
& CRYPTO_LOCK
) {
40 tsrm_mutex_lock(php_http_openssl_tsl
[n
]);
42 tsrm_mutex_unlock(php_http_openssl_tsl
[n
]);
46 static ulong
php_http_openssl_thread_id(void)
48 return (ulong
) tsrm_thread_id();
51 #if PHP_HTTP_NEED_GNUTLS_TSL
52 static int php_http_gnutls_mutex_create(void **m
)
54 if (*((MUTEX_T
*) m
) = tsrm_mutex_alloc()) {
61 static int php_http_gnutls_mutex_destroy(void **m
)
63 tsrm_mutex_free(*((MUTEX_T
*) m
));
67 static int php_http_gnutls_mutex_lock(void **m
)
69 return tsrm_mutex_lock(*((MUTEX_T
*) m
));
72 static int php_http_gnutls_mutex_unlock(void **m
)
74 return tsrm_mutex_unlock(*((MUTEX_T
*) m
));
77 static struct gcry_thread_cbs php_http_gnutls_tsl
= {
78 GCRY_THREAD_OPTION_USER
,
80 php_http_gnutls_mutex_create
,
81 php_http_gnutls_mutex_destroy
,
82 php_http_gnutls_mutex_lock
,
83 php_http_gnutls_mutex_unlock
88 PHP_MINIT_FUNCTION(http_curl
)
90 #if PHP_HTTP_NEED_OPENSSL_TSL
91 /* mod_ssl, libpq or ext/curl might already have set thread lock callbacks */
92 if (!CRYPTO_get_id_callback()) {
93 int i
, c
= CRYPTO_num_locks();
95 php_http_openssl_tsl
= malloc(c
* sizeof(MUTEX_T
));
97 for (i
= 0; i
< c
; ++i
) {
98 php_http_openssl_tsl
[i
] = tsrm_mutex_alloc();
101 CRYPTO_set_id_callback(php_http_openssl_thread_id
);
102 CRYPTO_set_locking_callback(php_http_openssl_thread_lock
);
105 #if PHP_HTTP_NEED_GNUTLS_TSL
106 gcry_control(GCRYCTL_SET_THREAD_CBS
, &php_http_gnutls_tsl
);
109 if (CURLE_OK
!= curl_global_init(CURL_GLOBAL_ALL
)) {
116 PHP_MSHUTDOWN_FUNCTION(http_curl
)
118 curl_global_cleanup();
119 #if PHP_HTTP_NEED_OPENSSL_TSL
120 if (php_http_openssl_tsl
) {
121 int i
, c
= CRYPTO_num_locks();
123 CRYPTO_set_id_callback(NULL
);
124 CRYPTO_set_locking_callback(NULL
);
126 for (i
= 0; i
< c
; ++i
) {
127 tsrm_mutex_free(php_http_openssl_tsl
[i
]);
130 free(php_http_openssl_tsl
);
131 php_http_openssl_tsl
= NULL
;
137 #endif /* PHP_HTTP_HAVE_LIBCURL */
144 * vim600: noet sw=4 ts=4 fdm=marker
145 * vim<600: noet sw=4 ts=4