let the ctor honor the options array too
[m6w6/ext-http] / 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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 #if PHP_HTTP_HAVE_CURL
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 # else
29 # warning \
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 */
36
37
38 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
39 static MUTEX_T *php_http_openssl_tsl = NULL;
40
41 static void php_http_openssl_thread_lock(int mode, int n, const char * file, int line)
42 {
43 if (mode & CRYPTO_LOCK) {
44 tsrm_mutex_lock(php_http_openssl_tsl[n]);
45 } else {
46 tsrm_mutex_unlock(php_http_openssl_tsl[n]);
47 }
48 }
49
50 static ulong php_http_openssl_thread_id(void)
51 {
52 return (ulong) tsrm_thread_id();
53 }
54 #endif
55 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
56 static int php_http_gnutls_mutex_create(void **m)
57 {
58 if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) {
59 return SUCCESS;
60 } else {
61 return FAILURE;
62 }
63 }
64
65 static int php_http_gnutls_mutex_destroy(void **m)
66 {
67 tsrm_mutex_free(*((MUTEX_T *) m));
68 return SUCCESS;
69 }
70
71 static int php_http_gnutls_mutex_lock(void **m)
72 {
73 return tsrm_mutex_lock(*((MUTEX_T *) m));
74 }
75
76 static int php_http_gnutls_mutex_unlock(void **m)
77 {
78 return tsrm_mutex_unlock(*((MUTEX_T *) m));
79 }
80
81 static struct gcry_thread_cbs php_http_gnutls_tsl = {
82 GCRY_THREAD_OPTION_USER,
83 NULL,
84 php_http_gnutls_mutex_create,
85 php_http_gnutls_mutex_destroy,
86 php_http_gnutls_mutex_lock,
87 php_http_gnutls_mutex_unlock
88 };
89 #endif
90
91 PHP_MINIT_FUNCTION(http_curl)
92 {
93 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
94 /* mod_ssl, libpq or ext/curl might already have set thread lock callbacks */
95 if (!CRYPTO_get_id_callback()) {
96 int i, c = CRYPTO_num_locks();
97
98 php_http_openssl_tsl = malloc(c * sizeof(MUTEX_T));
99
100 for (i = 0; i < c; ++i) {
101 php_http_openssl_tsl[i] = tsrm_mutex_alloc();
102 }
103
104 CRYPTO_set_id_callback(php_http_openssl_thread_id);
105 CRYPTO_set_locking_callback(php_http_openssl_thread_lock);
106 }
107 #endif
108 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
109 gcry_control(GCRYCTL_SET_THREAD_CBS, &php_http_gnutls_tsl);
110 #endif
111
112 if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
113 return FAILURE;
114 }
115
116 return SUCCESS;
117 }
118
119 PHP_MSHUTDOWN_FUNCTION(http_curl)
120 {
121 curl_global_cleanup();
122 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
123 if (php_http_openssl_tsl) {
124 int i, c = CRYPTO_num_locks();
125
126 CRYPTO_set_id_callback(NULL);
127 CRYPTO_set_locking_callback(NULL);
128
129 for (i = 0; i < c; ++i) {
130 tsrm_mutex_free(php_http_openssl_tsl[i]);
131 }
132
133 free(php_http_openssl_tsl);
134 php_http_openssl_tsl = NULL;
135 }
136 #endif
137 return SUCCESS;
138 }
139
140 #endif /* PHP_HTTP_HAVE_CURL */
141
142 /*
143 * Local variables:
144 * tab-width: 4
145 * c-basic-offset: 4
146 * End:
147 * vim600: noet sw=4 ts=4 fdm=marker
148 * vim<600: noet sw=4 ts=4
149 */
150