| 1 |
|
| 2 |
/* |
| 3 |
* Perl interface for libcurl. Check out the file README for more info. |
| 4 |
*/ |
| 5 |
|
| 6 |
/* |
| 7 |
* Copyright (C) 2000, 2001, 2002, 2005, 2008 Daniel Stenberg, Cris Bailiff, et al. |
| 8 |
* You may opt to use, copy, modify, merge, publish, distribute and/or |
| 9 |
* sell copies of the Software, and permit persons to whom the |
| 10 |
* Software is furnished to do so, under the terms of the MIT license. |
| 11 |
*/ |
| 12 |
#define PERL_NO_GET_CONTEXT |
| 13 |
#include "EXTERN.h" |
| 14 |
#include "perl.h" |
| 15 |
#include "XSUB.h" |
| 16 |
|
| 17 |
#include <curl/curl.h> |
| 18 |
#include <curl/easy.h> |
| 19 |
#include <curl/multi.h> |
| 20 |
|
| 21 |
#define header_callback_func writeheader_callback_func |
| 22 |
|
| 23 |
/* Do a favor for older perl versions */ |
| 24 |
#ifndef Newxz |
| 25 |
# define Newxz(v,n,t) Newz(0,v,n,t) |
| 26 |
#endif |
| 27 |
|
| 28 |
typedef enum { |
| 29 |
CALLBACK_WRITE = 0, |
| 30 |
CALLBACK_READ, |
| 31 |
CALLBACK_HEADER, |
| 32 |
CALLBACK_PROGRESS, |
| 33 |
CALLBACK_DEBUG, |
| 34 |
CALLBACK_LAST |
| 35 |
} perl_curl_easy_callback_code; |
| 36 |
|
| 37 |
typedef enum { |
| 38 |
SLIST_HTTPHEADER = 0, |
| 39 |
SLIST_QUOTE, |
| 40 |
SLIST_POSTQUOTE, |
| 41 |
#ifdef CURLOPT_RESOLVE |
| 42 |
SLIST_RESOLVE, |
| 43 |
#endif |
| 44 |
SLIST_LAST |
| 45 |
} perl_curl_easy_slist_code; |
| 46 |
|
| 47 |
|
| 48 |
typedef struct { |
| 49 |
/* The main curl handle */ |
| 50 |
struct CURL *curl; |
| 51 |
I32 *y; |
| 52 |
/* Lists that can be set via curl_easy_setopt() */ |
| 53 |
struct curl_slist *slist[SLIST_LAST]; |
| 54 |
SV *callback[CALLBACK_LAST]; |
| 55 |
SV *callback_ctx[CALLBACK_LAST]; |
| 56 |
|
| 57 |
/* copy of error buffer var for caller*/ |
| 58 |
char errbuf[CURL_ERROR_SIZE+1]; |
| 59 |
char *errbufvarname; |
| 60 |
I32 strings_index; |
| 61 |
char* strings[CURLOPTTYPE_FUNCTIONPOINT - 10000]; |
| 62 |
|
| 63 |
} perl_curl_easy; |
| 64 |
|
| 65 |
|
| 66 |
typedef struct { |
| 67 |
struct curl_httppost * post; |
| 68 |
struct curl_httppost * last; |
| 69 |
} perl_curl_form; |
| 70 |
|
| 71 |
|
| 72 |
typedef struct { |
| 73 |
#ifdef __CURL_MULTI_H |
| 74 |
struct CURLM *curlm; |
| 75 |
#else |
| 76 |
struct void *curlm; |
| 77 |
#endif |
| 78 |
} perl_curl_multi; |
| 79 |
|
| 80 |
typedef struct { |
| 81 |
struct CURLSH *curlsh; |
| 82 |
} perl_curl_share; |
| 83 |
|
| 84 |
|
| 85 |
/* switch from curl option codes to the relevant callback index */ |
| 86 |
static perl_curl_easy_callback_code |
| 87 |
callback_index(int option) |
| 88 |
{ |
| 89 |
switch(option) { |
| 90 |
case CURLOPT_WRITEFUNCTION: |
| 91 |
case CURLOPT_FILE: |
| 92 |
return CALLBACK_WRITE; |
| 93 |
break; |
| 94 |
|
| 95 |
case CURLOPT_READFUNCTION: |
| 96 |
case CURLOPT_INFILE: |
| 97 |
return CALLBACK_READ; |
| 98 |
break; |
| 99 |
|
| 100 |
case CURLOPT_HEADERFUNCTION: |
| 101 |
case CURLOPT_WRITEHEADER: |
| 102 |
return CALLBACK_HEADER; |
| 103 |
break; |
| 104 |
|
| 105 |
case CURLOPT_PROGRESSFUNCTION: |
| 106 |
case CURLOPT_PROGRESSDATA: |
| 107 |
return CALLBACK_PROGRESS; |
| 108 |
break; |
| 109 |
case CURLOPT_DEBUGFUNCTION: |
| 110 |
case CURLOPT_DEBUGDATA: |
| 111 |
return CALLBACK_DEBUG; |
| 112 |
break; |
| 113 |
} |
| 114 |
croak("Bad callback index requested\n"); |
| 115 |
return CALLBACK_LAST; |
| 116 |
} |
| 117 |
|
| 118 |
/* switch from curl slist names to an slist index */ |
| 119 |
static perl_curl_easy_slist_code |
| 120 |
slist_index(int option) |
| 121 |
{ |
| 122 |
switch(option) { |
| 123 |
case CURLOPT_HTTPHEADER: |
| 124 |
return SLIST_HTTPHEADER; |
| 125 |
break; |
| 126 |
case CURLOPT_QUOTE: |
| 127 |
return SLIST_QUOTE; |
| 128 |
break; |
| 129 |
case CURLOPT_POSTQUOTE: |
| 130 |
return SLIST_POSTQUOTE; |
| 131 |
break; |
| 132 |
#ifdef CURLOPT_RESOLVE |
| 133 |
case CURLOPT_RESOLVE: |
| 134 |
return SLIST_RESOLVE; |
| 135 |
break; |
| 136 |
#endif |
| 137 |
} |
| 138 |
croak("Bad slist index requested\n"); |
| 139 |
return SLIST_LAST; |
| 140 |
} |
| 141 |
|
| 142 |
static perl_curl_easy * perl_curl_easy_new() |
| 143 |
{ |
| 144 |
perl_curl_easy *self; |
| 145 |
Newz(1, self, 1, perl_curl_easy); |
| 146 |
self->curl=curl_easy_init(); |
| 147 |
return self; |
| 148 |
} |
| 149 |
|
| 150 |
static perl_curl_easy * perl_curl_easy_duphandle(perl_curl_easy *orig) |
| 151 |
{ |
| 152 |
perl_curl_easy *self; |
| 153 |
Newz(1, self, 1, perl_curl_easy); |
| 154 |
self->curl=curl_easy_duphandle(orig->curl); |
| 155 |
return self; |
| 156 |
} |
| 157 |
|
| 158 |
static void perl_curl_easy_delete(perl_curl_easy *self) |
| 159 |
{ |
| 160 |
dTHX; |
| 161 |
perl_curl_easy_slist_code index; |
| 162 |
perl_curl_easy_callback_code i; |
| 163 |
|
| 164 |
if (self->curl) |
| 165 |
curl_easy_cleanup(self->curl); |
| 166 |
|
| 167 |
*self->y = *self->y - 1; |
| 168 |
if (*self->y <= 0) { |
| 169 |
for (index=0;index<SLIST_LAST;index++) { |
| 170 |
if (self->slist[index]) curl_slist_free_all(self->slist[index]); |
| 171 |
}; |
| 172 |
Safefree(self->y); |
| 173 |
} |
| 174 |
for(i=0;i<CALLBACK_LAST;i++) { |
| 175 |
sv_2mortal(self->callback[i]); |
| 176 |
} |
| 177 |
for(i=0;i<CALLBACK_LAST;i++) { |
| 178 |
sv_2mortal(self->callback_ctx[i]); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
if (self->errbufvarname) |
| 183 |
free(self->errbufvarname); |
| 184 |
for (i=0;i<=self->strings_index;i++) { |
| 185 |
if (self->strings[i] != NULL) { |
| 186 |
char* ptr = self->strings[i]; |
| 187 |
Safefree(ptr); |
| 188 |
} |
| 189 |
} |
| 190 |
Safefree(self); |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
/* Register a callback function */ |
| 195 |
|
| 196 |
static void perl_curl_easy_register_callback(perl_curl_easy *self, SV **callback, SV *function) |
| 197 |
{ |
| 198 |
dTHX; |
| 199 |
if (function && SvOK(function)) { |
| 200 |
/* FIXME: need to check the ref-counts here */ |
| 201 |
if (*callback == NULL) { |
| 202 |
*callback = newSVsv(function); |
| 203 |
} else { |
| 204 |
SvSetSV(*callback, function); |
| 205 |
} |
| 206 |
} else { |
| 207 |
if (*callback != NULL) { |
| 208 |
sv_2mortal(*callback); |
| 209 |
*callback = NULL; |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
/* start of form functions - very un-finished! */ |
| 215 |
static perl_curl_form * perl_curl_form_new() |
| 216 |
{ |
| 217 |
perl_curl_form *self; |
| 218 |
Newz(1, self, 1, perl_curl_form); |
| 219 |
self->post=NULL; |
| 220 |
self->last=NULL; |
| 221 |
return self; |
| 222 |
} |
| 223 |
|
| 224 |
static void perl_curl_form_delete(perl_curl_form *self) |
| 225 |
{ |
| 226 |
if (self->post) { |
| 227 |
curl_formfree(self->post); |
| 228 |
} |
| 229 |
Safefree(self); |
| 230 |
} |
| 231 |
|
| 232 |
/* make a new multi */ |
| 233 |
static perl_curl_multi * perl_curl_multi_new() |
| 234 |
{ |
| 235 |
perl_curl_multi *self; |
| 236 |
Newz(1, self, 1, perl_curl_multi); |
| 237 |
#ifdef __CURL_MULTI_H |
| 238 |
self->curlm=curl_multi_init(); |
| 239 |
#else |
| 240 |
croak("curl version too old to support curl_multi_init()"); |
| 241 |
#endif |
| 242 |
return self; |
| 243 |
} |
| 244 |
|
| 245 |
/* delete the multi */ |
| 246 |
static void perl_curl_multi_delete(perl_curl_multi *self) |
| 247 |
{ |
| 248 |
#ifdef __CURL_MULTI_H |
| 249 |
if (self->curlm) |
| 250 |
curl_multi_cleanup(self->curlm); |
| 251 |
Safefree(self); |
| 252 |
#endif |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
/* make a new share */ |
| 257 |
static perl_curl_share * perl_curl_share_new() |
| 258 |
{ |
| 259 |
perl_curl_share *self; |
| 260 |
Newz(1, self, 1, perl_curl_share); |
| 261 |
self->curlsh=curl_share_init(); |
| 262 |
return self; |
| 263 |
} |
| 264 |
|
| 265 |
/* delete the share */ |
| 266 |
static void perl_curl_share_delete(perl_curl_share *self) |
| 267 |
{ |
| 268 |
if (self->curlsh) |
| 269 |
curl_share_cleanup(self->curlsh); |
| 270 |
Safefree(self); |
| 271 |
} |
| 272 |
|
| 273 |
static size_t |
| 274 |
write_to_ctx(pTHX_ SV* const call_ctx, const char* const ptr, size_t const n) { |
| 275 |
PerlIO *handle; |
| 276 |
SV* out_str; |
| 277 |
if (call_ctx) { /* a GLOB or a SCALAR ref */ |
| 278 |
if(SvROK(call_ctx) && SvTYPE(SvRV(call_ctx)) <= SVt_PVMG) { |
| 279 |
/* write to a scalar ref */ |
| 280 |
out_str = SvRV(call_ctx); |
| 281 |
if (SvOK(out_str)) { |
| 282 |
sv_catpvn(out_str, ptr, n); |
| 283 |
} else { |
| 284 |
sv_setpvn(out_str, ptr, n); |
| 285 |
} |
| 286 |
return n; |
| 287 |
} |
| 288 |
else { |
| 289 |
/* write to a filehandle */ |
| 290 |
handle = IoOFP(sv_2io(call_ctx)); |
| 291 |
} |
| 292 |
} else { /* punt to stdout */ |
| 293 |
handle = PerlIO_stdout(); |
| 294 |
} |
| 295 |
return PerlIO_write(handle, ptr, n); |
| 296 |
} |
| 297 |
|
| 298 |
/* generic fwrite callback, which decides which callback to call */ |
| 299 |
static size_t |
| 300 |
fwrite_wrapper ( |
| 301 |
const void *ptr, |
| 302 |
size_t size, |
| 303 |
size_t nmemb, |
| 304 |
perl_curl_easy *self, |
| 305 |
void *call_function, |
| 306 |
void *call_ctx) |
| 307 |
{ |
| 308 |
dTHX; |
| 309 |
if (call_function) { /* We are doing a callback to perl */ |
| 310 |
dSP; |
| 311 |
int count, status; |
| 312 |
|
| 313 |
ENTER; |
| 314 |
SAVETMPS; |
| 315 |
|
| 316 |
PUSHMARK(SP); |
| 317 |
|
| 318 |
if (ptr) { |
| 319 |
XPUSHs(sv_2mortal(newSVpvn((char *)ptr, (STRLEN)(size * nmemb)))); |
| 320 |
} else { /* just in case */ |
| 321 |
XPUSHs(&PL_sv_undef); |
| 322 |
} |
| 323 |
if (call_ctx) { |
| 324 |
XPUSHs(sv_2mortal(newSVsv(call_ctx))); |
| 325 |
} else { /* should be a stdio glob ? */ |
| 326 |
XPUSHs(&PL_sv_undef); |
| 327 |
} |
| 328 |
|
| 329 |
PUTBACK; |
| 330 |
count = perl_call_sv((SV *) call_function, G_SCALAR); |
| 331 |
SPAGAIN; |
| 332 |
|
| 333 |
if (count != 1) |
| 334 |
croak("callback for CURLOPT_WRITEFUNCTION didn't return a status\n"); |
| 335 |
|
| 336 |
status = POPi; |
| 337 |
|
| 338 |
PUTBACK; |
| 339 |
FREETMPS; |
| 340 |
LEAVE; |
| 341 |
return status; |
| 342 |
|
| 343 |
} else { |
| 344 |
return write_to_ctx(aTHX_ call_ctx, ptr, size * nmemb); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
/* debug fwrite callback */ |
| 349 |
static size_t |
| 350 |
fwrite_wrapper2 ( |
| 351 |
const void *ptr, |
| 352 |
size_t size, |
| 353 |
perl_curl_easy *self, |
| 354 |
void *call_function, |
| 355 |
void *call_ctx, |
| 356 |
int curl_infotype) |
| 357 |
{ |
| 358 |
dTHX; |
| 359 |
dSP; |
| 360 |
|
| 361 |
if (call_function) { /* We are doing a callback to perl */ |
| 362 |
int count, status; |
| 363 |
SV *sv; |
| 364 |
|
| 365 |
ENTER; |
| 366 |
SAVETMPS; |
| 367 |
|
| 368 |
PUSHMARK(SP); |
| 369 |
|
| 370 |
if (ptr) { |
| 371 |
XPUSHs(sv_2mortal(newSVpvn((char *)ptr, (STRLEN)(size * sizeof(char))))); |
| 372 |
} else { /* just in case */ |
| 373 |
XPUSHs(&PL_sv_undef); |
| 374 |
} |
| 375 |
|
| 376 |
if (call_ctx) { |
| 377 |
XPUSHs(sv_2mortal(newSVsv(call_ctx))); |
| 378 |
} else { /* should be a stdio glob ? */ |
| 379 |
XPUSHs(&PL_sv_undef); |
| 380 |
} |
| 381 |
|
| 382 |
XPUSHs(sv_2mortal(newSViv(curl_infotype))); |
| 383 |
|
| 384 |
PUTBACK; |
| 385 |
count = perl_call_sv((SV *) call_function, G_SCALAR); |
| 386 |
SPAGAIN; |
| 387 |
|
| 388 |
if (count != 1) |
| 389 |
croak("callback for CURLOPT_*FUNCTION didn't return a status\n"); |
| 390 |
|
| 391 |
status = POPi; |
| 392 |
|
| 393 |
PUTBACK; |
| 394 |
FREETMPS; |
| 395 |
LEAVE; |
| 396 |
return status; |
| 397 |
|
| 398 |
} else { |
| 399 |
return write_to_ctx(aTHX_ call_ctx, ptr, size * sizeof(char)); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
/* Write callback for calling a perl callback */ |
| 404 |
static size_t |
| 405 |
write_callback_func(const void *ptr, size_t size, size_t nmemb, void *stream) |
| 406 |
{ |
| 407 |
perl_curl_easy *self; |
| 408 |
self=(perl_curl_easy *)stream; |
| 409 |
return fwrite_wrapper(ptr,size,nmemb,self, |
| 410 |
self->callback[CALLBACK_WRITE],self->callback_ctx[CALLBACK_WRITE]); |
| 411 |
} |
| 412 |
|
| 413 |
/* header callback for calling a perl callback */ |
| 414 |
static size_t |
| 415 |
writeheader_callback_func(const void *ptr, size_t size, size_t nmemb, void *stream) |
| 416 |
{ |
| 417 |
perl_curl_easy *self; |
| 418 |
self=(perl_curl_easy *)stream; |
| 419 |
|
| 420 |
return fwrite_wrapper(ptr,size,nmemb,self, |
| 421 |
self->callback[CALLBACK_HEADER],self->callback_ctx[CALLBACK_HEADER]); |
| 422 |
} |
| 423 |
|
| 424 |
/* debug callback for calling a perl callback */ |
| 425 |
static size_t |
| 426 |
debug_callback_func(CURL* handle, int curl_infotype, const void *ptr, size_t size, void *stream) |
| 427 |
{ |
| 428 |
perl_curl_easy *self; |
| 429 |
self=(perl_curl_easy *)stream; |
| 430 |
|
| 431 |
return fwrite_wrapper2(ptr,size,self, |
| 432 |
self->callback[CALLBACK_DEBUG],self->callback_ctx[CALLBACK_DEBUG],curl_infotype); |
| 433 |
} |
| 434 |
|
| 435 |
/* read callback for calling a perl callback */ |
| 436 |
static size_t |
| 437 |
read_callback_func( void *ptr, size_t size, size_t nmemb, void *stream) |
| 438 |
{ |
| 439 |
dTHX; |
| 440 |
dSP ; |
| 441 |
|
| 442 |
size_t maxlen; |
| 443 |
perl_curl_easy *self; |
| 444 |
self=(perl_curl_easy *)stream; |
| 445 |
|
| 446 |
maxlen = size*nmemb; |
| 447 |
|
| 448 |
if (self->callback[CALLBACK_READ]) { /* We are doing a callback to perl */ |
| 449 |
char *data; |
| 450 |
int count; |
| 451 |
SV *sv; |
| 452 |
STRLEN len; |
| 453 |
|
| 454 |
ENTER ; |
| 455 |
SAVETMPS ; |
| 456 |
|
| 457 |
PUSHMARK(SP) ; |
| 458 |
|
| 459 |
if (self->callback_ctx[CALLBACK_READ]) { |
| 460 |
sv = self->callback_ctx[CALLBACK_READ]; |
| 461 |
} else { |
| 462 |
sv = &PL_sv_undef; |
| 463 |
} |
| 464 |
|
| 465 |
XPUSHs(sv_2mortal(newSViv(maxlen))); |
| 466 |
XPUSHs(sv_2mortal(newSVsv(sv))); |
| 467 |
|
| 468 |
PUTBACK ; |
| 469 |
count = perl_call_sv(self->callback[CALLBACK_READ], G_SCALAR); |
| 470 |
SPAGAIN; |
| 471 |
|
| 472 |
if (count != 1) |
| 473 |
croak("callback for CURLOPT_READFUNCTION didn't return any data\n"); |
| 474 |
|
| 475 |
sv = POPs; |
| 476 |
data = SvPV(sv,len); |
| 477 |
|
| 478 |
/* only allowed to return the number of bytes asked for */ |
| 479 |
len = (len<maxlen ? len : maxlen); |
| 480 |
/* memcpy(ptr,data,(size_t)len); */ |
| 481 |
Copy(data,ptr,len,char); |
| 482 |
|
| 483 |
PUTBACK ; |
| 484 |
FREETMPS ; |
| 485 |
LEAVE ; |
| 486 |
return (size_t) (len/size); |
| 487 |
|
| 488 |
} else { |
| 489 |
/* read input directly */ |
| 490 |
PerlIO *f; |
| 491 |
if (self->callback_ctx[CALLBACK_READ]) { /* hope its a GLOB! */ |
| 492 |
f = IoIFP(sv_2io(self->callback_ctx[CALLBACK_READ])); |
| 493 |
} else { /* punt to stdin */ |
| 494 |
f = PerlIO_stdin(); |
| 495 |
} |
| 496 |
return PerlIO_read(f,ptr,maxlen); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
/* Progress callback for calling a perl callback */ |
| 501 |
|
| 502 |
static int progress_callback_func(void *clientp, double dltotal, double dlnow, |
| 503 |
double ultotal, double ulnow) |
| 504 |
{ |
| 505 |
dTHX; |
| 506 |
dSP; |
| 507 |
|
| 508 |
int count; |
| 509 |
perl_curl_easy *self; |
| 510 |
self=(perl_curl_easy *)clientp; |
| 511 |
|
| 512 |
ENTER; |
| 513 |
SAVETMPS; |
| 514 |
PUSHMARK(sp); |
| 515 |
if (self->callback_ctx[CALLBACK_PROGRESS]) { |
| 516 |
XPUSHs(sv_2mortal(newSVsv(self->callback_ctx[CALLBACK_PROGRESS]))); |
| 517 |
} else { |
| 518 |
XPUSHs(&PL_sv_undef); |
| 519 |
} |
| 520 |
XPUSHs(sv_2mortal(newSVnv(dltotal))); |
| 521 |
XPUSHs(sv_2mortal(newSVnv(dlnow))); |
| 522 |
XPUSHs(sv_2mortal(newSVnv(ultotal))); |
| 523 |
XPUSHs(sv_2mortal(newSVnv(ulnow))); |
| 524 |
|
| 525 |
PUTBACK; |
| 526 |
count = perl_call_sv(self->callback[CALLBACK_PROGRESS], G_SCALAR); |
| 527 |
SPAGAIN; |
| 528 |
|
| 529 |
if (count != 1) |
| 530 |
croak("callback for CURLOPT_PROGRESSFUNCTION didn't return 1\n"); |
| 531 |
|
| 532 |
count = POPi; |
| 533 |
|
| 534 |
PUTBACK; |
| 535 |
FREETMPS; |
| 536 |
LEAVE; |
| 537 |
return count; |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
#if 0 |
| 543 |
/* awaiting closepolicy prototype */ |
| 544 |
int |
| 545 |
closepolicy_callback_func(void *clientp) |
| 546 |
{ |
| 547 |
dSP; |
| 548 |
int argc, status; |
| 549 |
SV *pl_status; |
| 550 |
|
| 551 |
ENTER; |
| 552 |
SAVETMPS; |
| 553 |
|
| 554 |
PUSHMARK(SP); |
| 555 |
PUTBACK; |
| 556 |
|
| 557 |
argc = perl_call_sv(closepolicy_callback, G_SCALAR); |
| 558 |
SPAGAIN; |
| 559 |
|
| 560 |
if (argc != 1) { |
| 561 |
croak("Unexpected number of arguments returned from closefunction callback\n"); |
| 562 |
} |
| 563 |
pl_status = POPs; |
| 564 |
status = SvTRUE(pl_status) ? 0 : 1; |
| 565 |
|
| 566 |
PUTBACK; |
| 567 |
FREETMPS; |
| 568 |
LEAVE; |
| 569 |
|
| 570 |
return status; |
| 571 |
} |
| 572 |
#endif |
| 573 |
|
| 574 |
#include "curlopt-constants.c" |
| 575 |
|
| 576 |
typedef perl_curl_easy * WWW__Curl__Easy; |
| 577 |
|
| 578 |
typedef perl_curl_form * WWW__Curl__Form; |
| 579 |
|
| 580 |
typedef perl_curl_multi * WWW__Curl__Multi; |
| 581 |
|
| 582 |
typedef perl_curl_share * WWW__Curl__Share; |
| 583 |
|
| 584 |
MODULE = WWW::Curl PACKAGE = WWW::Curl PREFIX = curl_ |
| 585 |
|
| 586 |
void |
| 587 |
curl__global_cleanup() |
| 588 |
CODE: |
| 589 |
curl_global_cleanup(); |
| 590 |
|
| 591 |
MODULE = WWW::Curl PACKAGE = WWW::Curl::Easy PREFIX = curl_easy_ |
| 592 |
|
| 593 |
BOOT: |
| 594 |
curl_global_init(CURL_GLOBAL_ALL); /* FIXME: does this need a mutex for ithreads? */ |
| 595 |
|
| 596 |
|
| 597 |
PROTOTYPES: ENABLE |
| 598 |
|
| 599 |
int |
| 600 |
constant(name) |
| 601 |
char * name |
| 602 |
|
| 603 |
|
| 604 |
void |
| 605 |
curl_easy_init(...) |
| 606 |
ALIAS: |
| 607 |
new = 1 |
| 608 |
PREINIT: |
| 609 |
perl_curl_easy *self; |
| 610 |
char *sclass = "WWW::Curl::Easy"; |
| 611 |
|
| 612 |
PPCODE: |
| 613 |
if (items>0 && !SvROK(ST(0))) { |
| 614 |
STRLEN dummy; |
| 615 |
sclass = SvPV(ST(0),dummy); |
| 616 |
} |
| 617 |
|
| 618 |
self=perl_curl_easy_new(); /* curl handle created by this point */ |
| 619 |
ST(0) = sv_newmortal(); |
| 620 |
sv_setref_pv(ST(0), sclass, (void*)self); |
| 621 |
SvREADONLY_on(SvRV(ST(0))); |
| 622 |
|
| 623 |
Newxz(self->y,1,I32); |
| 624 |
if (!self->y) { croak ("out of memory"); } |
| 625 |
(*self->y)++; |
| 626 |
/* configure curl to always callback to the XS interface layer */ |
| 627 |
curl_easy_setopt(self->curl, CURLOPT_WRITEFUNCTION, write_callback_func); |
| 628 |
curl_easy_setopt(self->curl, CURLOPT_READFUNCTION, read_callback_func); |
| 629 |
|
| 630 |
/* set our own object as the context for all curl callbacks */ |
| 631 |
curl_easy_setopt(self->curl, CURLOPT_FILE, self); |
| 632 |
curl_easy_setopt(self->curl, CURLOPT_INFILE, self); |
| 633 |
|
| 634 |
/* we always collect this, in case it's wanted */ |
| 635 |
curl_easy_setopt(self->curl, CURLOPT_ERRORBUFFER, self->errbuf); |
| 636 |
|
| 637 |
XSRETURN(1); |
| 638 |
|
| 639 |
void |
| 640 |
curl_easy_duphandle(self) |
| 641 |
WWW::Curl::Easy self |
| 642 |
PREINIT: |
| 643 |
perl_curl_easy *clone; |
| 644 |
char *sclass = "WWW::Curl::Easy"; |
| 645 |
perl_curl_easy_callback_code i; |
| 646 |
|
| 647 |
PPCODE: |
| 648 |
clone=perl_curl_easy_duphandle(self); |
| 649 |
clone->y = self->y; |
| 650 |
(*self->y)++; |
| 651 |
|
| 652 |
ST(0) = sv_newmortal(); |
| 653 |
sv_setref_pv(ST(0), sclass, (void*)clone); |
| 654 |
SvREADONLY_on(SvRV(ST(0))); |
| 655 |
|
| 656 |
/* configure curl to always callback to the XS interface layer */ |
| 657 |
|
| 658 |
curl_easy_setopt(clone->curl, CURLOPT_WRITEFUNCTION, write_callback_func); |
| 659 |
curl_easy_setopt(clone->curl, CURLOPT_READFUNCTION, read_callback_func); |
| 660 |
if (self->callback[callback_index(CURLOPT_HEADERFUNCTION)] || self->callback_ctx[callback_index(CURLOPT_WRITEHEADER)]) { |
| 661 |
curl_easy_setopt(clone->curl, CURLOPT_HEADERFUNCTION, header_callback_func); |
| 662 |
curl_easy_setopt(clone->curl, CURLOPT_WRITEHEADER, clone); |
| 663 |
} |
| 664 |
|
| 665 |
if (self->callback[callback_index(CURLOPT_PROGRESSFUNCTION)] || self->callback_ctx[callback_index(CURLOPT_PROGRESSDATA)]) { |
| 666 |
curl_easy_setopt(clone->curl, CURLOPT_PROGRESSFUNCTION, progress_callback_func); |
| 667 |
curl_easy_setopt(clone->curl, CURLOPT_PROGRESSDATA, clone); |
| 668 |
} |
| 669 |
|
| 670 |
if (self->callback[callback_index(CURLOPT_DEBUGFUNCTION)] || self->callback_ctx[callback_index(CURLOPT_DEBUGDATA)]) { |
| 671 |
curl_easy_setopt(clone->curl, CURLOPT_DEBUGFUNCTION, debug_callback_func); |
| 672 |
curl_easy_setopt(clone->curl, CURLOPT_DEBUGDATA, clone); |
| 673 |
} |
| 674 |
|
| 675 |
/* set our own object as the context for all curl callbacks */ |
| 676 |
curl_easy_setopt(clone->curl, CURLOPT_FILE, clone); |
| 677 |
curl_easy_setopt(clone->curl, CURLOPT_INFILE, clone); |
| 678 |
curl_easy_setopt(clone->curl, CURLOPT_ERRORBUFFER, clone->errbuf); |
| 679 |
|
| 680 |
for(i=0;i<CALLBACK_LAST;i++) { |
| 681 |
perl_curl_easy_register_callback(clone,&(clone->callback[i]), self->callback[i]); |
| 682 |
perl_curl_easy_register_callback(clone,&(clone->callback_ctx[i]), self->callback_ctx[i]); |
| 683 |
}; |
| 684 |
|
| 685 |
for (i=0;i<=self->strings_index;i++) { |
| 686 |
if (self->strings[i] != NULL) { |
| 687 |
clone->strings[i] = savepv(self->strings[i]); |
| 688 |
curl_easy_setopt(clone->curl, 10000 + i, clone->strings[i]); |
| 689 |
} |
| 690 |
} |
| 691 |
clone->strings_index = self->strings_index; |
| 692 |
XSRETURN(1); |
| 693 |
|
| 694 |
char * |
| 695 |
curl_easy_version(...) |
| 696 |
CODE: |
| 697 |
RETVAL=curl_version(); |
| 698 |
OUTPUT: |
| 699 |
RETVAL |
| 700 |
|
| 701 |
int |
| 702 |
curl_easy_setopt(self, option, value, push=0) |
| 703 |
WWW::Curl::Easy self |
| 704 |
int option |
| 705 |
SV * value |
| 706 |
int push |
| 707 |
CODE: |
| 708 |
RETVAL=CURLE_OK; |
| 709 |
switch(option) { |
| 710 |
/* SV * to user contexts for callbacks - any SV (glob,scalar,ref) */ |
| 711 |
case CURLOPT_FILE: |
| 712 |
case CURLOPT_INFILE: |
| 713 |
perl_curl_easy_register_callback(self, |
| 714 |
&(self->callback_ctx[callback_index(option)]), value); |
| 715 |
break; |
| 716 |
case CURLOPT_WRITEHEADER: |
| 717 |
curl_easy_setopt(self->curl, CURLOPT_HEADERFUNCTION, SvOK(value) ? header_callback_func : NULL); |
| 718 |
curl_easy_setopt(self->curl, option, SvOK(value) ? self : NULL); |
| 719 |
perl_curl_easy_register_callback(self,&(self->callback_ctx[callback_index(option)]),value); |
| 720 |
break; |
| 721 |
case CURLOPT_PROGRESSDATA: |
| 722 |
curl_easy_setopt(self->curl, CURLOPT_PROGRESSFUNCTION, SvOK(value) ? progress_callback_func : NULL); |
| 723 |
curl_easy_setopt(self->curl, option, SvOK(value) ? self : NULL); |
| 724 |
perl_curl_easy_register_callback(self,&(self->callback_ctx[callback_index(option)]), value); |
| 725 |
break; |
| 726 |
case CURLOPT_DEBUGDATA: |
| 727 |
curl_easy_setopt(self->curl, CURLOPT_DEBUGFUNCTION, SvOK(value) ? debug_callback_func : NULL); |
| 728 |
curl_easy_setopt(self->curl, option, SvOK(value) ? self : NULL); |
| 729 |
perl_curl_easy_register_callback(self,&(self->callback_ctx[callback_index(option)]), value); |
| 730 |
break; |
| 731 |
|
| 732 |
/* SV * to a subroutine ref */ |
| 733 |
case CURLOPT_WRITEFUNCTION: |
| 734 |
case CURLOPT_READFUNCTION: |
| 735 |
perl_curl_easy_register_callback(self,&(self->callback[callback_index(option)]), value); |
| 736 |
break; |
| 737 |
case CURLOPT_HEADERFUNCTION: |
| 738 |
curl_easy_setopt(self->curl, option, SvOK(value) ? header_callback_func : NULL); |
| 739 |
curl_easy_setopt(self->curl, CURLOPT_WRITEHEADER, SvOK(value) ? self : NULL); |
| 740 |
perl_curl_easy_register_callback(self,&(self->callback[callback_index(option)]), value); |
| 741 |
break; |
| 742 |
case CURLOPT_PROGRESSFUNCTION: |
| 743 |
curl_easy_setopt(self->curl, option, SvOK(value) ? progress_callback_func : NULL); |
| 744 |
curl_easy_setopt(self->curl, CURLOPT_PROGRESSDATA, SvOK(value) ? self : NULL); |
| 745 |
perl_curl_easy_register_callback(self,&(self->callback[callback_index(option)]), value); |
| 746 |
break; |
| 747 |
case CURLOPT_DEBUGFUNCTION: |
| 748 |
curl_easy_setopt(self->curl, option, SvOK(value) ? debug_callback_func : NULL); |
| 749 |
curl_easy_setopt(self->curl, CURLOPT_DEBUGDATA, SvOK(value) ? self : NULL); |
| 750 |
perl_curl_easy_register_callback(self,&(self->callback[callback_index(option)]), value); |
| 751 |
break; |
| 752 |
|
| 753 |
/* slist cases */ |
| 754 |
case CURLOPT_HTTPHEADER: |
| 755 |
case CURLOPT_QUOTE: |
| 756 |
case CURLOPT_POSTQUOTE: |
| 757 |
#ifdef CURLOPT_RESOLVE |
| 758 |
case CURLOPT_RESOLVE: |
| 759 |
#endif |
| 760 |
{ |
| 761 |
/* This is an option specifying a list, which we put in a curl_slist struct */ |
| 762 |
AV *array = (AV *)SvRV(value); |
| 763 |
struct curl_slist **slist = NULL; |
| 764 |
int last = av_len(array); |
| 765 |
int i; |
| 766 |
|
| 767 |
/* We have to find out which list to use... */ |
| 768 |
slist = &(self->slist[slist_index(option)]); |
| 769 |
|
| 770 |
/* free any previous list */ |
| 771 |
if (*slist && !push) { |
| 772 |
curl_slist_free_all(*slist); |
| 773 |
*slist=NULL; |
| 774 |
} |
| 775 |
/* copy perl values into this slist */ |
| 776 |
for (i=0;i<=last;i++) { |
| 777 |
SV **sv = av_fetch(array,i,0); |
| 778 |
STRLEN len = 0; |
| 779 |
char *string = SvPV(*sv, len); |
| 780 |
if (len == 0) /* FIXME: is this correct? */ |
| 781 |
break; |
| 782 |
*slist = curl_slist_append(*slist, string); |
| 783 |
} |
| 784 |
/* pass the list into curl_easy_setopt() */ |
| 785 |
RETVAL = curl_easy_setopt(self->curl, option, *slist); |
| 786 |
}; |
| 787 |
break; |
| 788 |
|
| 789 |
/* Pass in variable name for storing error messages. Yuck. */ |
| 790 |
case CURLOPT_ERRORBUFFER: |
| 791 |
{ |
| 792 |
STRLEN dummy; |
| 793 |
if (self->errbufvarname) |
| 794 |
free(self->errbufvarname); |
| 795 |
self->errbufvarname = strdup((char *)SvPV(value, dummy)); |
| 796 |
}; |
| 797 |
break; |
| 798 |
|
| 799 |
/* tell curl to redirect STDERR - value should be a glob */ |
| 800 |
case CURLOPT_STDERR: |
| 801 |
RETVAL = curl_easy_setopt(self->curl, option, IoOFP(sv_2io(value)) ); |
| 802 |
break; |
| 803 |
|
| 804 |
/* not working yet... */ |
| 805 |
case CURLOPT_HTTPPOST: |
| 806 |
if (sv_derived_from(value, "WWW::Curl::Form")) { |
| 807 |
WWW__Curl__Form wrapper; |
| 808 |
IV tmp = SvIV((SV*)SvRV(value)); |
| 809 |
wrapper = INT2PTR(WWW__Curl__Form,tmp); |
| 810 |
RETVAL = curl_easy_setopt(self->curl, option, wrapper->post); |
| 811 |
} else |
| 812 |
croak("value is not of type WWW::Curl::Form"); |
| 813 |
break; |
| 814 |
|
| 815 |
/* Curl share support from Anton Fedorov */ |
| 816 |
#if (LIBCURL_VERSION_NUM>=0x070a03) |
| 817 |
case CURLOPT_SHARE: |
| 818 |
if (sv_derived_from(value, "WWW::Curl::Share")) { |
| 819 |
WWW__Curl__Share wrapper; |
| 820 |
IV tmp = SvIV((SV*)SvRV(value)); |
| 821 |
wrapper = INT2PTR(WWW__Curl__Share,tmp); |
| 822 |
RETVAL = curl_easy_setopt(self->curl, option, wrapper->curlsh); |
| 823 |
} else |
| 824 |
croak("value is not of type WWW::Curl::Share"); |
| 825 |
break; |
| 826 |
#endif |
| 827 |
/* default cases */ |
| 828 |
default: |
| 829 |
if (option < CURLOPTTYPE_OBJECTPOINT) { /* A long (integer) value */ |
| 830 |
RETVAL = curl_easy_setopt(self->curl, option, (long)SvIV(value)); |
| 831 |
} |
| 832 |
else if (option < CURLOPTTYPE_FUNCTIONPOINT) { /* An objectpoint - string */ |
| 833 |
/* FIXME: Does curl really want NULL for empty strings? */ |
| 834 |
STRLEN dummy = 0; |
| 835 |
/* Pre 7.17.0, the strings aren't copied by libcurl.*/ |
| 836 |
char* pv = SvOK(value) ? SvPV(value, dummy) : ""; |
| 837 |
I32 len = (I32)dummy; |
| 838 |
pv = savepvn(pv, len); |
| 839 |
if (self->strings[option-10000] != NULL) Safefree(self->strings[option-10000]); |
| 840 |
self->strings[option-10000] = pv; |
| 841 |
if (self->strings_index < option - 10000) self->strings_index = option - 10000; |
| 842 |
RETVAL = curl_easy_setopt(self->curl, option, SvOK(value) ? pv : NULL); |
| 843 |
} |
| 844 |
#ifdef CURLOPTTYPE_OFF_T |
| 845 |
else if (option < CURLOPTTYPE_OFF_T) { /* A function - notreached? */ |
| 846 |
croak("Unknown curl option of type function"); |
| 847 |
} |
| 848 |
else { /* A LARGE file option using curl_off_t, handling larger than 32bit sizes without 64bit integer support */ |
| 849 |
if (SvOK(value) && looks_like_number(value)) { |
| 850 |
STRLEN dummy = 0; |
| 851 |
char* pv = SvPV(value, dummy); |
| 852 |
char* pdummy; |
| 853 |
RETVAL = curl_easy_setopt(self->curl, option, (curl_off_t) strtoll(pv,&pdummy,10)); |
| 854 |
} else { |
| 855 |
RETVAL = 0; |
| 856 |
} |
| 857 |
} |
| 858 |
#endif |
| 859 |
; |
| 860 |
break; |
| 861 |
}; |
| 862 |
OUTPUT: |
| 863 |
RETVAL |
| 864 |
|
| 865 |
int |
| 866 |
internal_setopt(self, option, value) |
| 867 |
WWW::Curl::Easy self |
| 868 |
int option |
| 869 |
int value |
| 870 |
CODE: |
| 871 |
croak("internal_setopt no longer supported - use a callback\n"); |
| 872 |
RETVAL = 0; |
| 873 |
OUTPUT: |
| 874 |
RETVAL |
| 875 |
|
| 876 |
int |
| 877 |
curl_easy_perform(self) |
| 878 |
WWW::Curl::Easy self |
| 879 |
CODE: |
| 880 |
/* perform the actual curl fetch */ |
| 881 |
RETVAL = curl_easy_perform(self->curl); |
| 882 |
|
| 883 |
if (RETVAL && self->errbufvarname) { |
| 884 |
/* If an error occurred and a varname for error messages has been |
| 885 |
specified, store the error message. */ |
| 886 |
SV *sv = perl_get_sv(self->errbufvarname, TRUE | GV_ADDMULTI); |
| 887 |
sv_setpv(sv, self->errbuf); |
| 888 |
} |
| 889 |
OUTPUT: |
| 890 |
RETVAL |
| 891 |
|
| 892 |
|
| 893 |
SV * |
| 894 |
curl_easy_getinfo(self, option, ... ) |
| 895 |
WWW::Curl::Easy self |
| 896 |
int option |
| 897 |
CODE: |
| 898 |
switch (option & CURLINFO_TYPEMASK) { |
| 899 |
case CURLINFO_STRING: |
| 900 |
{ |
| 901 |
char * vchar; |
| 902 |
curl_easy_getinfo(self->curl, option, &vchar); |
| 903 |
RETVAL = newSVpv(vchar,0); |
| 904 |
break; |
| 905 |
} |
| 906 |
case CURLINFO_LONG: |
| 907 |
{ |
| 908 |
long vlong; |
| 909 |
curl_easy_getinfo(self->curl, option, &vlong); |
| 910 |
RETVAL = newSViv(vlong); |
| 911 |
break; |
| 912 |
} |
| 913 |
case CURLINFO_DOUBLE: |
| 914 |
{ |
| 915 |
double vdouble; |
| 916 |
curl_easy_getinfo(self->curl, option, &vdouble); |
| 917 |
RETVAL = newSVnv(vdouble); |
| 918 |
break; |
| 919 |
} |
| 920 |
#ifdef CURLINFO_SLIST |
| 921 |
case CURLINFO_SLIST: |
| 922 |
{ |
| 923 |
struct curl_slist *vlist, *entry; |
| 924 |
AV *items = newAV(); |
| 925 |
curl_easy_getinfo(self->curl, option, &vlist); |
| 926 |
if (vlist != NULL) { |
| 927 |
entry = vlist; |
| 928 |
while (entry) { |
| 929 |
av_push(items, newSVpv(entry->data, 0)); |
| 930 |
entry = entry->next; |
| 931 |
} |
| 932 |
curl_slist_free_all(vlist); |
| 933 |
} |
| 934 |
RETVAL = newRV(sv_2mortal((SV *) items)); |
| 935 |
break; |
| 936 |
} |
| 937 |
#endif /* CURLINFO_SLIST */ |
| 938 |
default: { |
| 939 |
RETVAL = newSViv(CURLE_BAD_FUNCTION_ARGUMENT); |
| 940 |
break; |
| 941 |
} |
| 942 |
} |
| 943 |
if (items > 2) |
| 944 |
sv_setsv(ST(2),RETVAL); |
| 945 |
OUTPUT: |
| 946 |
RETVAL |
| 947 |
|
| 948 |
char * |
| 949 |
curl_easy_errbuf(self) |
| 950 |
WWW::Curl::Easy self |
| 951 |
CODE: |
| 952 |
RETVAL = self->errbuf; |
| 953 |
OUTPUT: |
| 954 |
RETVAL |
| 955 |
|
| 956 |
int |
| 957 |
curl_easy_cleanup(self) |
| 958 |
WWW::Curl::Easy self |
| 959 |
CODE: |
| 960 |
/* does nothing anymore - cleanup is automatic when a curl handle goes out of scope */ |
| 961 |
RETVAL = 0; |
| 962 |
OUTPUT: |
| 963 |
RETVAL |
| 964 |
|
| 965 |
void |
| 966 |
curl_easy_DESTROY(self) |
| 967 |
WWW::Curl::Easy self |
| 968 |
CODE: |
| 969 |
perl_curl_easy_delete(self); |
| 970 |
|
| 971 |
SV * |
| 972 |
curl_easy_strerror(self, errornum) |
| 973 |
WWW::Curl::Easy self |
| 974 |
int errornum |
| 975 |
CODE: |
| 976 |
{ |
| 977 |
#if (LIBCURL_VERSION_NUM>=0x070C00) |
| 978 |
const char * vchar = curl_easy_strerror(errornum); |
| 979 |
#else |
| 980 |
const char * vchar = "Unknown because curl_easy_strerror function not available}"; |
| 981 |
#endif |
| 982 |
RETVAL = newSVpv(vchar,0); |
| 983 |
} |
| 984 |
OUTPUT: |
| 985 |
RETVAL |
| 986 |
|
| 987 |
MODULE = WWW::Curl PACKAGE = WWW::Curl::Form PREFIX = curl_form_ |
| 988 |
|
| 989 |
int |
| 990 |
constant(name) |
| 991 |
char * name |
| 992 |
|
| 993 |
void |
| 994 |
curl_form_new(...) |
| 995 |
PREINIT: |
| 996 |
perl_curl_form *self; |
| 997 |
char *sclass = "WWW::Curl::Form"; |
| 998 |
PPCODE: |
| 999 |
if (items>0 && !SvROK(ST(0))) { |
| 1000 |
STRLEN dummy; |
| 1001 |
sclass = SvPV(ST(0),dummy); |
| 1002 |
} |
| 1003 |
|
| 1004 |
self=perl_curl_form_new(); |
| 1005 |
|
| 1006 |
ST(0) = sv_newmortal(); |
| 1007 |
sv_setref_pv(ST(0), sclass, (void*)self); |
| 1008 |
SvREADONLY_on(SvRV(ST(0))); |
| 1009 |
|
| 1010 |
XSRETURN(1); |
| 1011 |
|
| 1012 |
void |
| 1013 |
curl_form_formadd(self,name,value) |
| 1014 |
WWW::Curl::Form self |
| 1015 |
char *name |
| 1016 |
char *value |
| 1017 |
CODE: |
| 1018 |
curl_formadd(&(self->post),&(self->last), |
| 1019 |
CURLFORM_COPYNAME,name, |
| 1020 |
CURLFORM_COPYCONTENTS,value, |
| 1021 |
CURLFORM_END); |
| 1022 |
|
| 1023 |
void |
| 1024 |
curl_form_formaddfile(self,filename,description,type) |
| 1025 |
WWW::Curl::Form self |
| 1026 |
char *filename |
| 1027 |
char *description |
| 1028 |
char *type |
| 1029 |
CODE: |
| 1030 |
curl_formadd(&(self->post),&(self->last), |
| 1031 |
CURLFORM_FILE,filename, |
| 1032 |
CURLFORM_COPYNAME,description, |
| 1033 |
CURLFORM_CONTENTTYPE,type, |
| 1034 |
CURLFORM_END); |
| 1035 |
|
| 1036 |
void |
| 1037 |
curl_form_DESTROY(self) |
| 1038 |
WWW::Curl::Form self |
| 1039 |
CODE: |
| 1040 |
perl_curl_form_delete(self); |
| 1041 |
|
| 1042 |
MODULE = WWW::Curl PACKAGE = WWW::Curl::Multi PREFIX = curl_multi_ |
| 1043 |
|
| 1044 |
void |
| 1045 |
curl_multi_new(...) |
| 1046 |
PREINIT: |
| 1047 |
perl_curl_multi *self; |
| 1048 |
char *sclass = "WWW::Curl::Multi"; |
| 1049 |
PPCODE: |
| 1050 |
if (items>0 && !SvROK(ST(0))) { |
| 1051 |
STRLEN dummy; |
| 1052 |
sclass = SvPV(ST(0),dummy); |
| 1053 |
} |
| 1054 |
|
| 1055 |
self=perl_curl_multi_new(); |
| 1056 |
|
| 1057 |
ST(0) = sv_newmortal(); |
| 1058 |
sv_setref_pv(ST(0), sclass, (void*)self); |
| 1059 |
SvREADONLY_on(SvRV(ST(0))); |
| 1060 |
|
| 1061 |
XSRETURN(1); |
| 1062 |
|
| 1063 |
void |
| 1064 |
curl_multi_add_handle(curlm, curl) |
| 1065 |
WWW::Curl::Multi curlm |
| 1066 |
WWW::Curl::Easy curl |
| 1067 |
CODE: |
| 1068 |
#ifdef __CURL_MULTI_H |
| 1069 |
curl_multi_add_handle(curlm->curlm, curl->curl); |
| 1070 |
#endif |
| 1071 |
|
| 1072 |
void |
| 1073 |
curl_multi_remove_handle(curlm, curl) |
| 1074 |
WWW::Curl::Multi curlm |
| 1075 |
WWW::Curl::Easy curl |
| 1076 |
CODE: |
| 1077 |
#ifdef __CURL_MULTI_H |
| 1078 |
curl_multi_remove_handle(curlm->curlm, curl->curl); |
| 1079 |
#endif |
| 1080 |
|
| 1081 |
void |
| 1082 |
curl_multi_info_read(self) |
| 1083 |
WWW::Curl::Multi self |
| 1084 |
PREINIT: |
| 1085 |
CURL *easy = NULL; |
| 1086 |
CURLcode res; |
| 1087 |
char *stashid; |
| 1088 |
int queue; |
| 1089 |
CURLMsg *msg; |
| 1090 |
PPCODE: |
| 1091 |
while ((msg = curl_multi_info_read(self->curlm, &queue))) { |
| 1092 |
if (msg->msg == CURLMSG_DONE) { |
| 1093 |
easy=msg->easy_handle; |
| 1094 |
res=msg->data.result; |
| 1095 |
break; |
| 1096 |
} |
| 1097 |
}; |
| 1098 |
if (easy) { |
| 1099 |
curl_easy_getinfo(easy, CURLINFO_PRIVATE, &stashid); |
| 1100 |
curl_easy_setopt(easy, CURLINFO_PRIVATE, NULL); |
| 1101 |
curl_multi_remove_handle(self->curlm, easy); |
| 1102 |
XPUSHs(sv_2mortal(newSVpv(stashid,0))); |
| 1103 |
XPUSHs(sv_2mortal(newSViv(res))); |
| 1104 |
} else { |
| 1105 |
XSRETURN_EMPTY; |
| 1106 |
} |
| 1107 |
|
| 1108 |
SV * |
| 1109 |
curl_multi_fdset(self) |
| 1110 |
WWW::Curl::Multi self |
| 1111 |
PREINIT: |
| 1112 |
fd_set fdread; |
| 1113 |
fd_set fdwrite; |
| 1114 |
fd_set fdexcep; |
| 1115 |
int maxfd; |
| 1116 |
int i; |
| 1117 |
AV *readset; |
| 1118 |
AV *writeset; |
| 1119 |
AV *excepset; |
| 1120 |
PPCODE: |
| 1121 |
FD_ZERO(&fdread); |
| 1122 |
FD_ZERO(&fdwrite); |
| 1123 |
FD_ZERO(&fdexcep); |
| 1124 |
|
| 1125 |
readset = newAV(); |
| 1126 |
writeset = newAV(); |
| 1127 |
excepset = newAV(); |
| 1128 |
curl_multi_fdset(self->curlm, &fdread, &fdwrite, &fdexcep, &maxfd); |
| 1129 |
if ( maxfd != -1 ) { |
| 1130 |
for (i=0;i <= maxfd;i++) { |
| 1131 |
if (FD_ISSET(i, &fdread)) { |
| 1132 |
av_push(readset, newSViv(i)); |
| 1133 |
} |
| 1134 |
if (FD_ISSET(i, &fdwrite)) { |
| 1135 |
av_push(writeset, newSViv(i)); |
| 1136 |
} |
| 1137 |
if (FD_ISSET(i, &fdexcep)) { |
| 1138 |
av_push(excepset, newSViv(i)); |
| 1139 |
} |
| 1140 |
} |
| 1141 |
} |
| 1142 |
XPUSHs(sv_2mortal(newRV(sv_2mortal((SV *) readset)))); |
| 1143 |
XPUSHs(sv_2mortal(newRV(sv_2mortal((SV *) writeset)))); |
| 1144 |
XPUSHs(sv_2mortal(newRV(sv_2mortal((SV *) excepset)))); |
| 1145 |
|
| 1146 |
int |
| 1147 |
curl_multi_perform(self) |
| 1148 |
WWW::Curl::Multi self |
| 1149 |
PREINIT: |
| 1150 |
int remaining; |
| 1151 |
CODE: |
| 1152 |
#ifdef __CURL_MULTI_H |
| 1153 |
while(CURLM_CALL_MULTI_PERFORM == |
| 1154 |
curl_multi_perform(self->curlm, &remaining)); |
| 1155 |
RETVAL = remaining; |
| 1156 |
/* while(remaining) { |
| 1157 |
struct timeval timeout; |
| 1158 |
int rc; |
| 1159 |
fd_set fdread; |
| 1160 |
fd_set fdwrite; |
| 1161 |
fd_set fdexcep; |
| 1162 |
int maxfd; |
| 1163 |
FD_ZERO(&fdread); |
| 1164 |
FD_ZERO(&fdwrite); |
| 1165 |
FD_ZERO(&fdexcep); |
| 1166 |
timeout.tv_sec = 1; |
| 1167 |
timeout.tv_usec = 0; |
| 1168 |
curl_multi_fdset(self->curlm, &fdread, &fdwrite, &fdexcep, &maxfd); |
| 1169 |
rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); |
| 1170 |
switch(rc) { |
| 1171 |
case -1: |
| 1172 |
break; |
| 1173 |
default: |
| 1174 |
while(CURLM_CALL_MULTI_PERFORM == |
| 1175 |
curl_multi_perform(self->curlm, &remaining)); |
| 1176 |
break; |
| 1177 |
} |
| 1178 |
} */ |
| 1179 |
#endif |
| 1180 |
OUTPUT: |
| 1181 |
RETVAL |
| 1182 |
|
| 1183 |
void |
| 1184 |
curl_multi_DESTROY(self) |
| 1185 |
WWW::Curl::Multi self |
| 1186 |
CODE: |
| 1187 |
perl_curl_multi_delete(self); |
| 1188 |
|
| 1189 |
SV * |
| 1190 |
curl_multi_strerror(self, errornum) |
| 1191 |
WWW::Curl::Multi self |
| 1192 |
int errornum |
| 1193 |
CODE: |
| 1194 |
{ |
| 1195 |
#if (LIBCURL_VERSION_NUM>=0x070C00) |
| 1196 |
const char * vchar = curl_multi_strerror(errornum); |
| 1197 |
#else |
| 1198 |
const char * vchar = "Unknown because curl_multi_strerror function not available}"; |
| 1199 |
#endif |
| 1200 |
RETVAL = newSVpv(vchar,0); |
| 1201 |
} |
| 1202 |
OUTPUT: |
| 1203 |
RETVAL |
| 1204 |
|
| 1205 |
MODULE = WWW::Curl PACKAGE = WWW::Curl::Share PREFIX = curl_share_ |
| 1206 |
|
| 1207 |
PROTOTYPES: ENABLE |
| 1208 |
|
| 1209 |
int |
| 1210 |
constant(name) |
| 1211 |
char * name |
| 1212 |
|
| 1213 |
void |
| 1214 |
curl_share_new(...) |
| 1215 |
PREINIT: |
| 1216 |
perl_curl_share *self; |
| 1217 |
char *sclass = "WWW::Curl::Share"; |
| 1218 |
PPCODE: |
| 1219 |
if (items>0 && !SvROK(ST(0))) { |
| 1220 |
STRLEN dummy; |
| 1221 |
sclass = SvPV(ST(0),dummy); |
| 1222 |
} |
| 1223 |
|
| 1224 |
self=perl_curl_share_new(); |
| 1225 |
|
| 1226 |
ST(0) = sv_newmortal(); |
| 1227 |
sv_setref_pv(ST(0), sclass, (void*)self); |
| 1228 |
SvREADONLY_on(SvRV(ST(0))); |
| 1229 |
|
| 1230 |
XSRETURN(1); |
| 1231 |
|
| 1232 |
void |
| 1233 |
curl_share_DESTROY(self) |
| 1234 |
WWW::Curl::Share self |
| 1235 |
CODE: |
| 1236 |
perl_curl_share_delete(self); |
| 1237 |
|
| 1238 |
int |
| 1239 |
curl_share_setopt(self, option, value) |
| 1240 |
WWW::Curl::Share self |
| 1241 |
int option |
| 1242 |
SV * value |
| 1243 |
CODE: |
| 1244 |
RETVAL=CURLE_OK; |
| 1245 |
#if (LIBCURL_VERSION_NUM>=0x070a03) |
| 1246 |
switch(option) { |
| 1247 |
/* slist cases */ |
| 1248 |
case CURLSHOPT_SHARE: |
| 1249 |
case CURLSHOPT_UNSHARE: |
| 1250 |
if (option < CURLOPTTYPE_OBJECTPOINT) { /* An integer value: */ |
| 1251 |
RETVAL = curl_share_setopt(self->curlsh, option, (long)SvIV(value)); |
| 1252 |
} else { /* A char * value: */ |
| 1253 |
/* FIXME: Does curl really want NULL for empty strings? */ |
| 1254 |
STRLEN dummy; |
| 1255 |
char *pv = SvPV(value, dummy); |
| 1256 |
RETVAL = curl_share_setopt(self->curlsh, option, *pv ? pv : NULL); |
| 1257 |
}; |
| 1258 |
break; |
| 1259 |
}; |
| 1260 |
#else |
| 1261 |
croak("curl_share_setopt not supported in your libcurl version"); |
| 1262 |
#endif |
| 1263 |
OUTPUT: |
| 1264 |
RETVAL |
| 1265 |
|
| 1266 |
SV * |
| 1267 |
curl_share_strerror(self, errornum) |
| 1268 |
WWW::Curl::Share self |
| 1269 |
int errornum |
| 1270 |
CODE: |
| 1271 |
{ |
| 1272 |
#if (LIBCURL_VERSION_NUM>=0x070C00) |
| 1273 |
const char * vchar = curl_share_strerror(errornum); |
| 1274 |
#else |
| 1275 |
const char * vchar = "Unknown because curl_share_strerror function not available}"; |
| 1276 |
#endif |
| 1277 |
RETVAL = newSVpv(vchar,0); |
| 1278 |
} |
| 1279 |
OUTPUT: |
| 1280 |
RETVAL |