Debian Codegraph @

src/easy.c

raw
1 #include "pycurl.h"
2 #include "docstrings.h"
3
4
5 /*************************************************************************
6 // CurlSlistObject
7 **************************************************************************/
8
9 PYCURL_INTERNAL int
10 util_curlslist_update(CurlSlistObject **old, struct curl_slist *slist)
11 {
12 CurlSlistObject *new_obj = PyObject_New(CurlSlistObject, p_CurlSlist_Type);
13 if (new_obj == NULL) {
14 return -1;
15 }
16 new_obj->slist = slist;
17 Py_XDECREF(*old);
18 *old = new_obj;
19 return 0;
20 }
21
22 PYCURL_INTERNAL void
23 do_curlslist_dealloc(CurlSlistObject *self) {
24 if (self->slist != NULL) {
25 curl_slist_free_all(self->slist);
26 self->slist = NULL;
27 }
28 CurlSlist_Type.tp_free(self);
29 }
30
31 PYCURL_INTERNAL PyTypeObject CurlSlist_Type = {
32 PyVarObject_HEAD_INIT(NULL, 0)
33 "pycurl.CurlSlist", /* tp_name */
34 sizeof(CurlSlistObject), /* tp_basicsize */
35 0, /* tp_itemsize */
36 (destructor)do_curlslist_dealloc, /* tp_dealloc */
37 0, /* tp_print / tp_vectorcall_offset */
38 0, /* tp_getattr */
39 0, /* tp_setattr */
40 0, /* tp_reserved / tp_as_async */
41 0, /* tp_repr */
42 0, /* tp_as_number */
43 0, /* tp_as_sequence */
44 0, /* tp_as_mapping */
45 0, /* tp_hash */
46 0, /* tp_call */
47 0, /* tp_str */
48 0, /* tp_getattro */
49 0, /* tp_setattro */
50 0, /* tp_as_buffer */
51 0, /* tp_flags */
52 0, /* tp_doc */
53 0, /* tp_traverse */
54 0, /* tp_clear */
55 0, /* tp_richcompare */
56 0, /* tp_weaklistoffset */
57 0, /* tp_iter */
58 0, /* tp_iternext */
59 0, /* tp_methods */
60 0, /* tp_members */
61 0, /* tp_getset */
62 0, /* tp_base */
63 0, /* tp_dict */
64 0, /* tp_descr_get */
65 0, /* tp_descr_set */
66 0, /* tp_dictoffset */
67 0, /* tp_init */
68 0, /* tp_alloc */
69 0, /* tp_new */
70 0, /* tp_free */
71 0, /* tp_is_gc */
72 0, /* tp_bases */
73 0, /* tp_mro */
74 0, /* tp_cache */
75 0, /* tp_subclasses */
76 0, /* tp_weaklist */
77 0, /* tp_del */
78 0, /* tp_version_tag */
79 0, /* tp_finalize */
80 0, /* tp_vectorcall */
81 };
82
83
84 /*************************************************************************
85 // CurlHttppostObject
86 **************************************************************************/
87
88 PYCURL_INTERNAL int
89 util_curlhttppost_update(CurlObject *obj, struct curl_httppost *httppost, PyObject *reflist)
90 {
91 CurlHttppostObject *new_obj = PyObject_New(CurlHttppostObject, p_CurlHttppost_Type);
92 if (new_obj == NULL) {
93 return -1;
94 }
95 new_obj->httppost = httppost;
96 new_obj->reflist = reflist;
97 Py_XDECREF(obj->httppost);
98 obj->httppost = new_obj;
99 return 0;
100 }
101
102 PYCURL_INTERNAL void
103 do_curlhttppost_dealloc(CurlHttppostObject *self) {
104 if (self->httppost != NULL) {
105 PYCURL_IGNORE_DEPRECATED_BEGIN
106 curl_formfree(self->httppost);
107 PYCURL_IGNORE_DEPRECATED_END
108 self->httppost = NULL;
109 }
110 Py_CLEAR(self->reflist);
111 CurlHttppost_Type.tp_free(self);
112 }
113
114 PYCURL_INTERNAL PyTypeObject CurlHttppost_Type = {
115 PyVarObject_HEAD_INIT(NULL, 0)
116 "pycurl.CurlHttppost", /* tp_name */
117 sizeof(CurlHttppostObject), /* tp_basicsize */
118 0, /* tp_itemsize */
119 (destructor)do_curlhttppost_dealloc, /* tp_dealloc */
120 0, /* tp_print / tp_vectorcall_offset */
121 0, /* tp_getattr */
122 0, /* tp_setattr */
123 0, /* tp_reserved / tp_as_async */
124 0, /* tp_repr */
125 0, /* tp_as_number */
126 0, /* tp_as_sequence */
127 0, /* tp_as_mapping */
128 0, /* tp_hash */
129 0, /* tp_call */
130 0, /* tp_str */
131 0, /* tp_getattro */
132 0, /* tp_setattro */
133 0, /* tp_as_buffer */
134 0, /* tp_flags */
135 0, /* tp_doc */
136 0, /* tp_traverse */
137 0, /* tp_clear */
138 0, /* tp_richcompare */
139 0, /* tp_weaklistoffset */
140 0, /* tp_iter */
141 0, /* tp_iternext */
142 0, /* tp_methods */
143 0, /* tp_members */
144 0, /* tp_getset */
145 0, /* tp_base */
146 0, /* tp_dict */
147 0, /* tp_descr_get */
148 0, /* tp_descr_set */
149 0, /* tp_dictoffset */
150 0, /* tp_init */
151 0, /* tp_alloc */
152 0, /* tp_new */
153 0, /* tp_free */
154 0, /* tp_is_gc */
155 0, /* tp_bases */
156 0, /* tp_mro */
157 0, /* tp_cache */
158 0, /* tp_subclasses */
159 0, /* tp_weaklist */
160 0, /* tp_del */
161 0, /* tp_version_tag */
162 0, /* tp_finalize */
163 0, /* tp_vectorcall */
164 };
165
166
167 /*************************************************************************
168 // static utility functions
169 **************************************************************************/
170
171
172 /* assert some CurlObject invariants */
173 PYCURL_INTERNAL void
174 assert_curl_state(const CurlObject *self)
175 {
176 assert(self != NULL);
177 assert(PyObject_IsInstance((PyObject *) self, (PyObject *) p_Curl_Type) == 1);
178 (void) pycurl_get_thread_state(self);
179 }
180
181
182 /* check state for methods */
183 PYCURL_INTERNAL int
184 check_curl_state(const CurlObject *self, int flags, const char *name)
185 {
186 assert_curl_state(self);
187 if ((flags & 1) && self->handle == NULL) {
188 PyErr_Format(ErrorObject, "cannot invoke %s() - no curl handle", name);
189 return -1;
190 }
191 if ((flags & 2) && pycurl_get_thread_state(self) != NULL) {
192 PyErr_Format(ErrorObject, "cannot invoke %s() - perform() is currently running", name);
193 return -1;
194 }
195 return 0;
196 }
197
198
199 /*************************************************************************
200 // CurlObject
201 **************************************************************************/
202
203 /* --------------- construct/destruct (i.e. open/close) --------------- */
204
205 /* initializer - used to initialize curl easy handles for use with pycurl */
206 static int
207 util_curl_init(CurlObject *self)
208 {
209 int res;
210
211 /* Set curl error buffer and zero it */
212 res = curl_easy_setopt(self->handle, CURLOPT_ERRORBUFFER, self->error);
213 if (res != CURLE_OK) {
214 return (-1);
215 }
216 memset(self->error, 0, sizeof(self->error));
217
218 /* Set backreference */
219 res = curl_easy_setopt(self->handle, CURLOPT_PRIVATE, (char *) self);
220 if (res != CURLE_OK) {
221 return (-1);
222 }
223
224 /* Enable NOPROGRESS by default, i.e. no progress output */
225 res = curl_easy_setopt(self->handle, CURLOPT_NOPROGRESS, (long)1);
226 if (res != CURLE_OK) {
227 return (-1);
228 }
229
230 /* Disable VERBOSE by default, i.e. no verbose output */
231 res = curl_easy_setopt(self->handle, CURLOPT_VERBOSE, (long)0);
232 if (res != CURLE_OK) {
233 return (-1);
234 }
235
236 /* Set default USERAGENT */
237 assert(g_pycurl_useragent);
238 res = curl_easy_setopt(self->handle, CURLOPT_USERAGENT, g_pycurl_useragent);
239 if (res != CURLE_OK) {
240 return (-1);
241 }
242
243 /* Set CAINFO/PROXY_CAINFO or CAPATH/PROXY_CAPATH if runtime autodetection enabled */
244 #ifdef PYCURL_AUTODETECT_CA
245 if (g_pycurl_autodetected_cainfo) {
246 res = curl_easy_setopt(self->handle, CURLOPT_CAINFO, g_pycurl_autodetected_cainfo);
247 if (res != CURLE_OK) {
248 return (-1);
249 }
250
251 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 52, 0)
252 res = curl_easy_setopt(self->handle, CURLOPT_PROXY_CAINFO, g_pycurl_autodetected_cainfo);
253 if (res != CURLE_OK) {
254 return (-1);
255 }
256 #endif
257 } else if (g_pycurl_autodetected_capath) {
258 res = curl_easy_setopt(self->handle, CURLOPT_CAPATH, g_pycurl_autodetected_capath);
259 if (res != CURLE_OK) {
260 return (-1);
261 }
262
263 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 52, 0)
264 res = curl_easy_setopt(self->handle, CURLOPT_PROXY_CAPATH, g_pycurl_autodetected_capath);
265 if (res != CURLE_OK) {
266 return (-1);
267 }
268 #endif
269 }
270 #endif
271
272 return (0);
273 }
274
275 /* constructor */
276 PYCURL_INTERNAL CurlObject *
277 do_curl_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
278 {
279 CurlObject *self;
280 int res;
281 int *ptr;
282
283 if (subtype == p_Curl_Type && !PyArg_ParseTupleAndKeywords(args, kwds, "", empty_keywords)) {
284 return NULL;
285 }
286
287 /* Allocate python curl object */
288 self = (CurlObject *) subtype->tp_alloc(subtype, 0);
289 if (self == NULL)
290 return NULL;
291
292 /* tp_alloc is expected to return zeroed memory */
293 for (ptr = (int *) &self->dict;
294 ptr < (int *) (((char *) self) + sizeof(CurlObject));
295 ++ptr)
296 assert(*ptr == 0);
297
298 /* Initialize curl handle */
299 self->handle = curl_easy_init();
300 if (self->handle == NULL)
301 goto error;
302
303 res = util_curl_init(self);
304 if (res < 0)
305 goto error;
306 /* Success - return new object */
307 return self;
308
309 error:
310 Py_DECREF(self); /* this also closes self->handle */
311 PyErr_SetString(ErrorObject, "initializing curl failed");
312 return NULL;
313 }
314
315 /* duphandle */
316 PYCURL_INTERNAL CurlObject *
317 do_curl_duphandle(CurlObject *self, PyObject *Py_UNUSED(ignored))
318 {
319 PyTypeObject *subtype;
320 CurlObject *dup;
321 int res;
322 int *ptr;
323
324 /* Allocate python curl object */
325 subtype = Py_TYPE(self);
326 dup = (CurlObject *) subtype->tp_alloc(subtype, 0);
327 if (dup == NULL)
328 return NULL;
329
330 /* tp_alloc is expected to return zeroed memory */
331 for (ptr = (int *) &dup->dict;
332 ptr < (int *) (((char *) dup) + sizeof(CurlObject));
333 ++ptr)
334 assert(*ptr == 0);
335
336 /* Clone the curl handle */
337 dup->handle = curl_easy_duphandle(self->handle);
338 if (dup->handle == NULL)
339 goto error;
340
341 /* Set curl error buffer and zero it */
342 res = curl_easy_setopt(dup->handle, CURLOPT_ERRORBUFFER, dup->error);
343 if (res != CURLE_OK) {
344 goto error;
345 }
346 memset(dup->error, 0, sizeof(dup->error));
347
348 /* Set backreference */
349 res = curl_easy_setopt(dup->handle, CURLOPT_PRIVATE, (char *) dup);
350 if (res != CURLE_OK) {
351 goto error;
352 }
353
354 /* Copy attribute dictionary */
355 if (self->dict != NULL) {
356 dup->dict = PyDict_Copy(self->dict);
357 if (dup->dict == NULL) {
358 goto error;
359 }
360 }
361
362 /* Checking for CURLE_OK is not required here.
363 * All values have already been successfully setopt'ed with self->handle. */
364
365 /* Assign and incref python callback and update data pointers */
366 if (self->w_cb != NULL) {
367 dup->w_cb = Py_NewRef(self->w_cb);
368 curl_easy_setopt(dup->handle, CURLOPT_WRITEDATA, dup);
369 }
370 if (self->h_cb != NULL) {
371 dup->h_cb = Py_NewRef(self->h_cb);
372 curl_easy_setopt(dup->handle, CURLOPT_WRITEHEADER, dup);
373 }
374 if (self->r_cb != NULL) {
375 dup->r_cb = Py_NewRef(self->r_cb);
376 curl_easy_setopt(dup->handle, CURLOPT_READDATA, dup);
377 }
378 if (self->pro_cb != NULL) {
379 dup->pro_cb = Py_NewRef(self->pro_cb);
380 curl_easy_setopt(dup->handle, CURLOPT_PROGRESSDATA, dup);
381 }
382 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 32, 0)
383 if (self->xferinfo_cb != NULL) {
384 dup->xferinfo_cb = Py_NewRef(self->xferinfo_cb);
385 curl_easy_setopt(dup->handle, CURLOPT_XFERINFODATA, dup);
386 }
387 #endif
388 if (self->debug_cb != NULL) {
389 dup->debug_cb = Py_NewRef(self->debug_cb);
390 curl_easy_setopt(dup->handle, CURLOPT_DEBUGDATA, dup);
391 }
392 if (self->ioctl_cb != NULL) {
393 dup->ioctl_cb = Py_NewRef(self->ioctl_cb);
394 PYCURL_IGNORE_DEPRECATED_BEGIN
395 curl_easy_setopt(dup->handle, CURLOPT_IOCTLDATA, dup);
396 PYCURL_IGNORE_DEPRECATED_END
397 }
398 if (self->opensocket_cb != NULL) {
399 dup->opensocket_cb = Py_NewRef(self->opensocket_cb);
400 curl_easy_setopt(dup->handle, CURLOPT_OPENSOCKETDATA, dup);
401 }
402 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 7)
403 if (self->closesocket_cb != NULL) {
404 dup->closesocket_cb = Py_NewRef(self->closesocket_cb);
405 curl_easy_setopt(dup->handle, CURLOPT_CLOSESOCKETDATA, dup);
406 }
407 #endif
408 if (self->sockopt_cb != NULL) {
409 dup->sockopt_cb = Py_NewRef(self->sockopt_cb);
410 curl_easy_setopt(dup->handle, CURLOPT_SOCKOPTDATA, dup);
411 }
412 #ifdef HAVE_CURL_7_19_6_OPTS
413 if (self->ssh_key_cb != NULL) {
414 dup->ssh_key_cb = Py_NewRef(self->ssh_key_cb);
415 curl_easy_setopt(dup->handle, CURLOPT_SSH_KEYDATA, dup);
416 }
417 #endif
418 if (self->seek_cb != NULL) {
419 dup->seek_cb = Py_NewRef(self->seek_cb);
420 curl_easy_setopt(dup->handle, CURLOPT_SEEKDATA, dup);
421 }
422 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 80, 0)
423 if (self->prereq_cb != NULL) {
424 dup->prereq_cb = Py_NewRef(self->prereq_cb);
425 curl_easy_setopt(dup->handle, CURLOPT_PREREQDATA, dup);
426 }
427 #endif
428 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 0)
429 if (self->fnmatch_cb != NULL) {
430 dup->fnmatch_cb = Py_NewRef(self->fnmatch_cb);
431 curl_easy_setopt(dup->handle, CURLOPT_FNMATCH_DATA, dup);
432 }
433 #endif
434 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 59, 0)
435 if (self->resolver_start_cb != NULL) {
436 dup->resolver_start_cb = Py_NewRef(self->resolver_start_cb);
437 curl_easy_setopt(dup->handle, CURLOPT_RESOLVER_START_DATA, dup);
438 }
439 #endif
440 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 64, 0)
441 if (self->trailer_cb != NULL) {
442 dup->trailer_cb = Py_NewRef(self->trailer_cb);
443 curl_easy_setopt(dup->handle, CURLOPT_TRAILERDATA, dup);
444 }
445 #endif
446 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 74, 0)
447 if (self->hstsread_cb != NULL) {
448 dup->hstsread_cb = Py_NewRef(self->hstsread_cb);
449 curl_easy_setopt(dup->handle, CURLOPT_HSTSREADDATA, dup);
450 }
451 if (self->hstswrite_cb != NULL) {
452 dup->hstswrite_cb = Py_NewRef(self->hstswrite_cb);
453 curl_easy_setopt(dup->handle, CURLOPT_HSTSWRITEDATA, dup);
454 }
455 #endif
456
457 /* Assign and incref python file objects */
458 dup->readdata_fp = Py_XNewRef(self->readdata_fp);
459 dup->writedata_fp = Py_XNewRef(self->writedata_fp);
460 dup->writeheader_fp = Py_XNewRef(self->writeheader_fp);
461
462 /* Assign and incref postfields object */
463 dup->postfields_obj = Py_XNewRef(self->postfields_obj);
464
465 /* Assign and incref ca certs related references */
466 dup->ca_certs_obj = Py_XNewRef(self->ca_certs_obj);
467
468 /* Assign and incref every curl_slist allocated by setopt */
469 dup->httpheader = (CurlSlistObject *)Py_XNewRef((PyObject *)self->httpheader);
470 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 37, 0)
471 dup->proxyheader = (CurlSlistObject *)Py_XNewRef((PyObject *)self->proxyheader);
472 #endif
473 dup->http200aliases = (CurlSlistObject *)Py_XNewRef((PyObject *)self->http200aliases);
474 dup->quote = (CurlSlistObject *)Py_XNewRef((PyObject *)self->quote);
475 dup->postquote = (CurlSlistObject *)Py_XNewRef((PyObject *)self->postquote);
476 dup->prequote = (CurlSlistObject *)Py_XNewRef((PyObject *)self->prequote);
477 dup->telnetoptions = (CurlSlistObject *)Py_XNewRef((PyObject *)self->telnetoptions);
478 #ifdef HAVE_CURLOPT_RESOLVE
479 dup->resolve = (CurlSlistObject *)Py_XNewRef((PyObject *)self->resolve);
480 #endif
481 #ifdef HAVE_CURL_7_20_0_OPTS
482 dup->mail_rcpt = (CurlSlistObject *)Py_XNewRef((PyObject *)self->mail_rcpt);
483 #endif
484 #ifdef HAVE_CURLOPT_CONNECT_TO
485 dup->connect_to = (CurlSlistObject *)Py_XNewRef((PyObject *)self->connect_to);
486 #endif
487
488 /* Assign and incref httppost */
489 dup->httppost = (CurlHttppostObject *)Py_XNewRef((PyObject *)self->httppost);
490 #ifdef HAVE_CURL_MIME
491 dup->mimepost_obj = Py_XNewRef(self->mimepost_obj);
492
493 if (self->mimepost_obj != NULL) {
494 /*
495 * libcurl duplicates curl_mime_data_cb userdata pointers as-is.
496 * Add one logical owner reference for every duplicated easy handle so
497 * free callbacks run exactly once on the final release path.
498 */
499 curlmime_duphandle_incref_data_cb_owners(self->mimepost_obj);
500 }
501 #endif
502
503 /* Success - return cloned object */
504 return dup;
505
506 error:
507 Py_CLEAR(dup->dict);
508 Py_DECREF(dup); /* this also closes dup->handle */
509 PyErr_SetString(ErrorObject, "cloning curl failed");
510 return NULL;
511 }
512
513
514 /* util function shared by close() and clear() */
515 PYCURL_INTERNAL void
516 util_curl_xdecref(CurlObject *self, int flags, CURL *handle)
517 {
518 if (flags & PYCURL_MEMGROUP_ATTRDICT) {
519 /* Decrement refcount for attributes dictionary. */
520 Py_CLEAR(self->dict);
521 }
522
523 if (flags & PYCURL_MEMGROUP_MULTI) {
524 /* multi_stack is borrowed; multi_weakref is owned */
525 self->multi_stack = NULL;
526 Py_CLEAR(self->multi_weakref);
527 }
528
529 if (flags & PYCURL_MEMGROUP_CALLBACK) {
530 /* Decrement refcount for python callbacks. */
531 Py_CLEAR(self->w_cb);
532 Py_CLEAR(self->h_cb);
533 Py_CLEAR(self->r_cb);
534 Py_CLEAR(self->pro_cb);
535 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 32, 0)
536 Py_CLEAR(self->xferinfo_cb);
537 #endif
538 Py_CLEAR(self->debug_cb);
539 Py_CLEAR(self->ioctl_cb);
540 Py_CLEAR(self->seek_cb);
541 Py_CLEAR(self->opensocket_cb);
542 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 7)
543 Py_CLEAR(self->closesocket_cb);
544 #endif
545 Py_CLEAR(self->sockopt_cb);
546 Py_CLEAR(self->ssh_key_cb);
547 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 80, 0)
548 Py_CLEAR(self->prereq_cb);
549 #endif
550 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 0)
551 Py_CLEAR(self->fnmatch_cb);
552 #endif
553 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 59, 0)
554 Py_CLEAR(self->resolver_start_cb);
555 #endif
556 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 64, 0)
557 Py_CLEAR(self->trailer_cb);
558 #endif
559 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 74, 0)
560 Py_CLEAR(self->hstsread_cb);
561 Py_CLEAR(self->hstswrite_cb);
562 #endif
563 }
564
565 if (flags & PYCURL_MEMGROUP_FILE) {
566 /* Decrement refcount for python file objects. */
567 Py_CLEAR(self->readdata_fp);
568 Py_CLEAR(self->writedata_fp);
569 Py_CLEAR(self->writeheader_fp);
570 }
571
572 if (flags & PYCURL_MEMGROUP_POSTFIELDS) {
573 /* Decrement refcount for postfields object */
574 Py_CLEAR(self->postfields_obj);
575 }
576
577 if (flags & PYCURL_MEMGROUP_SHARE) {
578 /* Decrement refcount for share objects. */
579 if (self->share != NULL) {
580 CurlShareObject *share = self->share;
581 share_unregister_easy(share, self);
582 self->share = NULL;
583 if (share->share_handle != NULL && handle != NULL) {
584 curl_easy_setopt(handle, CURLOPT_SHARE, NULL);
585 }
586 Py_DECREF(share);
587 }
588 }
589
590 if (flags & PYCURL_MEMGROUP_HTTPPOST) {
591 /* Decrement refcounts for httppost object. */
592 Py_CLEAR(self->httppost);
593 }
594
595 #ifdef HAVE_CURL_MIME
596 if (flags & PYCURL_MEMGROUP_MIMEPOST) {
597 if (self->mimepost_obj != NULL && handle != NULL) {
598 (void)curl_easy_setopt(handle, CURLOPT_MIMEPOST, NULL);
599 }
600 /* Decrement refcounts for mimepost object. */
601 Py_CLEAR(self->mimepost_obj);
602 }
603 #endif
604
605 if (flags & PYCURL_MEMGROUP_CACERTS) {
606 /* Decrement refcounts for ca certs related references. */
607 Py_CLEAR(self->ca_certs_obj);
608 }
609
610 if (flags & PYCURL_MEMGROUP_SLIST) {
611 /* Decrement refcounts for slist objects. */
612 Py_CLEAR(self->httpheader);
613 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 37, 0)
614 Py_CLEAR(self->proxyheader);
615 #endif
616 Py_CLEAR(self->http200aliases);
617 Py_CLEAR(self->quote);
618 Py_CLEAR(self->postquote);
619 Py_CLEAR(self->prequote);
620 Py_CLEAR(self->telnetoptions);
621 #ifdef HAVE_CURLOPT_RESOLVE
622 Py_CLEAR(self->resolve);
623 #endif
624 #ifdef HAVE_CURL_7_20_0_OPTS
625 Py_CLEAR(self->mail_rcpt);
626 #endif
627 #ifdef HAVE_CURLOPT_CONNECT_TO
628 Py_CLEAR(self->connect_to);
629 #endif
630 }
631 }
632
633
634 static void
635 util_easy_detach_from_multi(CurlObject *self, CURL *easy_handle)
636 {
637 CurlMultiObject *multi;
638
639 assert(self != NULL);
640
641 multi = self->multi_stack;
642 if (multi == NULL || multi->multi_handle == NULL || easy_handle == NULL) {
643 return;
644 }
645
646 /* Allow threads because callbacks can be invoked */
647 PYCURL_BEGIN_ALLOW_THREADS_EASY
648 (void) curl_multi_remove_handle(multi->multi_handle, easy_handle);
649 PYCURL_END_ALLOW_THREADS_EASY
650
651 if (multi->easy_object_dict != NULL && PyDict_DelItem(multi->easy_object_dict, (PyObject *)self) < 0) {
652 PyErr_Clear();
653 }
654 }
655
656
657 static void
658 util_curl_close(CurlObject *self)
659 {
660 CURL *handle;
661
662 /* Zero handle and thread-state to disallow any operations to be run
663 * from now on */
664 assert(self != NULL);
665 assert(PyObject_IsInstance((PyObject *) self, (PyObject *) p_Curl_Type) == 1);
666 handle = self->handle;
667 if (handle == NULL) {
668 /* Some paranoia assertions just to make sure the object
669 * deallocation problem is finally really fixed... */
670 assert(self->state == NULL);
671 assert(self->multi_stack == NULL);
672 assert(self->multi_weakref == NULL);
673 assert(self->share == NULL);
674 return; /* already closed */
675 }
676 self->state = NULL;
677
678 /* Decref multi stuff which uses this handle */
679 if (self->multi_stack != NULL) {
680 util_easy_detach_from_multi(self, handle);
681 }
682 util_curl_xdecref(self, PYCURL_MEMGROUP_MULTI, handle);
683 /* Decref share which uses this handle */
684 util_curl_xdecref(self, PYCURL_MEMGROUP_SHARE, handle);
685
686 /* Cleanup curl handle - must be done without the gil */
687 PYCURL_BEGIN_ALLOW_THREADS
688 curl_easy_cleanup(handle);
689 PYCURL_END_ALLOW_THREADS
690 self->handle = NULL;
691 handle = NULL;
692
693 /* Decref easy related objects */
694 util_curl_xdecref(self, PYCURL_MEMGROUP_EASY, handle);
695 }
696
697
698 PYCURL_INTERNAL void
699 do_curl_dealloc(CurlObject *self)
700 {
701 PyObject_GC_UnTrack(self);
702 Py_TRASHCAN_BEGIN(self, do_curl_dealloc);
703
704 Py_CLEAR(self->dict);
705 util_curl_close(self);
706
707 if (self->weakreflist != NULL) {
708 PyObject_ClearWeakRefs((PyObject *) self);
709 }
710
711 Curl_Type.tp_free(self);
712 Py_TRASHCAN_END;
713 }
714
715
716 static PyObject *
717 do_curl_close(CurlObject *self, PyObject *Py_UNUSED(ignored))
718 {
719 if (check_curl_state(self, 2, "close") != 0) {
720 return NULL;
721 }
722 util_curl_close(self);
723 Py_RETURN_NONE;
724 }
725
726
727 static PyObject *do_curl_closed(CurlObject *self, PyObject *Py_UNUSED(ignored))
728 {
729 if (self->handle == NULL) {
730 Py_RETURN_TRUE;
731 } else {
732 Py_RETURN_FALSE;
733 }
734 }
735
736
737 /* --------------- GC support --------------- */
738
739 /* Drop references that may have created reference cycles. */
740 PYCURL_INTERNAL int
741 do_curl_clear(CurlObject *self)
742 {
743 assert(pycurl_get_thread_state(self) == NULL);
744 util_curl_xdecref(self, PYCURL_MEMGROUP_ALL, self->handle);
745 return 0;
746 }
747
748 /* Traverse all refcounted objects. */
749 PYCURL_INTERNAL int
750 do_curl_traverse(CurlObject *self, visitproc visit, void *arg)
751 {
752 int err;
753 #undef VISIT
754 #define VISIT(v) if ((v) != NULL && ((err = visit(v, arg)) != 0)) return err
755
756 VISIT(self->dict);
757 VISIT(self->multi_weakref);
758 VISIT((PyObject *) self->share);
759
760 VISIT(self->w_cb);
761 VISIT(self->h_cb);
762 VISIT(self->r_cb);
763 VISIT(self->pro_cb);
764 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 32, 0)
765 VISIT(self->xferinfo_cb);
766 #endif
767 VISIT(self->debug_cb);
768 VISIT(self->ioctl_cb);
769 VISIT(self->seek_cb);
770 VISIT(self->opensocket_cb);
771 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 7)
772 VISIT(self->closesocket_cb);
773 #endif
774 VISIT(self->sockopt_cb);
775 VISIT(self->ssh_key_cb);
776 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 80, 0)
777 VISIT(self->prereq_cb);
778 #endif
779 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 0)
780 VISIT(self->fnmatch_cb);
781 #endif
782 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 59, 0)
783 VISIT(self->resolver_start_cb);
784 #endif
785 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 64, 0)
786 VISIT(self->trailer_cb);
787 #endif
788 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 74, 0)
789 VISIT(self->hstsread_cb);
790 VISIT(self->hstswrite_cb);
791 #endif
792
793 VISIT(self->readdata_fp);
794 VISIT(self->writedata_fp);
795 VISIT(self->writeheader_fp);
796
797 VISIT(self->postfields_obj);
798
799 #ifdef HAVE_CURL_MIME
800 VISIT(self->mimepost_obj);
801 #endif
802
803 VISIT(self->ca_certs_obj);
804
805 VISIT((PyObject *) self->httpheader);
806 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 37, 0)
807 VISIT((PyObject *) self->proxyheader);
808 #endif
809 VISIT((PyObject *) self->http200aliases);
810 VISIT((PyObject *) self->quote);
811 VISIT((PyObject *) self->postquote);
812 VISIT((PyObject *) self->prequote);
813 VISIT((PyObject *) self->telnetoptions);
814 #ifdef HAVE_CURLOPT_RESOLVE
815 VISIT((PyObject *) self->resolve);
816 #endif
817 #ifdef HAVE_CURL_7_20_0_OPTS
818 VISIT((PyObject *) self->mail_rcpt);
819 #endif
820 #ifdef HAVE_CURLOPT_CONNECT_TO
821 VISIT((PyObject *) self->connect_to);
822 #endif
823
824 VISIT((PyObject *) self->httppost);
825
826 return 0;
827 #undef VISIT
828 }
829
830
831 /* ------------------------ reset ------------------------ */
832
833 static PyObject*
834 do_curl_reset(CurlObject *self, PyObject *Py_UNUSED(ignored))
835 {
836 int res;
837
838 if (check_curl_state(self, 1 | 2, "reset") != 0) {
839 return NULL;
840 }
841
842 curl_easy_reset(self->handle);
843
844 /* Decref easy interface related objects */
845 util_curl_xdecref(self, PYCURL_MEMGROUP_EASY, self->handle);
846
847 res = util_curl_init(self);
848 if (res < 0) {
849 /* util_curl_init failed to re-set the default options; the libcurl
850 * handle is in an inconsistent state. Close it so subsequent calls
851 * fail predictably instead of crashing on NULL derefs inside libcurl. */
852 util_curl_close(self);
853 PyErr_SetString(ErrorObject, "resetting curl failed");
854 return NULL;
855 }
856
857 Py_RETURN_NONE;
858 }
859
860
861 static PyObject *do_curl_getstate(CurlObject *self, PyObject *Py_UNUSED(ignored))
862 {
863 PyErr_SetString(PyExc_TypeError, "Curl objects do not support serialization");
864 return NULL;
865 }
866
867
868 static PyObject *do_curl_setstate(CurlObject *self, PyObject *args)
869 {
870 PyErr_SetString(PyExc_TypeError, "Curl objects do not support deserialization");
871 return NULL;
872 }
873
874
875 static PyObject *do_curl_enter(CurlObject *self, PyObject *Py_UNUSED(ignored))
876 {
877 Py_INCREF(self);
878 return (PyObject *)self;
879 }
880
881
882 static PyObject *
883 do_curl_multi(CurlObject *self, PyObject *Py_UNUSED(ignored))
884 {
885 PyObject *obj = NULL;;
886
887 assert_curl_state(self);
888
889 if (self->multi_weakref == NULL) {
890 Py_RETURN_NONE;
891 }
892
893 #if PY_VERSION_HEX >= 0x030D0000 /* Python 3.13+ */
894 {
895 int rc = PyWeakref_GetRef(self->multi_weakref, &obj);
896 if (rc < 0) {
897 return NULL;
898 }
899 if (rc == 0 || obj == NULL) {
900 Py_RETURN_NONE;
901 }
902 }
903 #else
904 obj = PyWeakref_GetObject(self->multi_weakref);
905 if (obj == Py_None) {
906 Py_RETURN_NONE;
907 }
908 Py_INCREF(obj);
909 #endif
910
911 if (!PyObject_IsInstance(obj, (PyObject *)p_CurlMulti_Type)) {
912 Py_DECREF(obj);
913 PyErr_SetString(PyExc_TypeError, "multi object is not a CurlMulti");
914 return NULL;
915 }
916
917 return obj;
918 }
919
920
921 static PyObject *
922 do_curl_share(CurlObject *self, PyObject *Py_UNUSED(ignored))
923 {
924 assert_curl_state(self);
925
926 if (self->share == NULL) {
927 Py_RETURN_NONE;
928 }
929
930 if (!PyObject_IsInstance((PyObject *)self->share, (PyObject *)p_CurlShare_Type)) {
931 PyErr_SetString(PyExc_TypeError, "share object is not a CurlShare");
932 return NULL;
933 }
934
935 Py_INCREF(self->share);
936 return (PyObject *)self->share;
937 }
938
939
940 /*************************************************************************
941 // type definitions
942 **************************************************************************/
943
944 /* --------------- methods --------------- */
945
946 PYCURL_INTERNAL PyMethodDef curlobject_methods[] = {
947 {"close", (PyCFunction)do_curl_close, METH_NOARGS, curl_close_doc},
948 {"closed", (PyCFunction)do_curl_closed, METH_NOARGS, curl_closed_doc},
949 {"duphandle", (PyCFunction)do_curl_duphandle, METH_NOARGS, curl_duphandle_doc},
950 {"errstr", (PyCFunction)do_curl_errstr, METH_NOARGS, curl_errstr_doc},
951 {"errstr_raw", (PyCFunction)do_curl_errstr_raw, METH_NOARGS, curl_errstr_raw_doc},
952 {"getinfo", (PyCFunction)do_curl_getinfo, METH_VARARGS, curl_getinfo_doc},
953 {"getinfo_raw", (PyCFunction)do_curl_getinfo_raw, METH_VARARGS, curl_getinfo_raw_doc},
954 {"multi", (PyCFunction)do_curl_multi, METH_NOARGS, curl_multi_doc},
955 {"pause", (PyCFunction)do_curl_pause, METH_VARARGS, curl_pause_doc},
956 {"perform", (PyCFunction)do_curl_perform, METH_NOARGS, curl_perform_doc},
957 {"perform_rb", (PyCFunction)do_curl_perform_rb, METH_NOARGS, curl_perform_rb_doc},
958 {"perform_rs", (PyCFunction)do_curl_perform_rs, METH_NOARGS, curl_perform_rs_doc},
959 {"recv", (PyCFunction)do_curl_recv, METH_VARARGS, curl_recv_doc},
960 {"recv_into", (PyCFunction)do_curl_recv_into, METH_VARARGS | METH_KEYWORDS, curl_recv_into_doc},
961 {"reset", (PyCFunction)do_curl_reset, METH_NOARGS, curl_reset_doc},
962 {"send", (PyCFunction)do_curl_send, METH_VARARGS, curl_send_doc},
963 {"setopt", (PyCFunction)do_curl_setopt, METH_VARARGS, curl_setopt_doc},
964 {"setopt_string", (PyCFunction)do_curl_setopt_string, METH_VARARGS, curl_setopt_string_doc},
965 {"share", (PyCFunction)do_curl_share, METH_NOARGS, curl_share_doc},
966 {"unpause", (PyCFunction)do_curl_unpause, METH_NOARGS, curl_unpause_doc},
967 {"unsetopt", (PyCFunction)do_curl_unsetopt, METH_VARARGS, curl_unsetopt_doc},
968 #if defined(HAVE_CURL_OPENSSL)
969 {"set_ca_certs", (PyCFunction)do_curl_set_ca_certs, METH_VARARGS, curl_set_ca_certs_doc},
970 #endif
971 #ifdef HAVE_CURL_WEBSOCKETS
972 {"ws_send", (PyCFunction)do_curl_ws_send, METH_VARARGS | METH_KEYWORDS, curl_ws_send_doc},
973 {"ws_recv", (PyCFunction)do_curl_ws_recv, METH_VARARGS, curl_ws_recv_doc},
974 {"ws_recv_into", (PyCFunction)do_curl_ws_recv_into, METH_VARARGS | METH_KEYWORDS, curl_ws_recv_into_doc},
975 {"ws_meta", (PyCFunction)do_curl_ws_meta, METH_NOARGS, curl_ws_meta_doc},
976 {"ws_close", (PyCFunction)do_curl_ws_close, METH_VARARGS | METH_KEYWORDS, curl_ws_close_doc},
977 #endif
978 {"__getstate__", (PyCFunction)do_curl_getstate, METH_NOARGS, NULL},
979 {"__setstate__", (PyCFunction)do_curl_setstate, METH_VARARGS, NULL},
980 {"__enter__", (PyCFunction)do_curl_enter, METH_NOARGS, NULL},
981 {"__exit__", (PyCFunction)do_curl_close, METH_VARARGS, NULL},
982 {NULL, NULL, 0, NULL}
983 };
984
985
986 /* --------------- setattr/getattr --------------- */
987
988
989 PYCURL_INTERNAL PyObject *
990 do_curl_getattro(PyObject *o, PyObject *n)
991 {
992 PyObject *v = PyObject_GenericGetAttr(o, n);
993 if( !v && PyErr_ExceptionMatches(PyExc_AttributeError) )
994 {
995 PyErr_Clear();
996 v = my_getattro(o, n, ((CurlObject *)o)->dict,
997 curlobject_constants, curlobject_methods);
998 }
999 return v;
1000 }
1001
1002 PYCURL_INTERNAL int
1003 do_curl_setattro(PyObject *o, PyObject *name, PyObject *v)
1004 {
1005 assert_curl_state((CurlObject *)o);
1006 return my_setattro(&((CurlObject *)o)->dict, name, v);
1007 }
1008
1009 PYCURL_INTERNAL PyTypeObject Curl_Type = {
1010 PyVarObject_HEAD_INIT(NULL, 0)
1011 "pycurl.Curl", /* tp_name */
1012 sizeof(CurlObject), /* tp_basicsize */
1013 0, /* tp_itemsize */
1014 (destructor)do_curl_dealloc, /* tp_dealloc */
1015 0, /* tp_print */
1016 0, /* tp_getattr */
1017 0, /* tp_setattr */
1018 0, /* tp_reserved */
1019 0, /* tp_repr */
1020 0, /* tp_as_number */
1021 0, /* tp_as_sequence */
1022 0, /* tp_as_mapping */
1023 0, /* tp_hash */
1024 0, /* tp_call */
1025 0, /* tp_str */
1026 (getattrofunc)do_curl_getattro, /* tp_getattro */
1027 (setattrofunc)do_curl_setattro, /* tp_setattro */
1028 0, /* tp_as_buffer */
1029 PYCURL_TYPE_FLAGS, /* tp_flags */
1030 curl_doc, /* tp_doc */
1031 (traverseproc)do_curl_traverse, /* tp_traverse */
1032 (inquiry)do_curl_clear, /* tp_clear */
1033 0, /* tp_richcompare */
1034 offsetof(CurlObject, weakreflist), /* tp_weaklistoffset */
1035 0, /* tp_iter */
1036 0, /* tp_iternext */
1037 curlobject_methods, /* tp_methods */
1038 0, /* tp_members */
1039 0, /* tp_getset */
1040 0, /* tp_base */
1041 0, /* tp_dict */
1042 0, /* tp_descr_get */
1043 0, /* tp_descr_set */
1044 0, /* tp_dictoffset */
1045 0, /* tp_init */
1046 PyType_GenericAlloc, /* tp_alloc */
1047 (newfunc)do_curl_new, /* tp_new */
1048 PyObject_GC_Del, /* tp_free */
1049 };
1050
1051 /* vi:ts=4:et:nowrap
1052 */