Fix a rare crash with unitialized CURLOPT_HTTPHEADER
[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-2014, 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
92 PHP_MINIT_FUNCTION(http_curl)
93 {
94 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
95 /* mod_ssl, libpq or ext/curl might already have set thread lock callbacks */
96 if (!CRYPTO_get_id_callback()) {
97 int i, c = CRYPTO_num_locks();
98
99 php_http_openssl_tsl = malloc(c * sizeof(MUTEX_T));
100
101 for (i = 0; i < c; ++i) {
102 php_http_openssl_tsl[i] = tsrm_mutex_alloc();
103 }
104
105 CRYPTO_set_id_callback(php_http_openssl_thread_id);
106 CRYPTO_set_locking_callback(php_http_openssl_thread_lock);
107 }
108 #endif
109 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
110 gcry_control(GCRYCTL_SET_THREAD_CBS, &php_http_gnutls_tsl);
111 #endif
112
113 if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
114 return FAILURE;
115 }
116
117 return SUCCESS;
118 }
119
120 PHP_MSHUTDOWN_FUNCTION(http_curl)
121 {
122 curl_global_cleanup();
123 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
124 if (php_http_openssl_tsl) {
125 int i, c = CRYPTO_num_locks();
126
127 CRYPTO_set_id_callback(NULL);
128 CRYPTO_set_locking_callback(NULL);
129
130 for (i = 0; i < c; ++i) {
131 tsrm_mutex_free(php_http_openssl_tsl[i]);
132 }
133
134 free(php_http_openssl_tsl);
135 php_http_openssl_tsl = NULL;
136 }
137 #endif
138 return SUCCESS;
139 }
140
141 #endif /* PHP_HTTP_HAVE_CURL */
142
143 /*
144 * Local variables:
145 * tab-width: 4
146 * c-basic-offset: 4
147 * End:
148 * vim600: noet sw=4 ts=4 fdm=marker
149 * vim<600: noet sw=4 ts=4
150 */
151