getinfo.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, 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 http://curl.haxx.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 "setup.h"
  23. #include <curl/curl.h>
  24. #include "urldata.h"
  25. #include "getinfo.h"
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include <stdlib.h>
  30. #include "curl_memory.h"
  31. #include "sslgen.h"
  32. #include "connect.h" /* Curl_getconnectinfo() */
  33. #include "progress.h"
  34. /* Make this the last #include */
  35. #include "memdebug.h"
  36. /*
  37. * This is supposed to be called in the beginning of a perform() session
  38. * and should reset all session-info variables
  39. */
  40. CURLcode Curl_initinfo(struct SessionHandle *data)
  41. {
  42. struct Progress *pro = &data->progress;
  43. struct PureInfo *info =&data->info;
  44. pro->t_nslookup = 0;
  45. pro->t_connect = 0;
  46. pro->t_pretransfer = 0;
  47. pro->t_starttransfer = 0;
  48. pro->timespent = 0;
  49. pro->t_redirect = 0;
  50. info->httpcode = 0;
  51. info->httpversion=0;
  52. info->filetime=-1; /* -1 is an illegal time and thus means unknown */
  53. if(info->contenttype)
  54. free(info->contenttype);
  55. info->contenttype = NULL;
  56. info->header_size = 0;
  57. info->request_size = 0;
  58. info->numconnects = 0;
  59. info->conn_primary_ip[0] = '\0';
  60. info->conn_local_ip[0] = '\0';
  61. info->conn_primary_port = 0;
  62. info->conn_local_port = 0;
  63. return CURLE_OK;
  64. }
  65. CURLcode Curl_getinfo(struct SessionHandle *data, CURLINFO info, ...)
  66. {
  67. va_list arg;
  68. long *param_longp=NULL;
  69. double *param_doublep=NULL;
  70. char **param_charp=NULL;
  71. struct curl_slist **param_slistp=NULL;
  72. int type;
  73. curl_socket_t sockfd;
  74. union {
  75. struct curl_certinfo * to_certinfo;
  76. struct curl_slist * to_slist;
  77. } ptr;
  78. if(!data)
  79. return CURLE_BAD_FUNCTION_ARGUMENT;
  80. va_start(arg, info);
  81. type = CURLINFO_TYPEMASK & (int)info;
  82. switch(type) {
  83. case CURLINFO_STRING:
  84. param_charp = va_arg(arg, char **);
  85. if(NULL == param_charp)
  86. return CURLE_BAD_FUNCTION_ARGUMENT;
  87. break;
  88. case CURLINFO_LONG:
  89. param_longp = va_arg(arg, long *);
  90. if(NULL == param_longp)
  91. return CURLE_BAD_FUNCTION_ARGUMENT;
  92. break;
  93. case CURLINFO_DOUBLE:
  94. param_doublep = va_arg(arg, double *);
  95. if(NULL == param_doublep)
  96. return CURLE_BAD_FUNCTION_ARGUMENT;
  97. break;
  98. case CURLINFO_SLIST:
  99. param_slistp = va_arg(arg, struct curl_slist **);
  100. if(NULL == param_slistp)
  101. return CURLE_BAD_FUNCTION_ARGUMENT;
  102. break;
  103. default:
  104. return CURLE_BAD_FUNCTION_ARGUMENT;
  105. }
  106. switch(info) {
  107. case CURLINFO_EFFECTIVE_URL:
  108. *param_charp = data->change.url?data->change.url:(char *)"";
  109. break;
  110. case CURLINFO_RESPONSE_CODE:
  111. *param_longp = data->info.httpcode;
  112. break;
  113. case CURLINFO_HTTP_CONNECTCODE:
  114. *param_longp = data->info.httpproxycode;
  115. break;
  116. case CURLINFO_FILETIME:
  117. *param_longp = data->info.filetime;
  118. break;
  119. case CURLINFO_HEADER_SIZE:
  120. *param_longp = data->info.header_size;
  121. break;
  122. case CURLINFO_REQUEST_SIZE:
  123. *param_longp = data->info.request_size;
  124. break;
  125. case CURLINFO_TOTAL_TIME:
  126. *param_doublep = data->progress.timespent;
  127. break;
  128. case CURLINFO_NAMELOOKUP_TIME:
  129. *param_doublep = data->progress.t_nslookup;
  130. break;
  131. case CURLINFO_CONNECT_TIME:
  132. *param_doublep = data->progress.t_connect;
  133. break;
  134. case CURLINFO_APPCONNECT_TIME:
  135. *param_doublep = data->progress.t_appconnect;
  136. break;
  137. case CURLINFO_PRETRANSFER_TIME:
  138. *param_doublep = data->progress.t_pretransfer;
  139. break;
  140. case CURLINFO_STARTTRANSFER_TIME:
  141. *param_doublep = data->progress.t_starttransfer;
  142. break;
  143. case CURLINFO_SIZE_UPLOAD:
  144. *param_doublep = (double)data->progress.uploaded;
  145. break;
  146. case CURLINFO_SIZE_DOWNLOAD:
  147. *param_doublep = (double)data->progress.downloaded;
  148. break;
  149. case CURLINFO_SPEED_DOWNLOAD:
  150. *param_doublep = (double)data->progress.dlspeed;
  151. break;
  152. case CURLINFO_SPEED_UPLOAD:
  153. *param_doublep = (double)data->progress.ulspeed;
  154. break;
  155. case CURLINFO_SSL_VERIFYRESULT:
  156. *param_longp = data->set.ssl.certverifyresult;
  157. break;
  158. case CURLINFO_CONTENT_LENGTH_DOWNLOAD:
  159. *param_doublep = (data->progress.flags & PGRS_DL_SIZE_KNOWN)?
  160. (double)data->progress.size_dl:-1;
  161. break;
  162. case CURLINFO_CONTENT_LENGTH_UPLOAD:
  163. *param_doublep = (data->progress.flags & PGRS_UL_SIZE_KNOWN)?
  164. (double)data->progress.size_ul:-1;
  165. break;
  166. case CURLINFO_REDIRECT_TIME:
  167. *param_doublep = data->progress.t_redirect;
  168. break;
  169. case CURLINFO_REDIRECT_COUNT:
  170. *param_longp = data->set.followlocation;
  171. break;
  172. case CURLINFO_CONTENT_TYPE:
  173. *param_charp = data->info.contenttype;
  174. break;
  175. case CURLINFO_PRIVATE:
  176. *param_charp = (char *) data->set.private_data;
  177. break;
  178. case CURLINFO_HTTPAUTH_AVAIL:
  179. *param_longp = data->info.httpauthavail;
  180. break;
  181. case CURLINFO_PROXYAUTH_AVAIL:
  182. *param_longp = data->info.proxyauthavail;
  183. break;
  184. case CURLINFO_OS_ERRNO:
  185. *param_longp = data->state.os_errno;
  186. break;
  187. case CURLINFO_NUM_CONNECTS:
  188. *param_longp = data->info.numconnects;
  189. break;
  190. case CURLINFO_SSL_ENGINES:
  191. *param_slistp = Curl_ssl_engines_list(data);
  192. break;
  193. case CURLINFO_COOKIELIST:
  194. *param_slistp = Curl_cookie_list(data);
  195. break;
  196. case CURLINFO_FTP_ENTRY_PATH:
  197. /* Return the entrypath string from the most recent connection.
  198. This pointer was copied from the connectdata structure by FTP.
  199. The actual string may be free()ed by subsequent libcurl calls so
  200. it must be copied to a safer area before the next libcurl call.
  201. Callers must never free it themselves. */
  202. *param_charp = data->state.most_recent_ftp_entrypath;
  203. break;
  204. case CURLINFO_LASTSOCKET:
  205. sockfd = Curl_getconnectinfo(data, NULL);
  206. /* note: this is not a good conversion for systems with 64 bit sockets and
  207. 32 bit longs */
  208. if(sockfd != CURL_SOCKET_BAD)
  209. *param_longp = (long)sockfd;
  210. else
  211. /* this interface is documented to return -1 in case of badness, which
  212. may not be the same as the CURL_SOCKET_BAD value */
  213. *param_longp = -1;
  214. break;
  215. case CURLINFO_REDIRECT_URL:
  216. /* Return the URL this request would have been redirected to if that
  217. option had been enabled! */
  218. *param_charp = data->info.wouldredirect;
  219. break;
  220. case CURLINFO_PRIMARY_IP:
  221. /* Return the ip address of the most recent (primary) connection */
  222. *param_charp = data->info.conn_primary_ip;
  223. break;
  224. case CURLINFO_PRIMARY_PORT:
  225. /* Return the (remote) port of the most recent (primary) connection */
  226. *param_longp = data->info.conn_primary_port;
  227. break;
  228. case CURLINFO_LOCAL_IP:
  229. /* Return the source/local ip address of the most recent (primary)
  230. connection */
  231. *param_charp = data->info.conn_local_ip;
  232. break;
  233. case CURLINFO_LOCAL_PORT:
  234. /* Return the local port of the most recent (primary) connection */
  235. *param_longp = data->info.conn_local_port;
  236. break;
  237. case CURLINFO_CERTINFO:
  238. /* Return the a pointer to the certinfo struct. Not really an slist
  239. pointer but we can pretend it is here */
  240. ptr.to_certinfo = &data->info.certs;
  241. *param_slistp = ptr.to_slist;
  242. break;
  243. case CURLINFO_CONDITION_UNMET:
  244. /* return if the condition prevented the document to get transfered */
  245. *param_longp = data->info.timecond;
  246. break;
  247. case CURLINFO_RTSP_SESSION_ID:
  248. *param_charp = data->set.str[STRING_RTSP_SESSION_ID];
  249. break;
  250. case CURLINFO_RTSP_CLIENT_CSEQ:
  251. *param_longp = data->state.rtsp_next_client_CSeq;
  252. break;
  253. case CURLINFO_RTSP_SERVER_CSEQ:
  254. *param_longp = data->state.rtsp_next_server_CSeq;
  255. break;
  256. case CURLINFO_RTSP_CSEQ_RECV:
  257. *param_longp = data->state.rtsp_CSeq_recv;
  258. break;
  259. default:
  260. return CURLE_BAD_FUNCTION_ARGUMENT;
  261. }
  262. return CURLE_OK;
  263. }