xref: /curl/src/tool_writeout_json.c (revision c0450488)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 #include "tool_setup.h"
25 
26 #include "curlx.h"
27 #include "tool_cfgable.h"
28 #include "tool_writeout_json.h"
29 #include "tool_writeout.h"
30 
31 #define MAX_JSON_STRING 100000
32 
33 /* provide the given string in dynbuf as a quoted json string, but without the
34    outer quotes. The buffer is not inited by this function.
35 
36    Return 0 on success, non-zero on error.
37 */
jsonquoted(const char * in,size_t len,struct curlx_dynbuf * out,bool lowercase)38 int jsonquoted(const char *in, size_t len,
39                struct curlx_dynbuf *out, bool lowercase)
40 {
41   const unsigned char *i = (unsigned char *)in;
42   const unsigned char *in_end = &i[len];
43   CURLcode result = CURLE_OK;
44 
45   for(; (i < in_end) && !result; i++) {
46     switch(*i) {
47     case '\\':
48       result = curlx_dyn_addn(out, "\\\\", 2);
49       break;
50     case '\"':
51       result = curlx_dyn_addn(out, "\\\"", 2);
52       break;
53     case '\b':
54       result = curlx_dyn_addn(out, "\\b", 2);
55       break;
56     case '\f':
57       result = curlx_dyn_addn(out, "\\f", 2);
58       break;
59     case '\n':
60       result = curlx_dyn_addn(out, "\\n", 2);
61       break;
62     case '\r':
63       result = curlx_dyn_addn(out, "\\r", 2);
64       break;
65     case '\t':
66       result = curlx_dyn_addn(out, "\\t", 2);
67       break;
68     default:
69       if(*i < 32)
70         result = curlx_dyn_addf(out, "\\u%04x", *i);
71       else {
72         char o = (char)*i;
73         if(lowercase && (o >= 'A' && o <= 'Z'))
74           /* do not use tolower() since that is locale specific */
75           o |= ('a' - 'A');
76         result = curlx_dyn_addn(out, &o, 1);
77       }
78       break;
79     }
80   }
81   if(result)
82     return (int)result;
83   return 0;
84 }
85 
jsonWriteString(FILE * stream,const char * in,bool lowercase)86 void jsonWriteString(FILE *stream, const char *in, bool lowercase)
87 {
88   struct curlx_dynbuf out;
89   curlx_dyn_init(&out, MAX_JSON_STRING);
90 
91   if(!jsonquoted(in, strlen(in), &out, lowercase)) {
92     fputc('\"', stream);
93     if(curlx_dyn_len(&out))
94       fputs(curlx_dyn_ptr(&out), stream);
95     fputc('\"', stream);
96   }
97   curlx_dyn_free(&out);
98 }
99 
ourWriteOutJSON(FILE * stream,const struct writeoutvar mappings[],size_t nentries,struct per_transfer * per,CURLcode per_result)100 void ourWriteOutJSON(FILE *stream, const struct writeoutvar mappings[],
101                      size_t nentries,
102                      struct per_transfer *per, CURLcode per_result)
103 {
104   size_t i;
105 
106   fputs("{", stream);
107 
108   for(i = 0; i < nentries; i++) {
109     if(mappings[i].writefunc &&
110        mappings[i].writefunc(stream, &mappings[i], per, per_result, true))
111       fputs(",", stream);
112   }
113 
114   /* The variables are sorted in alphabetical order but as a special case
115      curl_version (which is not actually a --write-out variable) is last. */
116   fprintf(stream, "\"curl_version\":");
117   jsonWriteString(stream, curl_version(), FALSE);
118   fprintf(stream, "}");
119 }
120 
121 #ifdef _MSC_VER
122 /* warning C4706: assignment within conditional expression */
123 #pragma warning(disable:4706)
124 #endif
125 
headerJSON(FILE * stream,struct per_transfer * per)126 void headerJSON(FILE *stream, struct per_transfer *per)
127 {
128   struct curl_header *header;
129   struct curl_header *prev = NULL;
130 
131   fputc('{', stream);
132   while((header = curl_easy_nextheader(per->curl, CURLH_HEADER, -1,
133                                        prev))) {
134     if(header->amount > 1) {
135       if(!header->index) {
136         /* act on the 0-index entry and pull the others in, then output in a
137            JSON list */
138         size_t a = header->amount;
139         size_t i = 0;
140         char *name = header->name;
141         if(prev)
142           fputs(",\n", stream);
143         jsonWriteString(stream, header->name, TRUE);
144         fputc(':', stream);
145         prev = header;
146         fputc('[', stream);
147         do {
148           jsonWriteString(stream, header->value, FALSE);
149           if(++i >= a)
150             break;
151           fputc(',', stream);
152           if(curl_easy_header(per->curl, name, i, CURLH_HEADER,
153                               -1, &header))
154             break;
155         } while(1);
156         fputc(']', stream);
157       }
158     }
159     else {
160       if(prev)
161         fputs(",\n", stream);
162       jsonWriteString(stream, header->name, TRUE);
163       fputc(':', stream);
164       fputc('[', stream);
165       jsonWriteString(stream, header->value, FALSE);
166       fputc(']', stream);
167       prev = header;
168     }
169   }
170   fputs("\n}", stream);
171 }
172