| 1 |
#include "pycurl.h" |
| 2 |
|
| 3 |
|
| 4 |
/* Convert a curl slist (a list of strings) to a Python list. |
| 5 |
* In case of error return NULL with an exception set. |
| 6 |
*/ |
| 7 |
static PyObject *convert_slist(struct curl_slist *slist, int free_flags) |
| 8 |
{ |
| 9 |
PyObject *ret = NULL; |
| 10 |
struct curl_slist *slist_start = slist; |
| 11 |
|
| 12 |
ret = PyList_New((Py_ssize_t)0); |
| 13 |
if (ret == NULL) goto error; |
| 14 |
|
| 15 |
for ( ; slist != NULL; slist = slist->next) { |
| 16 |
PyObject *v = NULL; |
| 17 |
|
| 18 |
if (slist->data == NULL) { |
| 19 |
v = Py_None; Py_INCREF(v); |
| 20 |
} else { |
| 21 |
v = PyBytes_FromString(slist->data); |
| 22 |
} |
| 23 |
if (v == NULL || PyList_Append(ret, v) != 0) { |
| 24 |
Py_XDECREF(v); |
| 25 |
goto error; |
| 26 |
} |
| 27 |
Py_DECREF(v); |
| 28 |
} |
| 29 |
|
| 30 |
if ((free_flags & 1) && slist_start) |
| 31 |
curl_slist_free_all(slist_start); |
| 32 |
return ret; |
| 33 |
|
| 34 |
error: |
| 35 |
Py_XDECREF(ret); |
| 36 |
if ((free_flags & 2) && slist_start) |
| 37 |
curl_slist_free_all(slist_start); |
| 38 |
return NULL; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
#ifdef HAVE_CURLOPT_CERTINFO |
| 43 |
/* Convert a struct curl_certinfo into a Python data structure. |
| 44 |
* In case of error return NULL with an exception set. |
| 45 |
*/ |
| 46 |
static PyObject *convert_certinfo(struct curl_certinfo *cinfo, int decode) |
| 47 |
{ |
| 48 |
PyObject *certs; |
| 49 |
int cert_index; |
| 50 |
|
| 51 |
if (!cinfo) |
| 52 |
Py_RETURN_NONE; |
| 53 |
|
| 54 |
certs = PyList_New((Py_ssize_t)(cinfo->num_of_certs)); |
| 55 |
if (!certs) |
| 56 |
return NULL; |
| 57 |
|
| 58 |
for (cert_index = 0; cert_index < cinfo->num_of_certs; cert_index ++) { |
| 59 |
struct curl_slist *fields = cinfo->certinfo[cert_index]; |
| 60 |
struct curl_slist *field_cursor; |
| 61 |
int field_count, field_index; |
| 62 |
PyObject *cert; |
| 63 |
|
| 64 |
field_count = 0; |
| 65 |
field_cursor = fields; |
| 66 |
while (field_cursor != NULL) { |
| 67 |
field_cursor = field_cursor->next; |
| 68 |
field_count ++; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
cert = PyTuple_New((Py_ssize_t)field_count); |
| 73 |
if (!cert) |
| 74 |
goto error; |
| 75 |
PyList_SetItem(certs, cert_index, cert); /* Eats the ref from New() */ |
| 76 |
|
| 77 |
for(field_index = 0, field_cursor = fields; |
| 78 |
field_cursor != NULL; |
| 79 |
field_index ++, field_cursor = field_cursor->next) { |
| 80 |
const char *field = field_cursor->data; |
| 81 |
PyObject *field_tuple; |
| 82 |
|
| 83 |
if (!field) { |
| 84 |
field_tuple = Py_None; Py_INCREF(field_tuple); |
| 85 |
} else { |
| 86 |
const char *sep = strchr(field, ':'); |
| 87 |
if (!sep) { |
| 88 |
if (decode) { |
| 89 |
field_tuple = PyUnicode_FromString(field); |
| 90 |
} else { |
| 91 |
field_tuple = PyBytes_FromString(field); |
| 92 |
} |
| 93 |
} else { |
| 94 |
Py_ssize_t name_len = sep - field; |
| 95 |
if (decode) { |
| 96 |
field_tuple = Py_BuildValue("s#s", field, name_len, sep+1); |
| 97 |
} else { |
| 98 |
field_tuple = Py_BuildValue("y#y", field, name_len, sep+1); |
| 99 |
} |
| 100 |
} |
| 101 |
if (!field_tuple) |
| 102 |
goto error; |
| 103 |
} |
| 104 |
PyTuple_SET_ITEM(cert, field_index, field_tuple); /* Eats the ref */ |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return certs; |
| 109 |
|
| 110 |
error: |
| 111 |
Py_DECREF(certs); |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
#endif |
| 115 |
|
| 116 |
PYCURL_INTERNAL PyObject * |
| 117 |
do_curl_getinfo_raw(CurlObject *self, PyObject *args) |
| 118 |
{ |
| 119 |
int option; |
| 120 |
int res; |
| 121 |
|
| 122 |
if (!PyArg_ParseTuple(args, "i:getinfo_raw", &option)) { |
| 123 |
return NULL; |
| 124 |
} |
| 125 |
if (check_curl_state(self, 1 | 2, "getinfo") != 0) { |
| 126 |
return NULL; |
| 127 |
} |
| 128 |
|
| 129 |
switch (option) { |
| 130 |
case CURLINFO_FILETIME: |
| 131 |
case CURLINFO_HEADER_SIZE: |
| 132 |
case CURLINFO_RESPONSE_CODE: |
| 133 |
case CURLINFO_REDIRECT_COUNT: |
| 134 |
case CURLINFO_REQUEST_SIZE: |
| 135 |
case CURLINFO_SSL_VERIFYRESULT: |
| 136 |
case CURLINFO_HTTP_CONNECTCODE: |
| 137 |
case CURLINFO_HTTPAUTH_AVAIL: |
| 138 |
case CURLINFO_PROXYAUTH_AVAIL: |
| 139 |
case CURLINFO_OS_ERRNO: |
| 140 |
case CURLINFO_NUM_CONNECTS: |
| 141 |
#if LIBCURL_VERSION_NUM < MAKE_LIBCURL_VERSION(7, 45, 0) |
| 142 |
case CURLINFO_LASTSOCKET: |
| 143 |
#endif |
| 144 |
#ifdef HAVE_CURLINFO_LOCAL_PORT |
| 145 |
case CURLINFO_LOCAL_PORT: |
| 146 |
#endif |
| 147 |
#ifdef HAVE_CURLINFO_PRIMARY_PORT |
| 148 |
case CURLINFO_PRIMARY_PORT: |
| 149 |
#endif |
| 150 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 20, 0) |
| 151 |
case CURLINFO_RTSP_CLIENT_CSEQ: |
| 152 |
case CURLINFO_RTSP_SERVER_CSEQ: |
| 153 |
case CURLINFO_RTSP_CSEQ_RECV: |
| 154 |
#endif |
| 155 |
#ifdef HAVE_CURLINFO_HTTP_VERSION |
| 156 |
case CURLINFO_HTTP_VERSION: |
| 157 |
#endif |
| 158 |
#ifdef HAVE_CURL_7_19_4_OPTS |
| 159 |
case CURLINFO_CONDITION_UNMET: |
| 160 |
#endif |
| 161 |
{ |
| 162 |
/* Return PyInt as result */ |
| 163 |
long l_res = -1; |
| 164 |
|
| 165 |
res = curl_easy_getinfo(self->handle, (CURLINFO)option, &l_res); |
| 166 |
/* Check for errors and return result */ |
| 167 |
if (res != CURLE_OK) { |
| 168 |
CURLERROR_RETVAL(); |
| 169 |
} |
| 170 |
return PyLong_FromLong(l_res); |
| 171 |
} |
| 172 |
|
| 173 |
case CURLINFO_CONTENT_TYPE: |
| 174 |
case CURLINFO_EFFECTIVE_URL: |
| 175 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 72, 0) |
| 176 |
case CURLINFO_EFFECTIVE_METHOD: |
| 177 |
#endif |
| 178 |
case CURLINFO_FTP_ENTRY_PATH: |
| 179 |
case CURLINFO_REDIRECT_URL: |
| 180 |
case CURLINFO_PRIMARY_IP: |
| 181 |
#ifdef HAVE_CURLINFO_LOCAL_IP |
| 182 |
case CURLINFO_LOCAL_IP: |
| 183 |
#endif |
| 184 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 20, 0) |
| 185 |
case CURLINFO_RTSP_SESSION_ID: |
| 186 |
#endif |
| 187 |
{ |
| 188 |
/* Return PyString as result */ |
| 189 |
char *s_res = NULL; |
| 190 |
|
| 191 |
res = curl_easy_getinfo(self->handle, (CURLINFO)option, &s_res); |
| 192 |
if (res != CURLE_OK) { |
| 193 |
CURLERROR_RETVAL(); |
| 194 |
} |
| 195 |
/* If the resulting string is NULL, return None */ |
| 196 |
if (s_res == NULL) { |
| 197 |
Py_RETURN_NONE; |
| 198 |
} |
| 199 |
return PyBytes_FromString(s_res); |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 204 |
case CURLINFO_CONTENT_LENGTH_DOWNLOAD: |
| 205 |
case CURLINFO_CONTENT_LENGTH_UPLOAD: |
| 206 |
case CURLINFO_SIZE_DOWNLOAD: |
| 207 |
case CURLINFO_SIZE_UPLOAD: |
| 208 |
case CURLINFO_SPEED_DOWNLOAD: |
| 209 |
case CURLINFO_SPEED_UPLOAD: |
| 210 |
PYCURL_IGNORE_DEPRECATED_END |
| 211 |
if (PyErr_WarnEx(PyExc_DeprecationWarning, "getinfo option is deprecated", 1) != 0) { |
| 212 |
return NULL; |
| 213 |
} |
| 214 |
case CURLINFO_CONNECT_TIME: |
| 215 |
case CURLINFO_APPCONNECT_TIME: |
| 216 |
case CURLINFO_NAMELOOKUP_TIME: |
| 217 |
case CURLINFO_PRETRANSFER_TIME: |
| 218 |
case CURLINFO_REDIRECT_TIME: |
| 219 |
case CURLINFO_STARTTRANSFER_TIME: |
| 220 |
case CURLINFO_TOTAL_TIME: |
| 221 |
{ |
| 222 |
/* Return PyFloat as result */ |
| 223 |
double d_res = 0.0; |
| 224 |
|
| 225 |
res = curl_easy_getinfo(self->handle, (CURLINFO)option, &d_res); |
| 226 |
if (res != CURLE_OK) { |
| 227 |
CURLERROR_RETVAL(); |
| 228 |
} |
| 229 |
return PyFloat_FromDouble(d_res); |
| 230 |
} |
| 231 |
|
| 232 |
case CURLINFO_SSL_ENGINES: |
| 233 |
case CURLINFO_COOKIELIST: |
| 234 |
{ |
| 235 |
/* Return a list of strings */ |
| 236 |
struct curl_slist *slist = NULL; |
| 237 |
|
| 238 |
res = curl_easy_getinfo(self->handle, (CURLINFO)option, &slist); |
| 239 |
if (res != CURLE_OK) { |
| 240 |
CURLERROR_RETVAL(); |
| 241 |
} |
| 242 |
return convert_slist(slist, 1 | 2); |
| 243 |
} |
| 244 |
|
| 245 |
#ifdef HAVE_CURLOPT_CERTINFO |
| 246 |
case CURLINFO_CERTINFO: |
| 247 |
{ |
| 248 |
/* Return a list of lists of 2-tuples */ |
| 249 |
struct curl_certinfo *clist = NULL; |
| 250 |
res = curl_easy_getinfo(self->handle, CURLINFO_CERTINFO, &clist); |
| 251 |
if (res != CURLE_OK) { |
| 252 |
CURLERROR_RETVAL(); |
| 253 |
} else { |
| 254 |
return convert_certinfo(clist, 0); |
| 255 |
} |
| 256 |
} |
| 257 |
#endif |
| 258 |
|
| 259 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 55, 0) |
| 260 |
case CURLINFO_CONTENT_LENGTH_DOWNLOAD_T: |
| 261 |
case CURLINFO_CONTENT_LENGTH_UPLOAD_T: |
| 262 |
case CURLINFO_SIZE_DOWNLOAD_T: |
| 263 |
case CURLINFO_SIZE_UPLOAD_T: |
| 264 |
case CURLINFO_SPEED_DOWNLOAD_T: |
| 265 |
case CURLINFO_SPEED_UPLOAD_T: |
| 266 |
/* endif for this section is after the block as this is the oldest _t */ |
| 267 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 59, 0) |
| 268 |
case CURLINFO_FILETIME_T: |
| 269 |
#endif |
| 270 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 61, 0) |
| 271 |
case CURLINFO_APPCONNECT_TIME_T: |
| 272 |
case CURLINFO_CONNECT_TIME_T: |
| 273 |
case CURLINFO_NAMELOOKUP_TIME_T: |
| 274 |
case CURLINFO_PRETRANSFER_TIME_T: |
| 275 |
case CURLINFO_REDIRECT_TIME_T: |
| 276 |
case CURLINFO_STARTTRANSFER_TIME_T: |
| 277 |
case CURLINFO_TOTAL_TIME_T: |
| 278 |
#endif |
| 279 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(8, 6, 0) |
| 280 |
case CURLINFO_QUEUE_TIME_T: |
| 281 |
#endif |
| 282 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(8, 10, 0) |
| 283 |
case CURLINFO_POSTTRANSFER_TIME_T: |
| 284 |
#endif |
| 285 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(8, 11, 0) |
| 286 |
case CURLINFO_EARLYDATA_SENT_T: |
| 287 |
#endif |
| 288 |
{ |
| 289 |
/* Return PyLong as result */ |
| 290 |
curl_off_t ot_res = 0; |
| 291 |
|
| 292 |
res = curl_easy_getinfo(self->handle, (CURLINFO)option, &ot_res); |
| 293 |
if (res != CURLE_OK) { |
| 294 |
CURLERROR_RETVAL(); |
| 295 |
} |
| 296 |
return PyLong_FromLongLong(ot_res); |
| 297 |
} |
| 298 |
#endif |
| 299 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 45, 0) |
| 300 |
/* CURLINFO_ACTIVESOCKET was added as a replacement for CURLINFO_LASTSOCKET |
| 301 |
* since that one is not working on all platforms. |
| 302 |
*/ |
| 303 |
PYCURL_IGNORE_DEPRECATED_BEGIN |
| 304 |
case CURLINFO_LASTSOCKET: |
| 305 |
PYCURL_IGNORE_DEPRECATED_END |
| 306 |
case CURLINFO_ACTIVESOCKET: |
| 307 |
{ |
| 308 |
/* Return PyLong as result */ |
| 309 |
curl_socket_t sockfd; |
| 310 |
|
| 311 |
res = curl_easy_getinfo(self->handle, CURLINFO_ACTIVESOCKET, &sockfd); |
| 312 |
if (res != CURLE_OK) { |
| 313 |
CURLERROR_RETVAL(); |
| 314 |
} |
| 315 |
|
| 316 |
/* Expose CURL_SOCKET_BAD as -1 in Python so ACTIVESOCKET matches |
| 317 |
* LASTSOCKET semantics for invalid sockets. |
| 318 |
*/ |
| 319 |
return PyLong_FromCurlSocket(sockfd); |
| 320 |
} |
| 321 |
#endif |
| 322 |
|
| 323 |
} |
| 324 |
|
| 325 |
/* Got wrong option on the method call */ |
| 326 |
PyErr_SetString(PyExc_ValueError, "invalid argument to getinfo"); |
| 327 |
return NULL; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
static PyObject * |
| 332 |
decode_string_list(PyObject *list) |
| 333 |
{ |
| 334 |
PyObject *decoded_list = NULL; |
| 335 |
Py_ssize_t size = PyList_Size(list); |
| 336 |
int i; |
| 337 |
|
| 338 |
decoded_list = PyList_New(size); |
| 339 |
if (decoded_list == NULL) { |
| 340 |
return NULL; |
| 341 |
} |
| 342 |
|
| 343 |
for (i = 0; i < size; ++i) { |
| 344 |
PyObject *decoded_item = PyUnicode_FromEncodedObject( |
| 345 |
PyList_GET_ITEM(list, i), |
| 346 |
NULL, |
| 347 |
NULL); |
| 348 |
|
| 349 |
if (decoded_item == NULL) { |
| 350 |
goto err; |
| 351 |
} |
| 352 |
PyList_SetItem(decoded_list, i, decoded_item); |
| 353 |
} |
| 354 |
|
| 355 |
return decoded_list; |
| 356 |
|
| 357 |
err: |
| 358 |
Py_DECREF(decoded_list); |
| 359 |
return NULL; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
PYCURL_INTERNAL PyObject * |
| 364 |
do_curl_getinfo(CurlObject *self, PyObject *args) |
| 365 |
{ |
| 366 |
int option, res; |
| 367 |
PyObject *rv; |
| 368 |
|
| 369 |
if (!PyArg_ParseTuple(args, "i:getinfo", &option)) { |
| 370 |
return NULL; |
| 371 |
} |
| 372 |
|
| 373 |
#ifdef HAVE_CURLOPT_CERTINFO |
| 374 |
if (option == CURLINFO_CERTINFO) { |
| 375 |
/* Return a list of lists of 2-tuples */ |
| 376 |
struct curl_certinfo *clist = NULL; |
| 377 |
res = curl_easy_getinfo(self->handle, CURLINFO_CERTINFO, &clist); |
| 378 |
if (res != CURLE_OK) { |
| 379 |
CURLERROR_RETVAL(); |
| 380 |
} else { |
| 381 |
return convert_certinfo(clist, 1); |
| 382 |
} |
| 383 |
} |
| 384 |
#endif |
| 385 |
|
| 386 |
rv = do_curl_getinfo_raw(self, args); |
| 387 |
if (rv == NULL) { |
| 388 |
return rv; |
| 389 |
} |
| 390 |
|
| 391 |
switch (option) { |
| 392 |
case CURLINFO_CONTENT_TYPE: |
| 393 |
case CURLINFO_EFFECTIVE_URL: |
| 394 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 72, 0) |
| 395 |
case CURLINFO_EFFECTIVE_METHOD: |
| 396 |
#endif |
| 397 |
case CURLINFO_FTP_ENTRY_PATH: |
| 398 |
case CURLINFO_REDIRECT_URL: |
| 399 |
case CURLINFO_PRIMARY_IP: |
| 400 |
#ifdef HAVE_CURLINFO_LOCAL_IP |
| 401 |
case CURLINFO_LOCAL_IP: |
| 402 |
#endif |
| 403 |
#if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 20, 0) |
| 404 |
case CURLINFO_RTSP_SESSION_ID: |
| 405 |
#endif |
| 406 |
if (rv != Py_None) { |
| 407 |
PyObject *decoded; |
| 408 |
|
| 409 |
// Decode bytes into a Unicode string using default encoding |
| 410 |
decoded = PyUnicode_FromEncodedObject(rv, NULL, NULL); |
| 411 |
// success and failure paths both need to free bytes object |
| 412 |
Py_DECREF(rv); |
| 413 |
return decoded; |
| 414 |
} |
| 415 |
return rv; |
| 416 |
|
| 417 |
case CURLINFO_SSL_ENGINES: |
| 418 |
case CURLINFO_COOKIELIST: |
| 419 |
{ |
| 420 |
PyObject *decoded = decode_string_list(rv); |
| 421 |
Py_DECREF(rv); |
| 422 |
return decoded; |
| 423 |
} |
| 424 |
|
| 425 |
default: |
| 426 |
return rv; |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
PYCURL_INTERNAL PyObject * |
| 432 |
do_curl_errstr(CurlObject *self, PyObject *Py_UNUSED(ignored)) |
| 433 |
{ |
| 434 |
if (check_curl_state(self, 1 | 2, "errstr") != 0) { |
| 435 |
return NULL; |
| 436 |
} |
| 437 |
self->error[sizeof(self->error) - 1] = 0; |
| 438 |
|
| 439 |
return PyUnicode_FromString(self->error); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
PYCURL_INTERNAL PyObject * |
| 444 |
do_curl_errstr_raw(CurlObject *self, PyObject *Py_UNUSED(ignored)) |
| 445 |
{ |
| 446 |
if (check_curl_state(self, 1 | 2, "errstr") != 0) { |
| 447 |
return NULL; |
| 448 |
} |
| 449 |
self->error[sizeof(self->error) - 1] = 0; |
| 450 |
|
| 451 |
return PyBytes_FromString(self->error); |
| 452 |
} |