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