1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2021, 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.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
24 #define ENABLE_CURLX_PRINTF
25
26 /* use our own printf() functions */
27 #include "curlx.h"
28 #include "tool_cfgable.h"
29 #include "tool_writeout_json.h"
30 #include "tool_writeout.h"
31
32
jsonWriteString(FILE * stream,const char * in)33 void jsonWriteString(FILE *stream, const char *in)
34 {
35 const char *i = in;
36 const char *in_end = in + strlen(in);
37
38 for(; i < in_end; i++) {
39 switch(*i) {
40 case '\\':
41 fputs("\\\\", stream);
42 break;
43 case '\"':
44 fputs("\\\"", stream);
45 break;
46 case '\b':
47 fputs("\\b", stream);
48 break;
49 case '\f':
50 fputs("\\f", stream);
51 break;
52 case '\n':
53 fputs("\\n", stream);
54 break;
55 case '\r':
56 fputs("\\r", stream);
57 break;
58 case '\t':
59 fputs("\\t", stream);
60 break;
61 default:
62 if (*i < 32) {
63 fprintf(stream, "u%04x", *i);
64 }
65 else {
66 fputc(*i, stream);
67 }
68 break;
69 }
70 }
71 }
72
ourWriteOutJSON(FILE * stream,const struct writeoutvar mappings[],struct per_transfer * per,CURLcode per_result)73 void ourWriteOutJSON(FILE *stream, const struct writeoutvar mappings[],
74 struct per_transfer *per, CURLcode per_result)
75 {
76 int i;
77
78 fputs("{", stream);
79
80 for(i = 0; mappings[i].name != NULL; i++) {
81 if(mappings[i].writefunc &&
82 mappings[i].writefunc(stream, &mappings[i], per, per_result, true))
83 fputs(",", stream);
84 }
85
86 /* The variables are sorted in alphabetical order but as a special case
87 curl_version (which is not actually a --write-out variable) is last. */
88 fprintf(stream, "\"curl_version\":\"");
89 jsonWriteString(stream, curl_version());
90 fprintf(stream, "\"}");
91 }
92