summaryrefslogtreecommitdiff
path: root/src/tool_writeout_json.c
blob: dfe51b9ffab3a5c857c7940d6c83ccac467867f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at https://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/
#include "tool_setup.h"

#define ENABLE_CURLX_PRINTF

/* use our own printf() functions */
#include "curlx.h"
#include "tool_cfgable.h"
#include "tool_writeout_json.h"
#include "tool_writeout.h"


static const char *http_version[] = {
  "0",   /* CURL_HTTP_VERSION_NONE */
  "1",   /* CURL_HTTP_VERSION_1_0 */
  "1.1", /* CURL_HTTP_VERSION_1_1 */
  "2",   /* CURL_HTTP_VERSION_2 */
  "3"    /* CURL_HTTP_VERSION_3 */
};

static void jsonEscape(FILE *stream, const char *in)
{
  const char *i = in;
  const char *in_end = in + strlen(in);

  for(; i < in_end; i++) {
    switch(*i) {
    case '\\':
      fputs("\\\\", stream);
      break;
    case '\"':
      fputs("\\\"", stream);
      break;
    case '\b':
      fputs("\\b", stream);
      break;
    case '\f':
      fputs("\\f", stream);
      break;
    case '\n':
      fputs("\\n", stream);
      break;
    case '\r':
      fputs("\\r", stream);
      break;
    case '\t':
      fputs("\\t", stream);
      break;
    default:
      if (*i < 32) {
        fprintf(stream, "u%04x", *i);
      }
      else {
        fputc(*i, stream);
      }
      break;
    };
  }
}

static int writeTime(FILE *str, CURL *curl, const char *key, CURLINFO ci)
{
  curl_off_t val = 0;
  if(CURLE_OK == curl_easy_getinfo(curl, ci, &val)) {
    curl_off_t s = val / 1000000l;
    curl_off_t ms = val % 1000000l;
    fprintf(str, "\"%s\":%" CURL_FORMAT_CURL_OFF_T
            ".%06" CURL_FORMAT_CURL_OFF_T, key, s, ms);
    return 1;
  }
  return 0;
}

static int writeString(FILE *str, CURL *curl, const char *key, CURLINFO ci)
{
  char *valp = NULL;
  if((CURLE_OK == curl_easy_getinfo(curl, ci, &valp)) && valp) {
    fprintf(str, "\"%s\":\"", key);
    jsonEscape(str, valp);
    fprintf(str, "\"");
    return 1;
  }
  return 0;
}

static int writeLong(FILE *str, CURL *curl, const char *key, CURLINFO ci)
{
  long val = 0;
  if(CURLE_OK == curl_easy_getinfo(curl, ci, &val)) {
    fprintf(str, "\"%s\":%ld", key, val);
    return 1;
  }
  return 0;
}

static int writeOffset(FILE *str, CURL *curl, const char *key, CURLINFO ci)
{
  curl_off_t val = 0;
  if(CURLE_OK == curl_easy_getinfo(curl, ci, &val)) {
    fprintf(str, "\"%s\":%" CURL_FORMAT_CURL_OFF_T, key, val);
    return 1;
  }
  return 0;
}

static int writeFilename(FILE *str, const char *key, const char *filename)
{
  if(filename) {
    fprintf(str, "\"%s\":\"", key);
    jsonEscape(str, filename);
    fprintf(str, "\"");
  }
  else {
    fprintf(str, "\"%s\":null", key);
  }
  return 1;
}

static int writeVersion(FILE *str, CURL *curl, const char *key, CURLINFO ci)
{
  long version = 0;
  if(CURLE_OK == curl_easy_getinfo(curl, ci, &version) &&
     (version >= 0) &&
     (version < (long)(sizeof(http_version)/sizeof(char *)))) {
    fprintf(str, "\"%s\":\"%s\"", key, http_version[version]);
    return 1;
  }
  return 0;
}

void ourWriteOutJSON(const struct writeoutvar mappings[], CURL *curl,
        struct OutStruct *outs, FILE *stream)
{
  int i;

  fputs("{", stream);
  for(i = 0; mappings[i].name != NULL; i++) {
    const char *name = mappings[i].name;
    CURLINFO cinfo = mappings[i].cinfo;
    int ok = 0;

    if(mappings[i].is_ctrl == 1) {
      continue;
    }

    switch(mappings[i].jsontype) {
    case JSON_STRING:
      ok = writeString(stream, curl, name, cinfo);
      break;
    case JSON_LONG:
      ok = writeLong(stream, curl, name, cinfo);
      break;
    case JSON_OFFSET:
      ok = writeOffset(stream, curl, name, cinfo);
      break;
    case JSON_TIME:
      ok = writeTime(stream, curl, name, cinfo);
      break;
    case JSON_FILENAME:
      ok = writeFilename(stream, name, outs->filename);
      break;
    case JSON_VERSION:
      ok = writeVersion(stream, curl, name, cinfo);
      break;
    default:
      break;
    }

    if(ok) {
      fputs(",", stream);
    }
  }

  fprintf(stream, "\"curl_version\":\"%s\"}", curl_version());
}