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