Skip to content
Permalink
Browse files

writeout: support to generate JSON output

This commit adds support to generate JSON via the writeout feature:

    -w "%{json}"

It leverages the existing infrastructure as much as possible. Thus,
generating the JSON on STDERR is possible by:

    -w "%{stderr}%{json}"

This implements a variant of
https://github.com/curl/curl/wiki/JSON#--write-out-json.

Closes #4870
  • Loading branch information
mgumz authored and bagder committed Feb 1, 2020
1 parent d834028 commit 04c03416e68fd635a15cae8201872f5c29fdcca8
@@ -50,6 +50,9 @@ curl CONNECT request. (Added in 7.12.4)
.B http_version
The http version that was effectively used. (Added in 7.50.0)
.TP
.B json
A JSON object with all available keys.
.TP
.B local_ip
The IP address of the local end of the most recently done connection - can be
either IPv4 or IPv6 (Added in 7.29.0)
@@ -127,6 +127,16 @@ char *curl_version(void)
int i = 0;
int j;

#ifdef DEBUGBUILD
/* Override version string when environment variable CURL_VERSION is set */
const char *debugversion = getenv("CURL_VERSION");
if(debugversion) {
strncpy(out, debugversion, sizeof(out)-1);
out[sizeof(out)-1] = '\0';
return out;
}
#endif

src[i++] = LIBCURL_NAME "/" LIBCURL_VERSION;
#ifdef USE_SSL
Curl_ssl_version(ssl_version, sizeof(ssl_version));
@@ -45,6 +45,7 @@ SOURCE \
tool_vms.c \
tool_writeenv.c \
tool_writeout.c \
tool_writeout_json.c \
tool_xattr.c

SOURCEPATH ../../../lib
@@ -62,6 +62,7 @@ CURL_CFILES = \
tool_util.c \
tool_vms.c \
tool_writeout.c \
tool_writeout_json.c \
tool_xattr.c

CURL_HFILES = \
@@ -107,6 +108,7 @@ CURL_HFILES = \
tool_version.h \
tool_vms.h \
tool_writeout.h \
tool_writeout_json.h \
tool_xattr.h

CURL_RCFILES = curl.rc
@@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
* 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
@@ -25,87 +25,81 @@
#include "curlx.h"
#include "tool_cfgable.h"
#include "tool_writeout.h"
#include "tool_writeout_json.h"

#include "memdebug.h" /* keep this as LAST include */

typedef enum {
VAR_NONE, /* must be the first */
VAR_TOTAL_TIME,
VAR_NAMELOOKUP_TIME,
VAR_CONNECT_TIME,
VAR_APPCONNECT_TIME,
VAR_PRETRANSFER_TIME,
VAR_STARTTRANSFER_TIME,
VAR_SIZE_DOWNLOAD,
VAR_SIZE_UPLOAD,
VAR_SPEED_DOWNLOAD,
VAR_SPEED_UPLOAD,
VAR_HTTP_CODE,
VAR_HTTP_CODE_PROXY,
VAR_HEADER_SIZE,
VAR_REQUEST_SIZE,
VAR_EFFECTIVE_URL,
VAR_CONTENT_TYPE,
VAR_NUM_CONNECTS,
VAR_REDIRECT_TIME,
VAR_REDIRECT_COUNT,
VAR_FTP_ENTRY_PATH,
VAR_REDIRECT_URL,
VAR_SSL_VERIFY_RESULT,
VAR_PROXY_SSL_VERIFY_RESULT,
VAR_EFFECTIVE_FILENAME,
VAR_PRIMARY_IP,
VAR_PRIMARY_PORT,
VAR_LOCAL_IP,
VAR_LOCAL_PORT,
VAR_HTTP_VERSION,
VAR_SCHEME,
VAR_STDOUT,
VAR_STDERR,
VAR_NUM_OF_VARS /* must be the last */
} replaceid;

struct variable {
const char *name;
replaceid id;
};


