• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2016, 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 https://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 #include "tool_setup.h"
23 #define ENABLE_CURLX_PRINTF
24 /* use our own printf() functions */
25 #include "curlx.h"
26 #include "tool_cfgable.h"
27 #include "tool_writeout.h"
28 
29 #include "memdebug.h" /* keep this as LAST include */
30 
31 typedef enum {
32   VAR_NONE,       /* must be the first */
33   VAR_TOTAL_TIME,
34   VAR_NAMELOOKUP_TIME,
35   VAR_CONNECT_TIME,
36   VAR_APPCONNECT_TIME,
37   VAR_PRETRANSFER_TIME,
38   VAR_STARTTRANSFER_TIME,
39   VAR_SIZE_DOWNLOAD,
40   VAR_SIZE_UPLOAD,
41   VAR_SPEED_DOWNLOAD,
42   VAR_SPEED_UPLOAD,
43   VAR_HTTP_CODE,
44   VAR_HTTP_CODE_PROXY,
45   VAR_HEADER_SIZE,
46   VAR_REQUEST_SIZE,
47   VAR_EFFECTIVE_URL,
48   VAR_CONTENT_TYPE,
49   VAR_NUM_CONNECTS,
50   VAR_REDIRECT_TIME,
51   VAR_REDIRECT_COUNT,
52   VAR_FTP_ENTRY_PATH,
53   VAR_REDIRECT_URL,
54   VAR_SSL_VERIFY_RESULT,
55   VAR_PROXY_SSL_VERIFY_RESULT,
56   VAR_EFFECTIVE_FILENAME,
57   VAR_PRIMARY_IP,
58   VAR_PRIMARY_PORT,
59   VAR_LOCAL_IP,
60   VAR_LOCAL_PORT,
61   VAR_HTTP_VERSION,
62   VAR_SCHEME,
63   VAR_NUM_OF_VARS /* must be the last */
64 } replaceid;
65 
66 struct variable {
67   const char *name;
68   replaceid id;
69 };
70 
71 
72 static const struct variable replacements[]={
73   {"url_effective", VAR_EFFECTIVE_URL},
74   {"http_code", VAR_HTTP_CODE},
75   {"response_code", VAR_HTTP_CODE},
76   {"http_connect", VAR_HTTP_CODE_PROXY},
77   {"time_total", VAR_TOTAL_TIME},
78   {"time_namelookup", VAR_NAMELOOKUP_TIME},
79   {"time_connect", VAR_CONNECT_TIME},
80   {"time_appconnect", VAR_APPCONNECT_TIME},
81   {"time_pretransfer", VAR_PRETRANSFER_TIME},
82   {"time_starttransfer", VAR_STARTTRANSFER_TIME},
83   {"size_header", VAR_HEADER_SIZE},
84   {"size_request", VAR_REQUEST_SIZE},
85   {"size_download", VAR_SIZE_DOWNLOAD},
86   {"size_upload", VAR_SIZE_UPLOAD},
87   {"speed_download", VAR_SPEED_DOWNLOAD},
88   {"speed_upload", VAR_SPEED_UPLOAD},
89   {"content_type", VAR_CONTENT_TYPE},
90   {"num_connects", VAR_NUM_CONNECTS},
91   {"time_redirect", VAR_REDIRECT_TIME},
92   {"num_redirects", VAR_REDIRECT_COUNT},
93   {"ftp_entry_path", VAR_FTP_ENTRY_PATH},
94   {"redirect_url", VAR_REDIRECT_URL},
95   {"ssl_verify_result", VAR_SSL_VERIFY_RESULT},
96   {"proxy_ssl_verify_result", VAR_PROXY_SSL_VERIFY_RESULT},
97   {"filename_effective", VAR_EFFECTIVE_FILENAME},
98   {"remote_ip", VAR_PRIMARY_IP},
99   {"remote_port", VAR_PRIMARY_PORT},
100   {"local_ip", VAR_LOCAL_IP},
101   {"local_port", VAR_LOCAL_PORT},
102   {"http_version", VAR_HTTP_VERSION},
103   {"scheme", VAR_SCHEME},
104   {NULL, VAR_NONE}
105 };
106 
ourWriteOut(CURL * curl,struct OutStruct * outs,const char * writeinfo)107 void ourWriteOut(CURL *curl, struct OutStruct *outs, const char *writeinfo)
108 {
109   FILE *stream = stdout;
110   const char *ptr = writeinfo;
111   char *stringp = NULL;
112   long longinfo;
113   double doubleinfo;
114 
115   while(ptr && *ptr) {
116     if('%' == *ptr) {
117       if('%' == ptr[1]) {
118         /* an escaped %-letter */
119         fputc('%', stream);
120         ptr += 2;
121       }
122       else {
123         /* this is meant as a variable to output */
124         char *end;
125         char keepit;
126         int i;
127         if(('{' == ptr[1]) && ((end = strchr(ptr, '}')) != NULL)) {
128           bool match = FALSE;
129           ptr += 2; /* pass the % and the { */
130           keepit = *end;
131           *end = 0; /* zero terminate */
132           for(i = 0; replacements[i].name; i++) {
133             if(curl_strequal(ptr, replacements[i].name)) {
134               match = TRUE;
135               switch(replacements[i].id) {
136               case VAR_EFFECTIVE_URL:
137                 if((CURLE_OK ==
138                     curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &stringp))
139                    && stringp)
140                   fputs(stringp, stream);
141                 break;
142               case VAR_HTTP_CODE:
143                 if(CURLE_OK ==
144                    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &longinfo))
145                   fprintf(stream, "%03ld", longinfo);
146                 break;
147               case VAR_HTTP_CODE_PROXY:
148                 if(CURLE_OK ==
149                    curl_easy_getinfo(curl, CURLINFO_HTTP_CONNECTCODE,
150                                      &longinfo))
151                   fprintf(stream, "%03ld", longinfo);
152                 break;
153               case VAR_HEADER_SIZE:
154                 if(CURLE_OK ==
155                    curl_easy_getinfo(curl, CURLINFO_HEADER_SIZE, &longinfo))
156                   fprintf(stream, "%ld", longinfo);
157                 break;
158               case VAR_REQUEST_SIZE:
159                 if(CURLE_OK ==
160                    curl_easy_getinfo(curl, CURLINFO_REQUEST_SIZE, &longinfo))
161                   fprintf(stream, "%ld", longinfo);
162                 break;
163               case VAR_NUM_CONNECTS:
164                 if(CURLE_OK ==
165                    curl_easy_getinfo(curl, CURLINFO_NUM_CONNECTS, &longinfo))
166                   fprintf(stream, "%ld", longinfo);
167                 break;
168               case VAR_REDIRECT_COUNT:
169                 if(CURLE_OK ==
170                    curl_easy_getinfo(curl, CURLINFO_REDIRECT_COUNT, &longinfo))
171                   fprintf(stream, "%ld", longinfo);
172                 break;
173               case VAR_REDIRECT_TIME:
174                 if(CURLE_OK ==
175                    curl_easy_getinfo(curl, CURLINFO_REDIRECT_TIME,
176                                      &doubleinfo))
177                   fprintf(stream, "%.6f", doubleinfo);
178                 break;
179               case VAR_TOTAL_TIME:
180                 if(CURLE_OK ==
181                    curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &doubleinfo))
182                   fprintf(stream, "%.6f", doubleinfo);
183                 break;
184               case VAR_NAMELOOKUP_TIME:
185                 if(CURLE_OK ==
186                    curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME,
187                                      &doubleinfo))
188                   fprintf(stream, "%.6f", doubleinfo);
189                 break;
190               case VAR_CONNECT_TIME:
191                 if(CURLE_OK ==
192                    curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &doubleinfo))
193                   fprintf(stream, "%.6f", doubleinfo);
194                 break;
195               case VAR_APPCONNECT_TIME:
196                 if(CURLE_OK ==
197                    curl_easy_getinfo(curl, CURLINFO_APPCONNECT_TIME,
198                                      &doubleinfo))
199                   fprintf(stream, "%.6f", doubleinfo);
200                 break;
201               case VAR_PRETRANSFER_TIME:
202                 if(CURLE_OK ==
203                    curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME,
204                                      &doubleinfo))
205                   fprintf(stream, "%.6f", doubleinfo);
206                 break;
207               case VAR_STARTTRANSFER_TIME:
208                 if(CURLE_OK ==
209                    curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME,
210                                      &doubleinfo))
211                   fprintf(stream, "%.6f", doubleinfo);
212                 break;
213               case VAR_SIZE_UPLOAD:
214                 if(CURLE_OK ==
215                    curl_easy_getinfo(curl, CURLINFO_SIZE_UPLOAD, &doubleinfo))
216                   fprintf(stream, "%.0f", doubleinfo);
217                 break;
218               case VAR_SIZE_DOWNLOAD:
219                 if(CURLE_OK ==
220                    curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD,
221                                      &doubleinfo))
222                   fprintf(stream, "%.0f", doubleinfo);
223                 break;
224               case VAR_SPEED_DOWNLOAD:
225                 if(CURLE_OK ==
226                    curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD,
227                                      &doubleinfo))
228                   fprintf(stream, "%.3f", doubleinfo);
229                 break;
230               case VAR_SPEED_UPLOAD:
231                 if(CURLE_OK ==
232                    curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &doubleinfo))
233                   fprintf(stream, "%.3f", doubleinfo);
234                 break;
235               case VAR_CONTENT_TYPE:
236                 if((CURLE_OK ==
237                     curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &stringp))
238                    && stringp)
239                   fputs(stringp, stream);
240                 break;
241               case VAR_FTP_ENTRY_PATH:
242                 if((CURLE_OK ==
243                     curl_easy_getinfo(curl, CURLINFO_FTP_ENTRY_PATH, &stringp))
244                    && stringp)
245                   fputs(stringp, stream);
246                 break;
247               case VAR_REDIRECT_URL:
248                 if((CURLE_OK ==
249                     curl_easy_getinfo(curl, CURLINFO_REDIRECT_URL, &stringp))
250                    && stringp)
251                   fputs(stringp, stream);
252                 break;
253               case VAR_SSL_VERIFY_RESULT:
254                 if(CURLE_OK ==
255                    curl_easy_getinfo(curl, CURLINFO_SSL_VERIFYRESULT,
256                                      &longinfo))
257                   fprintf(stream, "%ld", longinfo);
258                 break;
259               case VAR_PROXY_SSL_VERIFY_RESULT:
260                 if(CURLE_OK ==
261                    curl_easy_getinfo(curl, CURLINFO_PROXY_SSL_VERIFYRESULT,
262                                      &longinfo))
263                   fprintf(stream, "%ld", longinfo);
264                 break;
265               case VAR_EFFECTIVE_FILENAME:
266                 if(outs->filename)
267                   fprintf(stream, "%s", outs->filename);
268                 break;
269               case VAR_PRIMARY_IP:
270                 if(CURLE_OK ==
271                    curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP,
272                                      &stringp))
273                   fprintf(stream, "%s", stringp);
274                 break;
275               case VAR_PRIMARY_PORT:
276                 if(CURLE_OK ==
277                    curl_easy_getinfo(curl, CURLINFO_PRIMARY_PORT,
278                                      &longinfo))
279                   fprintf(stream, "%ld", longinfo);
280                 break;
281               case VAR_LOCAL_IP:
282                 if(CURLE_OK ==
283                    curl_easy_getinfo(curl, CURLINFO_LOCAL_IP,
284                                      &stringp))
285                   fprintf(stream, "%s", stringp);
286                 break;
287               case VAR_LOCAL_PORT:
288                 if(CURLE_OK ==
289                    curl_easy_getinfo(curl, CURLINFO_LOCAL_PORT,
290                                      &longinfo))
291                   fprintf(stream, "%ld", longinfo);
292                 break;
293               case VAR_HTTP_VERSION:
294                 if(CURLE_OK ==
295                    curl_easy_getinfo(curl, CURLINFO_HTTP_VERSION,
296                                      &longinfo)) {
297                   const char *version = "0";
298                   switch (longinfo) {
299                   case CURL_HTTP_VERSION_1_0:
300                     version = "1.0";
301                     break;
302                   case CURL_HTTP_VERSION_1_1:
303                     version = "1.1";
304                     break;
305                   case CURL_HTTP_VERSION_2_0:
306                     version = "2";
307                     break;
308                   }
309 
310                   fprintf(stream, version);
311                 }
312                 break;
313               case VAR_SCHEME:
314                 if(CURLE_OK ==
315                    curl_easy_getinfo(curl, CURLINFO_SCHEME,
316                                      &stringp))
317                   fprintf(stream, "%s", stringp);
318                 break;
319               default:
320                 break;
321               }
322               break;
323             }
324           }
325           if(!match) {
326             fprintf(stderr, "curl: unknown --write-out variable: '%s'\n", ptr);
327           }
328           ptr = end + 1; /* pass the end */
329           *end = keepit;
330         }
331         else {
332           /* illegal syntax, then just output the characters that are used */
333           fputc('%', stream);
334           fputc(ptr[1], stream);
335           ptr += 2;
336         }
337       }
338     }
339     else if('\\' == *ptr) {
340       switch(ptr[1]) {
341       case 'r':
342         fputc('\r', stream);
343         break;
344       case 'n':
345         fputc('\n', stream);
346         break;
347       case 't':
348         fputc('\t', stream);
349         break;
350       default:
351         /* unknown, just output this */
352         fputc(*ptr, stream);
353         fputc(ptr[1], stream);
354         break;
355       }
356       ptr += 2;
357     }
358     else {
359       fputc(*ptr, stream);
360       ptr++;
361     }
362   }
363 
364 }
365