fix compiler warning
[platform/upstream/curl.git] / lib / getinfo.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2007, 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 #ifdef MSG_PEEK
79   char buf;
80 #endif
81   int type;
82
83   if(!data)
84     return CURLE_BAD_FUNCTION_ARGUMENT;
85
86   va_start(arg, info);
87
88   type = CURLINFO_TYPEMASK & (int)info;
89   switch(type) {
90   case CURLINFO_STRING:
91     param_charp = va_arg(arg, char **);
92     if(NULL == param_charp)
93       return CURLE_BAD_FUNCTION_ARGUMENT;
94     break;
95   case CURLINFO_LONG:
96     param_longp = va_arg(arg, long *);
97     if(NULL == param_longp)
98       return CURLE_BAD_FUNCTION_ARGUMENT;
99     break;
100   case CURLINFO_DOUBLE:
101     param_doublep = va_arg(arg, double *);
102     if(NULL == param_doublep)
103       return CURLE_BAD_FUNCTION_ARGUMENT;
104     break;
105   case CURLINFO_SLIST:
106     param_slistp = va_arg(arg, struct curl_slist **);
107     if(NULL == param_slistp)
108       return CURLE_BAD_FUNCTION_ARGUMENT;
109     break;
110   default:
111     return CURLE_BAD_FUNCTION_ARGUMENT;
112   }
113
114   switch(info) {
115   case CURLINFO_EFFECTIVE_URL:
116     *param_charp = data->change.url?data->change.url:(char *)"";
117     break;
118   case CURLINFO_RESPONSE_CODE:
119     *param_longp = data->info.httpcode;
120     break;
121   case CURLINFO_HTTP_CONNECTCODE:
122     *param_longp = data->info.httpproxycode;
123     break;
124   case CURLINFO_FILETIME:
125     *param_longp = data->info.filetime;
126     break;
127   case CURLINFO_HEADER_SIZE:
128     *param_longp = data->info.header_size;
129     break;
130   case CURLINFO_REQUEST_SIZE:
131     *param_longp = data->info.request_size;
132     break;
133   case CURLINFO_TOTAL_TIME:
134     *param_doublep = data->progress.timespent;
135     break;
136   case CURLINFO_NAMELOOKUP_TIME:
137     *param_doublep = data->progress.t_nslookup;
138     break;
139   case CURLINFO_CONNECT_TIME:
140     *param_doublep = data->progress.t_connect;
141     break;
142   case CURLINFO_PRETRANSFER_TIME:
143     *param_doublep =  data->progress.t_pretransfer;
144     break;
145   case CURLINFO_STARTTRANSFER_TIME:
146     *param_doublep = data->progress.t_starttransfer;
147     break;
148   case CURLINFO_SIZE_UPLOAD:
149     *param_doublep =  (double)data->progress.uploaded;
150     break;
151   case CURLINFO_SIZE_DOWNLOAD:
152     *param_doublep = (double)data->progress.downloaded;
153     break;
154   case CURLINFO_SPEED_DOWNLOAD:
155     *param_doublep =  (double)data->progress.dlspeed;
156     break;
157   case CURLINFO_SPEED_UPLOAD:
158     *param_doublep = (double)data->progress.ulspeed;
159     break;
160   case CURLINFO_SSL_VERIFYRESULT:
161     *param_longp = data->set.ssl.certverifyresult;
162     break;
163   case CURLINFO_CONTENT_LENGTH_DOWNLOAD:
164     *param_doublep = (double)data->progress.size_dl;
165     break;
166   case CURLINFO_CONTENT_LENGTH_UPLOAD:
167     *param_doublep = (double)data->progress.size_ul;
168     break;
169   case CURLINFO_REDIRECT_TIME:
170     *param_doublep =  data->progress.t_redirect;
171     break;
172   case CURLINFO_REDIRECT_COUNT:
173     *param_longp = data->set.followlocation;
174     break;
175   case CURLINFO_CONTENT_TYPE:
176     *param_charp = data->info.contenttype;
177     break;
178   case CURLINFO_PRIVATE:
179     *param_charp = data->set.private_data;
180     break;
181   case CURLINFO_HTTPAUTH_AVAIL:
182     *param_longp = data->info.httpauthavail;
183     break;
184   case CURLINFO_PROXYAUTH_AVAIL:
185     *param_longp = data->info.proxyauthavail;
186     break;
187   case CURLINFO_OS_ERRNO:
188     *param_longp = data->state.os_errno;
189     break;
190   case CURLINFO_NUM_CONNECTS:
191     *param_longp = data->info.numconnects;
192     break;
193   case CURLINFO_SSL_ENGINES:
194     *param_slistp = Curl_ssl_engines_list(data);
195     break;
196   case CURLINFO_COOKIELIST:
197     *param_slistp = Curl_cookie_list(data);
198     break;
199   case CURLINFO_FTP_ENTRY_PATH:
200     /* Return the entrypath string from the most recent connection.
201        This pointer was copied from the connectdata structure by FTP.
202        The actual string may be free()ed by subsequent libcurl calls so
203        it must be copied to a safer area before the next libcurl call.
204        Callers must never free it themselves. */
205     *param_charp = data->state.most_recent_ftp_entrypath;
206     break;
207   case CURLINFO_LASTSOCKET:
208     if((data->state.lastconnect != -1) &&
209        (data->state.connc->connects[data->state.lastconnect] != NULL)) {
210       struct connectdata *c = data->state.connc->connects
211         [data->state.lastconnect];
212       *param_longp = c->sock[FIRSTSOCKET];
213       /* we have a socket connected, let's determine if the server shut down */
214       /* determine if ssl */
215       if(c->ssl[FIRSTSOCKET].use) {
216         /* use the SSL context */
217         if (!Curl_ssl_check_cxn(c))
218           *param_longp = -1;   /* FIN received */
219       }
220 /* Minix 3.1 doesn't support any flags on recv; just assume socket is OK */
221 #ifdef MSG_PEEK
222       else {
223         /* use the socket */
224         if(recv((RECV_TYPE_ARG1)c->sock[FIRSTSOCKET], (RECV_TYPE_ARG2)&buf,
225                 (RECV_TYPE_ARG3)1, (RECV_TYPE_ARG4)MSG_PEEK) == 0) {
226           *param_longp = -1;   /* FIN received */
227         }
228       }
229 #endif
230     }
231     else
232       *param_longp = -1;
233     break;
234   default:
235     return CURLE_BAD_FUNCTION_ARGUMENT;
236   }
237   return CURLE_OK;
238 }