2
0

getinfo.c 17 KB

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