Debian Codegraph @

src/pycurl.h

raw
1 #if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
2 # define WIN32 1
3 #endif
4 #define PY_SSIZE_T_CLEAN
5 #include <Python.h>
6 #include <pythread.h>
7 #include <stddef.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <limits.h>
12 #include <sys/types.h>
13
14 #if !defined(WIN32)
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <sys/un.h>
19 #endif
20
21 #if defined(WIN32)
22 /*
23 * Since setup.py uses a '-WX' in the CFLAGS (treat warnings as errors),
24 * the below will turn off some warnings when using MS-SDK 8.1+.
25 * This MUST be defined before including <winsock2.h> via the libcurl
26 * headers.
27 */
28 # if !defined(_WINSOCK_DEPRECATED_NO_WARNINGS)
29 # define _WINSOCK_DEPRECATED_NO_WARNINGS
30 # endif
31 #endif
32
33 #include <curl/curl.h>
34 #include <curl/easy.h>
35 #include <curl/multi.h>
36 #undef NDEBUG
37 #include <assert.h>
38
39 /* Use for ignoring libcurl deprecation warnings */
40 #if defined(__clang__) || defined(__GNUC__)
41 #define PYCURL_IGNORE_DEPRECATED_BEGIN \
42 _Pragma("GCC diagnostic push") \
43 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
44 #define PYCURL_IGNORE_DEPRECATED_END \
45 _Pragma("GCC diagnostic pop")
46 #else
47 # define PYCURL_IGNORE_DEPRECATED_BEGIN
48 # define PYCURL_IGNORE_DEPRECATED_END
49 #endif
50
51 #define MAKE_LIBCURL_VERSION(major, minor, patch) \
52 ((major) * 0x10000 + (minor) * 0x100 + (patch))
53
54 /* spot check */
55 #if MAKE_LIBCURL_VERSION(7, 21, 16) != 0x071510
56 # error MAKE_LIBCURL_VERSION is not working correctly
57 #endif
58
59 #if defined(PYCURL_SINGLE_FILE)
60 # define PYCURL_INTERNAL static
61 #else
62 # define PYCURL_INTERNAL
63 #endif
64
65 #if defined(WIN32)
66 /* supposedly not present in errno.h provided with VC */
67 # if !defined(EAFNOSUPPORT)
68 # define EAFNOSUPPORT 97
69 # endif
70
71 PYCURL_INTERNAL curl_socket_t
72 dup_winsock(curl_socket_t sock, const struct curl_sockaddr *address);
73 #endif
74
75 /* The inet_ntop() was added in ws2_32.dll on Windows Vista [1]. Hence the
76 * Windows SDK targeting lesser OS'es doesn't provide that prototype.
77 * Maybe we should use the local hidden inet_ntop() for all OS'es thus
78 * making a pycurl.pyd work across OS'es w/o rebuilding?
79 *
80 * [1] http://msdn.microsoft.com/en-us/library/windows/desktop/cc805843(v=vs.85).aspx
81 */
82 #if defined(WIN32) && ((_WIN32_WINNT < 0x0600) || (NTDDI_VERSION < NTDDI_VISTA))
83 PYCURL_INTERNAL const char *
84 pycurl_inet_ntop (int family, void *addr, char *string, size_t string_size);
85 #define inet_ntop(fam,addr,string,size) pycurl_inet_ntop(fam,addr,string,size)
86 #endif
87
88 #if !defined(LIBCURL_VERSION_NUM) || (LIBCURL_VERSION_NUM < 0x071300)
89 # error "Need libcurl version 7.19.0 or greater to compile pycurl."
90 #endif
91
92 #if LIBCURL_VERSION_NUM >= 0x071301 /* check for 7.19.1 or greater */
93 #define HAVE_CURLOPT_USERNAME
94 #define HAVE_CURLOPT_PROXYUSERNAME
95 #define HAVE_CURLOPT_CERTINFO
96 #define HAVE_CURLOPT_POSTREDIR
97 #endif
98
99 #if LIBCURL_VERSION_NUM >= 0x071303 /* check for 7.19.3 or greater */
100 #define HAVE_CURLAUTH_DIGEST_IE
101 #endif
102
103 #if LIBCURL_VERSION_NUM >= 0x071304 /* check for 7.19.4 or greater */
104 #define HAVE_CURLOPT_NOPROXY
105 #define HAVE_CURLOPT_PROTOCOLS
106 #define HAVE_CURL_7_19_4_OPTS
107 #endif
108
109 #if LIBCURL_VERSION_NUM >= 0x071305 /* check for 7.19.5 or greater */
110 #define HAVE_CURL_7_19_5_OPTS
111 #endif
112
113 #if LIBCURL_VERSION_NUM >= 0x071306 /* check for 7.19.6 or greater */
114 #define HAVE_CURL_7_19_6_OPTS
115 #endif
116
117 #if LIBCURL_VERSION_NUM >= 0x071400 /* check for 7.20.0 or greater */
118 #define HAVE_CURL_7_20_0_OPTS
119 #endif
120
121 #if LIBCURL_VERSION_NUM >= 0x071500 /* check for 7.21.0 or greater */
122 #define HAVE_CURLINFO_LOCAL_PORT
123 #define HAVE_CURLINFO_PRIMARY_PORT
124 #define HAVE_CURLINFO_LOCAL_IP
125 #define HAVE_CURL_7_21_0_OPTS
126 #endif
127
128 #if LIBCURL_VERSION_NUM >= 0x071502 /* check for 7.21.2 or greater */
129 #define HAVE_CURL_7_21_2_OPTS
130 #endif
131
132 #if LIBCURL_VERSION_NUM >= 0x071503 /* check for 7.21.3 or greater */
133 #define HAVE_CURLOPT_RESOLVE
134 #endif
135
136 #if LIBCURL_VERSION_NUM >= 0x071505 /* check for 7.21.5 or greater */
137 #define HAVE_CURL_7_21_5
138 #endif
139
140 #if LIBCURL_VERSION_NUM >= 0x071600 /* check for 7.22.0 or greater */
141 #define HAVE_CURL_7_22_0_OPTS
142 #endif
143
144 #if LIBCURL_VERSION_NUM >= 0x071800 /* check for 7.24.0 or greater */
145 #define HAVE_CURLOPT_DNS_SERVERS
146 #define HAVE_CURL_7_24_0
147 #endif
148
149 #if LIBCURL_VERSION_NUM >= 0x071900 /* check for 7.25.0 or greater */
150 #define HAVE_CURL_7_25_0_OPTS
151 #endif
152
153 #if LIBCURL_VERSION_NUM >= 0x071A00 /* check for 7.26.0 or greater */
154 #define HAVE_CURL_REDIR_POST_303
155 #endif
156
157 #if LIBCURL_VERSION_NUM >= 0x071E00 /* check for 7.30.0 or greater */
158 #define HAVE_CURL_7_30_0_PIPELINE_OPTS
159 #endif
160
161 #if LIBCURL_VERSION_NUM >= 0x073100 /* check for 7.49.0 or greater */
162 #define HAVE_CURLOPT_CONNECT_TO
163 #endif
164
165 #if LIBCURL_VERSION_NUM >= 0x073200 /* check for 7.50.0 or greater */
166 #define HAVE_CURLINFO_HTTP_VERSION
167 #endif
168
169 #if LIBCURL_VERSION_NUM >= 0x073C00 /* check for 7.60.0 or greater */
170 #define HAVE_CURLOPT_HAPROXYPROTOCOL
171 #endif
172
173 /* curl_global_sslset() was added in 7.56.0 but was buggy until 7.63.0 */
174 #if LIBCURL_VERSION_NUM >= 0x073F00 /* check for 7.63.0 or greater */
175 #define HAVE_CURL_GLOBAL_SSLSET
176 #endif
177
178 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 56, 0)
179 #define HAVE_CURL_MIME
180 #endif
181
182 #if LIBCURL_VERSION_NUM >= 0x074300 /* check for 7.67.0 or greater */
183 #define HAVE_CURL_7_67_0_MULTI_STREAMS
184 #endif
185
186 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 86, 0)
187 #define HAVE_CURL_WEBSOCKETS
188 #include <curl/websockets.h>
189 #endif
190
191 #undef UNUSED
192 #define UNUSED(var) ((void)&var)
193
194 /* Cruft for thread safe SSL crypto locks, snapped from the PHP curl extension */
195 #if defined(HAVE_CURL_SSL)
196 # if defined(HAVE_CURL_OPENSSL)
197 # define PYCURL_NEED_SSL_TSL
198 # define PYCURL_NEED_OPENSSL_TSL
199 # include <openssl/ssl.h>
200 # include <openssl/err.h>
201 # define COMPILE_SSL_LIB "openssl"
202 # define COMPILE_SUPPORTED_SSL_BACKEND_FOUND 1
203 # elif defined(HAVE_CURL_WOLFSSL)
204 # include <wolfssl/options.h>
205 # if defined(OPENSSL_EXTRA)
206 # define HAVE_CURL_OPENSSL
207 # define PYCURL_NEED_SSL_TSL
208 # define PYCURL_NEED_OPENSSL_TSL
209 # include <wolfssl/openssl/ssl.h>
210 # include <wolfssl/openssl/err.h>
211 # else
212 # ifdef _MSC_VER
213 # pragma message(\
214 "libcurl was compiled with wolfSSL, but the library was built without " \
215 "--enable-opensslextra; thus no SSL crypto locking callbacks will be set, " \
216 "which may cause random crashes on SSL requests")
217 # else
218 # warning \
219 "libcurl was compiled with wolfSSL, but the library was built without " \
220 "--enable-opensslextra; thus no SSL crypto locking callbacks will be set, " \
221 "which may cause random crashes on SSL requests"
222 # endif
223 # endif
224 # define COMPILE_SSL_LIB "wolfssl"
225 # define COMPILE_SUPPORTED_SSL_BACKEND_FOUND 1
226 # elif defined(HAVE_CURL_GNUTLS)
227 # include <gnutls/gnutls.h>
228 # if GNUTLS_VERSION_NUMBER <= 0x020b00
229 # define PYCURL_NEED_SSL_TSL
230 # define PYCURL_NEED_GNUTLS_TSL
231 # include <gcrypt.h>
232 # endif
233 # define COMPILE_SSL_LIB "gnutls"
234 # define COMPILE_SUPPORTED_SSL_BACKEND_FOUND 1
235 # elif defined(HAVE_CURL_NSS)
236 # define COMPILE_SSL_LIB "nss"
237 # define COMPILE_SUPPORTED_SSL_BACKEND_FOUND 1
238 # elif defined(HAVE_CURL_MBEDTLS)
239 # include <mbedtls/ssl.h>
240 # define PYCURL_NEED_SSL_TSL
241 # define PYCURL_NEED_MBEDTLS_TSL
242 # define COMPILE_SSL_LIB "mbedtls"
243 # define COMPILE_SUPPORTED_SSL_BACKEND_FOUND 1
244 # elif defined(HAVE_CURL_SECTRANSP)
245 # define COMPILE_SSL_LIB "secure-transport"
246 # define COMPILE_SUPPORTED_SSL_BACKEND_FOUND 1
247 # elif defined(HAVE_CURL_SCHANNEL)
248 # define COMPILE_SSL_LIB "schannel"
249 # define COMPILE_SUPPORTED_SSL_BACKEND_FOUND 1
250 # else
251 # ifdef _MSC_VER
252 /* sigh */
253 # pragma message(\
254 "libcurl was compiled with SSL support, but configure could not determine which " \
255 "library was used; thus no SSL crypto locking callbacks will be set, which may " \
256 "cause random crashes on SSL requests")
257 # else
258 # warning \
259 "libcurl was compiled with SSL support, but configure could not determine which " \
260 "library was used; thus no SSL crypto locking callbacks will be set, which may " \
261 "cause random crashes on SSL requests"
262 # endif
263 /* since we have no crypto callbacks for other ssl backends,
264 * no reason to require users match those */
265 # define COMPILE_SSL_LIB "none/other"
266 # define COMPILE_SUPPORTED_SSL_BACKEND_FOUND 0
267 # endif /* HAVE_CURL_OPENSSL || HAVE_CURL_WOLFSSL || HAVE_CURL_GNUTLS || HAVE_CURL_NSS || HAVE_CURL_MBEDTLS || HAVE_CURL_SECTRANSP || HAVE_CURL_SCHANNEL */
268 #else
269 # define COMPILE_SSL_LIB "none/other"
270 # define COMPILE_SUPPORTED_SSL_BACKEND_FOUND 0
271 #endif /* HAVE_CURL_SSL */
272
273 #if defined(PYCURL_NEED_SSL_TSL)
274 PYCURL_INTERNAL int pycurl_ssl_init(void);
275 PYCURL_INTERNAL void pycurl_ssl_cleanup(void);
276 #endif
277
278 # define PYCURL_DECLARE_THREAD_STATE PyThreadState *tmp_state
279 # define PYCURL_ACQUIRE_THREAD() pycurl_acquire_thread(self, &tmp_state)
280 # define PYCURL_ACQUIRE_THREAD_MULTI() pycurl_acquire_thread_multi(self, &tmp_state)
281 # define PYCURL_RELEASE_THREAD() pycurl_release_thread(tmp_state)
282 # define PYCURL_END_CALLBACK(retval) \
283 PYCURL_RELEASE_THREAD(); \
284 return (retval)
285 /* Replacement for Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS when python
286 callbacks are expected during blocking i/o operations: self->state will hold
287 the handle to current thread to be used as context */
288 # define PYCURL_BEGIN_ALLOW_THREADS \
289 self->state = PyThreadState_Get(); \
290 assert(self->state != NULL); \
291 Py_BEGIN_ALLOW_THREADS
292 # define PYCURL_END_ALLOW_THREADS \
293 Py_END_ALLOW_THREADS \
294 self->state = NULL;
295 # define PYCURL_BEGIN_ALLOW_THREADS_EASY \
296 if (self->multi_stack == NULL) { \
297 self->state = PyThreadState_Get(); \
298 assert(self->state != NULL); \
299 } else { \
300 self->multi_stack->state = PyThreadState_Get(); \
301 assert(self->multi_stack->state != NULL); \
302 } \
303 Py_BEGIN_ALLOW_THREADS
304 # define PYCURL_END_ALLOW_THREADS_EASY \
305 PYCURL_END_ALLOW_THREADS \
306 if (self->multi_stack != NULL) \
307 self->multi_stack->state = NULL;
308
309 #if PY_VERSION_HEX < 0x030D0000 /* Python 3.13 */
310 # define Py_IsFinalizing _Py_IsFinalizing
311 #endif
312
313 #define PYCURL_BEGIN_CALLBACK_COMMON(acquire_expr, retval, callback_name) \
314 if (Py_IsFinalizing()) { \
315 return (retval); \
316 } \
317 if (!(acquire_expr)) { \
318 warn_failed_to_acquire_thread(#callback_name " failed to acquire thread"); \
319 return (retval); \
320 }
321
322 /* Convert socket values without truncation on Win64 where curl_socket_t is SOCKET. */
323 PYCURL_INTERNAL PyObject *
324 PyLong_FromCurlSocket(curl_socket_t sockfd);
325 PYCURL_INTERNAL int
326 PyLong_AsCurlSocket(PyObject *obj, curl_socket_t *sockfd);
327
328 #define PYLISTORTUPLE_LIST 1
329 #define PYLISTORTUPLE_TUPLE 2
330 #define PYLISTORTUPLE_OTHER 0
331
332 PYCURL_INTERNAL int
333 PyListOrTuple_Check(PyObject *v);
334 PYCURL_INTERNAL Py_ssize_t
335 PyListOrTuple_Size(PyObject *v, int which);
336 PYCURL_INTERNAL PyObject *
337 PyListOrTuple_GetItem(PyObject *v, Py_ssize_t i, int which);
338
339 /*************************************************************************
340 // string helpers
341 **************************************************************************/
342
343 PYCURL_INTERNAL int
344 PyText_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length, PyObject **encoded_obj);
345 PYCURL_INTERNAL char *
346 PyText_AsString_NoNUL(PyObject *obj, PyObject **encoded_obj);
347 PYCURL_INTERNAL int
348 PyText_Check(PyObject *o);
349 PYCURL_INTERNAL PyObject *
350 PyText_FromString_Ignore(const char *string);
351
352 PYCURL_INTERNAL int
353 callback_return_value_to_int(PyObject *ret_obj, const char *callback_name, int *ret_out);
354
355 struct CurlObject;
356
357 PYCURL_INTERNAL void
358 create_and_set_error_object(struct CurlObject *self, int code);
359
360
361 /* Raise exception based on return value `res' and `self->error' */
362 #define CURLERROR_RETVAL() do {\
363 create_and_set_error_object((self), (int) (res)); \
364 return NULL; \
365 } while (0)
366
367 #define CURLERROR_SET_RETVAL() \
368 create_and_set_error_object((self), (int) (res));
369
370 #define CURLERROR_RETVAL_MULTI_DONE() do {\
371 PyObject *v; \
372 v = Py_BuildValue("(i)", (int) (res)); \
373 if (v != NULL) { PyErr_SetObject(ErrorObject, v); Py_DECREF(v); } \
374 goto done; \
375 } while (0)
376
377 /* Raise exception based on return value `res' and custom message */
378 /* msg should be ASCII */
379 #define CURLERROR_MSG(msg) do {\
380 PyObject *v; const char *m = (msg); \
381 v = Py_BuildValue("(is)", (int) (res), (m)); \
382 if (v != NULL) { PyErr_SetObject(ErrorObject, v); Py_DECREF(v); } \
383 return NULL; \
384 } while (0)
385
386
387 /* Calculate the number of OBJECTPOINT options we need to store */
388 #define OPTIONS_SIZE ((int)CURLOPT_LASTENTRY % 10000)
389 #define MOPTIONS_SIZE ((int)CURLMOPT_LASTENTRY % 10000)
390
391 /* Memory groups */
392 /* Attributes dictionary */
393 #define PYCURL_MEMGROUP_ATTRDICT 1
394 /* multi_stack */
395 #define PYCURL_MEMGROUP_MULTI 2
396 /* Python callbacks */
397 #define PYCURL_MEMGROUP_CALLBACK 4
398 /* Python file objects */
399 #define PYCURL_MEMGROUP_FILE 8
400 /* Share objects */
401 #define PYCURL_MEMGROUP_SHARE 16
402 /* httppost buffer references */
403 #define PYCURL_MEMGROUP_HTTPPOST 32
404 /* Postfields object */
405 #define PYCURL_MEMGROUP_POSTFIELDS 64
406 /* CA certs object */
407 #define PYCURL_MEMGROUP_CACERTS 128
408 /* Curl slist objects */
409 #define PYCURL_MEMGROUP_SLIST 256
410 /* CurlMime object pinned via CURLOPT_MIMEPOST */
411 #define PYCURL_MEMGROUP_MIMEPOST 512
412
413 #define PYCURL_MEMGROUP_EASY \
414 (PYCURL_MEMGROUP_CALLBACK | PYCURL_MEMGROUP_FILE | \
415 PYCURL_MEMGROUP_HTTPPOST | PYCURL_MEMGROUP_POSTFIELDS | \
416 PYCURL_MEMGROUP_CACERTS | PYCURL_MEMGROUP_SLIST | \
417 PYCURL_MEMGROUP_MIMEPOST)
418
419 #define PYCURL_MEMGROUP_ALL \
420 (PYCURL_MEMGROUP_ATTRDICT | PYCURL_MEMGROUP_EASY | \
421 PYCURL_MEMGROUP_MULTI | PYCURL_MEMGROUP_SHARE)
422
423 typedef struct CurlSlistObject {
424 PyObject_HEAD
425 struct curl_slist *slist;
426 } CurlSlistObject;
427
428 typedef struct CurlHttppostObject {
429 PyObject_HEAD
430 struct curl_httppost *httppost;
431 /* List of INC'ed references associated with httppost. */
432 PyObject *reflist;
433 } CurlHttppostObject;
434
435 typedef struct CurlObject {
436 PyObject_HEAD
437 PyObject *dict; /* Python attributes dictionary */
438 // https://docs.python.org/3/extending/newtypes.html
439 PyObject *weakreflist;
440 CURL *handle;
441 PyThreadState *state;
442 PyObject *multi_weakref;
443 struct CurlMultiObject *multi_stack;
444 struct CurlShareObject *share;
445 struct CurlHttppostObject *httppost;
446 #ifdef HAVE_CURL_MIME
447 PyObject *mimepost_obj;
448 #endif
449 struct CurlSlistObject *httpheader;
450 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 37, 0)
451 struct CurlSlistObject *proxyheader;
452 #endif
453 struct CurlSlistObject *http200aliases;
454 struct CurlSlistObject *quote;
455 struct CurlSlistObject *postquote;
456 struct CurlSlistObject *prequote;
457 struct CurlSlistObject *telnetoptions;
458 #ifdef HAVE_CURLOPT_RESOLVE
459 struct CurlSlistObject *resolve;
460 #endif
461 #ifdef HAVE_CURL_7_20_0_OPTS
462 struct CurlSlistObject *mail_rcpt;
463 #endif
464 #ifdef HAVE_CURLOPT_CONNECT_TO
465 struct CurlSlistObject *connect_to;
466 #endif
467 /* callbacks */
468 PyObject *w_cb;
469 PyObject *h_cb;
470 PyObject *r_cb;
471 PyObject *pro_cb;
472 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 32, 0)
473 PyObject *xferinfo_cb;
474 #endif
475 PyObject *debug_cb;
476 PyObject *ioctl_cb;
477 PyObject *opensocket_cb;
478 #if LIBCURL_VERSION_NUM >= 0x071507 /* check for 7.21.7 or greater */
479 PyObject *closesocket_cb;
480 #endif
481 PyObject *seek_cb;
482 PyObject *sockopt_cb;
483 PyObject *ssh_key_cb;
484 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 80, 0)
485 PyObject *prereq_cb;
486 #endif
487 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 0)
488 PyObject *fnmatch_cb;
489 #endif
490 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 59, 0)
491 PyObject *resolver_start_cb;
492 #endif
493 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 64, 0)
494 PyObject *trailer_cb;
495 #endif
496 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 74, 0)
497 PyObject *hstsread_cb;
498 PyObject *hstswrite_cb;
499 #endif
500 /* file objects */
501 PyObject *readdata_fp;
502 PyObject *writedata_fp;
503 PyObject *writeheader_fp;
504 /* reference to the object used for CURLOPT_POSTFIELDS */
505 PyObject *postfields_obj;
506 /* reference to the object containing ca certs */
507 PyObject *ca_certs_obj;
508 /* true while executing WRITEFUNCTION for this handle */
509 int ws_write_cb_running;
510 /* misc */
511 char error[CURL_ERROR_SIZE+1];
512 } CurlObject;
513
514 typedef struct CurlMultiObject {
515 PyObject_HEAD
516 PyObject *dict; /* Python attributes dictionary */
517 // https://docs.python.org/3/extending/newtypes.html
518 PyObject *weakreflist;
519 CURLM *multi_handle;
520 PyThreadState *state;
521 fd_set read_fd_set;
522 fd_set write_fd_set;
523 fd_set exc_fd_set;
524 /* callbacks */
525 PyObject *t_cb;
526 PyObject *s_cb;
527
528 /* socket-to-object mappings for curl_multi_assign */
529 PyObject *socket_object_dict;
530
531 PyObject *easy_object_dict;
532 int close_handles; /* boolean: False by default */
533 } CurlMultiObject;
534
535 typedef struct {
536 PyThread_type_lock locks[CURL_LOCK_DATA_LAST];
537 } ShareLock;
538
539 typedef struct CurlShareObject {
540 PyObject_HEAD
541 PyObject *dict; /* Python attributes dictionary */
542 // https://docs.python.org/3/extending/newtypes.html
543 PyObject *weakreflist;
544 CURLSH *share_handle;
545 ShareLock *lock; /* lock object to implement CURLSHOPT_LOCKFUNC */
546 PyThread_type_lock easy_weakrefs_lock; /* protects easy_weakrefs map */
547 /* Set of weakref.ref(CurlObject) */
548 PyObject *easy_weakrefs;
549 int detach_on_close; /* boolean: True by default */
550 } CurlShareObject;
551
552 #ifdef HAVE_CURL_MIME
553 typedef struct CurlMimeObject {
554 PyObject_HEAD
555 struct CurlObject *curl;
556 curl_mime *mime;
557 PyObject *parts;
558 PyObject *submimes;
559 PyObject *data_cb_owners;
560 int owns_mime;
561 } CurlMimeObject;
562
563 typedef struct CurlMimePartObject {
564 PyObject_HEAD
565 CurlMimeObject *mime;
566 curl_mimepart *part;
567 PyObject *data_cb_owner;
568 } CurlMimePartObject;
569
570 PYCURL_INTERNAL void
571 curlmime_duphandle_incref_data_cb_owners(PyObject *mime_obj);
572 #endif
573
574 PYCURL_INTERNAL PyThreadState *
575 pycurl_get_thread_state(const CurlObject *self);
576 PYCURL_INTERNAL PyThreadState *
577 pycurl_get_thread_state_multi(const CurlMultiObject *self);
578 PYCURL_INTERNAL int
579 pycurl_acquire_thread(const CurlObject *self, PyThreadState **state);
580 PYCURL_INTERNAL int
581 pycurl_acquire_thread_multi(const CurlMultiObject *self, PyThreadState **state);
582 PYCURL_INTERNAL void
583 pycurl_release_thread(PyThreadState *state);
584
585 PYCURL_INTERNAL void
586 share_lock_lock(ShareLock *lock, curl_lock_data data);
587 PYCURL_INTERNAL void
588 share_lock_unlock(ShareLock *lock, curl_lock_data data);
589 PYCURL_INTERNAL ShareLock *
590 share_lock_new(void);
591 PYCURL_INTERNAL void
592 share_lock_destroy(ShareLock *lock);
593 PYCURL_INTERNAL void
594 share_lock_callback(CURL *handle, curl_lock_data data, curl_lock_access locktype, void *userptr);
595 PYCURL_INTERNAL void
596 share_unlock_callback(CURL *handle, curl_lock_data data, void *userptr);
597
598 PYCURL_INTERNAL PyObject *
599 my_getattro(PyObject *co, PyObject *name, PyObject *dict1, PyObject *dict2, PyMethodDef *m);
600 PYCURL_INTERNAL int
601 my_setattro(PyObject **dict, PyObject *name, PyObject *v);
602
603 /* used by multi object */
604 PYCURL_INTERNAL void
605 assert_curl_state(const CurlObject *self);
606
607 PYCURL_INTERNAL int
608 check_pending_python_signal(void);
609
610 PYCURL_INTERNAL int
611 check_pending_python_exception_or_signal(void);
612
613 PYCURL_INTERNAL void
614 warn_failed_to_acquire_thread(const char *warning_message);
615
616 PYCURL_INTERNAL void
617 print_callback_error_if_regular_exception(void);
618
619 PYCURL_INTERNAL PyObject *
620 do_global_init(PyObject *dummy, PyObject *args);
621 PYCURL_INTERNAL PyObject *
622 do_global_cleanup(PyObject *dummy, PyObject *Py_UNUSED(ignored));
623 PYCURL_INTERNAL PyObject *
624 do_version_info(PyObject *dummy, PyObject *args);
625
626 PYCURL_INTERNAL PyObject *
627 do_curl_setopt(CurlObject *self, PyObject *args);
628 PYCURL_INTERNAL PyObject *
629 do_curl_setopt_string(CurlObject *self, PyObject *args);
630 PYCURL_INTERNAL PyObject *
631 do_curl_unsetopt(CurlObject *self, PyObject *args);
632 #if defined(HAVE_CURL_OPENSSL)
633 PYCURL_INTERNAL PyObject *
634 do_curl_set_ca_certs(CurlObject *self, PyObject *args);
635 #endif
636 PYCURL_INTERNAL PyObject *
637 do_curl_perform(CurlObject *self, PyObject *Py_UNUSED(ignored));
638 PYCURL_INTERNAL PyObject *
639 do_curl_perform_rb(CurlObject *self, PyObject *Py_UNUSED(ignored));
640 PYCURL_INTERNAL PyObject *
641 do_curl_perform_rs(CurlObject *self, PyObject *Py_UNUSED(ignored));
642
643 PYCURL_INTERNAL PyObject *
644 do_curl_pause(CurlObject *self, PyObject *args);
645 PYCURL_INTERNAL PyObject *
646 do_curl_unpause(CurlObject *self, PyObject *Py_UNUSED(ignored));
647 PYCURL_INTERNAL PyObject *
648 do_curl_send(CurlObject *self, PyObject *args);
649 PYCURL_INTERNAL PyObject *
650 do_curl_recv(CurlObject *self, PyObject *args);
651 PYCURL_INTERNAL PyObject *
652 do_curl_recv_into(CurlObject *self, PyObject *args, PyObject *kwds);
653
654 PYCURL_INTERNAL PyObject *set_would_block_error(void);
655 PYCURL_INTERNAL int
656 check_easy_recv_send_result(CurlObject *self, CURLcode res);
657
658 #ifdef HAVE_CURL_WEBSOCKETS
659 PYCURL_INTERNAL PyObject *
660 do_curl_ws_send(CurlObject *self, PyObject *args, PyObject *kwds);
661 PYCURL_INTERNAL PyObject *
662 do_curl_ws_recv(CurlObject *self, PyObject *args);
663 PYCURL_INTERNAL PyObject *
664 do_curl_ws_recv_into(CurlObject *self, PyObject *args, PyObject *kwds);
665 PYCURL_INTERNAL PyObject *
666 do_curl_ws_meta(CurlObject *self, PyObject *Py_UNUSED(ignored));
667 PYCURL_INTERNAL PyObject *
668 do_curl_ws_close(CurlObject *self, PyObject *args, PyObject *kwds);
669 #endif
670
671 PYCURL_INTERNAL int
672 check_curl_state(const CurlObject *self, int flags, const char *name);
673 PYCURL_INTERNAL void
674 util_curl_xdecref(CurlObject *self, int flags, CURL *handle);
675 PYCURL_INTERNAL PyObject *
676 do_curl_setopt_filelike(CurlObject *self, int option, PyObject *obj);
677
678 PYCURL_INTERNAL int
679 util_curlslist_update(CurlSlistObject **old, struct curl_slist *slist);
680 PYCURL_INTERNAL int
681 util_curlhttppost_update(CurlObject *obj, struct curl_httppost *httppost, PyObject *reflist);
682
683 PYCURL_INTERNAL PyObject *
684 do_curl_getinfo_raw(CurlObject *self, PyObject *args);
685 PYCURL_INTERNAL PyObject *
686 do_curl_getinfo(CurlObject *self, PyObject *args);
687 PYCURL_INTERNAL PyObject *
688 do_curl_errstr(CurlObject *self, PyObject *Py_UNUSED(ignored));
689 PYCURL_INTERNAL PyObject *
690 do_curl_errstr_raw(CurlObject *self, PyObject *Py_UNUSED(ignored));
691
692 PYCURL_INTERNAL size_t
693 write_callback(char *ptr, size_t size, size_t nmemb, void *stream);
694 PYCURL_INTERNAL size_t
695 header_callback(char *ptr, size_t size, size_t nmemb, void *stream);
696 PYCURL_INTERNAL curl_socket_t
697 opensocket_callback(void *clientp, curlsocktype purpose,
698 struct curl_sockaddr *address);
699 PYCURL_INTERNAL int
700 sockopt_cb(void *clientp, curl_socket_t curlfd, curlsocktype purpose);
701 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 7)
702 PYCURL_INTERNAL int
703 closesocket_callback(void *clientp, curl_socket_t curlfd);
704 #endif
705 #ifdef HAVE_CURL_7_19_6_OPTS
706 PYCURL_INTERNAL int
707 ssh_key_cb(CURL *easy, const struct curl_khkey *knownkey,
708 const struct curl_khkey *foundkey, enum curl_khmatch khmatch,
709 void *clientp);
710 #endif
711 PYCURL_INTERNAL int
712 seek_callback(void *stream, curl_off_t offset, int origin);
713 PYCURL_INTERNAL size_t
714 read_callback(char *ptr, size_t size, size_t nmemb, void *stream);
715 PYCURL_INTERNAL int
716 progress_callback(void *stream,
717 double dltotal, double dlnow, double ultotal, double ulnow);
718 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 32, 0)
719 PYCURL_INTERNAL int
720 xferinfo_callback(void *stream,
721 curl_off_t dltotal, curl_off_t dlnow,
722 curl_off_t ultotal, curl_off_t ulnow);
723 #endif
724 PYCURL_INTERNAL int
725 debug_callback(CURL *curlobj, curl_infotype type,
726 char *buffer, size_t total_size, void *stream);
727 PYCURL_INTERNAL curlioerr
728 ioctl_callback(CURL *curlobj, int cmd, void *stream);
729 #if defined(HAVE_CURL_OPENSSL)
730 PYCURL_INTERNAL CURLcode
731 ssl_ctx_callback(CURL *curl, void *ssl_ctx, void *ptr);
732 #endif
733 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 80, 0)
734 PYCURL_INTERNAL int
735 prereq_callback(void *clientp, char *conn_primary_ip, char *conn_local_ip,
736 int conn_primary_port, int conn_local_port);
737 #endif
738 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 0)
739 PYCURL_INTERNAL int
740 fnmatch_callback(void *clientp, const char *pattern, const char *string);
741 #endif
742 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 59, 0)
743 PYCURL_INTERNAL int
744 resolver_start_callback(void *resolver_state, void *reserved, void *clientp);
745 #endif
746 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 64, 0)
747 PYCURL_INTERNAL int
748 trailer_callback(struct curl_slist **list, void *clientp);
749 #endif
750 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 74, 0)
751 PYCURL_INTERNAL CURLSTScode
752 hstsread_callback(CURL *easy, struct curl_hstsentry *e, void *clientp);
753 PYCURL_INTERNAL CURLSTScode
754 hstswrite_callback(CURL *easy, struct curl_hstsentry *e,
755 struct curl_index *i, void *clientp);
756 #endif
757
758 PYCURL_INTERNAL struct curl_slist *
759 pycurl_list_or_tuple_to_slist(int which, PyObject *obj, Py_ssize_t len);
760
761 PYCURL_INTERNAL int share_register_easy(struct CurlShareObject *share, struct CurlObject *easy);
762 PYCURL_INTERNAL void share_unregister_easy(struct CurlShareObject *share, struct CurlObject *easy);
763
764 #if !defined(PYCURL_SINGLE_FILE)
765 /* Type objects */
766 extern PyTypeObject Curl_Type;
767 extern PyTypeObject CurlSlist_Type;
768 extern PyTypeObject CurlHttppost_Type;
769 extern PyTypeObject CurlMulti_Type;
770 extern PyTypeObject CurlShare_Type;
771 #ifdef HAVE_CURL_MIME
772 extern PyTypeObject CurlMime_Type;
773 extern PyTypeObject CurlMimePart_Type;
774 #endif
775
776 extern PyObject *ErrorObject;
777 extern PyTypeObject *p_Curl_Type;
778 extern PyTypeObject *p_CurlSlist_Type;
779 extern PyTypeObject *p_CurlHttppost_Type;
780 extern PyTypeObject *p_CurlMulti_Type;
781 extern PyTypeObject *p_CurlShare_Type;
782 #ifdef HAVE_CURL_MIME
783 extern PyTypeObject *p_CurlMime_Type;
784 extern PyTypeObject *p_CurlMimePart_Type;
785 #endif
786 extern PyObject *khkey_type;
787 extern PyObject *curl_sockaddr_type;
788 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 74, 0)
789 extern PyObject *hsts_entry_type;
790 extern PyObject *hsts_index_type;
791 extern PyObject *datetime_type;
792 extern PyObject *utc_tz;
793 #endif
794 #ifdef HAVE_CURL_WEBSOCKETS
795 extern PyObject *ws_frame_type;
796 #endif
797
798 extern PyObject *curlobject_constants;
799 extern PyObject *curlmultiobject_constants;
800 extern PyObject *curlshareobject_constants;
801
802 extern char *g_pycurl_useragent;
803
804 extern PYCURL_INTERNAL char *empty_keywords[];
805 extern PYCURL_INTERNAL PyObject *bytesio;
806 extern PYCURL_INTERNAL PyObject *stringio;
807
808 extern PyMethodDef curlobject_methods[];
809 extern PyMethodDef curlshareobject_methods[];
810 extern PyMethodDef curlmultiobject_methods[];
811 #endif /* !PYCURL_SINGLE_FILE */
812
813 #define PYCURL_TYPE_FLAGS Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE
814
815
816 #ifdef PYCURL_AUTODETECT_CA
817 extern char *g_pycurl_autodetected_cainfo;
818 extern char *g_pycurl_autodetected_capath;
819 #endif
820
821 /* vi:ts=4:et:nowrap
822 */