let the request be able to carry client options
[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 php_http_client_factory_driver_t driver = {
94 php_http_client_curl_get_ops(),
95 php_http_client_pool_curl_get_ops(),
96 php_http_client_datashare_curl_get_ops()
97 };
98
99 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
100 /* mod_ssl, libpq or ext/curl might already have set thread lock callbacks */
101 if (!CRYPTO_get_id_callback()) {
102 int i, c = CRYPTO_num_locks();
103
104 php_http_openssl_tsl = malloc(c * sizeof(MUTEX_T));
105
106 for (i = 0; i < c; ++i) {
107 php_http_openssl_tsl[i] = tsrm_mutex_alloc();
108 }
109
110 CRYPTO_set_id_callback(php_http_openssl_thread_id);
111 CRYPTO_set_locking_callback(php_http_openssl_thread_lock);
112 }
113 #endif
114 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
115 gcry_control(GCRYCTL_SET_THREAD_CBS, &php_http_gnutls_tsl);
116 #endif
117
118 if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
119 return FAILURE;
120 }
121
122 if (SUCCESS != php_http_client_factory_add_driver(ZEND_STRL("curl"), &driver)) {
123 return FAILURE;
124 }
125
126 return SUCCESS;
127 }
128
129 PHP_MSHUTDOWN_FUNCTION(http_curl)
130 {
131 curl_global_cleanup();
132 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
133 if (php_http_openssl_tsl) {
134 int i, c = CRYPTO_num_locks();
135
136 CRYPTO_set_id_callback(NULL);
137 CRYPTO_set_locking_callback(NULL);
138
139 for (i = 0; i < c; ++i) {
140 tsrm_mutex_free(php_http_openssl_tsl[i]);
141 }
142
143 free(php_http_openssl_tsl);
144 php_http_openssl_tsl = NULL;
145 }
146 #endif
147 return SUCCESS;
148 }
149
150 #endif /* PHP_HTTP_HAVE_CURL */
151
152 /*
153 * Local variables:
154 * tab-width: 4
155 * c-basic-offset: 4
156 * End:
157 * vim600: noet sw=4 ts=4 fdm=marker
158 * vim<600: noet sw=4 ts=4
159 */
160