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