getinfo.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include <curl/curl.h>
  24. #include "urldata.h"
  25. #include "getinfo.h"
  26. #include "vtls/vtls.h"
  27. #include "connect.h" /* Curl_getconnectinfo() */
  28. #include "progress.h"
  29. /* The last #include files should be: */
  30. #include "curl_memory.h"
  31. #include "memdebug.h"
  32. /*
  33. * Initialize statistical and informational data.
  34. *
  35. * This function is called in curl_easy_reset, curl_easy_duphandle and at the
  36. * beginning of a perform session. It must reset the session-info variables,
  37. * in particular all variables in struct PureInfo.
  38. */
  39. CURLcode Curl_initinfo(struct Curl_easy *data)
  40. {
  41. struct Progress *pro = &data->progress;
  42. struct PureInfo *info = &data->info;
  43. pro->t_nslookup = 0;
  44. pro->t_connect = 0;
  45. pro->t_appconnect = 0;
  46. pro->t_pretransfer = 0;
  47. pro->t_starttransfer = 0;
  48. pro->timespent = 0;
  49. pro->t_redirect = 0;
  50. pro->is_t_startransfer_set = false;
  51. info->httpcode = 0;
  52. info->httpproxycode = 0;
  53. info->httpversion = 0;
  54. info->filetime = -1; /* -1 is an illegal time and thus means unknown */
  55. info->timecond = FALSE;
  56. info->header_size = 0;
  57. info->request_size = 0;
  58. info->proxyauthavail = 0;
  59. info->httpauthavail = 0;
  60. info->numconnects = 0;
  61. free(info->contenttype);
  62. info->contenttype = NULL;
  63. free(info->wouldredirect);
  64. info->wouldredirect = NULL;
  65. info->conn_primary_ip[0] = '\0';
  66. info->conn_local_ip[0] = '\0';
  67. info->conn_primary_port = 0;
  68. info->conn_local_port = 0;
  69. info->retry_after = 0;
  70. info->conn_scheme = 0;
  71. info->conn_protocol = 0;
  72. #ifdef USE_SSL
  73. Curl_ssl_free_certinfo(data);
  74. #endif
  75. return CURLE_OK;
  76. }
  77. static CURLcode getinfo_char(struct Curl_easy *data, CURLINFO info,
  78. const char **param_charp)
  79. {
  80. switch(info) {
  81. case CURLINFO_EFFECTIVE_URL:
  82. *param_charp = data->state.url?data->state.url:(char *)"";
  83. break;
  84. case CURLINFO_EFFECTIVE_METHOD: {
  85. const char *m = data->set.str[STRING_CUSTOMREQUEST];
  86. if(!m) {
  87. if(data->set.opt_no_body)
  88. m = "HEAD";
  89. #ifndef CURL_DISABLE_HTTP
  90. else {
  91. switch(data->state.httpreq) {
  92. case HTTPREQ_POST:
  93. case HTTPREQ_POST_FORM:
  94. case HTTPREQ_POST_MIME:
  95. m = "POST";
  96. break;
  97. case HTTPREQ_PUT:
  98. m = "PUT";
  99. break;
  100. default: /* this should never happen */
  101. case HTTPREQ_GET:
  102. m = "GET";
  103. break;
  104. case HTTPREQ_HEAD:
  105. m = "HEAD";
  106. break;
  107. }
  108. }
  109. #endif
  110. }
  111. *param_charp = m;
  112. }
  113. break;
  114. case CURLINFO_CONTENT_TYPE:
  115. *param_charp = data->info.contenttype;
  116. break;
  117. case CURLINFO_PRIVATE:
  118. *param_charp = (char *) data->set.private_data;
  119. break;
  120. case CURLINFO_FTP_ENTRY_PATH:
  121. /* Return the entrypath string from the most recent connection.
  122. This pointer was copied from the connectdata structure by FTP.
  123. The actual string may be free()ed by subsequent libcurl calls so
  124. it must be copied to a safer area before the next libcurl call.
  125. Callers must never free it themselves. */
  126. *param_charp = data->state.most_recent_ftp_entrypath;
  127. break;
  128. case CURLINFO_REDIRECT_URL:
  129. /* Return the URL this request would have been redirected to if that
  130. option had been enabled! */
  131. *param_charp = data->info.wouldredirect;
  132. break;
  133. case CURLINFO_REFERER:
  134. /* Return the referrer header for this request, or NULL if unset */
  135. *param_charp = data->state.referer;
  136. break;
  137. case CURLINFO_PRIMARY_IP:
  138. /* Return the ip address of the most recent (primary) connection */
  139. *param_charp = data->info.conn_primary_ip;
  140. break;
  141. case CURLINFO_LOCAL_IP:
  142. /* Return the source/local ip address of the most recent (primary)
  143. connection */
  144. *param_charp = data->info.conn_local_ip;
  145. break;
  146. case CURLINFO_RTSP_SESSION_ID:
  147. *param_charp = data->set.str[STRING_RTSP_SESSION_ID];
  148. break;
  149. case CURLINFO_SCHEME:
  150. *param_charp = data->info.conn_scheme;
  151. break;
  152. default:
  153. return CURLE_UNKNOWN_OPTION;
  154. }
  155. return CURLE_OK;
  156. }
  157. static CURLcode getinfo_long(struct Curl_easy *data, CURLINFO info,
  158. long *param_longp)
  159. {
  160. curl_socket_t sockfd;
  161. union {
  162. unsigned long *to_ulong;
  163. long *to_long;
  164. } lptr;
  165. #ifdef DEBUGBUILD
  166. char *timestr = getenv("CURL_TIME");
  167. if(timestr) {
  168. unsigned long val = strtol(timestr, NULL, 10);
  169. switch(info) {
  170. case CURLINFO_LOCAL_PORT:
  171. *param_longp = (long)val;
  172. return CURLE_OK;
  173. default:
  174. break;
  175. }
  176. }
  177. /* use another variable for this to allow different values */
  178. timestr = getenv("CURL_DEBUG_SIZE");
  179. if(timestr) {
  180. unsigned long val = strtol(timestr, NULL, 10);
  181. switch(info) {
  182. case CURLINFO_HEADER_SIZE:
  183. case CURLINFO_REQUEST_SIZE:
  184. *param_longp = (long)val;
  185. return CURLE_OK;
  186. default:
  187. break;
  188. }
  189. }
  190. #endif
  191. switch(info) {
  192. case CURLINFO_RESPONSE_CODE:
  193. *param_longp = data->info.httpcode;
  194. break;
  195. case CURLINFO_HTTP_CONNECTCODE:
  196. *param_longp = data->info.httpproxycode;
  197. break;
  198. case CURLINFO_FILETIME:
  199. if(data->info.filetime > LONG_MAX)
  200. *param_longp = LONG_MAX;
  201. else if(data->info.filetime < LONG_MIN)
  202. *param_longp = LONG_MIN;
  203. else
  204. *param_longp = (long)data->info.filetime;
  205. break;
  206. case CURLINFO_HEADER_SIZE:
  207. *param_longp = (long)data->info.header_size;
  208. break;
  209. case CURLINFO_REQUEST_SIZE:
  210. *param_longp = (long)data->info.request_size;
  211. break;
  212. case CURLINFO_SSL_VERIFYRESULT:
  213. *param_longp = data->set.ssl.certverifyresult;
  214. break;
  215. #ifndef CURL_DISABLE_PROXY
  216. case CURLINFO_PROXY_SSL_VERIFYRESULT:
  217. *param_longp = data->set.proxy_ssl.certverifyresult;
  218. break;
  219. #endif
  220. case CURLINFO_REDIRECT_COUNT:
  221. *param_longp = data->state.followlocation;
  222. break;
  223. case CURLINFO_HTTPAUTH_AVAIL:
  224. lptr.to_long = param_longp;
  225. *lptr.to_ulong = data->info.httpauthavail;
  226. break;
  227. case CURLINFO_PROXYAUTH_AVAIL:
  228. lptr.to_long = param_longp;
  229. *lptr.to_ulong = data->info.proxyauthavail;
  230. break;
  231. case CURLINFO_OS_ERRNO:
  232. *param_longp = data->state.os_errno;
  233. break;
  234. case CURLINFO_NUM_CONNECTS:
  235. *param_longp = data->info.numconnects;
  236. break;
  237. case CURLINFO_LASTSOCKET:
  238. sockfd = Curl_getconnectinfo(data, NULL);
  239. /* note: this is not a good conversion for systems with 64 bit sockets and
  240. 32 bit longs */
  241. if(sockfd != CURL_SOCKET_BAD)
  242. *param_longp = (long)sockfd;
  243. else
  244. /* this interface is documented to return -1 in case of badness, which
  245. may not be the same as the CURL_SOCKET_BAD value */
  246. *param_longp = -1;
  247. break;
  248. case CURLINFO_PRIMARY_PORT:
  249. /* Return the (remote) port of the most recent (primary) connection */
  250. *param_longp = data->info.conn_primary_port;
  251. break;
  252. case CURLINFO_LOCAL_PORT:
  253. /* Return the local port of the most recent (primary) connection */
  254. *param_longp = data->info.conn_local_port;
  255. break;
  256. case CURLINFO_PROXY_ERROR:
  257. *param_longp = (long)data->info.pxcode;
  258. break;
  259. case CURLINFO_CONDITION_UNMET:
  260. if(data->info.httpcode == 304)
  261. *param_longp = 1L;
  262. else
  263. /* return if the condition prevented the document to get transferred */
  264. *param_longp = data->info.timecond ? 1L : 0L;
  265. break;
  266. case CURLINFO_RTSP_CLIENT_CSEQ:
  267. *param_longp = data->state.rtsp_next_client_CSeq;
  268. break;
  269. case CURLINFO_RTSP_SERVER_CSEQ:
  270. *param_longp = data->state.rtsp_next_server_CSeq;
  271. break;
  272. case CURLINFO_RTSP_CSEQ_RECV:
  273. *param_longp = data->state.rtsp_CSeq_recv;
  274. break;
  275. case CURLINFO_HTTP_VERSION:
  276. switch(data->info.httpversion) {
  277. case 10:
  278. *param_longp = CURL_HTTP_VERSION_1_0;
  279. break;
  280. case 11:
  281. *param_longp = CURL_HTTP_VERSION_1_1;
  282. break;
  283. case 20:
  284. *param_longp = CURL_HTTP_VERSION_2_0;
  285. break;
  286. case 30:
  287. *param_longp = CURL_HTTP_VERSION_3;
  288. break;
  289. default:
  290. *param_longp = CURL_HTTP_VERSION_NONE;
  291. break;
  292. }
  293. break;
  294. case CURLINFO_PROTOCOL:
  295. *param_longp = data->info.conn_protocol;
  296. break;
  297. default:
  298. return CURLE_UNKNOWN_OPTION;
  299. }
  300. return CURLE_OK;
  301. }
  302. #define DOUBLE_SECS(x) (double)(x)/1000000
  303. static CURLcode getinfo_offt(struct Curl_easy *data, CURLINFO info,
  304. curl_off_t *param_offt)
  305. {
  306. #ifdef DEBUGBUILD
  307. char *timestr = getenv("CURL_TIME");
  308. if(timestr) {
  309. unsigned long val = strtol(timestr, NULL, 10);
  310. switch(info) {
  311. case CURLINFO_TOTAL_TIME_T:
  312. case CURLINFO_NAMELOOKUP_TIME_T:
  313. case CURLINFO_CONNECT_TIME_T:
  314. case CURLINFO_APPCONNECT_TIME_T:
  315. case CURLINFO_PRETRANSFER_TIME_T:
  316. case CURLINFO_STARTTRANSFER_TIME_T:
  317. case CURLINFO_REDIRECT_TIME_T:
  318. case CURLINFO_SPEED_DOWNLOAD_T:
  319. case CURLINFO_SPEED_UPLOAD_T:
  320. *param_offt = (curl_off_t)val;
  321. return CURLE_OK;
  322. default:
  323. break;
  324. }
  325. }
  326. #endif
  327. switch(info) {
  328. case CURLINFO_FILETIME_T:
  329. *param_offt = (curl_off_t)data->info.filetime;
  330. break;
  331. case CURLINFO_SIZE_UPLOAD_T:
  332. *param_offt = data->progress.uploaded;
  333. break;
  334. case CURLINFO_SIZE_DOWNLOAD_T:
  335. *param_offt = data->progress.downloaded;
  336. break;
  337. case CURLINFO_SPEED_DOWNLOAD_T:
  338. *param_offt = data->progress.dlspeed;
  339. break;
  340. case CURLINFO_SPEED_UPLOAD_T:
  341. *param_offt = data->progress.ulspeed;
  342. break;
  343. case CURLINFO_CONTENT_LENGTH_DOWNLOAD_T:
  344. *param_offt = (data->progress.flags & PGRS_DL_SIZE_KNOWN)?
  345. data->progress.size_dl:-1;
  346. break;
  347. case CURLINFO_CONTENT_LENGTH_UPLOAD_T:
  348. *param_offt = (data->progress.flags & PGRS_UL_SIZE_KNOWN)?
  349. data->progress.size_ul:-1;
  350. break;
  351. case CURLINFO_TOTAL_TIME_T:
  352. *param_offt = data->progress.timespent;
  353. break;
  354. case CURLINFO_NAMELOOKUP_TIME_T:
  355. *param_offt = data->progress.t_nslookup;
  356. break;
  357. case CURLINFO_CONNECT_TIME_T:
  358. *param_offt = data->progress.t_connect;
  359. break;
  360. case CURLINFO_APPCONNECT_TIME_T:
  361. *param_offt = data->progress.t_appconnect;
  362. break;
  363. case CURLINFO_PRETRANSFER_TIME_T:
  364. *param_offt = data->progress.t_pretransfer;
  365. break;
  366. case CURLINFO_STARTTRANSFER_TIME_T:
  367. *param_offt = data->progress.t_starttransfer;
  368. break;
  369. case CURLINFO_REDIRECT_TIME_T:
  370. *param_offt = data->progress.t_redirect;
  371. break;
  372. case CURLINFO_RETRY_AFTER:
  373. *param_offt = data->info.retry_after;
  374. break;
  375. default:
  376. return CURLE_UNKNOWN_OPTION;
  377. }
  378. return CURLE_OK;
  379. }
  380. static CURLcode getinfo_double(struct Curl_easy *data, CURLINFO info,
  381. double *param_doublep)
  382. {
  383. #ifdef DEBUGBUILD
  384. char *timestr = getenv("CURL_TIME");
  385. if(timestr) {
  386. unsigned long val = strtol(timestr, NULL, 10);
  387. switch(info) {
  388. case CURLINFO_TOTAL_TIME:
  389. case CURLINFO_NAMELOOKUP_TIME:
  390. case CURLINFO_CONNECT_TIME:
  391. case CURLINFO_APPCONNECT_TIME:
  392. case CURLINFO_PRETRANSFER_TIME:
  393. case CURLINFO_STARTTRANSFER_TIME:
  394. case CURLINFO_REDIRECT_TIME:
  395. case CURLINFO_SPEED_DOWNLOAD:
  396. case CURLINFO_SPEED_UPLOAD:
  397. *param_doublep = (double)val;
  398. return CURLE_OK;
  399. default:
  400. break;
  401. }
  402. }
  403. #endif
  404. switch(info) {
  405. case CURLINFO_TOTAL_TIME:
  406. *param_doublep = DOUBLE_SECS(data->progress.timespent);
  407. break;
  408. case CURLINFO_NAMELOOKUP_TIME:
  409. *param_doublep = DOUBLE_SECS(data->progress.t_nslookup);
  410. break;
  411. case CURLINFO_CONNECT_TIME:
  412. *param_doublep = DOUBLE_SECS(data->progress.t_connect);
  413. break;
  414. case CURLINFO_APPCONNECT_TIME:
  415. *param_doublep = DOUBLE_SECS(data->progress.t_appconnect);
  416. break;
  417. case CURLINFO_PRETRANSFER_TIME:
  418. *param_doublep = DOUBLE_SECS(data->progress.t_pretransfer);
  419. break;
  420. case CURLINFO_STARTTRANSFER_TIME:
  421. *param_doublep = DOUBLE_SECS(data->progress.t_starttransfer);
  422. break;
  423. case CURLINFO_SIZE_UPLOAD:
  424. *param_doublep = (double)data->progress.uploaded;
  425. break;
  426. case CURLINFO_SIZE_DOWNLOAD:
  427. *param_doublep = (double)data->progress.downloaded;
  428. break;
  429. case CURLINFO_SPEED_DOWNLOAD:
  430. *param_doublep = (double)data->progress.dlspeed;
  431. break;
  432. case CURLINFO_SPEED_UPLOAD:
  433. *param_doublep = (double)data->progress.ulspeed;
  434. break;
  435. case CURLINFO_CONTENT_LENGTH_DOWNLOAD:
  436. *param_doublep = (data->progress.flags & PGRS_DL_SIZE_KNOWN)?
  437. (double)data->progress.size_dl:-1;
  438. break;
  439. case CURLINFO_CONTENT_LENGTH_UPLOAD:
  440. *param_doublep = (data->progress.flags & PGRS_UL_SIZE_KNOWN)?
  441. (double)data->progress.size_ul:-1;
  442. break;
  443. case CURLINFO_REDIRECT_TIME:
  444. *param_doublep = DOUBLE_SECS(data->progress.t_redirect);
  445. break;
  446. default:
  447. return CURLE_UNKNOWN_OPTION;
  448. }
  449. return CURLE_OK;
  450. }
  451. static CURLcode getinfo_slist(struct Curl_easy *data, CURLINFO info,
  452. struct curl_slist **param_slistp)
  453. {
  454. union {
  455. struct curl_certinfo *to_certinfo;
  456. struct curl_slist *to_slist;
  457. } ptr;
  458. switch(info) {
  459. case CURLINFO_SSL_ENGINES:
  460. *param_slistp = Curl_ssl_engines_list(data);
  461. break;
  462. case CURLINFO_COOKIELIST:
  463. *param_slistp = Curl_cookie_list(data);
  464. break;
  465. case CURLINFO_CERTINFO:
  466. /* Return the a pointer to the certinfo struct. Not really an slist
  467. pointer but we can pretend it is here */
  468. ptr.to_certinfo = &data->info.certs;
  469. *param_slistp = ptr.to_slist;
  470. break;
  471. case CURLINFO_TLS_SESSION:
  472. case CURLINFO_TLS_SSL_PTR:
  473. {
  474. struct curl_tlssessioninfo **tsip = (struct curl_tlssessioninfo **)
  475. param_slistp;
  476. struct curl_tlssessioninfo *tsi = &data->tsi;
  477. #ifdef USE_SSL
  478. struct connectdata *conn = data->conn;
  479. #endif
  480. *tsip = tsi;
  481. tsi->backend = Curl_ssl_backend();
  482. tsi->internals = NULL;
  483. #ifdef USE_SSL
  484. if(conn && tsi->backend != CURLSSLBACKEND_NONE) {
  485. unsigned int i;
  486. for(i = 0; i < (sizeof(conn->ssl) / sizeof(conn->ssl[0])); ++i) {
  487. if(conn->ssl[i].use) {
  488. tsi->internals = Curl_ssl->get_internals(&conn->ssl[i], info);
  489. break;
  490. }
  491. }
  492. }
  493. #endif
  494. }
  495. break;
  496. default:
  497. return CURLE_UNKNOWN_OPTION;
  498. }
  499. return CURLE_OK;
  500. }
  501. static CURLcode getinfo_socket(struct Curl_easy *data, CURLINFO info,
  502. curl_socket_t *param_socketp)
  503. {
  504. switch(info) {
  505. case CURLINFO_ACTIVESOCKET:
  506. *param_socketp = Curl_getconnectinfo(data, NULL);
  507. break;
  508. default:
  509. return CURLE_UNKNOWN_OPTION;
  510. }
  511. return CURLE_OK;
  512. }
  513. CURLcode Curl_getinfo(struct Curl_easy *data, CURLINFO info, ...)
  514. {
  515. va_list arg;
  516. long *param_longp = NULL;
  517. double *param_doublep = NULL;
  518. curl_off_t *param_offt = NULL;
  519. const char **param_charp = NULL;
  520. struct curl_slist **param_slistp = NULL;
  521. curl_socket_t *param_socketp = NULL;
  522. int type;
  523. CURLcode result = CURLE_UNKNOWN_OPTION;
  524. if(!data)
  525. return result;
  526. va_start(arg, info);
  527. type = CURLINFO_TYPEMASK & (int)info;
  528. switch(type) {
  529. case CURLINFO_STRING:
  530. param_charp = va_arg(arg, const char **);
  531. if(param_charp)
  532. result = getinfo_char(data, info, param_charp);
  533. break;
  534. case CURLINFO_LONG:
  535. param_longp = va_arg(arg, long *);
  536. if(param_longp)
  537. result = getinfo_long(data, info, param_longp);
  538. break;
  539. case CURLINFO_DOUBLE:
  540. param_doublep = va_arg(arg, double *);
  541. if(param_doublep)
  542. result = getinfo_double(data, info, param_doublep);
  543. break;
  544. case CURLINFO_OFF_T:
  545. param_offt = va_arg(arg, curl_off_t *);
  546. if(param_offt)
  547. result = getinfo_offt(data, info, param_offt);
  548. break;
  549. case CURLINFO_SLIST:
  550. param_slistp = va_arg(arg, struct curl_slist **);
  551. if(param_slistp)
  552. result = getinfo_slist(data, info, param_slistp);
  553. break;
  554. case CURLINFO_SOCKET:
  555. param_socketp = va_arg(arg, curl_socket_t *);
  556. if(param_socketp)
  557. result = getinfo_socket(data, info, param_socketp);
  558. break;
  559. default:
  560. break;
  561. }
  562. va_end(arg);
  563. return result;
  564. }