Karl M added the CURLOPT_CONNECT_ONLY and CURLINFO_LASTSOCKET options that
[platform/upstream/curl.git] / lib / getinfo.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2006, 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  * $Id$
22  ***************************************************************************/
23
24 #include "setup.h"
25
26 #include <curl/curl.h>
27
28 #include "urldata.h"
29 #include "getinfo.h"
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <stdlib.h>
35 #include "memory.h"
36 #include "sslgen.h"
37
38 /* Make this the last #include */
39 #include "memdebug.h"
40
41 /*
42  * This is supposed to be called in the beginning of a perform() session
43  * and should reset all session-info variables
44  */
45 CURLcode Curl_initinfo(struct SessionHandle *data)
46 {
47   struct Progress *pro = &data->progress;
48   struct PureInfo *info =&data->info;
49
50   pro->t_nslookup = 0;
51   pro->t_connect = 0;
52   pro->t_pretransfer = 0;
53   pro->t_starttransfer = 0;
54   pro->timespent = 0;
55   pro->t_redirect = 0;
56
57   info->httpcode = 0;
58   info->httpversion=0;
59   info->filetime=-1; /* -1 is an illegal time and thus means unknown */
60
61   if (info->contenttype)
62     free(info->contenttype);
63   info->contenttype = NULL;
64
65   info->header_size = 0;
66   info->request_size = 0;
67   info->numconnects = 0;
68   return CURLE_OK;
69 }
70
71 CURLcode Curl_getinfo(struct SessionHandle *data, CURLINFO info, ...)
72 {
73   va_list arg;
74   long *param_longp=NULL;
75   double *param_doublep=NULL;
76   char **param_charp=NULL;
77   struct curl_slist **param_slistp=NULL;
78   va_start(arg, info);
79
80   switch(info&CURLINFO_TYPEMASK) {
81   default:
82     return CURLE_BAD_FUNCTION_ARGUMENT;
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   }
104
105   switch(info) {
106   case CURLINFO_EFFECTIVE_URL:
107     *param_charp = data->change.url?data->change.url:(char *)"";
108     break;
109   case CURLINFO_RESPONSE_CODE:
110     *param_longp = data->info.httpcode;
111     break;
112   case CURLINFO_HTTP_CONNECTCODE:
113     *param_longp = data->info.httpproxycode;
114     break;
115   case CURLINFO_FILETIME:
116     *param_longp = data->info.filetime;
117     break;
118   case CURLINFO_HEADER_SIZE:
119     *param_longp = data->info.header_size;
120     break;
121   case CURLINFO_REQUEST_SIZE:
122     *param_longp = data->info.request_size;
123     break;
124   case CURLINFO_TOTAL_TIME:
125     *param_doublep = data->progress.timespent;
126     break;
127   case CURLINFO_NAMELOOKUP_TIME:
128     *param_doublep = data->progress.t_nslookup;
129     break;
130   case CURLINFO_CONNECT_TIME:
131     *param_doublep = data->progress.t_connect;
132     break;
133   case CURLINFO_PRETRANSFER_TIME:
134     *param_doublep =  data->progress.t_pretransfer;
135     break;
136   case CURLINFO_STARTTRANSFER_TIME:
137     *param_doublep = data->progress.t_starttransfer;
138     break;
139   case CURLINFO_SIZE_UPLOAD:
140     *param_doublep =  (double)data->progress.uploaded;
141     break;
142   case CURLINFO_SIZE_DOWNLOAD:
143     *param_doublep = (double)data->progress.downloaded;
144     break;
145   case CURLINFO_SPEED_DOWNLOAD:
146     *param_doublep =  (double)data->progress.dlspeed;
147     break;
148   case CURLINFO_SPEED_UPLOAD:
149     *param_doublep = (double)data->progress.ulspeed;
150     break;
151   case CURLINFO_SSL_VERIFYRESULT:
152     *param_longp = data->set.ssl.certverifyresult;
153     break;
154   case CURLINFO_CONTENT_LENGTH_DOWNLOAD:
155     *param_doublep = (double)data->progress.size_dl;
156     break;
157   case CURLINFO_CONTENT_LENGTH_UPLOAD:
158     *param_doublep = (double)data->progress.size_ul;
159     break;
160   case CURLINFO_REDIRECT_TIME:
161     *param_doublep =  data->progress.t_redirect;
162     break;
163   case CURLINFO_REDIRECT_COUNT:
164     *param_longp = data->set.followlocation;
165     break;
166   case CURLINFO_CONTENT_TYPE:
167     *param_charp = data->info.contenttype;
168     break;
169   case CURLINFO_PRIVATE:
170     *param_charp = data->set.private_data;
171     break;
172   case CURLINFO_HTTPAUTH_AVAIL:
173     *param_longp = data->info.httpauthavail;
174     break;
175   case CURLINFO_PROXYAUTH_AVAIL:
176     *param_longp = data->info.proxyauthavail;
177     break;
178   case CURLINFO_OS_ERRNO:
179     *param_longp = data->state.os_errno;
180     break;
181   case CURLINFO_NUM_CONNECTS:
182     *param_longp = data->info.numconnects;
183     break;
184   case CURLINFO_SSL_ENGINES:
185     *param_slistp = Curl_ssl_engines_list(data);
186     break;
187   case CURLINFO_COOKIELIST:
188     *param_slistp = Curl_cookie_list(data);
189     break;
190   case CURLINFO_LASTSOCKET:
191     if((data->state.lastconnect != -1) &&
192        (data->state.connects[data->state.lastconnect] != NULL))
193       *param_longp = data->state.connects[data->state.lastconnect]->
194         sock[FIRSTSOCKET];
195     else
196       *param_longp = -1;
197     break;
198   default:
199     return CURLE_BAD_FUNCTION_ARGUMENT;
200   }
201   return CURLE_OK;
202 }