-
Notifications
You must be signed in to change notification settings - Fork 18
/
Curl_Easy_setopt.c
462 lines (419 loc) · 10.6 KB
/
Curl_Easy_setopt.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* vim: ts=4:sw=4:ft=xs:fdm=marker
*
* Copyright 2011-2015 (C) Przemyslaw Iskra <sparky at pld-linux.org>
*
* Loosely based on code by Cris Bailiff <c.bailiff+curl at devsecure.com>,
* and subsequent fixes by other contributors.
*/
static void
perl_curl_easy_setopt_long( pTHX_ perl_curl_easy_t *easy, long option,
SV *value )
{
CURLcode ret = CURLE_OK;
long value_num = 0;
if ( SvOK( value ) )
value_num = (long) SvIV( value );
ret = curl_easy_setopt( easy->handle, option, value_num );
EASY_DIE( ret );
}
static void
perl_curl_easy_setopt_function( pTHX_ perl_curl_easy_t *easy, long option,
SV *value )
{
int cbnum = CB_EASY_LAST;
int dataopt = 0;
void *funcptr = NULL;
switch ( option ) {
case CURLOPT_WRITEFUNCTION:
/* function registered already */
cbnum = CB_EASY_WRITE;
break;
case CURLOPT_READFUNCTION:
/* function registered already */
cbnum = CB_EASY_READ;
break;
case CURLOPT_HEADERFUNCTION:
funcptr = cb_easy_header;
dataopt = CURLOPT_WRITEHEADER;
cbnum = CB_EASY_HEADER;
break;
case CURLOPT_PROGRESSFUNCTION:
funcptr = cb_easy_progress;
dataopt = CURLOPT_PROGRESSDATA;
cbnum = CB_EASY_PROGRESS;
break;
#ifdef CURLOPT_XFERINFODATA
# ifdef CURLOPT_XFERINFOFUNCTION
case CURLOPT_XFERINFOFUNCTION:
funcptr = cb_easy_xferinfo;
dataopt = CURLOPT_XFERINFODATA;
cbnum = CB_EASY_XFERINFO;
break;
# endif
#endif
case CURLOPT_DEBUGFUNCTION:
funcptr = cb_easy_debug;
dataopt = CURLOPT_DEBUGDATA;
cbnum = CB_EASY_DEBUG;
break;
case CURLOPT_IOCTLFUNCTION:
funcptr = cb_easy_ioctl;
dataopt = CURLOPT_IOCTLDATA;
cbnum = CB_EASY_IOCTL;
break;
#ifdef CURLOPT_SEEKDATA
# ifdef CURLOPT_SEEKFUNCTION
case CURLOPT_SEEKFUNCTION:
funcptr = cb_easy_seek;
dataopt = CURLOPT_SEEKDATA;
cbnum = CB_EASY_SEEK;
break;
# endif
#endif
#ifdef CURLOPT_SOCKOPTDATA
# ifdef CURLOPT_SOCKOPTFUNCTION
case CURLOPT_SOCKOPTFUNCTION:
funcptr = cb_easy_sockopt;
dataopt = CURLOPT_SOCKOPTDATA;
cbnum = CB_EASY_SOCKOPT;
break;
# endif
#endif
#ifdef CURLOPT_OPENSOCKETDATA
# ifdef CURLOPT_OPENSOCKETFUNCTION
case CURLOPT_OPENSOCKETFUNCTION:
funcptr = cb_easy_opensocket;
dataopt = CURLOPT_OPENSOCKETDATA;
cbnum = CB_EASY_OPENSOCKET;
break;
# endif
#endif
#ifdef CURLOPT_CLOSESOCKETDATA
# ifdef CURLOPT_CLOSESOCKETFUNCTION
case CURLOPT_CLOSESOCKETFUNCTION:
funcptr = cb_easy_closesocket;
dataopt = CURLOPT_CLOSESOCKETDATA;
cbnum = CB_EASY_CLOSESOCKET;
break;
# endif
#endif
#ifdef CURLOPT_INTERLEAVEDATA
# ifdef CURLOPT_INTERLEAVEFUNCTION
case CURLOPT_INTERLEAVEFUNCTION:
funcptr = cb_easy_interleave;
dataopt = CURLOPT_INTERLEAVEDATA;
cbnum = CB_EASY_INTERLEAVE;
break;
# endif
#endif
#ifdef CURLOPT_CHUNK_DATA
# ifdef CURLOPT_CHUNK_BGN_FUNCTION
case CURLOPT_CHUNK_BGN_FUNCTION:
funcptr = cb_easy_chunk_bgn;
dataopt = CURLOPT_CHUNK_DATA;
cbnum = CB_EASY_CHUNK_BGN;
break;
# endif
# ifdef CURLOPT_CHUNK_END_FUNCTION
case CURLOPT_CHUNK_END_FUNCTION:
funcptr = cb_easy_chunk_end;
dataopt = CURLOPT_CHUNK_DATA;
cbnum = CB_EASY_CHUNK_END;
break;
# endif
#endif
#ifdef CURLOPT_FNMATCH_DATA
# ifdef CURLOPT_FNMATCH_FUNCTION
case CURLOPT_FNMATCH_FUNCTION:
funcptr = cb_easy_fnmatch;
dataopt = CURLOPT_FNMATCH_DATA;
cbnum = CB_EASY_FNMATCH;
break;
# endif
#endif
#ifdef CURLOPT_SSH_KEYDATA
# ifdef CURLOPT_SSH_KEYFUNCTION
case CURLOPT_SSH_KEYFUNCTION:
funcptr = cb_easy_sshkey;
dataopt = CURLOPT_SSH_KEYDATA;
cbnum = CB_EASY_SSHKEY;
break;
# endif
#endif
default:
croak( "unrecognized function option %ld", option );
}
if ( cbnum != CB_EASY_LAST )
SvREPLACE( easy->cb[ cbnum ].func, value );
if ( dataopt ) {
CURLcode ret1, ret2;
ret1 = curl_easy_setopt( easy->handle, option,
SvOK( value ) ? funcptr : NULL );
ret2 = curl_easy_setopt( easy->handle, dataopt,
SvOK( value ) ? easy : NULL );
EASY_DIE( ret1 ? ret1 : ret2 );
}
}
static long
perl_curl_easy_setopt_functiondata( pTHX_ perl_curl_easy_t *easy, long option,
SV *value )
{
int cbnum = CB_EASY_LAST;
CURLcode ret = CURLE_OK;
switch ( option ) {
case CURLOPT_FILE:
cbnum = CB_EASY_WRITE;
break;
case CURLOPT_INFILE:
cbnum = CB_EASY_READ;
break;
case CURLOPT_WRITEHEADER:
/* cb_easy_header has default writer function,
* but no default destination */
{
CURLcode ret2;
ret = curl_easy_setopt( easy->handle, CURLOPT_HEADERFUNCTION,
SvOK( value ) ? cb_easy_header : NULL );
ret2 = curl_easy_setopt( easy->handle, option,
SvOK( value ) ? easy : NULL );
if ( ret == CURLE_OK )
ret = ret2;
}
cbnum = CB_EASY_HEADER;
break;
case CURLOPT_PROGRESSDATA:
cbnum = CB_EASY_PROGRESS;
#ifdef CURLOPT_XFERINFODATA
/* duplicate data for CB_EASY_XFERINFO since CURLOPT_XFERINFODATA is an alias for CURLOPT_PROGRESSDATA */
SvREPLACE( easy->cb[ CB_EASY_XFERINFO ].data, value );
#endif
break;
case CURLOPT_DEBUGDATA:
cbnum = CB_EASY_DEBUG;
break;
case CURLOPT_IOCTLDATA:
cbnum = CB_EASY_IOCTL;
break;
#ifdef CURLOPT_SEEKDATA
case CURLOPT_SEEKDATA:
cbnum = CB_EASY_SEEK;
break;
#endif
#ifdef CURLOPT_SOCKOPTDATA
case CURLOPT_SOCKOPTDATA:
cbnum = CB_EASY_SOCKOPT;
break;
#endif
#ifdef CURLOPT_OPENSOCKETDATA
case CURLOPT_OPENSOCKETDATA:
cbnum = CB_EASY_OPENSOCKET;
break;
#endif
#ifdef CURLOPT_CLOSESOCKETDATA
case CURLOPT_CLOSESOCKETDATA:
cbnum = CB_EASY_CLOSESOCKET;
break;
#endif
#ifdef CURLOPT_INTERLEAVEDATA
case CURLOPT_INTERLEAVEDATA:
# ifdef CURLOPT_INTERLEAVEFUNCTION
/* cb_easy_interleave has default writer function,
* but no default destination */
{
CURLcode ret2;
ret = curl_easy_setopt( easy->handle, CURLOPT_INTERLEAVEFUNCTION,
SvOK( value ) ? cb_easy_interleave : NULL );
ret2 = curl_easy_setopt( easy->handle, option,
SvOK( value ) ? easy : NULL );
if ( ret == CURLE_OK )
ret = ret2;
}
# endif
cbnum = CB_EASY_INTERLEAVE;
break;
#endif
#ifdef CURLOPT_CHUNK_DATA
case CURLOPT_CHUNK_DATA:
cbnum = CB_EASY_CHUNK_BGN;
SvREPLACE( easy->cb[ cbnum ].data, value );
cbnum = CB_EASY_CHUNK_END;
break;
#endif
#ifdef CURLOPT_FNMATCH_DATA
case CURLOPT_FNMATCH_DATA:
cbnum = CB_EASY_FNMATCH;
break;
#endif
#ifdef CURLOPT_SSH_KEYDATA
case CURLOPT_SSH_KEYDATA:
cbnum = CB_EASY_SSHKEY;
break;
#endif
default:
return -1;
}
SvREPLACE( easy->cb[ cbnum ].data, value );
return ret;
}
static void perl_curl_croak_invalid_option( pTHX_ long option )
{
croak( "invalid option %ld", option );
}
#ifdef CURLOPTTYPE_BLOB
static void
perl_curl_easy_setopt_blob( pTHX_ perl_curl_easy_t *easy, long option,
SV *value )
{
int ret = CURLE_OK;
struct curl_blob blob_struct = {
.flags = CURL_BLOB_COPY,
};
switch ( option ) {
#ifdef CURLOPT_CAINFO_BLOB
case CURLOPT_CAINFO_BLOB:
#endif
#ifdef CURLOPT_PROXY_CAINFO_BLOB
case CURLOPT_PROXY_CAINFO_BLOB:
#endif
#ifdef CURLOPT_SSLCERT_BLOB
case CURLOPT_SSLCERT_BLOB:
#endif
#ifdef CURLOPT_PROXY_SSLCERT_BLOB
case CURLOPT_PROXY_SSLCERT_BLOB:
#endif
#ifdef CURLOPT_SSLKEY_BLOB
case CURLOPT_SSLKEY_BLOB:
#endif
#ifdef CURLOPT_PROXY_SSLKEY_BLOB
case CURLOPT_PROXY_SSLKEY_BLOB:
#endif
#ifdef CURLOPT_ISSUERCERT_BLOB
case CURLOPT_ISSUERCERT_BLOB:
#endif
#ifdef CURLOPT_PROXY_ISSUERCERT_BLOB
case CURLOPT_PROXY_ISSUERCERT_BLOB:
#endif
case 0xffffffffL:
if (!SvOK(value)) croak("undef is nonsensical");
if (SvROK(value)) croak("Reference (%" SVf ") is nonsensical", value);
STRLEN bytelen;
const char* blob = SvPVbyte(value, bytelen);
blob_struct.data = (void*) blob;
blob_struct.len = bytelen;
break;
default:
perl_curl_croak_invalid_option(aTHX_ option);
}
ret = curl_easy_setopt( easy->handle, option, &blob_struct );
EASY_DIE(ret);
}
#endif
static void
perl_curl_easy_setopt_object( pTHX_ perl_curl_easy_t *easy, long option,
SV *value )
{
int ret = CURLE_OK;
char *pv;
/* is it a function data ? */
ret = perl_curl_easy_setopt_functiondata( aTHX_ easy, option, value );
if ( ret >= 0 ) {
EASY_DIE( ret );
return;
}
/* is it a slist option ? */
ret = perl_curl_easy_setoptslist( aTHX_ easy, option, value, 1 );
if ( ret >= 0 ) {
EASY_DIE( ret );
return;
}
switch ( option ) {
case CURLOPT_ERRORBUFFER:
croak( "CURLOPT_ERRORBUFFER is not supported, use $easy->error instead" );
return;
/* tell curl to redirect STDERR - value should be a glob */
case CURLOPT_STDERR:
ret = curl_easy_setopt( easy->handle, option,
PerlIO_findFILE( IoOFP( sv_2io( value ) ) ) );
return;
case CURLOPT_HTTPPOST:
if ( easy->form_sv ) {
curl_easy_setopt( easy->handle, option, NULL );
sv_2mortal( easy->form_sv );
easy->form_sv = NULL;
}
if ( SvOK( value ) ) {
perl_curl_form_t *form;
form = perl_curl_getptr_fatal( aTHX_ value, &perl_curl_form_vtbl,
"CURLOPT_HTTPPOST", "Net::Curl::Form" );
easy->form_sv = newSVsv( value );
ret = curl_easy_setopt( easy->handle, option, form->post );
EASY_DIE( ret );
}
return;
case CURLOPT_SHARE:
if ( easy->share_sv ) {
curl_easy_setopt( easy->handle, option, NULL );
sv_2mortal( easy->share_sv );
easy->share_sv = NULL;
}
if ( SvOK( value ) ) {
perl_curl_share_t *share;
share = perl_curl_getptr_fatal( aTHX_ value, &perl_curl_share_vtbl,
"CURLOPT_SHARE", "Net::Curl::Share" );
/* copy sv before setopt because this may trigger a callback */
easy->share_sv = newSVsv( value );
ret = curl_easy_setopt( easy->handle, option, share->handle );
EASY_DIE( ret );
}
return;
case CURLOPT_PRIVATE:
croak( "CURLOPT_PRIVATE is not available, use your base object" );
return;
};
/* default, assume it's data */
if ( SvOK( value ) ) {
char **ppv;
ppv = perl_curl_simplell_add( aTHX_ &easy->strings, option );
if ( ppv )
Safefree( *ppv );
#ifdef savesvpv
pv = *ppv = savesvpv( value );
#else
{
STRLEN len;
char *src = SvPV( value, len );
pv = *ppv = savepvn( src, len );
}
#endif
} else {
pv = perl_curl_simplell_del( aTHX_ &easy->strings, option );
if ( pv )
Safefree( pv );
pv = NULL;
}
ret = curl_easy_setopt( easy->handle, option, pv );
EASY_DIE( ret );
}
static void
perl_curl_easy_setopt_off_t( pTHX_ perl_curl_easy_t *easy, long option,
SV *value )
{
CURLcode ret = CURLE_OK;
/* this should be curl_off_t, but there is a bug in older curl - 7.18.2 */
long long v = 0;
if ( SvOK( value ) ) {
if ( SvIOK( value ) ) {
v = SvIV( value );
} else if ( looks_like_number( value ) ) {
#if IVSIZE == 8
v = SvIV( value );
#else
char *pv = SvPV_nolen( value );
char *pdummy;
v = strtoll( pv, &pdummy, 10 );
#endif
}
}
ret = curl_easy_setopt( easy->handle, option, v );
EASY_DIE( ret );
}