summaryrefslogtreecommitdiff
path: root/src/tool_writeout_json.c
diff options
context:
space:
mode:
authorJay Satiro <raysatiro@yahoo.com>2021-01-28 18:56:50 -0500
committerJay Satiro <raysatiro@yahoo.com>2021-02-09 02:48:27 -0500
commit65ca2294619f865362332ceddc622db92d0f50a6 (patch)
treea1f2575bd296da4eee3c1dd4cc3552ab2b625010 /src/tool_writeout_json.c
parentb9f11ae87564447e61533eeafea9a1caa4cfa31d (diff)
downloadcurl-65ca2294619f865362332ceddc622db92d0f50a6.tar.gz
tool_writeout: refactor write-out and write-out json
- Deduplicate the logic used by write-out and write-out json. Rather than have separate writeLong, writeString, etc, logic for each of write-out and write-out json instead have respective shared functions that can output either format and a 'use_json' parameter to indicate whether it is json that is output. This will make it easier to maintain. Rather than have to go through two sets of logic now we only have to go through one. - Support write-out %{errormsg} and %{exitcode} in json. - Clarify in the doc that %{exitcode} is the exit code of the transfer. Prior to this change it just said "The numerical exitcode" which implies it's the exit code of the tool, and it's not necessarily that. Closes https://github.com/curl/curl/pull/6544
Diffstat (limited to 'src/tool_writeout_json.c')
-rw-r--r--src/tool_writeout_json.c134
1 files changed, 11 insertions, 123 deletions
diff --git a/src/tool_writeout_json.c b/src/tool_writeout_json.c
index 186ed6851..ca1e70868 100644
--- a/src/tool_writeout_json.c
+++ b/src/tool_writeout_json.c
@@ -30,15 +30,7 @@
#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)
+void jsonWriteString(FILE *stream, const char *in)
{
const char *i = in;
const char *in_end = in + strlen(in);
@@ -78,126 +70,22 @@ static void jsonEscape(FILE *stream, const char *in)
}
}
-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,
- struct per_transfer *per, const struct writeoutvar *wovar)
-{
- if(wovar->id == VAR_NUM_HEADERS) {
- fprintf(str, "\"%s\":%ld", key, per->num_headers);
- return 1;
- }
- else {
- 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 per_transfer *per, FILE *stream)
+void ourWriteOutJSON(FILE *stream, const struct writeoutvar mappings[],
+ struct per_transfer *per, CURLcode per_result)
{
int i;
fputs("{", stream);
- for(i = 0; mappings[i].name != NULL; i++) {
- const struct writeoutvar *wovar = &mappings[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, per, wovar);
- 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, per->outs.filename);
- break;
- case JSON_VERSION:
- ok = writeVersion(stream, curl, name, cinfo);
- break;
- default:
- break;
- }
-
- if(ok) {
+ for(i = 0; mappings[i].name != NULL; i++) {
+ if(mappings[i].writefunc &&
+ mappings[i].writefunc(stream, &mappings[i], per, per_result, true))
fputs(",", stream);
- }
}
- fprintf(stream, "\"curl_version\":\"%s\"}", curl_version());
+ /* The variables are sorted in alphabetical order but as a special case
+ curl_version (which is not actually a --write-out variable) is last. */
+ fprintf(stream, "\"curl_version\":\"");
+ jsonWriteString(stream, curl_version());
+ fprintf(stream, "\"}");
}