b8801a4bb5df46a2451ca88bf8ffd94d4ed57433
[platform/upstream/curl.git] / src / writeout.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2009, 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 <stdio.h>
26 #include <string.h>
27
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
30 #endif
31 #ifdef HAVE_SYS_SELECT_H
32 #include <sys/select.h>
33 #endif
34
35 #include <curl/curl.h>
36
37 #define _MPRINTF_REPLACE /* we want curl-functions instead of native ones */
38 #include <curl/mprintf.h>
39
40 #include "writeout.h"
41
42 typedef enum {
43   VAR_NONE,       /* must be the first */
44   VAR_TOTAL_TIME,
45   VAR_NAMELOOKUP_TIME,
46   VAR_CONNECT_TIME,
47   VAR_APPCONNECT_TIME,
48   VAR_PRETRANSFER_TIME,
49   VAR_STARTTRANSFER_TIME,
50   VAR_SIZE_DOWNLOAD,
51   VAR_SIZE_UPLOAD,
52   VAR_SPEED_DOWNLOAD,
53   VAR_SPEED_UPLOAD,
54   VAR_HTTP_CODE,
55   VAR_HTTP_CODE_PROXY,
56   VAR_HEADER_SIZE,
57   VAR_REQUEST_SIZE,
58   VAR_EFFECTIVE_URL,
59   VAR_CONTENT_TYPE,
60   VAR_NUM_CONNECTS,
61   VAR_REDIRECT_TIME,
62   VAR_REDIRECT_COUNT,
63   VAR_FTP_ENTRY_PATH,
64   VAR_REDIRECT_URL,
65   VAR_SSL_VERIFY_RESULT,
66   VAR_NUM_OF_VARS /* must be the last */
67 } replaceid;
68
69 struct variable {
70   const char *name;
71   replaceid id;
72 };
73
74
75 static const struct variable replacements[]={
76   {"url_effective", VAR_EFFECTIVE_URL},
77   {"http_code", VAR_HTTP_CODE},
78   {"response_code", VAR_HTTP_CODE},
79   {"http_connect", VAR_HTTP_CODE_PROXY},
80   {"time_total", VAR_TOTAL_TIME},
81   {"time_namelookup", VAR_NAMELOOKUP_TIME},
82   {"time_connect", VAR_CONNECT_TIME},
83   {"time_appconnect", VAR_APPCONNECT_TIME},
84   {"time_pretransfer", VAR_PRETRANSFER_TIME},
85   {"time_starttransfer", VAR_STARTTRANSFER_TIME},
86   {"size_header", VAR_HEADER_SIZE},
87   {"size_request", VAR_REQUEST_SIZE},
88   {"size_download", VAR_SIZE_DOWNLOAD},
89   {"size_upload", VAR_SIZE_UPLOAD},
90   {"speed_download", VAR_SPEED_DOWNLOAD},
91   {"speed_upload", VAR_SPEED_UPLOAD},
92   {"content_type", VAR_CONTENT_TYPE},
93   {"num_connects", VAR_NUM_CONNECTS},
94   {"time_redirect", VAR_REDIRECT_TIME},
95   {"num_redirects", VAR_REDIRECT_COUNT},
96   {"ftp_entry_path", VAR_FTP_ENTRY_PATH},
97   {"redirect_url", VAR_REDIRECT_URL},
98   {"ssl_verify_result", VAR_SSL_VERIFY_RESULT},
99   {NULL, VAR_NONE}
100 };
101
102 void ourWriteOut(CURL *curl, const char *writeinfo)
103 {
104   FILE *stream = stdout;
105   const char *ptr=writeinfo;
106   char *stringp;
107   long longinfo;
108   double doubleinfo;
109
110   while(ptr && *ptr) {
111     if('%' == *ptr) {
112       if('%' == ptr[1]) {
113         /* an escaped %-letter */
114         fputc('%', stream);
115         ptr+=2;
116       }
117       else {
118         /* this is meant as a variable to output */
119         char *end;
120         char keepit;
121         int i;
122         if(('{' == ptr[1]) && ((end = strchr(ptr, '}')) != NULL)) {
123           bool match = FALSE;
124           ptr+=2; /* pass the % and the { */
125           keepit=*end;
126           *end=0; /* zero terminate */
127           for(i=0; replacements[i].name; i++) {
128             if(curl_strequal(ptr, replacements[i].name)) {
129               match = TRUE;
130               switch(replacements[i].id) {
131               case VAR_EFFECTIVE_URL:
132                 if((CURLE_OK ==
133                     curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &stringp))
134                    && stringp)
135                   fputs(stringp, stream);
136                 break;
137               case VAR_HTTP_CODE:
138                 if(CURLE_OK ==
139                    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &longinfo))
140                   fprintf(stream, "%03ld", longinfo);
141                 break;
142               case VAR_HTTP_CODE_PROXY:
143                 if(CURLE_OK ==
144                    curl_easy_getinfo(curl, CURLINFO_HTTP_CONNECTCODE,
145                                      &longinfo))
146                   fprintf(stream, "%03ld", longinfo);
147                 break;
148               case VAR_HEADER_SIZE:
149                 if(CURLE_OK ==
150                    curl_easy_getinfo(curl, CURLINFO_HEADER_SIZE, &longinfo))
151                   fprintf(stream, "%ld", longinfo);
152                 break;
153               case VAR_REQUEST_SIZE:
154                 if(CURLE_OK ==
155                    curl_easy_getinfo(curl, CURLINFO_REQUEST_SIZE, &longinfo))
156                   fprintf(stream, "%ld", longinfo);
157                 break;
158               case VAR_NUM_CONNECTS:
159                 if(CURLE_OK ==
160                    curl_easy_getinfo(curl, CURLINFO_NUM_CONNECTS, &longinfo))
161                   fprintf(stream, "%ld", longinfo);
162                 break;
163               case VAR_REDIRECT_COUNT:
164                 if(CURLE_OK ==
165                    curl_easy_getinfo(curl, CURLINFO_REDIRECT_COUNT, &longinfo))
166                   fprintf(stream, "%ld", longinfo);
167                 break;
168               case VAR_REDIRECT_TIME:
169                 if(CURLE_OK ==
170                    curl_easy_getinfo(curl, CURLINFO_REDIRECT_TIME, &doubleinfo))
171                   fprintf(stream, "%.3f", doubleinfo);
172                 break;
173               case VAR_TOTAL_TIME:
174                 if(CURLE_OK ==
175                    curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &doubleinfo))
176                   fprintf(stream, "%.3f", doubleinfo);
177                 break;
178               case VAR_NAMELOOKUP_TIME:
179                 if(CURLE_OK ==
180                    curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME,
181                                      &doubleinfo))
182                   fprintf(stream, "%.3f", doubleinfo);
183                 break;
184               case VAR_CONNECT_TIME:
185                 if(CURLE_OK ==
186                    curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &doubleinfo))
187                   fprintf(stream, "%.3f", doubleinfo);
188                 break;
189               case VAR_APPCONNECT_TIME:
190                 if(CURLE_OK ==
191                    curl_easy_getinfo(curl, CURLINFO_APPCONNECT_TIME,
192                                      &doubleinfo))
193                   fprintf(stream, "%.3f", doubleinfo);
194                 break;
195               case VAR_PRETRANSFER_TIME:
196                 if(CURLE_OK ==
197                    curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME,
198                                      &doubleinfo))
199                   fprintf(stream, "%.3f", doubleinfo);
200                 break;
201               case VAR_STARTTRANSFER_TIME:
202                 if(CURLE_OK ==
203                    curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME,
204                                      &doubleinfo))
205                   fprintf(stream, "%.3f", doubleinfo);
206                 break;
207               case VAR_SIZE_UPLOAD:
208                 if(CURLE_OK ==
209                    curl_easy_getinfo(curl, CURLINFO_SIZE_UPLOAD, &doubleinfo))
210                   fprintf(stream, "%.0f", doubleinfo);
211                 break;
212               case VAR_SIZE_DOWNLOAD:
213                 if(CURLE_OK ==
214                    curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &doubleinfo))
215                   fprintf(stream, "%.0f", doubleinfo);
216                 break;
217               case VAR_SPEED_DOWNLOAD:
218                 if(CURLE_OK ==
219                    curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &doubleinfo))
220                   fprintf(stream, "%.3f", doubleinfo);
221                 break;
222               case VAR_SPEED_UPLOAD:
223                 if(CURLE_OK ==
224                    curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &doubleinfo))
225                   fprintf(stream, "%.3f", doubleinfo);
226                 break;
227               case VAR_CONTENT_TYPE:
228                 if((CURLE_OK ==
229                     curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &stringp))
230                    && stringp)
231                   fputs(stringp, stream);
232                 break;
233               case VAR_FTP_ENTRY_PATH:
234                 if((CURLE_OK ==
235                     curl_easy_getinfo(curl, CURLINFO_FTP_ENTRY_PATH, &stringp))
236                    && stringp)
237                   fputs(stringp, stream);
238                 break;
239               case VAR_REDIRECT_URL:
240                 if((CURLE_OK ==
241                     curl_easy_getinfo(curl, CURLINFO_REDIRECT_URL, &stringp))
242                    && stringp)
243                   fputs(stringp, stream);
244                 break;
245               case VAR_SSL_VERIFY_RESULT:
246                 if(CURLE_OK ==
247                    curl_easy_getinfo(curl, CURLINFO_SSL_VERIFYRESULT, &longinfo))
248                   fprintf(stream, "%ld", longinfo);
249                 break;
250               default:
251                 break;
252               }
253               break;
254             }
255           }
256           if(FALSE == match) {
257             fprintf(stderr, "curl: unknown --write-out variable: '%s'\n", ptr);
258           }
259           ptr=end+1; /* pass the end */
260           *end = keepit;
261         }
262         else {
263           /* illegal syntax, then just output the characters that are used */
264           fputc('%', stream);
265           fputc(ptr[1], stream);
266           ptr+=2;
267         }
268       }
269     }
270     else if('\\' == *ptr) {
271       switch(ptr[1]) {
272       case 'r':
273         fputc('\r', stream);
274         break;
275       case 'n':
276         fputc('\n', stream);
277         break;
278       case 't':
279         fputc('\t', stream);
280         break;
281       default:
282         /* unknown, just output this */
283         fputc(*ptr, stream);
284         fputc(ptr[1], stream);
285         break;
286       }
287       ptr+=2;
288     }
289     else {
290       fputc(*ptr, stream);
291       ptr++;
292     }
293   }
294
295 }