3a67443b820cf99d80c26ac0f521d1607590fb2a
[m6w6/ext-http] / src / php_http_curl.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
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 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 #if PHP_HTTP_HAVE_LIBCURL
16
17 #if defined(ZTS) && defined(PHP_HTTP_HAVE_SSL)
18 # ifdef PHP_WIN32
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
27 # include <gcrypt.h>
28 # endif /* PHP_HTTP_HAVE_OPENSSL || PHP_HTTP_HAVE_GNUTLS */
29 # endif /* PHP_WIN32 */
30 #endif /* ZTS && PHP_HTTP_HAVE_SSL */
31
32
33 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
34 static MUTEX_T *php_http_openssl_tsl = NULL;
35
36 static void php_http_openssl_thread_lock(int mode, int n, const char * file, int line)
37 {
38 if (mode & CRYPTO_LOCK) {
39 tsrm_mutex_lock(php_http_openssl_tsl[n]);
40 } else {
41 tsrm_mutex_unlock(php_http_openssl_tsl[n]);
42 }
43 }
44
45 static ulong php_http_openssl_thread_id(void)
46 {
47 return (ulong) tsrm_thread_id();
48 }
49 #endif
50 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
51 static int php_http_gnutls_mutex_create(void **m)
52 {
53 if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) {
54 return SUCCESS;
55 } else {
56 return FAILURE;
57 }
58 }
59
60 static int php_http_gnutls_mutex_destroy(void **m)
61 {
62 tsrm_mutex_free(*((MUTEX_T *) m));
63 return SUCCESS;
64 }
65
66 static int php_http_gnutls_mutex_lock(void **m)
67 {
68 return tsrm_mutex_lock(*((MUTEX_T *) m));
69 }
70
71 static int php_http_gnutls_mutex_unlock(void **m)
72 {
73 return tsrm_mutex_unlock(*((MUTEX_T *) m));
74 }
75
76 static struct gcry_thread_cbs php_http_gnutls_tsl = {
77 GCRY_THREAD_OPTION_USER,
78 NULL,
79 php_http_gnutls_mutex_create,
80 php_http_gnutls_mutex_destroy,
81 php_http_gnutls_mutex_lock,
82 php_http_gnutls_mutex_unlock
83 };
84 #endif
85
86
87 PHP_MINIT_FUNCTION(http_curl)
88 {
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();
93
94 php_http_openssl_tsl = malloc(c * sizeof(MUTEX_T));
95
96 for (i = 0; i < c; ++i) {
97 php_http_openssl_tsl[i] = tsrm_mutex_alloc();
98 }
99
100 CRYPTO_set_id_callback(php_http_openssl_thread_id);
101 CRYPTO_set_locking_callback(php_http_openssl_thread_lock);
102 }
103 #endif
104 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
105 gcry_control(GCRYCTL_SET_THREAD_CBS, &php_http_gnutls_tsl);
106 #endif
107
108 if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
109 return FAILURE;
110 }
111
112 return SUCCESS;
113 }
114
115 PHP_MSHUTDOWN_FUNCTION(http_curl)
116 {
117 curl_global_cleanup();
118 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
119 if (php_http_openssl_tsl) {
120 int i, c = CRYPTO_num_locks();
121
122 CRYPTO_set_id_callback(NULL);
123 CRYPTO_set_locking_callback(NULL);
124
125 for (i = 0; i < c; ++i) {
126 tsrm_mutex_free(php_http_openssl_tsl[i]);
127 }
128
129 free(php_http_openssl_tsl);
130 php_http_openssl_tsl = NULL;
131 }
132 #endif
133 return SUCCESS;
134 }
135
136 #endif /* PHP_HTTP_HAVE_LIBCURL */
137
138 /*
139 * Local variables:
140 * tab-width: 4
141 * c-basic-offset: 4
142 * End:
143 * vim600: noet sw=4 ts=4 fdm=marker
144 * vim<600: noet sw=4 ts=4
145 */
146