- Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathshare.c
335 lines (256 loc) · 9.21 KB
/
share.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Pierrick Charron <pierrick@php.net> |
+----------------------------------------------------------------------+
*/
#defineZEND_INCLUDE_FULL_WINDOWS_HEADERS
#ifdefHAVE_CONFIG_H
#include<config.h>
#endif
#include"php.h"
#include"Zend/zend_exceptions.h"
#include"curl_private.h"
#include<curl/curl.h>
#defineSAVE_CURLSH_ERROR(__handle, __err) (__handle)->err.no = (int) __err;
/* {{{ Initialize a share curl handle */
PHP_FUNCTION(curl_share_init)
{
php_curlsh*sh;
ZEND_PARSE_PARAMETERS_NONE();
object_init_ex(return_value, curl_share_ce);
sh=Z_CURL_SHARE_P(return_value);
sh->share=curl_share_init();
}
/* }}} */
/* {{{ Close a set of cURL handles */
PHP_FUNCTION(curl_share_close)
{
zval*z_sh;
ZEND_PARSE_PARAMETERS_START(1,1)
Z_PARAM_OBJECT_OF_CLASS(z_sh, curl_share_ce)
ZEND_PARSE_PARAMETERS_END();
}
/* }}} */
staticbool_php_curl_share_setopt(php_curlsh*sh, zend_longoption, constzval*zvalue) /* {{{ */
{
CURLSHcodeerror=CURLSHE_OK;
switch (option) {
caseCURLSHOPT_SHARE:
caseCURLSHOPT_UNSHARE:
error=curl_share_setopt(sh->share, option, zval_get_long(zvalue));
break;
default:
zend_argument_value_error(2, "is not a valid cURL share option");
error=CURLSHE_BAD_OPTION;
break;
}
SAVE_CURLSH_ERROR(sh, error);
returnerror==CURLSHE_OK;
}
/* }}} */
/* {{{ Set an option for a cURL transfer */
PHP_FUNCTION(curl_share_setopt)
{
zval*z_sh, *zvalue;
zend_longoptions;
php_curlsh*sh;
ZEND_PARSE_PARAMETERS_START(3,3)
Z_PARAM_OBJECT_OF_CLASS(z_sh, curl_share_ce)
Z_PARAM_LONG(options)
Z_PARAM_ZVAL(zvalue)
ZEND_PARSE_PARAMETERS_END();
sh=Z_CURL_SHARE_P(z_sh);
RETURN_BOOL(_php_curl_share_setopt(sh, options, zvalue));
}
/* }}} */
/* {{{ Return an integer containing the last share curl error number */
PHP_FUNCTION(curl_share_errno)
{
zval*z_sh;
php_curlsh*sh;
ZEND_PARSE_PARAMETERS_START(1,1)
Z_PARAM_OBJECT_OF_CLASS(z_sh, curl_share_ce)
ZEND_PARSE_PARAMETERS_END();
sh=Z_CURL_SHARE_P(z_sh);
RETURN_LONG(sh->err.no);
}
/* }}} */
/* {{{ return string describing error code */
PHP_FUNCTION(curl_share_strerror)
{
zend_longcode;
constchar*str;
ZEND_PARSE_PARAMETERS_START(1,1)
Z_PARAM_LONG(code)
ZEND_PARSE_PARAMETERS_END();
str=curl_share_strerror(code);
if (str) {
RETURN_STRING(str);
} else {
RETURN_NULL();
}
}
/* }}} */
/**
* Initialize a persistent curl share handle with the given share options, reusing an existing one if found.
*
* Throws an exception if the share options are invalid.
*/
PHP_FUNCTION(curl_share_init_persistent)
{
// Options specified by the user.
HashTable*share_opts=NULL;
// De-duplicated and sorted options.
zvalshare_opts_prop;
ZVAL_UNDEF(&share_opts_prop);
// An ID representing the unique set of options specified by the user.
zend_ulongpersistent_id=0;
php_curlsh*sh=NULL;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY_HT(share_opts)
ZEND_PARSE_PARAMETERS_END();
if (zend_hash_num_elements(share_opts) ==0) {
zend_argument_must_not_be_empty_error(1);
goto error;
}
ZEND_HASH_FOREACH_VAL(share_opts, zval*entry) {
ZVAL_DEREF(entry);
boolfailed= false;
zend_ulongoption=zval_try_get_long(entry, &failed);
if (failed) {
zend_argument_type_error(1, "must contain only int values, %s given", zend_zval_value_name(entry));
goto error;
}
switch (option) {
// Disallowed options
caseCURL_LOCK_DATA_COOKIE:
zend_argument_value_error(1, "must not contain CURL_LOCK_DATA_COOKIE because sharing cookies across PHP requests is unsafe");
goto error;
// Allowed options
caseCURL_LOCK_DATA_DNS:
persistent_id |= 1 << 0;
break;
caseCURL_LOCK_DATA_SSL_SESSION:
persistent_id |= 1 << 1;
break;
caseCURL_LOCK_DATA_CONNECT:
persistent_id |= 1 << 2;
break;
caseCURL_LOCK_DATA_PSL:
persistent_id |= 1 << 3;
break;
// Unknown options
default:
zend_argument_value_error(1, "must contain only CURL_LOCK_DATA_* constants");
goto error;
}
} ZEND_HASH_FOREACH_END();
// We're now decently confident that we'll be returning a CurlSharePersistentHandle object, so we construct it here.
object_init_ex(return_value, curl_share_persistent_ce);
// Next we initialize a property field for the CurlSharePersistentHandle object with the enabled share options.
array_init(&share_opts_prop);
if (persistent_id& (1 << 0)) {
add_next_index_long(&share_opts_prop, CURL_LOCK_DATA_DNS);
}
if (persistent_id& (1 << 1)) {
add_next_index_long(&share_opts_prop, CURL_LOCK_DATA_SSL_SESSION);
}
if (persistent_id& (1 << 2)) {
add_next_index_long(&share_opts_prop, CURL_LOCK_DATA_CONNECT);
}
if (persistent_id& (1 << 3)) {
add_next_index_long(&share_opts_prop, CURL_LOCK_DATA_PSL);
}
zend_update_property(curl_share_persistent_ce, Z_OBJ_P(return_value), ZEND_STRL("options"), &share_opts_prop);
sh=Z_CURL_SHARE_P(return_value);
// If we are able to find an existing persistent share handle, we can use it and return early.
zval*persisted=zend_hash_index_find(&CURL_G(persistent_curlsh), persistent_id);
if (persisted) {
sh->share=Z_PTR_P(persisted);
goto cleanup;
}
// We could not find an existing share handle, so we'll have to create one.
sh->share=curl_share_init();
// Apply the options property to the handle. We avoid using $share_opts as zval_get_long may not necessarily return
// the same value as it did in the initial ZEND_HASH_FOREACH_VAL above.
ZEND_HASH_PACKED_FOREACH_VAL(Z_ARRVAL_P(&share_opts_prop), zval*entry) {
CURLSHcodecurlsh_error=curl_share_setopt(sh->share, CURLSHOPT_SHARE, Z_LVAL_P(entry));
if (curlsh_error!=CURLSHE_OK) {
zend_throw_exception_ex(NULL, 0, "Could not construct persistent cURL share handle: %s", curl_share_strerror(curlsh_error));
goto error;
}
} ZEND_HASH_FOREACH_END();
zend_hash_index_add_new_ptr(&CURL_G(persistent_curlsh), persistent_id, sh->share);
cleanup:
zval_ptr_dtor(&share_opts_prop);
return;
error:
if (sh) {
curl_share_cleanup(sh->share);
}
zval_ptr_dtor(&share_opts_prop);
RETURN_THROWS();
}
/**
* Free a persistent curl share handle from the module global HashTable.
*
* See PHP_GINIT_FUNCTION in ext/curl/interface.c.
*/
voidcurl_share_free_persistent_curlsh(zval*data)
{
CURLSH*handle=Z_PTR_P(data);
curl_share_cleanup(handle);
}
/* CurlShareHandle class */
staticzend_object*curl_share_create_object(zend_class_entry*class_type) {
php_curlsh*intern=zend_object_alloc(sizeof(php_curlsh), class_type);
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
return&intern->std;
}
staticzend_function*curl_share_get_constructor(zend_object*object) {
zend_throw_error(NULL, "Cannot directly construct CurlShareHandle, use curl_share_init() instead");
returnNULL;
}
voidcurl_share_free_obj(zend_object*object)
{
php_curlsh*sh=curl_share_from_obj(object);
curl_share_cleanup(sh->share);
zend_object_std_dtor(&sh->std);
}
staticzend_object_handlerscurl_share_handlers;
voidcurl_share_register_handlers(void) {
curl_share_ce->create_object=curl_share_create_object;
curl_share_ce->default_object_handlers=&curl_share_handlers;
memcpy(&curl_share_handlers, &std_object_handlers, sizeof(zend_object_handlers));
curl_share_handlers.offset=XtOffsetOf(php_curlsh, std);
curl_share_handlers.free_obj=curl_share_free_obj;
curl_share_handlers.get_constructor=curl_share_get_constructor;
curl_share_handlers.clone_obj=NULL;
curl_share_handlers.compare=zend_objects_not_comparable;
}
/* CurlSharePersistentHandle class */
staticzend_object_handlerscurl_share_persistent_handlers;
staticzend_function*curl_share_persistent_get_constructor(zend_object*object) {
zend_throw_error(NULL, "Cannot directly construct CurlSharePersistentHandle, use curl_share_init_persistent() instead");
returnNULL;
}
voidcurl_share_persistent_register_handlers(void) {
curl_share_persistent_ce->create_object=curl_share_create_object;
curl_share_persistent_ce->default_object_handlers=&curl_share_persistent_handlers;
memcpy(&curl_share_persistent_handlers, &std_object_handlers, sizeof(zend_object_handlers));
curl_share_persistent_handlers.offset=XtOffsetOf(php_curlsh, std);
curl_share_persistent_handlers.get_constructor=curl_share_persistent_get_constructor;
curl_share_persistent_handlers.clone_obj=NULL;
curl_share_persistent_handlers.compare=zend_objects_not_comparable;
}