static const struct variable replacements[]={
{"url_effective", VAR_EFFECTIVE_URL},
{"http_code", VAR_HTTP_CODE},
{"response_code", VAR_HTTP_CODE},
{"http_connect", VAR_HTTP_CODE_PROXY},
{"time_total", VAR_TOTAL_TIME},
{"time_namelookup", VAR_NAMELOOKUP_TIME},
{"time_connect", VAR_CONNECT_TIME},
{"time_appconnect", VAR_APPCONNECT_TIME},
{"time_pretransfer", VAR_PRETRANSFER_TIME},
{"time_starttransfer", VAR_STARTTRANSFER_TIME},
{"size_header", VAR_HEADER_SIZE},
{"size_request", VAR_REQUEST_SIZE},
{"size_download", VAR_SIZE_DOWNLOAD},
{"size_upload", VAR_SIZE_UPLOAD},
{"speed_download", VAR_SPEED_DOWNLOAD},
{"speed_upload", VAR_SPEED_UPLOAD},
{"content_type", VAR_CONTENT_TYPE},
{"num_connects", VAR_NUM_CONNECTS},
{"time_redirect", VAR_REDIRECT_TIME},
{"num_redirects", VAR_REDIRECT_COUNT},
{"ftp_entry_path", VAR_FTP_ENTRY_PATH},
{"redirect_url", VAR_REDIRECT_URL},
{"ssl_verify_result", VAR_SSL_VERIFY_RESULT},
{"proxy_ssl_verify_result", VAR_PROXY_SSL_VERIFY_RESULT},
{"filename_effective", VAR_EFFECTIVE_FILENAME},
{"remote_ip", VAR_PRIMARY_IP},
{"remote_port", VAR_PRIMARY_PORT},
{"local_ip", VAR_LOCAL_IP},
{"local_port", VAR_LOCAL_PORT},
{"http_version", VAR_HTTP_VERSION},
{"scheme", VAR_SCHEME},
{"stdout", VAR_STDOUT},
{"stderr", VAR_STDERR},
{NULL, VAR_NONE}
static const struct writeoutvar variables[] = {
{"url_effective", VAR_EFFECTIVE_URL, 0,
CURLINFO_EFFECTIVE_URL, JSON_STRING},
{"http_code", VAR_HTTP_CODE, 0,
CURLINFO_RESPONSE_CODE, JSON_LONG},
{"response_code", VAR_HTTP_CODE, 0,
CURLINFO_RESPONSE_CODE, JSON_LONG},
{"http_connect", VAR_HTTP_CODE_PROXY, 0,
CURLINFO_HTTP_CONNECTCODE, JSON_LONG},
{"time_total", VAR_TOTAL_TIME, 0,
CURLINFO_TOTAL_TIME_T, JSON_TIME},
{"time_namelookup", VAR_NAMELOOKUP_TIME, 0,
CURLINFO_NAMELOOKUP_TIME_T, JSON_TIME},
{"time_connect", VAR_CONNECT_TIME, 0,
CURLINFO_CONNECT_TIME_T, JSON_TIME},
{"time_appconnect", VAR_APPCONNECT_TIME, 0,
CURLINFO_APPCONNECT_TIME_T, JSON_TIME},
{"time_pretransfer", VAR_PRETRANSFER_TIME, 0,
CURLINFO_PRETRANSFER_TIME_T, JSON_TIME},
{"time_starttransfer", VAR_STARTTRANSFER_TIME, 0,
CURLINFO_STARTTRANSFER_TIME_T, JSON_TIME},
{"size_header", VAR_HEADER_SIZE, 0,
CURLINFO_HEADER_SIZE, JSON_LONG},
{"size_request", VAR_REQUEST_SIZE, 0,
CURLINFO_REQUEST_SIZE, JSON_LONG},
{"size_download", VAR_SIZE_DOWNLOAD, 0,
CURLINFO_SIZE_DOWNLOAD_T, JSON_LONG},
{"size_upload", VAR_SIZE_UPLOAD, 0,
CURLINFO_SIZE_UPLOAD_T, JSON_LONG},
{"speed_download", VAR_SPEED_DOWNLOAD, 0,
CURLINFO_SPEED_DOWNLOAD_T, JSON_TIME},
{"speed_upload", VAR_SPEED_UPLOAD, 0,
CURLINFO_SPEED_UPLOAD_T, JSON_TIME},
{"content_type", VAR_CONTENT_TYPE, 0,
CURLINFO_CONTENT_TYPE, JSON_STRING},
{"num_connects", VAR_NUM_CONNECTS, 0,
CURLINFO_NUM_CONNECTS, JSON_LONG},
{"time_redirect", VAR_REDIRECT_TIME, 0,
CURLINFO_REDIRECT_TIME_T, JSON_TIME},
{"num_redirects", VAR_REDIRECT_COUNT, 0,
CURLINFO_REDIRECT_COUNT, JSON_LONG},
{"ftp_entry_path", VAR_FTP_ENTRY_PATH, 0,
CURLINFO_FTP_ENTRY_PATH, JSON_STRING},
{"redirect_url", VAR_REDIRECT_URL, 0,
CURLINFO_REDIRECT_URL, JSON_STRING},
{"ssl_verify_result", VAR_SSL_VERIFY_RESULT, 0,
CURLINFO_SSL_VERIFYRESULT, JSON_LONG},
{"proxy_ssl_verify_result", VAR_PROXY_SSL_VERIFY_RESULT, 0,
CURLINFO_PROXY_SSL_VERIFYRESULT, JSON_LONG},
{"filename_effective", VAR_EFFECTIVE_FILENAME, 0,
0, JSON_FILENAME},
{"remote_ip", VAR_PRIMARY_IP, 0,
CURLINFO_PRIMARY_IP, JSON_STRING},
{"remote_port", VAR_PRIMARY_PORT, 0,
CURLINFO_PRIMARY_PORT, JSON_LONG},
{"local_ip", VAR_LOCAL_IP, 0,
CURLINFO_LOCAL_IP, JSON_STRING},
{"local_port", VAR_LOCAL_PORT, 0,
CURLINFO_LOCAL_PORT, JSON_LONG},
{"http_version", VAR_HTTP_VERSION, 0,
CURLINFO_HTTP_VERSION, JSON_VERSION},
{"scheme", VAR_SCHEME, 0,
CURLINFO_SCHEME, JSON_STRING},
{"stdout", VAR_STDOUT, 1,
0, JSON_NONE},
{"stderr", VAR_STDERR, 1,
0, JSON_NONE},
{"json", VAR_JSON, 1,
0, JSON_NONE},
{NULL, VAR_NONE, 1,
0, JSON_NONE}
};

void ourWriteOut(CURL *curl, struct OutStruct *outs, const char *writeinfo)
@@ -138,10 +132,10 @@ void ourWriteOut(CURL *curl, struct OutStruct *outs, const char *writeinfo)
}
keepit = *end;
*end = 0; /* zero terminate */
for(i = 0; replacements[i].name; i++) {
if(curl_strequal(ptr, replacements[i].name)) {
for(i = 0; variables[i].name; i++) {
if(curl_strequal(ptr, variables[i].name)) {
match = TRUE;
switch(replacements[i].id) {
switch(variables[i].id) {
case VAR_EFFECTIVE_URL:
if((CURLE_OK ==
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &stringp))
@@ -334,6 +328,8 @@ void ourWriteOut(CURL *curl, struct OutStruct *outs, const char *writeinfo)
case VAR_STDERR:
stream = stderr;
break;
case VAR_JSON:
ourWriteOutJSON(variables, curl, outs, stream);
default:
break;
}
@@ -7,7 +7,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
* 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
@@ -23,6 +23,61 @@
***************************************************************************/
#include "tool_setup.h"

typedef enum {
VAR_NONE, /* must be the first */
VAR_TOTAL_TIME,
VAR_NAMELOOKUP_TIME,
VAR_CONNECT_TIME,
VAR_APPCONNECT_TIME,
VAR_PRETRANSFER_TIME,
VAR_STARTTRANSFER_TIME,
VAR_SIZE_DOWNLOAD,
VAR_SIZE_UPLOAD,
VAR_SPEED_DOWNLOAD,
VAR_SPEED_UPLOAD,
VAR_HTTP_CODE,
VAR_HTTP_CODE_PROXY,
VAR_HEADER_SIZE,
VAR_REQUEST_SIZE,
VAR_EFFECTIVE_URL,
VAR_CONTENT_TYPE,
VAR_NUM_CONNECTS,
VAR_REDIRECT_TIME,
VAR_REDIRECT_COUNT,
VAR_FTP_ENTRY_PATH,
VAR_REDIRECT_URL,
VAR_SSL_VERIFY_RESULT,
VAR_PROXY_SSL_VERIFY_RESULT,
VAR_EFFECTIVE_FILENAME,
VAR_PRIMARY_IP,
VAR_PRIMARY_PORT,
VAR_LOCAL_IP,
VAR_LOCAL_PORT,
VAR_HTTP_VERSION,
VAR_SCHEME,
VAR_STDOUT,
VAR_STDERR,
VAR_JSON,
VAR_NUM_OF_VARS /* must be the last */
} writeoutid;

typedef enum {
JSON_NONE,
JSON_STRING,
JSON_LONG,
JSON_TIME,
JSON_VERSION,
JSON_FILENAME
} jsontype;

struct writeoutvar {
const char *name;
writeoutid id;
int is_ctrl;
CURLINFO cinfo;
jsontype jsontype;
};

void ourWriteOut(CURL *curl, struct OutStruct *outs, const char *writeinfo);

#endif /* HEADER_CURL_TOOL_WRITEOUT_H */

0 comments on commit 04c0341

Please sign in to comment.
You can’t perform that action at this time.