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