| 1 |
#include "pycurl.h" |
| 2 |
|
| 3 |
|
| 4 |
PYCURL_INTERNAL struct curl_slist * |
| 5 |
pycurl_list_or_tuple_to_slist(int which, PyObject *obj, Py_ssize_t len) |
| 6 |
{ |
| 7 |
struct curl_slist *slist = NULL; |
| 8 |
Py_ssize_t i; |
| 9 |
|
| 10 |
for (i = 0; i < len; i++) { |
| 11 |
PyObject *listitem = PyListOrTuple_GetItem(obj, i, which); |
| 12 |
struct curl_slist *nlist; |
| 13 |
char *str; |
| 14 |
PyObject *sencoded_obj; |
| 15 |
|
| 16 |
if (!PyText_Check(listitem)) { |
| 17 |
curl_slist_free_all(slist); |
| 18 |
PyErr_SetString(PyExc_TypeError, "list items must be byte strings or Unicode strings with ASCII code points only"); |
| 19 |
return NULL; |
| 20 |
} |
| 21 |
/* INFO: curl_slist_append() internally does strdup() the data, so |
| 22 |
* no embedded NUL characters allowed here. */ |
| 23 |
str = PyText_AsString_NoNUL(listitem, &sencoded_obj); |
| 24 |
if (str == NULL) { |
| 25 |
curl_slist_free_all(slist); |
| 26 |
return NULL; |
| 27 |
} |
| 28 |
nlist = curl_slist_append(slist, str); |
| 29 |
Py_XDECREF(sencoded_obj); |
| 30 |
if (nlist == NULL || nlist->data == NULL) { |
| 31 |
curl_slist_free_all(slist); |
| 32 |
PyErr_NoMemory(); |
| 33 |
return NULL; |
| 34 |
} |
| 35 |
slist = nlist; |
| 36 |
} |
| 37 |
return slist; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
static PyObject * |
| 42 |
util_curl_unsetopt(CurlObject *self, int option) |
| 43 |
{ |
| 44 |
int res; |
| 45 |
CurlShareObject *share; |
| 46 |
|
| 47 |
#define SETOPT2(o,x) \ |
| 48 |
if ((res = curl_easy_setopt(self->handle, (o), (x))) != CURLE_OK) goto error |
| 49 |
#define SETOPT(x) SETOPT2((CURLoption)option, (x)) |
| 50 |
#define CLEAR_OBJECT(object_option, object_field) \ |
| 51 |
case object_option: \ |
| 52 |
if ((res = curl_easy_setopt(self->handle, object_option, NULL)) != CURLE_OK) \ |
| 53 |
goto error; \ |
| 54 |
Py_CLEAR(object_field); \ |
| 55 |
break |
| 56 |
#define CLEAR_CALLBACK(callback_option, data_option, callback_field) \ |
| 57 |
case callback_option: \ |
| 58 |
if ((res = curl_easy_setopt(self->handle, callback_option, NULL)) != CURLE_OK) \ |
| 59 |
goto error; \ |
| 60 |
if ((res = curl_easy_setopt(self->handle, data_option, NULL)) != CURLE_OK) \ |
| 61 |
goto error; \ |
| 62 |
Py_CLEAR(callback_field); \ |
| 63 |
break |
| 64 |
|
| 65 |
/* FIXME: implement more options. Have to carefully check lib/url.c in the |
| 66 |
* libcurl source code to see if it's actually safe to simply |
| 67 |
* unset the option. */ |
| 68 |
switch (option) |
| 69 |
{ |
| 70 |
case CURLOPT_SHARE: |
| 71 |
share = self->share; |
| 72 |
SETOPT((CURLSH *) NULL); |
| 73 |
if (share != NULL) { |
| 74 |
share_unregister_easy(share, self); |
| 75 |
Py_DECREF(share); |
| 76 |
self->share = NULL; |
| 77 |
} |
| 78 |
break; |
| 79 |
case CURLOPT_INFILESIZE: |
| 80 |
SETOPT((long) -1); |
| 81 |
break; |
| 82 |
case CURLOPT_WRITEHEADER: |
| 83 |
SETOPT((void *) 0); |
| 84 |
Py_CLEAR(self->writeheader_fp); |
| 85 |
break; |
| 86 |
case CURLOPT_CAINFO: |
| 87 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 77, 0) |
| 88 |
case CURLOPT_CAINFO_BLOB: |
| 89 |
#endif |
| 90 |
case CURLOPT_CAPATH: |
| 91 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 71, 0) |
| 92 |
case CURLOPT_SSLCERT_BLOB: |
| 93 |
case CURLOPT_SSLKEY_BLOB: |
| 94 |
case CURLOPT_ISSUERCERT_BLOB: |
| 95 |
#endif |
| 96 |
case CURLOPT_COOKIE: |
| 97 |
case CURLOPT_COOKIEJAR: |
| 98 |
case CURLOPT_CUSTOMREQUEST: |
| 99 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 100 |
case CURLOPT_EGDSOCKET: |
| 101 |
PYCURL_IGNORE_DEPRECATED_END |
| 102 |
case CURLOPT_ENCODING: |
| 103 |
case CURLOPT_FTPPORT: |
| 104 |
case CURLOPT_PROXYUSERPWD: |
| 105 |
#ifdef HAVE_CURLOPT_PROXYUSERNAME |
| 106 |
case CURLOPT_PROXYUSERNAME: |
| 107 |
case CURLOPT_PROXYPASSWORD: |
| 108 |
#endif |
| 109 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 110 |
case CURLOPT_RANDOM_FILE: |
| 111 |
PYCURL_IGNORE_DEPRECATED_END |
| 112 |
case CURLOPT_SSL_CIPHER_LIST: |
| 113 |
case CURLOPT_USERPWD: |
| 114 |
#ifdef HAVE_CURLOPT_USERNAME |
| 115 |
case CURLOPT_USERNAME: |
| 116 |
case CURLOPT_PASSWORD: |
| 117 |
#endif |
| 118 |
case CURLOPT_RANGE: |
| 119 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 10, 0) |
| 120 |
/* added by 0ff89b9c3c02a911e1e5ea9a4182c373a6e0f1c7 */ |
| 121 |
case CURLOPT_PROXY: |
| 122 |
#endif |
| 123 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 43, 0) |
| 124 |
case CURLOPT_SERVICE_NAME: |
| 125 |
case CURLOPT_PROXY_SERVICE_NAME: |
| 126 |
#endif |
| 127 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 52, 0) |
| 128 |
case CURLOPT_PROXY_CAPATH: |
| 129 |
case CURLOPT_PROXY_CAINFO: |
| 130 |
case CURLOPT_PRE_PROXY: |
| 131 |
case CURLOPT_PROXY_SSLCERT: |
| 132 |
case CURLOPT_PROXY_SSLCERTTYPE: |
| 133 |
case CURLOPT_PROXY_SSLKEY: |
| 134 |
case CURLOPT_PROXY_SSLKEYTYPE: |
| 135 |
case CURLOPT_PROXY_SSL_CIPHER_LIST: |
| 136 |
#endif |
| 137 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 77, 0) |
| 138 |
case CURLOPT_PROXY_CAINFO_BLOB: |
| 139 |
#endif |
| 140 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 71, 0) |
| 141 |
case CURLOPT_PROXY_SSLCERT_BLOB: |
| 142 |
case CURLOPT_PROXY_SSLKEY_BLOB: |
| 143 |
case CURLOPT_PROXY_ISSUERCERT_BLOB: |
| 144 |
#endif |
| 145 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 74, 0) |
| 146 |
case CURLOPT_HSTS: |
| 147 |
#endif |
| 148 |
SETOPT((char *) NULL); |
| 149 |
break; |
| 150 |
|
| 151 |
#ifdef HAVE_CURLOPT_CERTINFO |
| 152 |
case CURLOPT_CERTINFO: |
| 153 |
SETOPT((long) 0); |
| 154 |
break; |
| 155 |
#endif |
| 156 |
|
| 157 |
CLEAR_OBJECT(CURLOPT_HTTPHEADER, self->httpheader); |
| 158 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 37, 0) |
| 159 |
CLEAR_OBJECT(CURLOPT_PROXYHEADER, self->proxyheader); |
| 160 |
#endif |
| 161 |
CLEAR_OBJECT(CURLOPT_HTTP200ALIASES, self->http200aliases); |
| 162 |
CLEAR_OBJECT(CURLOPT_QUOTE, self->quote); |
| 163 |
CLEAR_OBJECT(CURLOPT_POSTQUOTE, self->postquote); |
| 164 |
CLEAR_OBJECT(CURLOPT_PREQUOTE, self->prequote); |
| 165 |
CLEAR_OBJECT(CURLOPT_TELNETOPTIONS, self->telnetoptions); |
| 166 |
#ifdef HAVE_CURLOPT_RESOLVE |
| 167 |
CLEAR_OBJECT(CURLOPT_RESOLVE, self->resolve); |
| 168 |
#endif |
| 169 |
#ifdef HAVE_CURL_7_20_0_OPTS |
| 170 |
CLEAR_OBJECT(CURLOPT_MAIL_RCPT, self->mail_rcpt); |
| 171 |
#endif |
| 172 |
#ifdef HAVE_CURLOPT_CONNECT_TO |
| 173 |
CLEAR_OBJECT(CURLOPT_CONNECT_TO, self->connect_to); |
| 174 |
#endif |
| 175 |
/* FIXME: what about data->set.httpreq ?? */ |
| 176 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 177 |
CLEAR_OBJECT(CURLOPT_HTTPPOST, self->httppost); |
| 178 |
PYCURL_IGNORE_DEPRECATED_END |
| 179 |
#ifdef HAVE_CURL_MIME |
| 180 |
case CURLOPT_MIMEPOST: |
| 181 |
if ((res = curl_easy_setopt(self->handle, CURLOPT_MIMEPOST, NULL)) != CURLE_OK) |
| 182 |
goto error; |
| 183 |
Py_CLEAR(self->mimepost_obj); |
| 184 |
break; |
| 185 |
#endif |
| 186 |
|
| 187 |
/* "If you do not use a write callback, you must make pointer a 'FILE*' |
| 188 |
* as libcurl passes this to fwrite(3) when writing data" (default: stdout) |
| 189 |
*/ |
| 190 |
case CURLOPT_WRITEFUNCTION: |
| 191 |
SETOPT(NULL); |
| 192 |
if ((res = curl_easy_setopt(self->handle, CURLOPT_WRITEDATA, stdout)) != CURLE_OK) |
| 193 |
goto error; |
| 194 |
Py_CLEAR(self->w_cb); |
| 195 |
break; |
| 196 |
|
| 197 |
/* Same for READFUNCTION, with stdin as the default */ |
| 198 |
case CURLOPT_READFUNCTION: |
| 199 |
SETOPT(NULL); |
| 200 |
if ((res = curl_easy_setopt(self->handle, CURLOPT_READDATA, stdin)) != CURLE_OK) |
| 201 |
goto error; |
| 202 |
Py_CLEAR(self->r_cb); |
| 203 |
break; |
| 204 |
|
| 205 |
CLEAR_CALLBACK(CURLOPT_HEADERFUNCTION, CURLOPT_WRITEHEADER, self->h_cb); |
| 206 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 207 |
CLEAR_CALLBACK(CURLOPT_PROGRESSFUNCTION, CURLOPT_PROGRESSDATA, self->pro_cb); |
| 208 |
PYCURL_IGNORE_DEPRECATED_END |
| 209 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 32, 0) |
| 210 |
CLEAR_CALLBACK(CURLOPT_XFERINFOFUNCTION, CURLOPT_XFERINFODATA, self->xferinfo_cb); |
| 211 |
#endif |
| 212 |
CLEAR_CALLBACK(CURLOPT_DEBUGFUNCTION, CURLOPT_DEBUGDATA, self->debug_cb); |
| 213 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 214 |
CLEAR_CALLBACK(CURLOPT_IOCTLFUNCTION, CURLOPT_IOCTLDATA, self->ioctl_cb); |
| 215 |
PYCURL_IGNORE_DEPRECATED_END |
| 216 |
CLEAR_CALLBACK(CURLOPT_OPENSOCKETFUNCTION, CURLOPT_OPENSOCKETDATA, self->opensocket_cb); |
| 217 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 7) |
| 218 |
CLEAR_CALLBACK(CURLOPT_CLOSESOCKETFUNCTION, CURLOPT_CLOSESOCKETDATA, self->closesocket_cb); |
| 219 |
#endif |
| 220 |
CLEAR_CALLBACK(CURLOPT_SOCKOPTFUNCTION, CURLOPT_SOCKOPTDATA, self->sockopt_cb); |
| 221 |
#ifdef HAVE_CURL_7_19_6_OPTS |
| 222 |
CLEAR_CALLBACK(CURLOPT_SSH_KEYFUNCTION, CURLOPT_SSH_KEYDATA, self->ssh_key_cb); |
| 223 |
#endif |
| 224 |
CLEAR_CALLBACK(CURLOPT_SEEKFUNCTION, CURLOPT_SEEKDATA, self->seek_cb); |
| 225 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 80, 0) |
| 226 |
CLEAR_CALLBACK(CURLOPT_PREREQFUNCTION, CURLOPT_PREREQDATA, self->prereq_cb); |
| 227 |
#endif |
| 228 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 0) |
| 229 |
CLEAR_CALLBACK(CURLOPT_FNMATCH_FUNCTION, CURLOPT_FNMATCH_DATA, self->fnmatch_cb); |
| 230 |
#endif |
| 231 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 59, 0) |
| 232 |
CLEAR_CALLBACK(CURLOPT_RESOLVER_START_FUNCTION, CURLOPT_RESOLVER_START_DATA, self->resolver_start_cb); |
| 233 |
#endif |
| 234 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 64, 0) |
| 235 |
CLEAR_CALLBACK(CURLOPT_TRAILERFUNCTION, CURLOPT_TRAILERDATA, self->trailer_cb); |
| 236 |
#endif |
| 237 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 74, 0) |
| 238 |
CLEAR_CALLBACK(CURLOPT_HSTSREADFUNCTION, CURLOPT_HSTSREADDATA, self->hstsread_cb); |
| 239 |
CLEAR_CALLBACK(CURLOPT_HSTSWRITEFUNCTION, CURLOPT_HSTSWRITEDATA, self->hstswrite_cb); |
| 240 |
#endif |
| 241 |
|
| 242 |
/* info: we explicitly list unsupported options here */ |
| 243 |
case CURLOPT_COOKIEFILE: |
| 244 |
default: |
| 245 |
PyErr_SetString(PyExc_TypeError, "unsetopt() is not supported for this option"); |
| 246 |
return NULL; |
| 247 |
} |
| 248 |
|
| 249 |
Py_RETURN_NONE; |
| 250 |
|
| 251 |
error: |
| 252 |
CURLERROR_RETVAL(); |
| 253 |
|
| 254 |
#undef SETOPT |
| 255 |
#undef SETOPT2 |
| 256 |
#undef CLEAR_CALLBACK |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
PYCURL_INTERNAL PyObject * |
| 261 |
do_curl_unsetopt(CurlObject *self, PyObject *args) |
| 262 |
{ |
| 263 |
int option; |
| 264 |
|
| 265 |
if (!PyArg_ParseTuple(args, "i:unsetopt", &option)) { |
| 266 |
return NULL; |
| 267 |
} |
| 268 |
if (check_curl_state(self, 1 | 2, "unsetopt") != 0) { |
| 269 |
return NULL; |
| 270 |
} |
| 271 |
|
| 272 |
/* early checks of option value */ |
| 273 |
if (option <= 0) |
| 274 |
goto error; |
| 275 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 71, 0) |
| 276 |
if (option >= (int)CURLOPTTYPE_BLOB + OPTIONS_SIZE) |
| 277 |
goto error; |
| 278 |
#else |
| 279 |
if (option >= (int)CURLOPTTYPE_OFF_T + OPTIONS_SIZE) |
| 280 |
goto error; |
| 281 |
#endif |
| 282 |
if (option % 10000 >= OPTIONS_SIZE) |
| 283 |
goto error; |
| 284 |
|
| 285 |
return util_curl_unsetopt(self, option); |
| 286 |
|
| 287 |
error: |
| 288 |
PyErr_SetString(PyExc_TypeError, "invalid arguments to unsetopt"); |
| 289 |
return NULL; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
static PyObject * |
| 294 |
do_curl_setopt_string_impl(CurlObject *self, int option, PyObject *obj) |
| 295 |
{ |
| 296 |
char *str = NULL; |
| 297 |
Py_ssize_t len = -1; |
| 298 |
PyObject *encoded_obj; |
| 299 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 71, 0) |
| 300 |
struct curl_blob curlblob; |
| 301 |
#endif |
| 302 |
int res; |
| 303 |
|
| 304 |
/* Check that the option specified a string as well as the input */ |
| 305 |
switch (option) { |
| 306 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 307 |
case CURLOPT_EGDSOCKET: |
| 308 |
case CURLOPT_KRBLEVEL: |
| 309 |
case CURLOPT_RANDOM_FILE: |
| 310 |
#ifdef HAVE_CURL_7_19_4_OPTS |
| 311 |
case CURLOPT_SOCKS5_GSSAPI_SERVICE: |
| 312 |
#endif |
| 313 |
PYCURL_IGNORE_DEPRECATED_END |
| 314 |
if (PyErr_WarnEx(PyExc_DeprecationWarning, "setopt option is deprecated", 1) != 0) { |
| 315 |
return NULL; |
| 316 |
} |
| 317 |
case CURLOPT_CAINFO: |
| 318 |
case CURLOPT_CAPATH: |
| 319 |
case CURLOPT_COOKIE: |
| 320 |
case CURLOPT_COOKIEFILE: |
| 321 |
case CURLOPT_COOKIELIST: |
| 322 |
case CURLOPT_COOKIEJAR: |
| 323 |
case CURLOPT_CUSTOMREQUEST: |
| 324 |
/* use CURLOPT_ENCODING instead of CURLOPT_ACCEPT_ENCODING |
| 325 |
for compatibility with older libcurls */ |
| 326 |
case CURLOPT_ENCODING: |
| 327 |
case CURLOPT_FTPPORT: |
| 328 |
case CURLOPT_INTERFACE: |
| 329 |
case CURLOPT_KEYPASSWD: |
| 330 |
case CURLOPT_NETRC_FILE: |
| 331 |
case CURLOPT_PROXY: |
| 332 |
case CURLOPT_PROXYUSERPWD: |
| 333 |
#ifdef HAVE_CURLOPT_PROXYUSERNAME |
| 334 |
case CURLOPT_PROXYUSERNAME: |
| 335 |
case CURLOPT_PROXYPASSWORD: |
| 336 |
#endif |
| 337 |
case CURLOPT_RANGE: |
| 338 |
case CURLOPT_REFERER: |
| 339 |
case CURLOPT_SSLCERT: |
| 340 |
case CURLOPT_SSLCERTTYPE: |
| 341 |
case CURLOPT_SSLENGINE: |
| 342 |
case CURLOPT_SSLKEY: |
| 343 |
case CURLOPT_SSLKEYTYPE: |
| 344 |
case CURLOPT_SSL_CIPHER_LIST: |
| 345 |
case CURLOPT_URL: |
| 346 |
case CURLOPT_USERAGENT: |
| 347 |
case CURLOPT_USERPWD: |
| 348 |
#ifdef HAVE_CURLOPT_USERNAME |
| 349 |
case CURLOPT_USERNAME: |
| 350 |
case CURLOPT_PASSWORD: |
| 351 |
#endif |
| 352 |
case CURLOPT_FTP_ALTERNATIVE_TO_USER: |
| 353 |
case CURLOPT_SSH_PUBLIC_KEYFILE: |
| 354 |
case CURLOPT_SSH_PRIVATE_KEYFILE: |
| 355 |
case CURLOPT_COPYPOSTFIELDS: |
| 356 |
case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5: |
| 357 |
case CURLOPT_CRLFILE: |
| 358 |
case CURLOPT_ISSUERCERT: |
| 359 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 20, 0) |
| 360 |
case CURLOPT_RTSP_STREAM_URI: |
| 361 |
case CURLOPT_RTSP_SESSION_ID: |
| 362 |
case CURLOPT_RTSP_TRANSPORT: |
| 363 |
#endif |
| 364 |
#ifdef HAVE_CURLOPT_DNS_SERVERS |
| 365 |
case CURLOPT_DNS_SERVERS: |
| 366 |
#endif |
| 367 |
#ifdef HAVE_CURLOPT_NOPROXY |
| 368 |
case CURLOPT_NOPROXY: |
| 369 |
#endif |
| 370 |
#ifdef HAVE_CURL_7_19_6_OPTS |
| 371 |
case CURLOPT_SSH_KNOWNHOSTS: |
| 372 |
#endif |
| 373 |
#ifdef HAVE_CURL_7_20_0_OPTS |
| 374 |
case CURLOPT_MAIL_FROM: |
| 375 |
#endif |
| 376 |
#ifdef HAVE_CURL_7_25_0_OPTS |
| 377 |
case CURLOPT_MAIL_AUTH: |
| 378 |
#endif |
| 379 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 39, 0) |
| 380 |
case CURLOPT_PINNEDPUBLICKEY: |
| 381 |
#endif |
| 382 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 43, 0) |
| 383 |
case CURLOPT_SERVICE_NAME: |
| 384 |
case CURLOPT_PROXY_SERVICE_NAME: |
| 385 |
#endif |
| 386 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 0) |
| 387 |
case CURLOPT_WILDCARDMATCH: |
| 388 |
#endif |
| 389 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 40, 0) |
| 390 |
case CURLOPT_UNIX_SOCKET_PATH: |
| 391 |
#endif |
| 392 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 4) |
| 393 |
case CURLOPT_TLSAUTH_TYPE: |
| 394 |
case CURLOPT_TLSAUTH_USERNAME: |
| 395 |
case CURLOPT_TLSAUTH_PASSWORD: |
| 396 |
#endif |
| 397 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 45, 0) |
| 398 |
case CURLOPT_DEFAULT_PROTOCOL: |
| 399 |
#endif |
| 400 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) |
| 401 |
case CURLOPT_LOGIN_OPTIONS: |
| 402 |
#endif |
| 403 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 33, 0) |
| 404 |
case CURLOPT_XOAUTH2_BEARER: |
| 405 |
#endif |
| 406 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 52, 0) |
| 407 |
case CURLOPT_PROXY_CAPATH: |
| 408 |
case CURLOPT_PROXY_CAINFO: |
| 409 |
case CURLOPT_PROXY_CRLFILE: |
| 410 |
case CURLOPT_PRE_PROXY: |
| 411 |
case CURLOPT_PROXY_SSLCERT: |
| 412 |
case CURLOPT_PROXY_SSLCERTTYPE: |
| 413 |
case CURLOPT_PROXY_SSLKEY: |
| 414 |
case CURLOPT_PROXY_SSLKEYTYPE: |
| 415 |
case CURLOPT_PROXY_KEYPASSWD: |
| 416 |
case CURLOPT_PROXY_PINNEDPUBLICKEY: |
| 417 |
case CURLOPT_PROXY_SSL_CIPHER_LIST: |
| 418 |
case CURLOPT_PROXY_TLSAUTH_TYPE: |
| 419 |
case CURLOPT_PROXY_TLSAUTH_USERNAME: |
| 420 |
case CURLOPT_PROXY_TLSAUTH_PASSWORD: |
| 421 |
#endif |
| 422 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 55, 0) |
| 423 |
case CURLOPT_REQUEST_TARGET: |
| 424 |
#endif |
| 425 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 61, 0) |
| 426 |
case CURLOPT_TLS13_CIPHERS: |
| 427 |
case CURLOPT_PROXY_TLS13_CIPHERS: |
| 428 |
#endif |
| 429 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 62, 0) |
| 430 |
case CURLOPT_DOH_URL: |
| 431 |
#endif |
| 432 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 71, 0) |
| 433 |
case CURLOPT_PROXY_ISSUERCERT: |
| 434 |
#endif |
| 435 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 74, 0) |
| 436 |
case CURLOPT_HSTS: |
| 437 |
#endif |
| 438 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 75, 0) |
| 439 |
case CURLOPT_AWS_SIGV4: |
| 440 |
#endif |
| 441 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(8, 2, 0) |
| 442 |
case CURLOPT_HAPROXY_CLIENT_IP: |
| 443 |
#endif |
| 444 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(8, 8, 0) |
| 445 |
case CURLOPT_ECH: |
| 446 |
#endif |
| 447 |
str = PyText_AsString_NoNUL(obj, &encoded_obj); |
| 448 |
if (str == NULL) |
| 449 |
return NULL; |
| 450 |
break; |
| 451 |
case CURLOPT_POSTFIELDS: |
| 452 |
if (PyText_AsStringAndSize(obj, &str, &len, &encoded_obj) != 0) |
| 453 |
return NULL; |
| 454 |
/* automatically set POSTFIELDSIZE */ |
| 455 |
if (len <= INT_MAX) { |
| 456 |
res = curl_easy_setopt(self->handle, CURLOPT_POSTFIELDSIZE, (long)len); |
| 457 |
} else { |
| 458 |
res = curl_easy_setopt(self->handle, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)len); |
| 459 |
} |
| 460 |
if (res != CURLE_OK) { |
| 461 |
Py_XDECREF(encoded_obj); |
| 462 |
CURLERROR_RETVAL(); |
| 463 |
} |
| 464 |
break; |
| 465 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 71, 0) |
| 466 |
case CURLOPT_SSLCERT_BLOB: |
| 467 |
case CURLOPT_SSLKEY_BLOB: |
| 468 |
case CURLOPT_PROXY_SSLCERT_BLOB: |
| 469 |
case CURLOPT_PROXY_SSLKEY_BLOB: |
| 470 |
case CURLOPT_ISSUERCERT_BLOB: |
| 471 |
case CURLOPT_PROXY_ISSUERCERT_BLOB: |
| 472 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 77, 0) |
| 473 |
case CURLOPT_CAINFO_BLOB: |
| 474 |
case CURLOPT_PROXY_CAINFO_BLOB: |
| 475 |
#endif |
| 476 |
if (PyText_AsStringAndSize(obj, &str, &len, &encoded_obj) != 0) |
| 477 |
return NULL; |
| 478 |
|
| 479 |
curlblob.data = str; |
| 480 |
curlblob.len = len; |
| 481 |
curlblob.flags = CURL_BLOB_COPY; |
| 482 |
|
| 483 |
res = curl_easy_setopt(self->handle, (CURLoption)option, &curlblob); |
| 484 |
if (res != CURLE_OK) { |
| 485 |
Py_XDECREF(encoded_obj); |
| 486 |
CURLERROR_RETVAL(); |
| 487 |
} |
| 488 |
Py_XDECREF(encoded_obj); |
| 489 |
Py_RETURN_NONE; |
| 490 |
break; |
| 491 |
#endif |
| 492 |
default: |
| 493 |
PyErr_SetString(PyExc_TypeError, "strings are not supported for this option"); |
| 494 |
return NULL; |
| 495 |
} |
| 496 |
assert(str != NULL); |
| 497 |
/* Call setopt */ |
| 498 |
res = curl_easy_setopt(self->handle, (CURLoption)option, str); |
| 499 |
/* Check for errors */ |
| 500 |
if (res != CURLE_OK) { |
| 501 |
Py_XDECREF(encoded_obj); |
| 502 |
CURLERROR_RETVAL(); |
| 503 |
} |
| 504 |
/* libcurl does not copy the value of CURLOPT_POSTFIELDS */ |
| 505 |
if (option == CURLOPT_POSTFIELDS) { |
| 506 |
PyObject *store_obj; |
| 507 |
|
| 508 |
/* if obj was bytes, it was not encoded, and we need to incref obj. |
| 509 |
* if obj was unicode, it was encoded, and we need to incref |
| 510 |
* encoded_obj - except we can simply transfer ownership. |
| 511 |
*/ |
| 512 |
if (encoded_obj) { |
| 513 |
store_obj = encoded_obj; |
| 514 |
} else { |
| 515 |
/* no encoding is performed, incref the original object. */ |
| 516 |
store_obj = obj; |
| 517 |
Py_INCREF(store_obj); |
| 518 |
} |
| 519 |
|
| 520 |
util_curl_xdecref(self, PYCURL_MEMGROUP_POSTFIELDS, self->handle); |
| 521 |
self->postfields_obj = store_obj; |
| 522 |
} else { |
| 523 |
Py_XDECREF(encoded_obj); |
| 524 |
} |
| 525 |
Py_RETURN_NONE; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
#define IS_LONG_OPTION(o) (o < CURLOPTTYPE_OBJECTPOINT) |
| 530 |
#define IS_OFF_T_OPTION(o) (o >= CURLOPTTYPE_OFF_T) |
| 531 |
|
| 532 |
|
| 533 |
static PyObject * |
| 534 |
do_curl_setopt_int(CurlObject *self, int option, PyObject *obj) |
| 535 |
{ |
| 536 |
long d; |
| 537 |
PY_LONG_LONG ld; |
| 538 |
int res; |
| 539 |
|
| 540 |
if (IS_LONG_OPTION(option)) { |
| 541 |
d = PyLong_AsLong(obj); |
| 542 |
if (d == -1 && PyErr_Occurred()) { |
| 543 |
return NULL; |
| 544 |
} |
| 545 |
res = curl_easy_setopt(self->handle, (CURLoption)option, (long)d); |
| 546 |
} else if (IS_OFF_T_OPTION(option)) { |
| 547 |
ld = PyLong_AsLongLong(obj); |
| 548 |
if (ld == -1 && PyErr_Occurred()) { |
| 549 |
return NULL; |
| 550 |
} |
| 551 |
res = curl_easy_setopt(self->handle, (CURLoption)option, (curl_off_t)ld); |
| 552 |
} else { |
| 553 |
PyErr_SetString(PyExc_TypeError, "integers are not supported for this option"); |
| 554 |
return NULL; |
| 555 |
} |
| 556 |
if (res != CURLE_OK) { |
| 557 |
CURLERROR_RETVAL(); |
| 558 |
} |
| 559 |
Py_RETURN_NONE; |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
#undef IS_LONG_OPTION |
| 564 |
#undef IS_OFF_T_OPTION |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
static PyObject * |
| 570 |
do_curl_setopt_httppost(CurlObject *self, int option, int which, PyObject *obj) |
| 571 |
{ |
| 572 |
struct curl_httppost *post = NULL; |
| 573 |
struct curl_httppost *last = NULL; |
| 574 |
/* List of all references that have been INCed as a result of |
| 575 |
* this operation */ |
| 576 |
PyObject *ref_params = NULL; |
| 577 |
PyObject *nencoded_obj, *cencoded_obj, *oencoded_obj; |
| 578 |
int which_httppost_item, which_httppost_option; |
| 579 |
PyObject *httppost_option; |
| 580 |
Py_ssize_t i, len; |
| 581 |
int res; |
| 582 |
|
| 583 |
if (PyErr_WarnEx(PyExc_DeprecationWarning, "HTTPPOST is deprecated; use MIMEPOST", 1) != 0) { |
| 584 |
return NULL; |
| 585 |
} |
| 586 |
|
| 587 |
len = PyListOrTuple_Size(obj, which); |
| 588 |
if (len == 0) |
| 589 |
Py_RETURN_NONE; |
| 590 |
|
| 591 |
for (i = 0; i < len; i++) { |
| 592 |
char *nstr = NULL, *cstr = NULL; |
| 593 |
Py_ssize_t nlen = -1, clen = -1; |
| 594 |
PyObject *listitem = PyListOrTuple_GetItem(obj, i, which); |
| 595 |
|
| 596 |
which_httppost_item = PyListOrTuple_Check(listitem); |
| 597 |
if (!which_httppost_item) { |
| 598 |
PyErr_SetString(PyExc_TypeError, "list items must be list or tuple objects"); |
| 599 |
goto error; |
| 600 |
} |
| 601 |
if (PyListOrTuple_Size(listitem, which_httppost_item) != 2) { |
| 602 |
PyErr_SetString(PyExc_TypeError, "list or tuple must contain two elements (name, value)"); |
| 603 |
goto error; |
| 604 |
} |
| 605 |
if (PyText_AsStringAndSize(PyListOrTuple_GetItem(listitem, 0, which_httppost_item), |
| 606 |
&nstr, &nlen, &nencoded_obj) != 0) { |
| 607 |
PyErr_SetString(PyExc_TypeError, "list or tuple must contain a byte string or Unicode string with ASCII code points only as first element"); |
| 608 |
goto error; |
| 609 |
} |
| 610 |
httppost_option = PyListOrTuple_GetItem(listitem, 1, which_httppost_item); |
| 611 |
if (PyText_Check(httppost_option)) { |
| 612 |
/* Handle strings as second argument for backwards compatibility */ |
| 613 |
|
| 614 |
if (PyText_AsStringAndSize(httppost_option, &cstr, &clen, &cencoded_obj)) { |
| 615 |
Py_XDECREF(nencoded_obj); |
| 616 |
create_and_set_error_object(self, CURLE_BAD_FUNCTION_ARGUMENT); |
| 617 |
goto error; |
| 618 |
} |
| 619 |
/* INFO: curl_formadd() internally does memdup() the data, so |
| 620 |
* embedded NUL characters _are_ allowed here. */ |
| 621 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 622 |
res = curl_formadd(&post, &last, |
| 623 |
CURLFORM_COPYNAME, nstr, |
| 624 |
CURLFORM_NAMELENGTH, (long) nlen, |
| 625 |
CURLFORM_COPYCONTENTS, cstr, |
| 626 |
CURLFORM_CONTENTSLENGTH, (long) clen, |
| 627 |
CURLFORM_END); |
| 628 |
PYCURL_IGNORE_DEPRECATED_END |
| 629 |
Py_XDECREF(cencoded_obj); |
| 630 |
if (res != CURLE_OK) { |
| 631 |
Py_XDECREF(nencoded_obj); |
| 632 |
CURLERROR_SET_RETVAL(); |
| 633 |
goto error; |
| 634 |
} |
| 635 |
} |
| 636 |
/* assignment is intended */ |
| 637 |
else if ((which_httppost_option = PyListOrTuple_Check(httppost_option))) { |
| 638 |
/* Supports content, file and content-type */ |
| 639 |
Py_ssize_t tlen = PyListOrTuple_Size(httppost_option, which_httppost_option); |
| 640 |
int j, k, l; |
| 641 |
struct curl_forms *forms = NULL; |
| 642 |
|
| 643 |
/* Sanity check that there are at least two tuple items */ |
| 644 |
if (tlen < 2) { |
| 645 |
Py_XDECREF(nencoded_obj); |
| 646 |
PyErr_SetString(PyExc_TypeError, "list or tuple must contain at least one option and one value"); |
| 647 |
goto error; |
| 648 |
} |
| 649 |
|
| 650 |
if (tlen % 2 == 1) { |
| 651 |
Py_XDECREF(nencoded_obj); |
| 652 |
PyErr_SetString(PyExc_TypeError, "list or tuple must contain an even number of items"); |
| 653 |
goto error; |
| 654 |
} |
| 655 |
|
| 656 |
/* Allocate enough space to accommodate length options for content or buffers, plus a terminator. */ |
| 657 |
forms = PyMem_New(struct curl_forms, (tlen*2) + 1); |
| 658 |
if (forms == NULL) { |
| 659 |
Py_XDECREF(nencoded_obj); |
| 660 |
PyErr_NoMemory(); |
| 661 |
goto error; |
| 662 |
} |
| 663 |
|
| 664 |
/* Iterate all the tuple members pairwise */ |
| 665 |
for (j = 0, k = 0, l = 0; j < tlen; j += 2, l++) { |
| 666 |
char *ostr; |
| 667 |
Py_ssize_t olen; |
| 668 |
int val; |
| 669 |
|
| 670 |
if (j == (tlen-1)) { |
| 671 |
PyErr_SetString(PyExc_TypeError, "expected value"); |
| 672 |
PyMem_Free(forms); |
| 673 |
Py_XDECREF(nencoded_obj); |
| 674 |
goto error; |
| 675 |
} |
| 676 |
if (!PyLong_Check(PyListOrTuple_GetItem(httppost_option, j, which_httppost_option))) { |
| 677 |
PyErr_SetString(PyExc_TypeError, "option must be an integer"); |
| 678 |
PyMem_Free(forms); |
| 679 |
Py_XDECREF(nencoded_obj); |
| 680 |
goto error; |
| 681 |
} |
| 682 |
if (!PyText_Check(PyListOrTuple_GetItem(httppost_option, j+1, which_httppost_option))) { |
| 683 |
PyErr_SetString(PyExc_TypeError, "value must be a byte string or a Unicode string with ASCII code points only"); |
| 684 |
PyMem_Free(forms); |
| 685 |
Py_XDECREF(nencoded_obj); |
| 686 |
goto error; |
| 687 |
} |
| 688 |
|
| 689 |
val = PyLong_AsLong(PyListOrTuple_GetItem(httppost_option, j, which_httppost_option)); |
| 690 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 691 |
if (val != CURLFORM_COPYCONTENTS && |
| 692 |
val != CURLFORM_FILE && |
| 693 |
val != CURLFORM_FILENAME && |
| 694 |
val != CURLFORM_CONTENTTYPE && |
| 695 |
val != CURLFORM_BUFFER && |
| 696 |
val != CURLFORM_BUFFERPTR) |
| 697 |
{ |
| 698 |
PyErr_SetString(PyExc_TypeError, "unsupported option"); |
| 699 |
PyMem_Free(forms); |
| 700 |
Py_XDECREF(nencoded_obj); |
| 701 |
goto error; |
| 702 |
} |
| 703 |
PYCURL_IGNORE_DEPRECATED_END |
| 704 |
|
| 705 |
if (PyText_AsStringAndSize(PyListOrTuple_GetItem(httppost_option, j+1, which_httppost_option), &ostr, &olen, &oencoded_obj)) { |
| 706 |
/* exception should be already set */ |
| 707 |
PyMem_Free(forms); |
| 708 |
Py_XDECREF(nencoded_obj); |
| 709 |
goto error; |
| 710 |
} |
| 711 |
forms[k].option = val; |
| 712 |
forms[k].value = ostr; |
| 713 |
++k; |
| 714 |
|
| 715 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 716 |
if (val == CURLFORM_COPYCONTENTS) { |
| 717 |
/* Contents can contain \0 bytes so we specify the length */ |
| 718 |
forms[k].option = CURLFORM_CONTENTSLENGTH; |
| 719 |
forms[k].value = (const char *)olen; |
| 720 |
++k; |
| 721 |
} else if (val == CURLFORM_BUFFERPTR) { |
| 722 |
PyObject *obj = NULL; |
| 723 |
|
| 724 |
if (ref_params == NULL) { |
| 725 |
ref_params = PyList_New((Py_ssize_t)0); |
| 726 |
if (ref_params == NULL) { |
| 727 |
Py_XDECREF(oencoded_obj); |
| 728 |
PyMem_Free(forms); |
| 729 |
Py_XDECREF(nencoded_obj); |
| 730 |
goto error; |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
/* Keep a reference to the object that holds the ostr buffer. */ |
| 735 |
if (oencoded_obj == NULL) { |
| 736 |
obj = PyListOrTuple_GetItem(httppost_option, j+1, which_httppost_option); |
| 737 |
} |
| 738 |
else { |
| 739 |
obj = oencoded_obj; |
| 740 |
} |
| 741 |
|
| 742 |
/* Ensure that the buffer remains alive until curl_easy_cleanup() */ |
| 743 |
if (PyList_Append(ref_params, obj) != 0) { |
| 744 |
Py_XDECREF(oencoded_obj); |
| 745 |
PyMem_Free(forms); |
| 746 |
Py_XDECREF(nencoded_obj); |
| 747 |
goto error; |
| 748 |
} |
| 749 |
|
| 750 |
/* As with CURLFORM_COPYCONTENTS, specify the length. */ |
| 751 |
forms[k].option = CURLFORM_BUFFERLENGTH; |
| 752 |
forms[k].value = (const char *)olen; |
| 753 |
++k; |
| 754 |
} |
| 755 |
PYCURL_IGNORE_DEPRECATED_END |
| 756 |
} |
| 757 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 758 |
forms[k].option = CURLFORM_END; |
| 759 |
res = curl_formadd(&post, &last, |
| 760 |
CURLFORM_COPYNAME, nstr, |
| 761 |
CURLFORM_NAMELENGTH, (long) nlen, |
| 762 |
CURLFORM_ARRAY, forms, |
| 763 |
CURLFORM_END); |
| 764 |
PYCURL_IGNORE_DEPRECATED_END |
| 765 |
Py_XDECREF(oencoded_obj); |
| 766 |
PyMem_Free(forms); |
| 767 |
if (res != CURLE_OK) { |
| 768 |
Py_XDECREF(nencoded_obj); |
| 769 |
CURLERROR_SET_RETVAL(); |
| 770 |
goto error; |
| 771 |
} |
| 772 |
} else { |
| 773 |
/* Some other type was given, ignore */ |
| 774 |
Py_XDECREF(nencoded_obj); |
| 775 |
PyErr_SetString(PyExc_TypeError, "unsupported second type in tuple"); |
| 776 |
goto error; |
| 777 |
} |
| 778 |
Py_XDECREF(nencoded_obj); |
| 779 |
} |
| 780 |
#ifdef HAVE_CURL_MIME |
| 781 |
if (self->mimepost_obj != NULL) { |
| 782 |
res = curl_easy_setopt(self->handle, CURLOPT_MIMEPOST, NULL); |
| 783 |
if (res != CURLE_OK) { |
| 784 |
CURLERROR_SET_RETVAL(); |
| 785 |
goto error; |
| 786 |
} |
| 787 |
} |
| 788 |
#endif |
| 789 |
|
| 790 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 791 |
res = curl_easy_setopt(self->handle, CURLOPT_HTTPPOST, post); |
| 792 |
PYCURL_IGNORE_DEPRECATED_END |
| 793 |
/* Check for errors */ |
| 794 |
if (res != CURLE_OK) { |
| 795 |
CURLERROR_SET_RETVAL(); |
| 796 |
goto error; |
| 797 |
} |
| 798 |
/* Finally, decref previous httppost object and replace it with a |
| 799 |
* new one. */ |
| 800 |
#ifdef HAVE_CURL_MIME |
| 801 |
/* |
| 802 |
* CURLOPT_HTTPPOST and CURLOPT_MIMEPOST are mutually exclusive in libcurl. |
| 803 |
* Avoid resetting CURLOPT_MIMEPOST after CURLOPT_HTTPPOST succeeds because |
| 804 |
* that can clear the newly-set HTTPPOST payload on some libcurl versions. |
| 805 |
*/ |
| 806 |
util_curl_xdecref(self, PYCURL_MEMGROUP_MIMEPOST, NULL); |
| 807 |
#endif |
| 808 |
if (util_curlhttppost_update(self, post, ref_params) != 0) { |
| 809 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 810 |
(void)curl_easy_setopt(self->handle, CURLOPT_HTTPPOST, NULL); |
| 811 |
PYCURL_IGNORE_DEPRECATED_END |
| 812 |
goto error; |
| 813 |
} |
| 814 |
|
| 815 |
Py_RETURN_NONE; |
| 816 |
|
| 817 |
error: |
| 818 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 819 |
curl_formfree(post); |
| 820 |
PYCURL_IGNORE_DEPRECATED_END |
| 821 |
Py_XDECREF(ref_params); |
| 822 |
return NULL; |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
static PyObject * |
| 827 |
do_curl_setopt_list(CurlObject *self, int option, int which, PyObject *obj) |
| 828 |
{ |
| 829 |
CurlSlistObject **old_slist_obj = NULL; |
| 830 |
struct curl_slist *slist = NULL; |
| 831 |
Py_ssize_t len; |
| 832 |
int res; |
| 833 |
|
| 834 |
switch (option) { |
| 835 |
case CURLOPT_HTTP200ALIASES: |
| 836 |
old_slist_obj = &self->http200aliases; |
| 837 |
break; |
| 838 |
case CURLOPT_HTTPHEADER: |
| 839 |
old_slist_obj = &self->httpheader; |
| 840 |
break; |
| 841 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 37, 0) |
| 842 |
case CURLOPT_PROXYHEADER: |
| 843 |
old_slist_obj = &self->proxyheader; |
| 844 |
break; |
| 845 |
#endif |
| 846 |
case CURLOPT_POSTQUOTE: |
| 847 |
old_slist_obj = &self->postquote; |
| 848 |
break; |
| 849 |
case CURLOPT_PREQUOTE: |
| 850 |
old_slist_obj = &self->prequote; |
| 851 |
break; |
| 852 |
case CURLOPT_QUOTE: |
| 853 |
old_slist_obj = &self->quote; |
| 854 |
break; |
| 855 |
case CURLOPT_TELNETOPTIONS: |
| 856 |
old_slist_obj = &self->telnetoptions; |
| 857 |
break; |
| 858 |
#ifdef HAVE_CURLOPT_RESOLVE |
| 859 |
case CURLOPT_RESOLVE: |
| 860 |
old_slist_obj = &self->resolve; |
| 861 |
break; |
| 862 |
#endif |
| 863 |
#ifdef HAVE_CURL_7_20_0_OPTS |
| 864 |
case CURLOPT_MAIL_RCPT: |
| 865 |
old_slist_obj = &self->mail_rcpt; |
| 866 |
break; |
| 867 |
#endif |
| 868 |
#ifdef HAVE_CURLOPT_CONNECT_TO |
| 869 |
case CURLOPT_CONNECT_TO: |
| 870 |
old_slist_obj = &self->connect_to; |
| 871 |
break; |
| 872 |
#endif |
| 873 |
default: |
| 874 |
/* None of the list options were recognized, raise exception */ |
| 875 |
PyErr_SetString(PyExc_TypeError, "lists are not supported for this option"); |
| 876 |
return NULL; |
| 877 |
} |
| 878 |
|
| 879 |
len = PyListOrTuple_Size(obj, which); |
| 880 |
if (len == 0) |
| 881 |
Py_RETURN_NONE; |
| 882 |
|
| 883 |
/* Just to be sure we do not bug off here */ |
| 884 |
assert(old_slist_obj != NULL && slist == NULL); |
| 885 |
|
| 886 |
/* Handle regular list operations on the other options */ |
| 887 |
slist = pycurl_list_or_tuple_to_slist(which, obj, len); |
| 888 |
if (slist == NULL) { |
| 889 |
return NULL; |
| 890 |
} |
| 891 |
res = curl_easy_setopt(self->handle, (CURLoption)option, slist); |
| 892 |
/* Check for errors */ |
| 893 |
if (res != CURLE_OK) { |
| 894 |
curl_slist_free_all(slist); |
| 895 |
CURLERROR_RETVAL(); |
| 896 |
} |
| 897 |
/* Finally, decref previous slist object and replace it with a |
| 898 |
* new one. */ |
| 899 |
if (util_curlslist_update(old_slist_obj, slist) != 0) { |
| 900 |
(void)curl_easy_setopt(self->handle, (CURLoption)option, NULL); |
| 901 |
curl_slist_free_all(slist); |
| 902 |
return NULL; |
| 903 |
} |
| 904 |
|
| 905 |
Py_RETURN_NONE; |
| 906 |
} |
| 907 |
|
| 908 |
|
| 909 |
static PyObject * |
| 910 |
do_curl_setopt_callable(CurlObject *self, int option, PyObject *obj) |
| 911 |
{ |
| 912 |
/* We use function types here to make sure that our callback |
| 913 |
* definitions exactly match the <curl/curl.h> interface. |
| 914 |
*/ |
| 915 |
const curl_write_callback w_cb = write_callback; |
| 916 |
const curl_write_callback h_cb = header_callback; |
| 917 |
const curl_read_callback r_cb = read_callback; |
| 918 |
const curl_progress_callback pro_cb = progress_callback; |
| 919 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 32, 0) |
| 920 |
const curl_xferinfo_callback xferinfo_cb = xferinfo_callback; |
| 921 |
#endif |
| 922 |
const curl_debug_callback debug_cb = debug_callback; |
| 923 |
const curl_ioctl_callback ioctl_cb = ioctl_callback; |
| 924 |
const curl_opensocket_callback opensocket_cb = opensocket_callback; |
| 925 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 7) |
| 926 |
const curl_closesocket_callback closesocket_cb = closesocket_callback; |
| 927 |
#endif |
| 928 |
const curl_seek_callback seek_cb = seek_callback; |
| 929 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 80, 0) |
| 930 |
const curl_prereq_callback prereq_cb = prereq_callback; |
| 931 |
#endif |
| 932 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 0) |
| 933 |
const curl_fnmatch_callback fnmatch_cb = fnmatch_callback; |
| 934 |
#endif |
| 935 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 59, 0) |
| 936 |
const curl_resolver_start_callback resolver_start_cb = resolver_start_callback; |
| 937 |
#endif |
| 938 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 64, 0) |
| 939 |
const curl_trailer_callback trailer_cb = trailer_callback; |
| 940 |
#endif |
| 941 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 74, 0) |
| 942 |
const curl_hstsread_callback hstsread_cb = hstsread_callback; |
| 943 |
const curl_hstswrite_callback hstswrite_cb = hstswrite_callback; |
| 944 |
#endif |
| 945 |
|
| 946 |
switch(option) { |
| 947 |
case CURLOPT_WRITEFUNCTION: |
| 948 |
Py_INCREF(obj); |
| 949 |
Py_CLEAR(self->writedata_fp); |
| 950 |
Py_CLEAR(self->w_cb); |
| 951 |
self->w_cb = obj; |
| 952 |
curl_easy_setopt(self->handle, CURLOPT_WRITEFUNCTION, w_cb); |
| 953 |
curl_easy_setopt(self->handle, CURLOPT_WRITEDATA, self); |
| 954 |
break; |
| 955 |
case CURLOPT_HEADERFUNCTION: |
| 956 |
Py_INCREF(obj); |
| 957 |
Py_CLEAR(self->writeheader_fp); |
| 958 |
Py_CLEAR(self->h_cb); |
| 959 |
self->h_cb = obj; |
| 960 |
curl_easy_setopt(self->handle, CURLOPT_HEADERFUNCTION, h_cb); |
| 961 |
curl_easy_setopt(self->handle, CURLOPT_WRITEHEADER, self); |
| 962 |
break; |
| 963 |
case CURLOPT_READFUNCTION: |
| 964 |
Py_INCREF(obj); |
| 965 |
Py_CLEAR(self->readdata_fp); |
| 966 |
Py_CLEAR(self->r_cb); |
| 967 |
self->r_cb = obj; |
| 968 |
curl_easy_setopt(self->handle, CURLOPT_READFUNCTION, r_cb); |
| 969 |
curl_easy_setopt(self->handle, CURLOPT_READDATA, self); |
| 970 |
break; |
| 971 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 972 |
case CURLOPT_PROGRESSFUNCTION: |
| 973 |
if (PyErr_WarnEx(PyExc_DeprecationWarning, "PROGRESSFUNCTION is deprecated; use XFERINFOFUNCTION", 1) != 0) { |
| 974 |
return NULL; |
| 975 |
} |
| 976 |
Py_INCREF(obj); |
| 977 |
Py_CLEAR(self->pro_cb); |
| 978 |
self->pro_cb = obj; |
| 979 |
curl_easy_setopt(self->handle, CURLOPT_PROGRESSFUNCTION, pro_cb); |
| 980 |
curl_easy_setopt(self->handle, CURLOPT_PROGRESSDATA, self); |
| 981 |
break; |
| 982 |
PYCURL_IGNORE_DEPRECATED_END |
| 983 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 32, 0) |
| 984 |
case CURLOPT_XFERINFOFUNCTION: |
| 985 |
Py_INCREF(obj); |
| 986 |
Py_CLEAR(self->xferinfo_cb); |
| 987 |
self->xferinfo_cb = obj; |
| 988 |
curl_easy_setopt(self->handle, CURLOPT_XFERINFOFUNCTION, xferinfo_cb); |
| 989 |
curl_easy_setopt(self->handle, CURLOPT_XFERINFODATA, self); |
| 990 |
break; |
| 991 |
#endif |
| 992 |
case CURLOPT_DEBUGFUNCTION: |
| 993 |
Py_INCREF(obj); |
| 994 |
Py_CLEAR(self->debug_cb); |
| 995 |
self->debug_cb = obj; |
| 996 |
curl_easy_setopt(self->handle, CURLOPT_DEBUGFUNCTION, debug_cb); |
| 997 |
curl_easy_setopt(self->handle, CURLOPT_DEBUGDATA, self); |
| 998 |
break; |
| 999 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 1000 |
case CURLOPT_IOCTLFUNCTION: |
| 1001 |
if (PyErr_WarnEx(PyExc_DeprecationWarning, "IOCTLFUNCTION is deprecated; use SEEKFUNCTION", 1) != 0) { |
| 1002 |
return NULL; |
| 1003 |
} |
| 1004 |
Py_INCREF(obj); |
| 1005 |
Py_CLEAR(self->ioctl_cb); |
| 1006 |
self->ioctl_cb = obj; |
| 1007 |
curl_easy_setopt(self->handle, CURLOPT_IOCTLFUNCTION, ioctl_cb); |
| 1008 |
curl_easy_setopt(self->handle, CURLOPT_IOCTLDATA, self); |
| 1009 |
break; |
| 1010 |
PYCURL_IGNORE_DEPRECATED_END |
| 1011 |
case CURLOPT_OPENSOCKETFUNCTION: |
| 1012 |
Py_INCREF(obj); |
| 1013 |
Py_CLEAR(self->opensocket_cb); |
| 1014 |
self->opensocket_cb = obj; |
| 1015 |
curl_easy_setopt(self->handle, CURLOPT_OPENSOCKETFUNCTION, opensocket_cb); |
| 1016 |
curl_easy_setopt(self->handle, CURLOPT_OPENSOCKETDATA, self); |
| 1017 |
break; |
| 1018 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 7) |
| 1019 |
case CURLOPT_CLOSESOCKETFUNCTION: |
| 1020 |
Py_INCREF(obj); |
| 1021 |
Py_CLEAR(self->closesocket_cb); |
| 1022 |
self->closesocket_cb = obj; |
| 1023 |
curl_easy_setopt(self->handle, CURLOPT_CLOSESOCKETFUNCTION, closesocket_cb); |
| 1024 |
curl_easy_setopt(self->handle, CURLOPT_CLOSESOCKETDATA, self); |
| 1025 |
break; |
| 1026 |
#endif |
| 1027 |
case CURLOPT_SOCKOPTFUNCTION: |
| 1028 |
Py_INCREF(obj); |
| 1029 |
Py_CLEAR(self->sockopt_cb); |
| 1030 |
self->sockopt_cb = obj; |
| 1031 |
curl_easy_setopt(self->handle, CURLOPT_SOCKOPTFUNCTION, sockopt_cb); |
| 1032 |
curl_easy_setopt(self->handle, CURLOPT_SOCKOPTDATA, self); |
| 1033 |
break; |
| 1034 |
#ifdef HAVE_CURL_7_19_6_OPTS |
| 1035 |
case CURLOPT_SSH_KEYFUNCTION: |
| 1036 |
Py_INCREF(obj); |
| 1037 |
Py_CLEAR(self->ssh_key_cb); |
| 1038 |
self->ssh_key_cb = obj; |
| 1039 |
curl_easy_setopt(self->handle, CURLOPT_SSH_KEYFUNCTION, ssh_key_cb); |
| 1040 |
curl_easy_setopt(self->handle, CURLOPT_SSH_KEYDATA, self); |
| 1041 |
break; |
| 1042 |
#endif |
| 1043 |
case CURLOPT_SEEKFUNCTION: |
| 1044 |
Py_INCREF(obj); |
| 1045 |
Py_CLEAR(self->seek_cb); |
| 1046 |
self->seek_cb = obj; |
| 1047 |
curl_easy_setopt(self->handle, CURLOPT_SEEKFUNCTION, seek_cb); |
| 1048 |
curl_easy_setopt(self->handle, CURLOPT_SEEKDATA, self); |
| 1049 |
break; |
| 1050 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 80, 0) |
| 1051 |
case CURLOPT_PREREQFUNCTION: |
| 1052 |
Py_INCREF(obj); |
| 1053 |
Py_CLEAR(self->prereq_cb); |
| 1054 |
self->prereq_cb = obj; |
| 1055 |
curl_easy_setopt(self->handle, CURLOPT_PREREQFUNCTION, prereq_cb); |
| 1056 |
curl_easy_setopt(self->handle, CURLOPT_PREREQDATA, self); |
| 1057 |
break; |
| 1058 |
#endif |
| 1059 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 0) |
| 1060 |
case CURLOPT_FNMATCH_FUNCTION: |
| 1061 |
Py_INCREF(obj); |
| 1062 |
Py_CLEAR(self->fnmatch_cb); |
| 1063 |
self->fnmatch_cb = obj; |
| 1064 |
curl_easy_setopt(self->handle, CURLOPT_FNMATCH_FUNCTION, fnmatch_cb); |
| 1065 |
curl_easy_setopt(self->handle, CURLOPT_FNMATCH_DATA, self); |
| 1066 |
break; |
| 1067 |
#endif |
| 1068 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 59, 0) |
| 1069 |
case CURLOPT_RESOLVER_START_FUNCTION: |
| 1070 |
Py_INCREF(obj); |
| 1071 |
Py_CLEAR(self->resolver_start_cb); |
| 1072 |
self->resolver_start_cb = obj; |
| 1073 |
curl_easy_setopt(self->handle, CURLOPT_RESOLVER_START_FUNCTION, resolver_start_cb); |
| 1074 |
curl_easy_setopt(self->handle, CURLOPT_RESOLVER_START_DATA, self); |
| 1075 |
break; |
| 1076 |
#endif |
| 1077 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 64, 0) |
| 1078 |
case CURLOPT_TRAILERFUNCTION: |
| 1079 |
Py_INCREF(obj); |
| 1080 |
Py_CLEAR(self->trailer_cb); |
| 1081 |
self->trailer_cb = obj; |
| 1082 |
curl_easy_setopt(self->handle, CURLOPT_TRAILERFUNCTION, trailer_cb); |
| 1083 |
curl_easy_setopt(self->handle, CURLOPT_TRAILERDATA, self); |
| 1084 |
break; |
| 1085 |
#endif |
| 1086 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 74, 0) |
| 1087 |
case CURLOPT_HSTSREADFUNCTION: |
| 1088 |
Py_INCREF(obj); |
| 1089 |
Py_CLEAR(self->hstsread_cb); |
| 1090 |
self->hstsread_cb = obj; |
| 1091 |
curl_easy_setopt(self->handle, CURLOPT_HSTSREADFUNCTION, hstsread_cb); |
| 1092 |
curl_easy_setopt(self->handle, CURLOPT_HSTSREADDATA, self); |
| 1093 |
break; |
| 1094 |
case CURLOPT_HSTSWRITEFUNCTION: |
| 1095 |
Py_INCREF(obj); |
| 1096 |
Py_CLEAR(self->hstswrite_cb); |
| 1097 |
self->hstswrite_cb = obj; |
| 1098 |
curl_easy_setopt(self->handle, CURLOPT_HSTSWRITEFUNCTION, hstswrite_cb); |
| 1099 |
curl_easy_setopt(self->handle, CURLOPT_HSTSWRITEDATA, self); |
| 1100 |
break; |
| 1101 |
#endif |
| 1102 |
|
| 1103 |
default: |
| 1104 |
/* None of the function options were recognized, raise exception */ |
| 1105 |
PyErr_SetString(PyExc_TypeError, "functions are not supported for this option"); |
| 1106 |
return NULL; |
| 1107 |
} |
| 1108 |
Py_RETURN_NONE; |
| 1109 |
} |
| 1110 |
|
| 1111 |
|
| 1112 |
static PyObject * |
| 1113 |
do_curl_setopt_share(CurlObject *self, PyObject *obj) |
| 1114 |
{ |
| 1115 |
CurlShareObject *share; |
| 1116 |
int res; |
| 1117 |
|
| 1118 |
if (self->share == NULL && (obj == NULL || obj == Py_None)) |
| 1119 |
Py_RETURN_NONE; |
| 1120 |
|
| 1121 |
if (self->share) { |
| 1122 |
if (obj != Py_None) { |
| 1123 |
PyErr_SetString(ErrorObject, "Curl object already sharing. Unshare first."); |
| 1124 |
return NULL; |
| 1125 |
} |
| 1126 |
else { |
| 1127 |
share = self->share; |
| 1128 |
res = curl_easy_setopt(self->handle, CURLOPT_SHARE, NULL); |
| 1129 |
if (res != CURLE_OK) { |
| 1130 |
CURLERROR_RETVAL(); |
| 1131 |
} |
| 1132 |
share_unregister_easy(share, self); |
| 1133 |
self->share = NULL; |
| 1134 |
Py_DECREF(share); |
| 1135 |
Py_RETURN_NONE; |
| 1136 |
} |
| 1137 |
} |
| 1138 |
if (Py_TYPE(obj) != p_CurlShare_Type) { |
| 1139 |
PyErr_SetString(PyExc_TypeError, "invalid arguments to setopt"); |
| 1140 |
return NULL; |
| 1141 |
} |
| 1142 |
share = (CurlShareObject*)obj; |
| 1143 |
res = curl_easy_setopt(self->handle, CURLOPT_SHARE, share->share_handle); |
| 1144 |
if (res != CURLE_OK) { |
| 1145 |
CURLERROR_RETVAL(); |
| 1146 |
} |
| 1147 |
self->share = share; |
| 1148 |
Py_INCREF(share); |
| 1149 |
|
| 1150 |
if (share_register_easy(share, self) < 0) { |
| 1151 |
curl_easy_setopt(self->handle, CURLOPT_SHARE, NULL); |
| 1152 |
self->share = NULL; |
| 1153 |
Py_DECREF(share); |
| 1154 |
return NULL; |
| 1155 |
} |
| 1156 |
|
| 1157 |
Py_RETURN_NONE; |
| 1158 |
} |
| 1159 |
|
| 1160 |
#ifdef HAVE_CURL_MIME |
| 1161 |
static PyObject * |
| 1162 |
do_curl_setopt_mimepost(CurlObject *self, PyObject *obj) |
| 1163 |
{ |
| 1164 |
CurlMimeObject *mime; |
| 1165 |
PyObject *old_mimepost_obj; |
| 1166 |
int res; |
| 1167 |
|
| 1168 |
if (!PyObject_TypeCheck(obj, p_CurlMime_Type)) { |
| 1169 |
PyErr_SetString(PyExc_TypeError, "setopt(option=MIMEPOST) expects a CurlMime object"); |
| 1170 |
return NULL; |
| 1171 |
} |
| 1172 |
|
| 1173 |
mime = (CurlMimeObject *)obj; |
| 1174 |
if (mime->mime == NULL) { |
| 1175 |
PyErr_SetString(PyExc_ValueError, "setopt(option=MIMEPOST) received a closed mime object"); |
| 1176 |
return NULL; |
| 1177 |
} |
| 1178 |
if (!mime->owns_mime) { |
| 1179 |
PyErr_SetString(PyExc_ValueError, "setopt(option=MIMEPOST) received a mime object already attached as subparts"); |
| 1180 |
return NULL; |
| 1181 |
} |
| 1182 |
if (mime->curl != self) { |
| 1183 |
PyErr_SetString(PyExc_ValueError, "setopt(option=MIMEPOST) requires a CurlMime created with this Curl handle"); |
| 1184 |
return NULL; |
| 1185 |
} |
| 1186 |
|
| 1187 |
res = curl_easy_setopt(self->handle, CURLOPT_MIMEPOST, mime->mime); |
| 1188 |
if (res != CURLE_OK) { |
| 1189 |
CURLERROR_RETVAL(); |
| 1190 |
} |
| 1191 |
|
| 1192 |
old_mimepost_obj = self->mimepost_obj; |
| 1193 |
Py_INCREF(obj); |
| 1194 |
self->mimepost_obj = obj; |
| 1195 |
Py_XDECREF(old_mimepost_obj); |
| 1196 |
|
| 1197 |
util_curl_xdecref(self, PYCURL_MEMGROUP_HTTPPOST, self->handle); |
| 1198 |
|
| 1199 |
Py_RETURN_NONE; |
| 1200 |
} |
| 1201 |
#endif |
| 1202 |
|
| 1203 |
|
| 1204 |
PYCURL_INTERNAL PyObject * |
| 1205 |
do_curl_setopt_filelike(CurlObject *self, int option, PyObject *obj) |
| 1206 |
{ |
| 1207 |
const char *method_name; |
| 1208 |
PyObject *method; |
| 1209 |
|
| 1210 |
if (option == CURLOPT_READDATA) { |
| 1211 |
method_name = "read"; |
| 1212 |
} else { |
| 1213 |
method_name = "write"; |
| 1214 |
} |
| 1215 |
method = PyObject_GetAttrString(obj, method_name); |
| 1216 |
if (method) { |
| 1217 |
PyObject *arglist; |
| 1218 |
PyObject *rv; |
| 1219 |
|
| 1220 |
switch (option) { |
| 1221 |
case CURLOPT_READDATA: |
| 1222 |
option = CURLOPT_READFUNCTION; |
| 1223 |
break; |
| 1224 |
case CURLOPT_WRITEDATA: |
| 1225 |
option = CURLOPT_WRITEFUNCTION; |
| 1226 |
break; |
| 1227 |
case CURLOPT_WRITEHEADER: |
| 1228 |
option = CURLOPT_HEADERFUNCTION; |
| 1229 |
break; |
| 1230 |
default: |
| 1231 |
PyErr_SetString(PyExc_TypeError, "objects are not supported for this option"); |
| 1232 |
Py_DECREF(method); |
| 1233 |
return NULL; |
| 1234 |
} |
| 1235 |
|
| 1236 |
arglist = Py_BuildValue("(iO)", option, method); |
| 1237 |
/* reference is now in arglist */ |
| 1238 |
Py_DECREF(method); |
| 1239 |
if (arglist == NULL) { |
| 1240 |
return NULL; |
| 1241 |
} |
| 1242 |
rv = do_curl_setopt(self, arglist); |
| 1243 |
Py_DECREF(arglist); |
| 1244 |
return rv; |
| 1245 |
} else { |
| 1246 |
if (option == CURLOPT_READDATA) { |
| 1247 |
PyErr_SetString(PyExc_TypeError, "object given without a read method"); |
| 1248 |
} else { |
| 1249 |
PyErr_SetString(PyExc_TypeError, "object given without a write method"); |
| 1250 |
} |
| 1251 |
return NULL; |
| 1252 |
} |
| 1253 |
} |
| 1254 |
|
| 1255 |
|
| 1256 |
PYCURL_INTERNAL PyObject * |
| 1257 |
do_curl_setopt(CurlObject *self, PyObject *args) |
| 1258 |
{ |
| 1259 |
int option; |
| 1260 |
PyObject *obj; |
| 1261 |
int which; |
| 1262 |
|
| 1263 |
if (!PyArg_ParseTuple(args, "iO:setopt", &option, &obj)) |
| 1264 |
return NULL; |
| 1265 |
if (check_curl_state(self, 1 | 2, "setopt") != 0) |
| 1266 |
return NULL; |
| 1267 |
|
| 1268 |
/* early checks of option value */ |
| 1269 |
if (option <= 0) |
| 1270 |
goto error; |
| 1271 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 71, 0) |
| 1272 |
if (option >= (int)CURLOPTTYPE_BLOB + OPTIONS_SIZE) |
| 1273 |
goto error; |
| 1274 |
#else |
| 1275 |
if (option >= (int)CURLOPTTYPE_OFF_T + OPTIONS_SIZE) |
| 1276 |
goto error; |
| 1277 |
#endif |
| 1278 |
if (option % 10000 >= OPTIONS_SIZE) |
| 1279 |
goto error; |
| 1280 |
|
| 1281 |
/* Handle the case of None as the call of unsetopt() */ |
| 1282 |
if (obj == Py_None) { |
| 1283 |
return util_curl_unsetopt(self, option); |
| 1284 |
} |
| 1285 |
|
| 1286 |
/* Handle the case of string arguments */ |
| 1287 |
if (PyText_Check(obj)) { |
| 1288 |
return do_curl_setopt_string_impl(self, option, obj); |
| 1289 |
} |
| 1290 |
|
| 1291 |
/* Handle the case of integer arguments */ |
| 1292 |
if (PyLong_Check(obj)) { |
| 1293 |
return do_curl_setopt_int(self, option, obj); |
| 1294 |
} |
| 1295 |
|
| 1296 |
/* Handle the case of list or tuple objects */ |
| 1297 |
which = PyListOrTuple_Check(obj); |
| 1298 |
if (which) { |
| 1299 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 1300 |
if (option == CURLOPT_HTTPPOST) { |
| 1301 |
return do_curl_setopt_httppost(self, option, which, obj); |
| 1302 |
} else { |
| 1303 |
return do_curl_setopt_list(self, option, which, obj); |
| 1304 |
} |
| 1305 |
PYCURL_IGNORE_DEPRECATED_END |
| 1306 |
} |
| 1307 |
|
| 1308 |
/* Handle the case of function objects for callbacks */ |
| 1309 |
if (PyFunction_Check(obj) || PyCFunction_Check(obj) || |
| 1310 |
PyCallable_Check(obj) || PyMethod_Check(obj)) { |
| 1311 |
return do_curl_setopt_callable(self, option, obj); |
| 1312 |
} |
| 1313 |
/* handle the SHARE case */ |
| 1314 |
if (option == CURLOPT_SHARE) { |
| 1315 |
return do_curl_setopt_share(self, obj); |
| 1316 |
} |
| 1317 |
|
| 1318 |
#ifdef HAVE_CURL_MIME |
| 1319 |
if (option == CURLOPT_MIMEPOST) { |
| 1320 |
return do_curl_setopt_mimepost(self, obj); |
| 1321 |
} |
| 1322 |
#endif |
| 1323 |
|
| 1324 |
/* |
| 1325 |
Handle the case of file-like objects. |
| 1326 |
|
| 1327 |
Given an object with a write method, we will call the write method |
| 1328 |
from the appropriate callback. |
| 1329 |
|
| 1330 |
Files in Python 3 are not FILE * instances and therefore cannot |
| 1331 |
be directly given to curl, so this method handles all I/O to |
| 1332 |
Python objects. |
| 1333 |
*/ |
| 1334 |
if (option == CURLOPT_READDATA || |
| 1335 |
option == CURLOPT_WRITEDATA || |
| 1336 |
option == CURLOPT_WRITEHEADER) |
| 1337 |
{ |
| 1338 |
return do_curl_setopt_filelike(self, option, obj); |
| 1339 |
} |
| 1340 |
|
| 1341 |
/* Failed to match any of the function signatures -- return error */ |
| 1342 |
error: |
| 1343 |
PyErr_SetString(PyExc_TypeError, "invalid arguments to setopt"); |
| 1344 |
return NULL; |
| 1345 |
} |
| 1346 |
|
| 1347 |
|
| 1348 |
PYCURL_INTERNAL PyObject * |
| 1349 |
do_curl_setopt_string(CurlObject *self, PyObject *args) |
| 1350 |
{ |
| 1351 |
int option; |
| 1352 |
PyObject *obj; |
| 1353 |
|
| 1354 |
if (!PyArg_ParseTuple(args, "iO:setopt", &option, &obj)) |
| 1355 |
return NULL; |
| 1356 |
if (check_curl_state(self, 1 | 2, "setopt") != 0) |
| 1357 |
return NULL; |
| 1358 |
|
| 1359 |
/* Handle the case of string arguments */ |
| 1360 |
if (PyText_Check(obj)) { |
| 1361 |
return do_curl_setopt_string_impl(self, option, obj); |
| 1362 |
} |
| 1363 |
|
| 1364 |
/* Failed to match any of the function signatures -- return error */ |
| 1365 |
PyErr_SetString(PyExc_TypeError, "invalid arguments to setopt_string"); |
| 1366 |
return NULL; |
| 1367 |
} |
| 1368 |
|
| 1369 |
|
| 1370 |
#if defined(HAVE_CURL_OPENSSL) |
| 1371 |
/* load ca certs from string */ |
| 1372 |
PYCURL_INTERNAL PyObject * |
| 1373 |
do_curl_set_ca_certs(CurlObject *self, PyObject *args) |
| 1374 |
{ |
| 1375 |
PyObject *cadata; |
| 1376 |
PyObject *encoded_obj; |
| 1377 |
char *buffer; |
| 1378 |
Py_ssize_t length; |
| 1379 |
int res; |
| 1380 |
|
| 1381 |
if (!PyArg_ParseTuple(args, "O:cadata", &cadata)) |
| 1382 |
return NULL; |
| 1383 |
|
| 1384 |
// This may result in cadata string being encoded twice, |
| 1385 |
// not going to worry about it for now |
| 1386 |
if (!PyText_Check(cadata)) { |
| 1387 |
PyErr_SetString(PyExc_TypeError, "set_ca_certs argument must be a byte string or a Unicode string with ASCII code points only"); |
| 1388 |
return NULL; |
| 1389 |
} |
| 1390 |
|
| 1391 |
res = PyText_AsStringAndSize(cadata, &buffer, &length, &encoded_obj); |
| 1392 |
if (res) { |
| 1393 |
PyErr_SetString(PyExc_TypeError, "set_ca_certs argument must be a byte string or a Unicode string with ASCII code points only"); |
| 1394 |
return NULL; |
| 1395 |
} |
| 1396 |
|
| 1397 |
Py_CLEAR(self->ca_certs_obj); |
| 1398 |
if (encoded_obj) { |
| 1399 |
self->ca_certs_obj = encoded_obj; |
| 1400 |
} else { |
| 1401 |
Py_INCREF(cadata); |
| 1402 |
self->ca_certs_obj = cadata; |
| 1403 |
} |
| 1404 |
|
| 1405 |
res = curl_easy_setopt(self->handle, CURLOPT_SSL_CTX_FUNCTION, (curl_ssl_ctx_callback) ssl_ctx_callback); |
| 1406 |
if (res != CURLE_OK) { |
| 1407 |
Py_CLEAR(self->ca_certs_obj); |
| 1408 |
CURLERROR_RETVAL(); |
| 1409 |
} |
| 1410 |
|
| 1411 |
res = curl_easy_setopt(self->handle, CURLOPT_SSL_CTX_DATA, self); |
| 1412 |
if (res != CURLE_OK) { |
| 1413 |
Py_CLEAR(self->ca_certs_obj); |
| 1414 |
CURLERROR_RETVAL(); |
| 1415 |
} |
| 1416 |
|
| 1417 |
Py_RETURN_NONE; |
| 1418 |
} |
| 1419 |
#endif |