| 1 |
#include "pycurl.h" |
| 2 |
#include "docstrings.h" |
| 3 |
|
| 4 |
#define PYCURL_BEGIN_MULTI_CALLBACK(callback_name, retval) \ |
| 5 |
PYCURL_BEGIN_CALLBACK_COMMON(PYCURL_ACQUIRE_THREAD_MULTI(), retval, callback_name) |
| 6 |
|
| 7 |
/************************************************************************* |
| 8 |
// static utility functions |
| 9 |
**************************************************************************/ |
| 10 |
|
| 11 |
|
| 12 |
/* assert some CurlMultiObject invariants */ |
| 13 |
static void |
| 14 |
assert_multi_state(const CurlMultiObject *self) |
| 15 |
{ |
| 16 |
assert(self != NULL); |
| 17 |
assert(PyObject_IsInstance((PyObject *) self, (PyObject *) p_CurlMulti_Type) == 1); |
| 18 |
if (self->state != NULL) { |
| 19 |
assert(self->multi_handle != NULL); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
static int |
| 25 |
check_multi_state(const CurlMultiObject *self, int flags, const char *name) |
| 26 |
{ |
| 27 |
assert_multi_state(self); |
| 28 |
if ((flags & 1) && self->multi_handle == NULL) { |
| 29 |
PyErr_Format(ErrorObject, "cannot invoke %s() - no multi handle", name); |
| 30 |
return -1; |
| 31 |
} |
| 32 |
if ((flags & 2) && self->state != NULL) { |
| 33 |
PyErr_Format(ErrorObject, "cannot invoke %s() - multi_perform() is currently running", name); |
| 34 |
return -1; |
| 35 |
} |
| 36 |
return 0; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
static int |
| 41 |
easy_set_multi_ref(CurlObject *easy, CurlMultiObject *multi) |
| 42 |
{ |
| 43 |
PyObject *wr; |
| 44 |
|
| 45 |
assert(easy != NULL); |
| 46 |
assert(multi != NULL); |
| 47 |
|
| 48 |
wr = PyWeakref_NewRef((PyObject *) multi, NULL); |
| 49 |
if (wr == NULL) { |
| 50 |
return -1; |
| 51 |
} |
| 52 |
|
| 53 |
Py_CLEAR(easy->multi_weakref); |
| 54 |
easy->multi_weakref = wr; |
| 55 |
|
| 56 |
easy->multi_stack = multi; |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
static void |
| 62 |
easy_clear_multi_ref(CurlObject *easy, CurlMultiObject *multi) |
| 63 |
{ |
| 64 |
assert(easy != NULL); |
| 65 |
|
| 66 |
if (easy->multi_stack == multi) { |
| 67 |
easy->multi_stack = NULL; |
| 68 |
} |
| 69 |
|
| 70 |
Py_CLEAR(easy->multi_weakref); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
/************************************************************************* |
| 75 |
// CurlMultiObject |
| 76 |
**************************************************************************/ |
| 77 |
|
| 78 |
/* --------------- construct/destruct (i.e. open/close) --------------- */ |
| 79 |
|
| 80 |
/* constructor */ |
| 81 |
PYCURL_INTERNAL CurlMultiObject * |
| 82 |
do_multi_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) |
| 83 |
{ |
| 84 |
CurlMultiObject *self; |
| 85 |
int *ptr; |
| 86 |
|
| 87 |
static char *kwlist[] = {"close_handles", NULL}; |
| 88 |
int close_handles = 0; |
| 89 |
|
| 90 |
if (subtype == p_CurlMulti_Type && !PyArg_ParseTupleAndKeywords(args, kwds, "|$p", kwlist, &close_handles)) { |
| 91 |
return NULL; |
| 92 |
} |
| 93 |
|
| 94 |
/* Allocate python curl-multi object */ |
| 95 |
self = (CurlMultiObject *) subtype->tp_alloc(subtype, 0); |
| 96 |
if (!self) { |
| 97 |
return NULL; |
| 98 |
} |
| 99 |
|
| 100 |
/* tp_alloc is expected to return zeroed memory */ |
| 101 |
for (ptr = (int *) &self->dict; |
| 102 |
ptr < (int *) (((char *) self) + sizeof(CurlMultiObject)); |
| 103 |
++ptr) |
| 104 |
assert(*ptr == 0); |
| 105 |
|
| 106 |
self->easy_object_dict = PyDict_New(); |
| 107 |
if (self->easy_object_dict == NULL) { |
| 108 |
Py_DECREF(self); |
| 109 |
return NULL; |
| 110 |
} |
| 111 |
|
| 112 |
self->socket_object_dict = PyDict_New(); |
| 113 |
if (self->socket_object_dict == NULL) { |
| 114 |
Py_DECREF(self); |
| 115 |
return NULL; |
| 116 |
} |
| 117 |
|
| 118 |
/* Allocate libcurl multi handle */ |
| 119 |
self->multi_handle = curl_multi_init(); |
| 120 |
if (self->multi_handle == NULL) { |
| 121 |
Py_DECREF(self); |
| 122 |
PyErr_SetString(ErrorObject, "initializing curl-multi failed"); |
| 123 |
return NULL; |
| 124 |
} |
| 125 |
|
| 126 |
self->close_handles = close_handles ? 1 : 0; |
| 127 |
return self; |
| 128 |
} |
| 129 |
|
| 130 |
static void |
| 131 |
util_multi_close(CurlMultiObject *self) |
| 132 |
{ |
| 133 |
assert(self != NULL); |
| 134 |
|
| 135 |
self->state = NULL; |
| 136 |
|
| 137 |
if (self->multi_handle != NULL) { |
| 138 |
CURLM *multi_handle = self->multi_handle; |
| 139 |
/* Allow threads because callbacks can be invoked */ |
| 140 |
PYCURL_BEGIN_ALLOW_THREADS |
| 141 |
curl_multi_cleanup(multi_handle); |
| 142 |
PYCURL_END_ALLOW_THREADS |
| 143 |
self->multi_handle = NULL; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
static void |
| 149 |
util_multi_xdecref(CurlMultiObject *self) |
| 150 |
{ |
| 151 |
Py_CLEAR(self->dict); |
| 152 |
Py_CLEAR(self->t_cb); |
| 153 |
Py_CLEAR(self->s_cb); |
| 154 |
Py_CLEAR(self->socket_object_dict); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
static int |
| 159 |
util_multi_detach_easies(CurlMultiObject *self, int close_handles, int swallow_exceptions) |
| 160 |
{ |
| 161 |
if (!self->easy_object_dict) { |
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
/* Iterate over a snapshot since closing easies can mutate the dict. */ |
| 166 |
PyObject *keys = PyDict_Keys(self->easy_object_dict); |
| 167 |
if (keys == NULL) { |
| 168 |
return -1; |
| 169 |
} |
| 170 |
|
| 171 |
Py_ssize_t i; |
| 172 |
Py_ssize_t keys_len = PyList_Size(keys); |
| 173 |
for (i = 0; i < keys_len; ++i) { |
| 174 |
PyObject *key = PyList_GetItem(keys, i); |
| 175 |
CurlObject *easy = (CurlObject *) key; |
| 176 |
|
| 177 |
if (easy->multi_stack == NULL || easy->multi_stack != self) { |
| 178 |
continue; |
| 179 |
} |
| 180 |
|
| 181 |
if (self->multi_handle && easy->handle) { |
| 182 |
PYCURL_BEGIN_ALLOW_THREADS |
| 183 |
(void)curl_multi_remove_handle(self->multi_handle, easy->handle); |
| 184 |
PYCURL_END_ALLOW_THREADS |
| 185 |
} |
| 186 |
|
| 187 |
easy_clear_multi_ref(easy, self); |
| 188 |
|
| 189 |
if (close_handles) { |
| 190 |
PyObject *result = PyObject_CallMethod((PyObject *)easy, "close", NULL); |
| 191 |
if (result) { |
| 192 |
Py_DECREF(result); |
| 193 |
} else if (swallow_exceptions) { |
| 194 |
PyErr_Clear(); |
| 195 |
} else { |
| 196 |
Py_DECREF(keys); |
| 197 |
return -1; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
Py_DECREF(keys); |
| 203 |
Py_CLEAR(self->easy_object_dict); |
| 204 |
|
| 205 |
return 0; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
PYCURL_INTERNAL void |
| 210 |
do_multi_dealloc(CurlMultiObject *self) |
| 211 |
{ |
| 212 |
PyObject_GC_UnTrack(self); |
| 213 |
Py_TRASHCAN_BEGIN(self, do_multi_dealloc); |
| 214 |
|
| 215 |
util_multi_detach_easies(self, 0, 1); |
| 216 |
|
| 217 |
util_multi_xdecref(self); |
| 218 |
util_multi_close(self); |
| 219 |
|
| 220 |
if (self->weakreflist != NULL) { |
| 221 |
PyObject_ClearWeakRefs((PyObject *) self); |
| 222 |
} |
| 223 |
|
| 224 |
CurlMulti_Type.tp_free(self); |
| 225 |
Py_TRASHCAN_END |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
static PyObject * |
| 230 |
do_multi_close(CurlMultiObject *self, PyObject *Py_UNUSED(ignored)) |
| 231 |
{ |
| 232 |
if (check_multi_state(self, 2, "close") != 0) { |
| 233 |
return NULL; |
| 234 |
} |
| 235 |
|
| 236 |
if (util_multi_detach_easies(self, self->close_handles, 0) < 0) { |
| 237 |
return NULL; |
| 238 |
} |
| 239 |
|
| 240 |
util_multi_close(self); |
| 241 |
if (self->socket_object_dict) { |
| 242 |
PyDict_Clear(self->socket_object_dict); |
| 243 |
} |
| 244 |
Py_RETURN_NONE; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
static PyObject *do_multi_closed(CurlMultiObject *self, PyObject *Py_UNUSED(ignored)) |
| 249 |
{ |
| 250 |
if (self->multi_handle == NULL) { |
| 251 |
Py_RETURN_TRUE; |
| 252 |
} else { |
| 253 |
Py_RETURN_FALSE; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
/* --------------- GC support --------------- */ |
| 259 |
|
| 260 |
/* Drop references that may have created reference cycles. */ |
| 261 |
PYCURL_INTERNAL int |
| 262 |
do_multi_clear(CurlMultiObject *self) |
| 263 |
{ |
| 264 |
util_multi_xdecref(self); |
| 265 |
return 0; |
| 266 |
} |
| 267 |
|
| 268 |
PYCURL_INTERNAL int |
| 269 |
do_multi_traverse(CurlMultiObject *self, visitproc visit, void *arg) |
| 270 |
{ |
| 271 |
int err; |
| 272 |
#undef VISIT |
| 273 |
#define VISIT(v) if ((v) != NULL && ((err = visit(v, arg)) != 0)) return err |
| 274 |
|
| 275 |
VISIT(self->dict); |
| 276 |
VISIT(self->easy_object_dict); |
| 277 |
VISIT(self->socket_object_dict); |
| 278 |
VISIT(self->t_cb); |
| 279 |
VISIT(self->s_cb); |
| 280 |
|
| 281 |
return 0; |
| 282 |
#undef VISIT |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
/* --------------- setopt --------------- */ |
| 287 |
|
| 288 |
static int |
| 289 |
multi_socket_callback(CURL *easy, |
| 290 |
curl_socket_t s, |
| 291 |
int what, |
| 292 |
void *userp, |
| 293 |
void *socketp) |
| 294 |
{ |
| 295 |
CurlMultiObject *self; |
| 296 |
PyObject *arglist; |
| 297 |
PyObject *py_socket = NULL; |
| 298 |
PyObject *result = NULL; |
| 299 |
int ret = -1; /* assume error */ |
| 300 |
PYCURL_DECLARE_THREAD_STATE; |
| 301 |
|
| 302 |
/* acquire thread */ |
| 303 |
self = (CurlMultiObject *)userp; |
| 304 |
PYCURL_BEGIN_MULTI_CALLBACK(multi_socket_callback, ret); |
| 305 |
|
| 306 |
/* check args */ |
| 307 |
if (self->s_cb == NULL) { |
| 308 |
ret = 0; |
| 309 |
goto silent_error; |
| 310 |
} |
| 311 |
|
| 312 |
if (socketp == NULL) { |
| 313 |
socketp = Py_None; |
| 314 |
} |
| 315 |
|
| 316 |
/* run callback */ |
| 317 |
py_socket = PyLong_FromCurlSocket(s); |
| 318 |
if (py_socket == NULL) { |
| 319 |
goto verbose_error; |
| 320 |
} |
| 321 |
arglist = Py_BuildValue("(iOOO)", what, py_socket, userp, (PyObject *)socketp); |
| 322 |
Py_DECREF(py_socket); |
| 323 |
if (arglist == NULL) { |
| 324 |
goto verbose_error; |
| 325 |
} |
| 326 |
result = PyObject_Call(self->s_cb, arglist, NULL); |
| 327 |
Py_DECREF(arglist); |
| 328 |
if (result == NULL) { |
| 329 |
goto verbose_error; |
| 330 |
} |
| 331 |
|
| 332 |
/* None => success; otherwise reuse the easy-callback decode helper. |
| 333 |
* -1 aborts all in-progress transfers per CURLMOPT_SOCKETFUNCTION. */ |
| 334 |
if (result == Py_None) { |
| 335 |
ret = 0; |
| 336 |
} else if (callback_return_value_to_int(result, "multi socket", &ret) != 0) { |
| 337 |
goto silent_error; |
| 338 |
} |
| 339 |
|
| 340 |
silent_error: |
| 341 |
Py_XDECREF(result); |
| 342 |
PYCURL_END_CALLBACK(ret); |
| 343 |
verbose_error: |
| 344 |
print_callback_error_if_regular_exception(); |
| 345 |
goto silent_error; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
static int |
| 350 |
multi_timer_callback(CURLM *multi, |
| 351 |
long timeout_ms, |
| 352 |
void *userp) |
| 353 |
{ |
| 354 |
CurlMultiObject *self; |
| 355 |
PyObject *arglist; |
| 356 |
PyObject *result = NULL; |
| 357 |
int ret = -1; /* assume error */ |
| 358 |
PYCURL_DECLARE_THREAD_STATE; |
| 359 |
|
| 360 |
UNUSED(multi); |
| 361 |
|
| 362 |
/* acquire thread */ |
| 363 |
self = (CurlMultiObject *)userp; |
| 364 |
PYCURL_BEGIN_MULTI_CALLBACK(multi_timer_callback, ret); |
| 365 |
|
| 366 |
/* check args */ |
| 367 |
if (self->t_cb == NULL) { |
| 368 |
ret = 0; |
| 369 |
goto silent_error; |
| 370 |
} |
| 371 |
|
| 372 |
/* run callback */ |
| 373 |
arglist = Py_BuildValue("(i)", timeout_ms); |
| 374 |
if (arglist == NULL) { |
| 375 |
goto verbose_error; |
| 376 |
} |
| 377 |
result = PyObject_Call(self->t_cb, arglist, NULL); |
| 378 |
Py_DECREF(arglist); |
| 379 |
if (result == NULL) { |
| 380 |
goto verbose_error; |
| 381 |
} |
| 382 |
|
| 383 |
/* None => success; otherwise reuse the easy-callback decode helper. |
| 384 |
* -1 aborts all in-progress transfers per CURLMOPT_TIMERFUNCTION. */ |
| 385 |
if (result == Py_None) { |
| 386 |
ret = 0; |
| 387 |
} else if (callback_return_value_to_int(result, "multi timer", &ret) != 0) { |
| 388 |
goto silent_error; |
| 389 |
} |
| 390 |
|
| 391 |
silent_error: |
| 392 |
Py_XDECREF(result); |
| 393 |
PYCURL_END_CALLBACK(ret); |
| 394 |
verbose_error: |
| 395 |
print_callback_error_if_regular_exception(); |
| 396 |
goto silent_error; |
| 397 |
} |
| 398 |
|
| 399 |
#undef PYCURL_BEGIN_MULTI_CALLBACK |
| 400 |
|
| 401 |
|
| 402 |
static PyObject * |
| 403 |
do_multi_setopt_int(CurlMultiObject *self, int option, PyObject *obj) |
| 404 |
{ |
| 405 |
long d = PyLong_AsLong(obj); |
| 406 |
if (d == -1 && PyErr_Occurred()) { |
| 407 |
return NULL; |
| 408 |
} |
| 409 |
switch(option) { |
| 410 |
case CURLMOPT_MAXCONNECTS: |
| 411 |
case CURLMOPT_PIPELINING: |
| 412 |
#ifdef HAVE_CURL_7_30_0_PIPELINE_OPTS |
| 413 |
case CURLMOPT_MAX_HOST_CONNECTIONS: |
| 414 |
case CURLMOPT_MAX_TOTAL_CONNECTIONS: |
| 415 |
case CURLMOPT_MAX_PIPELINE_LENGTH: |
| 416 |
case CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE: |
| 417 |
case CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE: |
| 418 |
#endif |
| 419 |
#ifdef HAVE_CURL_7_67_0_MULTI_STREAMS |
| 420 |
case CURLMOPT_MAX_CONCURRENT_STREAMS: |
| 421 |
#endif |
| 422 |
curl_multi_setopt(self->multi_handle, option, d); |
| 423 |
break; |
| 424 |
default: |
| 425 |
PyErr_SetString(PyExc_TypeError, "integers are not supported for this option"); |
| 426 |
return NULL; |
| 427 |
} |
| 428 |
Py_RETURN_NONE; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
static PyObject * |
| 433 |
do_multi_setopt_charpp(CurlMultiObject *self, int option, int which, PyObject *obj) |
| 434 |
{ |
| 435 |
Py_ssize_t len, i; |
| 436 |
int res; |
| 437 |
static const char *empty_list[] = { NULL }; |
| 438 |
char **list = NULL; |
| 439 |
PyObject **encoded_objs = NULL; |
| 440 |
PyObject *encoded_obj = NULL; |
| 441 |
char *encoded_str; |
| 442 |
PyObject *rv = NULL; |
| 443 |
|
| 444 |
len = PyListOrTuple_Size(obj, which); |
| 445 |
if (len == 0) { |
| 446 |
res = curl_multi_setopt(self->multi_handle, option, empty_list); |
| 447 |
if (res != CURLE_OK) { |
| 448 |
CURLERROR_RETVAL_MULTI_DONE(); |
| 449 |
} |
| 450 |
Py_RETURN_NONE; |
| 451 |
} |
| 452 |
|
| 453 |
/* add NULL terminator as the last list item */ |
| 454 |
list = PyMem_New(char *, len+1); |
| 455 |
if (list == NULL) { |
| 456 |
PyErr_NoMemory(); |
| 457 |
return NULL; |
| 458 |
} |
| 459 |
/* no need for the NULL terminator here */ |
| 460 |
encoded_objs = PyMem_New(PyObject *, len); |
| 461 |
if (encoded_objs == NULL) { |
| 462 |
PyErr_NoMemory(); |
| 463 |
goto done; |
| 464 |
} |
| 465 |
memset(encoded_objs, 0, sizeof(PyObject *) * len); |
| 466 |
|
| 467 |
for (i = 0; i < len; i++) { |
| 468 |
PyObject *listitem = PyListOrTuple_GetItem(obj, i, which); |
| 469 |
if (!PyText_Check(listitem)) { |
| 470 |
PyErr_SetString(ErrorObject, "list/tuple items must be strings"); |
| 471 |
goto done; |
| 472 |
} |
| 473 |
encoded_str = PyText_AsString_NoNUL(listitem, &encoded_obj); |
| 474 |
if (encoded_str == NULL) { |
| 475 |
goto done; |
| 476 |
} |
| 477 |
list[i] = encoded_str; |
| 478 |
encoded_objs[i] = encoded_obj; |
| 479 |
} |
| 480 |
list[len] = NULL; |
| 481 |
|
| 482 |
res = curl_multi_setopt(self->multi_handle, option, list); |
| 483 |
if (res != CURLE_OK) { |
| 484 |
rv = NULL; |
| 485 |
CURLERROR_RETVAL_MULTI_DONE(); |
| 486 |
} |
| 487 |
|
| 488 |
Py_INCREF(Py_None); |
| 489 |
rv = Py_None; |
| 490 |
done: |
| 491 |
if (encoded_objs) { |
| 492 |
for (i = 0; i < len; i++) { |
| 493 |
Py_XDECREF(encoded_objs[i]); |
| 494 |
} |
| 495 |
PyMem_Free(encoded_objs); |
| 496 |
} |
| 497 |
PyMem_Free(list); |
| 498 |
return rv; |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
static PyObject * |
| 503 |
do_multi_setopt_list(CurlMultiObject *self, int option, int which, PyObject *obj) |
| 504 |
{ |
| 505 |
switch(option) { |
| 506 |
#ifdef HAVE_CURL_7_30_0_PIPELINE_OPTS |
| 507 |
case CURLMOPT_PIPELINING_SITE_BL: |
| 508 |
case CURLMOPT_PIPELINING_SERVER_BL: |
| 509 |
#endif |
| 510 |
return do_multi_setopt_charpp(self, option, which, obj); |
| 511 |
break; |
| 512 |
default: |
| 513 |
PyErr_SetString(PyExc_TypeError, "lists/tuples are not supported for this option"); |
| 514 |
return NULL; |
| 515 |
} |
| 516 |
Py_RETURN_NONE; |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
static PyObject * |
| 521 |
do_multi_setopt_callable(CurlMultiObject *self, int option, PyObject *obj) |
| 522 |
{ |
| 523 |
/* We use function types here to make sure that our callback |
| 524 |
* definitions exactly match the <curl/multi.h> interface. |
| 525 |
*/ |
| 526 |
const curl_multi_timer_callback t_cb = multi_timer_callback; |
| 527 |
const curl_socket_callback s_cb = multi_socket_callback; |
| 528 |
|
| 529 |
switch(option) { |
| 530 |
case CURLMOPT_SOCKETFUNCTION: |
| 531 |
curl_multi_setopt(self->multi_handle, CURLMOPT_SOCKETFUNCTION, s_cb); |
| 532 |
curl_multi_setopt(self->multi_handle, CURLMOPT_SOCKETDATA, self); |
| 533 |
Py_INCREF(obj); |
| 534 |
Py_CLEAR(self->s_cb); |
| 535 |
self->s_cb = obj; |
| 536 |
break; |
| 537 |
case CURLMOPT_TIMERFUNCTION: |
| 538 |
curl_multi_setopt(self->multi_handle, CURLMOPT_TIMERFUNCTION, t_cb); |
| 539 |
curl_multi_setopt(self->multi_handle, CURLMOPT_TIMERDATA, self); |
| 540 |
Py_INCREF(obj); |
| 541 |
Py_CLEAR(self->t_cb); |
| 542 |
self->t_cb = obj; |
| 543 |
break; |
| 544 |
default: |
| 545 |
PyErr_SetString(PyExc_TypeError, "callables are not supported for this option"); |
| 546 |
return NULL; |
| 547 |
} |
| 548 |
Py_RETURN_NONE; |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
static PyObject * |
| 553 |
do_multi_setopt_none(CurlMultiObject *self, int option, PyObject *obj) |
| 554 |
{ |
| 555 |
switch(option) { |
| 556 |
#ifdef HAVE_CURL_7_30_0_PIPELINE_OPTS |
| 557 |
case CURLMOPT_PIPELINING_SITE_BL: |
| 558 |
case CURLMOPT_PIPELINING_SERVER_BL: |
| 559 |
curl_multi_setopt(self->multi_handle, option, NULL); |
| 560 |
break; |
| 561 |
#endif |
| 562 |
case CURLMOPT_SOCKETFUNCTION: |
| 563 |
curl_multi_setopt(self->multi_handle, CURLMOPT_SOCKETFUNCTION, NULL); |
| 564 |
curl_multi_setopt(self->multi_handle, CURLMOPT_SOCKETDATA, NULL); |
| 565 |
Py_CLEAR(self->s_cb); |
| 566 |
break; |
| 567 |
case CURLMOPT_TIMERFUNCTION: |
| 568 |
curl_multi_setopt(self->multi_handle, CURLMOPT_TIMERFUNCTION, NULL); |
| 569 |
curl_multi_setopt(self->multi_handle, CURLMOPT_TIMERDATA, NULL); |
| 570 |
Py_CLEAR(self->t_cb); |
| 571 |
break; |
| 572 |
default: |
| 573 |
PyErr_SetString(PyExc_TypeError, "unsetting is not supported for this option"); |
| 574 |
return NULL; |
| 575 |
} |
| 576 |
Py_RETURN_NONE; |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
static PyObject * |
| 581 |
do_multi_setopt(CurlMultiObject *self, PyObject *args) |
| 582 |
{ |
| 583 |
int option, which; |
| 584 |
PyObject *obj; |
| 585 |
|
| 586 |
if (!PyArg_ParseTuple(args, "iO:setopt", &option, &obj)) |
| 587 |
return NULL; |
| 588 |
if (check_multi_state(self, 1 | 2, "setopt") != 0) |
| 589 |
return NULL; |
| 590 |
|
| 591 |
/* Early checks of option value */ |
| 592 |
if (option <= 0) |
| 593 |
goto error; |
| 594 |
if (option >= (int)CURLOPTTYPE_OFF_T + MOPTIONS_SIZE) |
| 595 |
goto error; |
| 596 |
if (option % 10000 >= MOPTIONS_SIZE) |
| 597 |
goto error; |
| 598 |
|
| 599 |
/* Handle unsetting of options */ |
| 600 |
if (obj == Py_None) { |
| 601 |
return do_multi_setopt_none(self, option, obj); |
| 602 |
} |
| 603 |
|
| 604 |
/* Handle the case of integer arguments */ |
| 605 |
if (PyLong_Check(obj)) { |
| 606 |
return do_multi_setopt_int(self, option, obj); |
| 607 |
} |
| 608 |
|
| 609 |
/* Handle the case of list or tuple objects */ |
| 610 |
which = PyListOrTuple_Check(obj); |
| 611 |
if (which) { |
| 612 |
return do_multi_setopt_list(self, option, which, obj); |
| 613 |
} |
| 614 |
|
| 615 |
if (PyFunction_Check(obj) || PyCFunction_Check(obj) || |
| 616 |
PyCallable_Check(obj) || PyMethod_Check(obj)) { |
| 617 |
return do_multi_setopt_callable(self, option, obj); |
| 618 |
} |
| 619 |
|
| 620 |
/* Failed to match any of the function signatures -- return error */ |
| 621 |
error: |
| 622 |
PyErr_SetString(PyExc_TypeError, "invalid arguments to setopt"); |
| 623 |
return NULL; |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
/* --------------- timeout --------------- */ |
| 628 |
|
| 629 |
static PyObject * |
| 630 |
do_multi_timeout(CurlMultiObject *self, PyObject *Py_UNUSED(ignored)) |
| 631 |
{ |
| 632 |
CURLMcode res; |
| 633 |
long timeout; |
| 634 |
|
| 635 |
if (check_multi_state(self, 1 | 2, "timeout") != 0) { |
| 636 |
return NULL; |
| 637 |
} |
| 638 |
|
| 639 |
res = curl_multi_timeout(self->multi_handle, &timeout); |
| 640 |
if (res != CURLM_OK) { |
| 641 |
CURLERROR_MSG("timeout failed"); |
| 642 |
} |
| 643 |
|
| 644 |
/* Return number of millisecs until timeout */ |
| 645 |
return Py_BuildValue("l", timeout); |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
/* --------------- assign / unassign --------------- */ |
| 650 |
|
| 651 |
static PyObject * |
| 652 |
util_multi_assign(CurlMultiObject *self, PyObject *socket_obj, |
| 653 |
PyObject *obj, const char *name) |
| 654 |
{ |
| 655 |
CURLMcode res; |
| 656 |
curl_socket_t socket; |
| 657 |
int clear; |
| 658 |
PyObject *old; |
| 659 |
|
| 660 |
if (PyLong_AsCurlSocket(socket_obj, &socket) != 0) { |
| 661 |
return NULL; |
| 662 |
} |
| 663 |
/* Flag 1 only: curl_multi_assign() is callback-safe, so we do not block |
| 664 |
* calls made while multi_perform()/socket_action() is on the stack. */ |
| 665 |
if (check_multi_state(self, 1, name) != 0) { |
| 666 |
return NULL; |
| 667 |
} |
| 668 |
|
| 669 |
clear = (obj == Py_None); |
| 670 |
old = PyDict_GetItem(self->socket_object_dict, socket_obj); |
| 671 |
Py_XINCREF(old); |
| 672 |
|
| 673 |
/* First, update dict keeping previous value */ |
| 674 |
if (clear) { |
| 675 |
if (old && PyDict_DelItem(self->socket_object_dict, socket_obj) < 0) { |
| 676 |
Py_DECREF(old); |
| 677 |
return NULL; |
| 678 |
} |
| 679 |
} else { |
| 680 |
if (PyDict_SetItem(self->socket_object_dict, socket_obj, obj) < 0) { |
| 681 |
Py_XDECREF(old); |
| 682 |
return NULL; |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
res = curl_multi_assign(self->multi_handle, socket, clear ? NULL : (void *)obj); |
| 687 |
if (res != CURLM_OK) { |
| 688 |
/* Oversized: formatted message is at most "unassign failed". */ |
| 689 |
char err_msg[64]; |
| 690 |
/* Restore previous value */ |
| 691 |
if (old) { |
| 692 |
if (PyDict_SetItem(self->socket_object_dict, socket_obj, old) < 0) { |
| 693 |
PyErr_Clear(); |
| 694 |
} |
| 695 |
Py_DECREF(old); |
| 696 |
} else if (!clear) { |
| 697 |
if (PyDict_DelItem(self->socket_object_dict, socket_obj) < 0) { |
| 698 |
PyErr_Clear(); |
| 699 |
} |
| 700 |
} |
| 701 |
snprintf(err_msg, sizeof(err_msg), "%s failed", name); |
| 702 |
CURLERROR_MSG(err_msg); |
| 703 |
return NULL; |
| 704 |
} |
| 705 |
|
| 706 |
Py_XDECREF(old); |
| 707 |
Py_RETURN_NONE; |
| 708 |
} |
| 709 |
|
| 710 |
static PyObject * |
| 711 |
do_multi_assign(CurlMultiObject *self, PyObject *args) |
| 712 |
{ |
| 713 |
PyObject *socket_obj; |
| 714 |
PyObject *obj; |
| 715 |
|
| 716 |
if (!PyArg_ParseTuple(args, "OO:assign", &socket_obj, &obj)) { |
| 717 |
return NULL; |
| 718 |
} |
| 719 |
return util_multi_assign(self, socket_obj, obj, "assign"); |
| 720 |
} |
| 721 |
|
| 722 |
static PyObject * |
| 723 |
do_multi_unassign(CurlMultiObject *self, PyObject *args) |
| 724 |
{ |
| 725 |
PyObject *socket_obj; |
| 726 |
|
| 727 |
if (!PyArg_ParseTuple(args, "O:unassign", &socket_obj)) { |
| 728 |
return NULL; |
| 729 |
} |
| 730 |
return util_multi_assign(self, socket_obj, Py_None, "unassign"); |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
/* --------------- socket_action --------------- */ |
| 735 |
static PyObject * |
| 736 |
do_multi_socket_action(CurlMultiObject *self, PyObject *args) |
| 737 |
{ |
| 738 |
CURLMcode res; |
| 739 |
curl_socket_t socket; |
| 740 |
PyObject *socket_obj; |
| 741 |
int ev_bitmask; |
| 742 |
int running = -1; |
| 743 |
|
| 744 |
if (!PyArg_ParseTuple(args, "Oi:socket_action", &socket_obj, &ev_bitmask)) { |
| 745 |
return NULL; |
| 746 |
} |
| 747 |
if (PyLong_AsCurlSocket(socket_obj, &socket) != 0) { |
| 748 |
return NULL; |
| 749 |
} |
| 750 |
if (check_multi_state(self, 1 | 2, "socket_action") != 0) { |
| 751 |
return NULL; |
| 752 |
} |
| 753 |
|
| 754 |
PYCURL_BEGIN_ALLOW_THREADS |
| 755 |
res = curl_multi_socket_action(self->multi_handle, socket, ev_bitmask, &running); |
| 756 |
PYCURL_END_ALLOW_THREADS |
| 757 |
|
| 758 |
if (check_pending_python_exception_or_signal() != 0) { |
| 759 |
return NULL; |
| 760 |
} |
| 761 |
|
| 762 |
if (res != CURLM_OK) { |
| 763 |
CURLERROR_MSG("multi_socket_action failed"); |
| 764 |
} |
| 765 |
/* Return a tuple with the result and the number of running handles */ |
| 766 |
return Py_BuildValue("(ii)", (int)res, running); |
| 767 |
} |
| 768 |
|
| 769 |
/* --------------- socket_all --------------- */ |
| 770 |
|
| 771 |
static PyObject * |
| 772 |
do_multi_socket_all(CurlMultiObject *self, PyObject *Py_UNUSED(ignored)) |
| 773 |
{ |
| 774 |
CURLMcode res; |
| 775 |
int running = -1; |
| 776 |
|
| 777 |
if (PyErr_WarnEx(PyExc_DeprecationWarning, "socket_all is deprecated; use socket_action", 1) != 0) { |
| 778 |
return NULL; |
| 779 |
} |
| 780 |
|
| 781 |
if (check_multi_state(self, 1 | 2, "socket_all") != 0) { |
| 782 |
return NULL; |
| 783 |
} |
| 784 |
|
| 785 |
PYCURL_BEGIN_ALLOW_THREADS |
| 786 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 787 |
res = curl_multi_socket_all(self->multi_handle, &running); |
| 788 |
PYCURL_IGNORE_DEPRECATED_END |
| 789 |
PYCURL_END_ALLOW_THREADS |
| 790 |
|
| 791 |
if (check_pending_python_exception_or_signal() != 0) { |
| 792 |
return NULL; |
| 793 |
} |
| 794 |
|
| 795 |
/* We assume these errors are ok, otherwise raise exception */ |
| 796 |
if (res != CURLM_OK && res != CURLM_CALL_MULTI_PERFORM) { |
| 797 |
CURLERROR_MSG("perform failed"); |
| 798 |
} |
| 799 |
|
| 800 |
/* Return a tuple with the result and the number of running handles */ |
| 801 |
return Py_BuildValue("(ii)", (int)res, running); |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
/* --------------- perform --------------- */ |
| 806 |
|
| 807 |
static PyObject * |
| 808 |
do_multi_perform(CurlMultiObject *self, PyObject *Py_UNUSED(ignored)) |
| 809 |
{ |
| 810 |
CURLMcode res; |
| 811 |
int running = -1; |
| 812 |
|
| 813 |
if (check_multi_state(self, 1 | 2, "perform") != 0) { |
| 814 |
return NULL; |
| 815 |
} |
| 816 |
|
| 817 |
PYCURL_BEGIN_ALLOW_THREADS |
| 818 |
res = curl_multi_perform(self->multi_handle, &running); |
| 819 |
PYCURL_END_ALLOW_THREADS |
| 820 |
|
| 821 |
if (check_pending_python_exception_or_signal() != 0) { |
| 822 |
return NULL; |
| 823 |
} |
| 824 |
|
| 825 |
/* We assume these errors are ok, otherwise raise exception */ |
| 826 |
if (res != CURLM_OK && res != CURLM_CALL_MULTI_PERFORM) { |
| 827 |
CURLERROR_MSG("perform failed"); |
| 828 |
} |
| 829 |
|
| 830 |
/* Return a tuple with the result and the number of running handles */ |
| 831 |
return Py_BuildValue("(ii)", (int)res, running); |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
/* --------------- add_handle/remove_handle --------------- */ |
| 836 |
|
| 837 |
/* static utility function */ |
| 838 |
static int |
| 839 |
check_multi_add_remove(const CurlMultiObject *self, const CurlObject *obj) |
| 840 |
{ |
| 841 |
/* check CurlMultiObject status */ |
| 842 |
assert_multi_state(self); |
| 843 |
if (self->multi_handle == NULL) { |
| 844 |
PyErr_SetString(ErrorObject, "cannot add/remove handle - multi-stack is closed"); |
| 845 |
return -1; |
| 846 |
} |
| 847 |
if (self->state != NULL) { |
| 848 |
PyErr_SetString(ErrorObject, "cannot add/remove handle - multi_perform() already running"); |
| 849 |
return -1; |
| 850 |
} |
| 851 |
/* check CurlObject status */ |
| 852 |
assert_curl_state(obj); |
| 853 |
if (obj->state != NULL) { |
| 854 |
PyErr_SetString(ErrorObject, "cannot add/remove handle - perform() of curl object already running"); |
| 855 |
return -1; |
| 856 |
} |
| 857 |
if (obj->multi_stack != NULL && obj->multi_stack != self) { |
| 858 |
PyErr_SetString(ErrorObject, "cannot add/remove handle - curl object already on another multi-stack"); |
| 859 |
return -1; |
| 860 |
} |
| 861 |
return 0; |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
static PyObject * |
| 866 |
do_multi_add_handle(CurlMultiObject *self, PyObject *args) |
| 867 |
{ |
| 868 |
CurlObject *obj; |
| 869 |
CURLMcode res; |
| 870 |
|
| 871 |
if (!PyArg_ParseTuple(args, "O!:add_handle", p_Curl_Type, &obj)) { |
| 872 |
return NULL; |
| 873 |
} |
| 874 |
if (check_multi_add_remove(self, obj) != 0) { |
| 875 |
return NULL; |
| 876 |
} |
| 877 |
if (obj->handle == NULL) { |
| 878 |
PyErr_SetString(ErrorObject, "curl object already closed"); |
| 879 |
return NULL; |
| 880 |
} |
| 881 |
if (obj->multi_stack == self) { |
| 882 |
PyErr_SetString(ErrorObject, "curl object already on this multi-stack"); |
| 883 |
return NULL; |
| 884 |
} |
| 885 |
|
| 886 |
if (PyDict_SetItem(self->easy_object_dict, (PyObject *) obj, Py_True) < 0) { |
| 887 |
return NULL; |
| 888 |
} |
| 889 |
|
| 890 |
assert(obj->multi_stack == NULL); |
| 891 |
/* Allow threads because callbacks can be invoked */ |
| 892 |
PYCURL_BEGIN_ALLOW_THREADS |
| 893 |
res = curl_multi_add_handle(self->multi_handle, obj->handle); |
| 894 |
PYCURL_END_ALLOW_THREADS |
| 895 |
if (res != CURLM_OK) { |
| 896 |
PyDict_DelItem(self->easy_object_dict, (PyObject *) obj); |
| 897 |
CURLERROR_MSG("curl_multi_add_handle() failed due to internal errors"); |
| 898 |
} |
| 899 |
|
| 900 |
if (easy_set_multi_ref(obj, self) < 0) { |
| 901 |
/* undo dict + libcurl add */ |
| 902 |
PYCURL_BEGIN_ALLOW_THREADS |
| 903 |
(void) curl_multi_remove_handle(self->multi_handle, obj->handle); |
| 904 |
PYCURL_END_ALLOW_THREADS |
| 905 |
(void) PyDict_DelItem(self->easy_object_dict, (PyObject *) obj); |
| 906 |
return NULL; |
| 907 |
} |
| 908 |
|
| 909 |
Py_RETURN_NONE; |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
static PyObject * |
| 914 |
do_multi_remove_handle(CurlMultiObject *self, PyObject *args) |
| 915 |
{ |
| 916 |
CurlObject *obj; |
| 917 |
CURLMcode res; |
| 918 |
|
| 919 |
if (!PyArg_ParseTuple(args, "O!:remove_handle", p_Curl_Type, &obj)) { |
| 920 |
return NULL; |
| 921 |
} |
| 922 |
if (check_multi_add_remove(self, obj) != 0) { |
| 923 |
return NULL; |
| 924 |
} |
| 925 |
if (obj->handle == NULL) { |
| 926 |
/* CurlObject handle already closed -- ignore */ |
| 927 |
if (PyDict_DelItem(self->easy_object_dict, (PyObject *)obj) < 0) { |
| 928 |
PyErr_Clear(); |
| 929 |
} |
| 930 |
goto done; |
| 931 |
} |
| 932 |
if (obj->multi_stack != self) { |
| 933 |
PyErr_SetString(ErrorObject, "curl object not on this multi-stack"); |
| 934 |
return NULL; |
| 935 |
} |
| 936 |
/* Allow threads because callbacks can be invoked */ |
| 937 |
PYCURL_BEGIN_ALLOW_THREADS |
| 938 |
res = curl_multi_remove_handle(self->multi_handle, obj->handle); |
| 939 |
PYCURL_END_ALLOW_THREADS |
| 940 |
if (res == CURLM_OK) { |
| 941 |
PyDict_DelItem(self->easy_object_dict, (PyObject *) obj); |
| 942 |
// if PyDict_DelItem fails, remove_handle call will also fail. |
| 943 |
// but the dictionary should always have our object in it |
| 944 |
// hence this failure shouldn't happen unless something unaccounted |
| 945 |
// for went wrong |
| 946 |
} else { |
| 947 |
CURLERROR_MSG("curl_multi_remove_handle() failed due to internal errors"); |
| 948 |
} |
| 949 |
assert(obj->multi_stack == self); |
| 950 |
easy_clear_multi_ref(obj, self); |
| 951 |
done: |
| 952 |
Py_RETURN_NONE; |
| 953 |
} |
| 954 |
|
| 955 |
|
| 956 |
/* --------------- fdset ---------------------- */ |
| 957 |
|
| 958 |
static PyObject * |
| 959 |
do_multi_fdset(CurlMultiObject *self, PyObject *Py_UNUSED(ignored)) |
| 960 |
{ |
| 961 |
CURLMcode res; |
| 962 |
int max_fd = -1, fd; |
| 963 |
PyObject *ret = NULL; |
| 964 |
PyObject *read_list = NULL, *write_list = NULL, *except_list = NULL; |
| 965 |
PyObject *py_fd = NULL; |
| 966 |
|
| 967 |
if (check_multi_state(self, 1 | 2, "fdset") != 0) { |
| 968 |
return NULL; |
| 969 |
} |
| 970 |
|
| 971 |
/* Clear file descriptor sets */ |
| 972 |
FD_ZERO(&self->read_fd_set); |
| 973 |
FD_ZERO(&self->write_fd_set); |
| 974 |
FD_ZERO(&self->exc_fd_set); |
| 975 |
|
| 976 |
/* Don't bother releasing the gil as this is just a data structure operation */ |
| 977 |
res = curl_multi_fdset(self->multi_handle, &self->read_fd_set, |
| 978 |
&self->write_fd_set, &self->exc_fd_set, &max_fd); |
| 979 |
if (res != CURLM_OK) { |
| 980 |
CURLERROR_MSG("curl_multi_fdset() failed due to internal errors"); |
| 981 |
} |
| 982 |
|
| 983 |
/* Allocate lists. */ |
| 984 |
if ((read_list = PyList_New((Py_ssize_t)0)) == NULL) goto error; |
| 985 |
if ((write_list = PyList_New((Py_ssize_t)0)) == NULL) goto error; |
| 986 |
if ((except_list = PyList_New((Py_ssize_t)0)) == NULL) goto error; |
| 987 |
|
| 988 |
/* Populate lists */ |
| 989 |
for (fd = 0; fd < max_fd + 1; fd++) { |
| 990 |
if (FD_ISSET(fd, &self->read_fd_set)) { |
| 991 |
if ((py_fd = PyLong_FromLong((long)fd)) == NULL) goto error; |
| 992 |
if (PyList_Append(read_list, py_fd) != 0) goto error; |
| 993 |
Py_DECREF(py_fd); |
| 994 |
py_fd = NULL; |
| 995 |
} |
| 996 |
if (FD_ISSET(fd, &self->write_fd_set)) { |
| 997 |
if ((py_fd = PyLong_FromLong((long)fd)) == NULL) goto error; |
| 998 |
if (PyList_Append(write_list, py_fd) != 0) goto error; |
| 999 |
Py_DECREF(py_fd); |
| 1000 |
py_fd = NULL; |
| 1001 |
} |
| 1002 |
if (FD_ISSET(fd, &self->exc_fd_set)) { |
| 1003 |
if ((py_fd = PyLong_FromLong((long)fd)) == NULL) goto error; |
| 1004 |
if (PyList_Append(except_list, py_fd) != 0) goto error; |
| 1005 |
Py_DECREF(py_fd); |
| 1006 |
py_fd = NULL; |
| 1007 |
} |
| 1008 |
} |
| 1009 |
|
| 1010 |
/* Return a tuple with the 3 lists */ |
| 1011 |
ret = Py_BuildValue("(OOO)", read_list, write_list, except_list); |
| 1012 |
error: |
| 1013 |
Py_XDECREF(py_fd); |
| 1014 |
Py_XDECREF(except_list); |
| 1015 |
Py_XDECREF(write_list); |
| 1016 |
Py_XDECREF(read_list); |
| 1017 |
return ret; |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
/* --------------- info_read --------------- */ |
| 1022 |
|
| 1023 |
static PyObject * |
| 1024 |
do_multi_info_read(CurlMultiObject *self, PyObject *args) |
| 1025 |
{ |
| 1026 |
PyObject *ret = NULL; |
| 1027 |
PyObject *ok_list = NULL, *err_list = NULL; |
| 1028 |
CURLMsg *msg; |
| 1029 |
int in_queue = 0, num_results = INT_MAX; |
| 1030 |
|
| 1031 |
/* Sanity checks */ |
| 1032 |
if (!PyArg_ParseTuple(args, "|i:info_read", &num_results)) { |
| 1033 |
return NULL; |
| 1034 |
} |
| 1035 |
if (num_results <= 0) { |
| 1036 |
PyErr_SetString(ErrorObject, "argument to info_read must be greater than zero"); |
| 1037 |
return NULL; |
| 1038 |
} |
| 1039 |
if (check_multi_state(self, 1 | 2, "info_read") != 0) { |
| 1040 |
return NULL; |
| 1041 |
} |
| 1042 |
|
| 1043 |
if ((ok_list = PyList_New((Py_ssize_t)0)) == NULL) goto error; |
| 1044 |
if ((err_list = PyList_New((Py_ssize_t)0)) == NULL) goto error; |
| 1045 |
|
| 1046 |
/* Loop through up to 'num_results' messages */ |
| 1047 |
while (num_results-- > 0) { |
| 1048 |
CURLcode res; |
| 1049 |
CurlObject *co = NULL; |
| 1050 |
|
| 1051 |
if ((msg = curl_multi_info_read(self->multi_handle, &in_queue)) == NULL) { |
| 1052 |
break; |
| 1053 |
} |
| 1054 |
|
| 1055 |
/* Fetch the curl object that corresponds to the curl handle in the message */ |
| 1056 |
res = curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char **) &co); |
| 1057 |
if (res != CURLE_OK || co == NULL) { |
| 1058 |
Py_DECREF(err_list); |
| 1059 |
Py_DECREF(ok_list); |
| 1060 |
CURLERROR_MSG("Unable to fetch curl handle from curl object"); |
| 1061 |
} |
| 1062 |
assert(PyObject_IsInstance((PyObject *) co, (PyObject *) p_Curl_Type) == 1); |
| 1063 |
if (msg->msg != CURLMSG_DONE) { |
| 1064 |
/* FIXME: what does this mean ??? */ |
| 1065 |
} |
| 1066 |
if (msg->data.result == CURLE_OK) { |
| 1067 |
/* Append curl object to list of objects which succeeded */ |
| 1068 |
if (PyList_Append(ok_list, (PyObject *)co) != 0) { |
| 1069 |
goto error; |
| 1070 |
} |
| 1071 |
} |
| 1072 |
else { |
| 1073 |
/* Create a result tuple that will get added to err_list. */ |
| 1074 |
PyObject *error_str = NULL; |
| 1075 |
PyObject *v; |
| 1076 |
error_str = PyUnicode_DecodeLocale(co->error, "surrogateescape"); |
| 1077 |
if (error_str == NULL) { |
| 1078 |
goto error; |
| 1079 |
} |
| 1080 |
v = Py_BuildValue("(OiO)", (PyObject *)co, (int)msg->data.result, error_str); |
| 1081 |
/* Append curl object to list of objects which failed */ |
| 1082 |
if (v == NULL || PyList_Append(err_list, v) != 0) { |
| 1083 |
Py_XDECREF(error_str); |
| 1084 |
Py_XDECREF(v); |
| 1085 |
goto error; |
| 1086 |
} |
| 1087 |
Py_DECREF(v); |
| 1088 |
} |
| 1089 |
} |
| 1090 |
/* Return (number of queued messages, [ok_objects], [error_objects]) */ |
| 1091 |
ret = Py_BuildValue("(iOO)", in_queue, ok_list, err_list); |
| 1092 |
error: |
| 1093 |
Py_XDECREF(err_list); |
| 1094 |
Py_XDECREF(ok_list); |
| 1095 |
return ret; |
| 1096 |
} |
| 1097 |
|
| 1098 |
|
| 1099 |
/* --------------- select --------------- */ |
| 1100 |
|
| 1101 |
static PyObject * |
| 1102 |
do_multi_select(CurlMultiObject *self, PyObject *args) |
| 1103 |
{ |
| 1104 |
int max_fd = -1, n; |
| 1105 |
double timeout = -1.0; |
| 1106 |
struct timeval tv, *tvp; |
| 1107 |
CURLMcode res; |
| 1108 |
|
| 1109 |
if (!PyArg_ParseTuple(args, "d:select", &timeout)) { |
| 1110 |
return NULL; |
| 1111 |
} |
| 1112 |
if (check_multi_state(self, 1 | 2, "select") != 0) { |
| 1113 |
return NULL; |
| 1114 |
} |
| 1115 |
|
| 1116 |
if (timeout < 0 || timeout >= 365 * 24 * 60 * 60) { |
| 1117 |
PyErr_SetString(PyExc_OverflowError, "invalid timeout period"); |
| 1118 |
return NULL; |
| 1119 |
} else { |
| 1120 |
long seconds = (long)timeout; |
| 1121 |
timeout = timeout - (double)seconds; |
| 1122 |
assert(timeout >= 0.0); assert(timeout < 1.0); |
| 1123 |
tv.tv_sec = seconds; |
| 1124 |
tv.tv_usec = (long)(timeout*1000000.0); |
| 1125 |
tvp = &tv; |
| 1126 |
} |
| 1127 |
|
| 1128 |
FD_ZERO(&self->read_fd_set); |
| 1129 |
FD_ZERO(&self->write_fd_set); |
| 1130 |
FD_ZERO(&self->exc_fd_set); |
| 1131 |
|
| 1132 |
res = curl_multi_fdset(self->multi_handle, &self->read_fd_set, |
| 1133 |
&self->write_fd_set, &self->exc_fd_set, &max_fd); |
| 1134 |
if (res != CURLM_OK) { |
| 1135 |
CURLERROR_MSG("multi_fdset failed"); |
| 1136 |
} |
| 1137 |
|
| 1138 |
if (max_fd < 0) { |
| 1139 |
n = 0; |
| 1140 |
} |
| 1141 |
else { |
| 1142 |
Py_BEGIN_ALLOW_THREADS |
| 1143 |
n = select(max_fd + 1, &self->read_fd_set, &self->write_fd_set, &self->exc_fd_set, tvp); |
| 1144 |
Py_END_ALLOW_THREADS |
| 1145 |
/* info: like Python's socketmodule.c we do not raise an exception |
| 1146 |
* if select() fails - we'll leave it to the actual libcurl |
| 1147 |
* socket code to report any errors. |
| 1148 |
*/ |
| 1149 |
} |
| 1150 |
|
| 1151 |
return PyLong_FromLong(n); |
| 1152 |
} |
| 1153 |
|
| 1154 |
|
| 1155 |
static PyObject *do_curlmulti_getstate(CurlMultiObject *self, PyObject *Py_UNUSED(ignored)) |
| 1156 |
{ |
| 1157 |
PyErr_SetString(PyExc_TypeError, "CurlMulti objects do not support serialization"); |
| 1158 |
return NULL; |
| 1159 |
} |
| 1160 |
|
| 1161 |
|
| 1162 |
static PyObject *do_curlmulti_setstate(CurlMultiObject *self, PyObject *args) |
| 1163 |
{ |
| 1164 |
PyErr_SetString(PyExc_TypeError, "CurlMulti objects do not support deserialization"); |
| 1165 |
return NULL; |
| 1166 |
} |
| 1167 |
|
| 1168 |
|
| 1169 |
static PyObject *do_curlmulti_enter(CurlMultiObject *self, PyObject *Py_UNUSED(ignored)) |
| 1170 |
{ |
| 1171 |
Py_INCREF(self); |
| 1172 |
return (PyObject *)self; |
| 1173 |
} |
| 1174 |
|
| 1175 |
|
| 1176 |
static PyObject *do_curlmulti_exit(CurlMultiObject *self, PyObject *args) |
| 1177 |
{ |
| 1178 |
PyObject *exc_type, *exc, *tb; |
| 1179 |
|
| 1180 |
if (!PyArg_ParseTuple(args, "OOO:__exit__", &exc_type, &exc, &tb)) |
| 1181 |
return NULL; |
| 1182 |
|
| 1183 |
PyObject *result = PyObject_CallMethod((PyObject *)self, "close", NULL); |
| 1184 |
if (!result) { |
| 1185 |
return NULL; |
| 1186 |
} |
| 1187 |
Py_DECREF(result); |
| 1188 |
|
| 1189 |
Py_RETURN_NONE; |
| 1190 |
} |
| 1191 |
|
| 1192 |
|
| 1193 |
static int |
| 1194 |
do_multi_sq_contains(PyObject *o, PyObject *obj) |
| 1195 |
{ |
| 1196 |
CurlMultiObject *self = (CurlMultiObject *)o; |
| 1197 |
int rc; |
| 1198 |
|
| 1199 |
assert_multi_state(self); |
| 1200 |
|
| 1201 |
if (!PyObject_TypeCheck(obj, p_Curl_Type)) { |
| 1202 |
PyErr_SetString(PyExc_TypeError, |
| 1203 |
"__contains__ expects a Curl object"); |
| 1204 |
return -1; |
| 1205 |
} |
| 1206 |
|
| 1207 |
if (self->easy_object_dict == NULL) { |
| 1208 |
return 0; |
| 1209 |
} |
| 1210 |
|
| 1211 |
rc = PyDict_Contains(self->easy_object_dict, obj); |
| 1212 |
return rc; |
| 1213 |
} |
| 1214 |
|
| 1215 |
|
| 1216 |
/************************************************************************* |
| 1217 |
// type definitions |
| 1218 |
**************************************************************************/ |
| 1219 |
|
| 1220 |
/* --------------- methods --------------- */ |
| 1221 |
|
| 1222 |
PYCURL_INTERNAL PyMethodDef curlmultiobject_methods[] = { |
| 1223 |
{"add_handle", (PyCFunction)do_multi_add_handle, METH_VARARGS, multi_add_handle_doc}, |
| 1224 |
{"close", (PyCFunction)do_multi_close, METH_NOARGS, multi_close_doc}, |
| 1225 |
{"closed", (PyCFunction)do_multi_closed, METH_NOARGS, multi_closed_doc}, |
| 1226 |
{"fdset", (PyCFunction)do_multi_fdset, METH_NOARGS, multi_fdset_doc}, |
| 1227 |
{"info_read", (PyCFunction)do_multi_info_read, METH_VARARGS, multi_info_read_doc}, |
| 1228 |
{"perform", (PyCFunction)do_multi_perform, METH_NOARGS, multi_perform_doc}, |
| 1229 |
{"socket_action", (PyCFunction)do_multi_socket_action, METH_VARARGS, multi_socket_action_doc}, |
| 1230 |
{"socket_all", (PyCFunction)do_multi_socket_all, METH_NOARGS, multi_socket_all_doc}, |
| 1231 |
{"setopt", (PyCFunction)do_multi_setopt, METH_VARARGS, multi_setopt_doc}, |
| 1232 |
{"timeout", (PyCFunction)do_multi_timeout, METH_NOARGS, multi_timeout_doc}, |
| 1233 |
{"assign", (PyCFunction)do_multi_assign, METH_VARARGS, multi_assign_doc}, |
| 1234 |
{"unassign", (PyCFunction)do_multi_unassign, METH_VARARGS, multi_unassign_doc}, |
| 1235 |
{"remove_handle", (PyCFunction)do_multi_remove_handle, METH_VARARGS, multi_remove_handle_doc}, |
| 1236 |
{"select", (PyCFunction)do_multi_select, METH_VARARGS, multi_select_doc}, |
| 1237 |
{"__getstate__", (PyCFunction)do_curlmulti_getstate, METH_NOARGS, NULL}, |
| 1238 |
{"__setstate__", (PyCFunction)do_curlmulti_setstate, METH_VARARGS, NULL}, |
| 1239 |
{"__enter__", (PyCFunction)do_curlmulti_enter, METH_NOARGS, NULL}, |
| 1240 |
{"__exit__", (PyCFunction)do_curlmulti_exit, METH_VARARGS, NULL}, |
| 1241 |
{NULL, NULL, 0, NULL} |
| 1242 |
}; |
| 1243 |
|
| 1244 |
|
| 1245 |
/* --------------- Sequence methods --------------- */ |
| 1246 |
|
| 1247 |
|
| 1248 |
static PySequenceMethods curlmulti_as_sequence = { |
| 1249 |
0, /* sq_length */ |
| 1250 |
0, /* sq_concat */ |
| 1251 |
0, /* sq_repeat */ |
| 1252 |
0, /* sq_item */ |
| 1253 |
0, /* was_sq_slice */ |
| 1254 |
0, /* sq_ass_item */ |
| 1255 |
0, /* was_sq_ass_slice */ |
| 1256 |
(objobjproc)do_multi_sq_contains, /* sq_contains */ |
| 1257 |
0, /* sq_inplace_concat */ |
| 1258 |
0, /* sq_inplace_repeat */ |
| 1259 |
}; |
| 1260 |
|
| 1261 |
|
| 1262 |
/* --------------- setattr/getattr --------------- */ |
| 1263 |
|
| 1264 |
|
| 1265 |
PYCURL_INTERNAL PyObject * |
| 1266 |
do_multi_getattro(PyObject *o, PyObject *n) |
| 1267 |
{ |
| 1268 |
PyObject *v; |
| 1269 |
assert_multi_state((CurlMultiObject *)o); |
| 1270 |
v = PyObject_GenericGetAttr(o, n); |
| 1271 |
if( !v && PyErr_ExceptionMatches(PyExc_AttributeError) ) |
| 1272 |
{ |
| 1273 |
PyErr_Clear(); |
| 1274 |
v = my_getattro(o, n, ((CurlMultiObject *)o)->dict, |
| 1275 |
curlmultiobject_constants, curlmultiobject_methods); |
| 1276 |
} |
| 1277 |
return v; |
| 1278 |
} |
| 1279 |
|
| 1280 |
PYCURL_INTERNAL int |
| 1281 |
do_multi_setattro(PyObject *o, PyObject *n, PyObject *v) |
| 1282 |
{ |
| 1283 |
assert_multi_state((CurlMultiObject *)o); |
| 1284 |
return my_setattro(&((CurlMultiObject *)o)->dict, n, v); |
| 1285 |
} |
| 1286 |
|
| 1287 |
PYCURL_INTERNAL PyTypeObject CurlMulti_Type = { |
| 1288 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 1289 |
"pycurl.CurlMulti", /* tp_name */ |
| 1290 |
sizeof(CurlMultiObject), /* tp_basicsize */ |
| 1291 |
0, /* tp_itemsize */ |
| 1292 |
(destructor)do_multi_dealloc, /* tp_dealloc */ |
| 1293 |
0, /* tp_print */ |
| 1294 |
0, /* tp_getattr */ |
| 1295 |
0, /* tp_setattr */ |
| 1296 |
0, /* tp_reserved */ |
| 1297 |
0, /* tp_repr */ |
| 1298 |
0, /* tp_as_number */ |
| 1299 |
&curlmulti_as_sequence, /* tp_as_sequence */ |
| 1300 |
0, /* tp_as_mapping */ |
| 1301 |
0, /* tp_hash */ |
| 1302 |
0, /* tp_call */ |
| 1303 |
0, /* tp_str */ |
| 1304 |
(getattrofunc)do_multi_getattro, /* tp_getattro */ |
| 1305 |
(setattrofunc)do_multi_setattro, /* tp_setattro */ |
| 1306 |
0, /* tp_as_buffer */ |
| 1307 |
PYCURL_TYPE_FLAGS, /* tp_flags */ |
| 1308 |
multi_doc, /* tp_doc */ |
| 1309 |
(traverseproc)do_multi_traverse, /* tp_traverse */ |
| 1310 |
(inquiry)do_multi_clear, /* tp_clear */ |
| 1311 |
0, /* tp_richcompare */ |
| 1312 |
offsetof(CurlMultiObject, weakreflist), /* tp_weaklistoffset */ |
| 1313 |
0, /* tp_iter */ |
| 1314 |
0, /* tp_iternext */ |
| 1315 |
curlmultiobject_methods, /* tp_methods */ |
| 1316 |
0, /* tp_members */ |
| 1317 |
0, /* tp_getset */ |
| 1318 |
0, /* tp_base */ |
| 1319 |
0, /* tp_dict */ |
| 1320 |
0, /* tp_descr_get */ |
| 1321 |
0, /* tp_descr_set */ |
| 1322 |
0, /* tp_dictoffset */ |
| 1323 |
0, /* tp_init */ |
| 1324 |
PyType_GenericAlloc, /* tp_alloc */ |
| 1325 |
(newfunc)do_multi_new, /* tp_new */ |
| 1326 |
PyObject_GC_Del, /* tp_free */ |
| 1327 |
}; |
| 1328 |
|
| 1329 |
/* vi:ts=4:et:nowrap |
| 1330 |
*/ |