mirror of
https://github.com/curl/curl.git
synced 2026-07-27 07:37:15 +03:00
curl: add %{header_json} support in -w handling
Outputs all response headers as a JSON object.
This commit is contained in:
parent
2d45137e1e
commit
4133a69f2d
6 changed files with 66 additions and 10 deletions
|
|
@ -5,7 +5,7 @@
|
|||
* | (__| |_| | _ <| |___
|
||||
* \___|\___/|_| \_\_____|
|
||||
*
|
||||
* Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
|
||||
* Copyright (C) 1998 - 2022, 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
|
||||
|
|
@ -29,12 +29,12 @@
|
|||
#include "tool_writeout_json.h"
|
||||
#include "tool_writeout.h"
|
||||
|
||||
|
||||
void jsonWriteString(FILE *stream, const char *in)
|
||||
{
|
||||
const char *i = in;
|
||||
const char *in_end = in + strlen(in);
|
||||
|
||||
fputc('\"', stream);
|
||||
for(; i < in_end; i++) {
|
||||
switch(*i) {
|
||||
case '\\':
|
||||
|
|
@ -68,6 +68,7 @@ void jsonWriteString(FILE *stream, const char *in)
|
|||
break;
|
||||
}
|
||||
}
|
||||
fputc('\"', stream);
|
||||
}
|
||||
|
||||
void ourWriteOutJSON(FILE *stream, const struct writeoutvar mappings[],
|
||||
|
|
@ -85,7 +86,54 @@ void ourWriteOutJSON(FILE *stream, const struct writeoutvar mappings[],
|
|||
|
||||
/* 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\":\"");
|
||||
fprintf(stream, "\"curl_version\":");
|
||||
jsonWriteString(stream, curl_version());
|
||||
fprintf(stream, "\"}");
|
||||
fprintf(stream, "}");
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
/* warning C4706: assignment within conditional expression */
|
||||
#pragma warning(disable:4706)
|
||||
#endif
|
||||
|
||||
void headerJSON(FILE *stream, struct per_transfer *per)
|
||||
{
|
||||
struct curl_header *header;
|
||||
struct curl_header *prev = NULL;
|
||||
|
||||
fputc('{', stream);
|
||||
while((header = curl_easy_nextheader(per->curl, CURLH_HEADER, -1,
|
||||
prev))) {
|
||||
if(prev)
|
||||
fputs(",\n", stream);
|
||||
jsonWriteString(stream, header->name);
|
||||
fputc(':', stream);
|
||||
prev = header;
|
||||
if(header->amount > 1) {
|
||||
if(!header->index) {
|
||||
/* act on the 0-index entry and pull the others in, then output in a
|
||||
JSON list */
|
||||
size_t a = header->amount;
|
||||
size_t i = 0;
|
||||
char *name = header->name;
|
||||
fputc('[', stream);
|
||||
do {
|
||||
jsonWriteString(stream, header->value);
|
||||
if(++i >= a)
|
||||
break;
|
||||
fputc(',', stream);
|
||||
if(curl_easy_header(per->curl, name, i, CURLH_HEADER,
|
||||
-1, &header))
|
||||
break;
|
||||
} while(1);
|
||||
}
|
||||
fputc(']', stream);
|
||||
}
|
||||
else {
|
||||
fputc('[', stream);
|
||||
jsonWriteString(stream, header->value);
|
||||
fputc(']', stream);
|
||||
}
|
||||
}
|
||||
fputs("\n}", stream);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue