curl/header_json: output the header names in lowercase

To better allow json[“header”].

Reported-by: Peter Korsgaard
Bug: https://daniel.haxx.se/blog/2022/03/24/easier-header-picking-with-curl/comment-page-1/#comment-25878
Closes #8633
This commit is contained in:
Daniel Stenberg 2022-03-25 11:24:27 +01:00
parent f718a91547
commit e7793cb57b
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
5 changed files with 23 additions and 20 deletions

View File

@ -61,10 +61,9 @@ A JSON object with all HTTP response headers from the recent transfer. Values
are provided as arrays, since in the case of multiple headers there can be
multiple values.
The header names are listed in order of appearance over the wire using the
same case as was used over the network. Except for duplicated headers. They
are grouped on the first occurance of that header, using the casing of the
first header, and then each value is presented in the JSON array.
The header names provided in lowercase, listed in order of appearance over the
wire. Except for duplicated headers. They are grouped on the first occurance
of that header, each value is presented in the JSON array.
.TP
.B http_code
The numerical response code that was found in the last retrieved HTTP(S) or

View File

@ -220,7 +220,7 @@ static int writeString(FILE *stream, const struct writeoutvar *wovar,
DEBUGASSERT(strinfo);
if(use_json) {
fprintf(stream, "\"%s\":", wovar->name);
jsonWriteString(stream, strinfo);
jsonWriteString(stream, strinfo, FALSE);
}
else
fputs(strinfo, stream);

View File

@ -29,7 +29,7 @@
#include "tool_writeout_json.h"
#include "tool_writeout.h"
void jsonWriteString(FILE *stream, const char *in)
void jsonWriteString(FILE *stream, const char *in, bool lowercase)
{
const char *i = in;
const char *in_end = in + strlen(in);
@ -63,7 +63,11 @@ void jsonWriteString(FILE *stream, const char *in)
fprintf(stream, "u%04x", *i);
}
else {
fputc(*i, stream);
char out = *i;
if(lowercase && (out >= 'A' && out <= 'Z'))
/* do not use tolower() since that's locale specific */
out |= ('a' - 'A');
fputc(out, stream);
}
break;
}
@ -87,7 +91,7 @@ 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\":");
jsonWriteString(stream, curl_version());
jsonWriteString(stream, curl_version(), FALSE);
fprintf(stream, "}");
}
@ -106,7 +110,7 @@ void headerJSON(FILE *stream, struct per_transfer *per)
prev))) {
if(prev)
fputs(",\n", stream);
jsonWriteString(stream, header->name);
jsonWriteString(stream, header->name, TRUE);
fputc(':', stream);
prev = header;
if(header->amount > 1) {
@ -118,7 +122,7 @@ void headerJSON(FILE *stream, struct per_transfer *per)
char *name = header->name;
fputc('[', stream);
do {
jsonWriteString(stream, header->value);
jsonWriteString(stream, header->value, FALSE);
if(++i >= a)
break;
fputc(',', stream);
@ -131,7 +135,7 @@ void headerJSON(FILE *stream, struct per_transfer *per)
}
else {
fputc('[', stream);
jsonWriteString(stream, header->value);
jsonWriteString(stream, header->value, FALSE);
fputc(']', stream);
}
}

View File

@ -27,6 +27,6 @@
void ourWriteOutJSON(FILE *stream, const struct writeoutvar mappings[],
struct per_transfer *per, CURLcode per_result);
void headerJSON(FILE *stream, struct per_transfer *per);
void jsonWriteString(FILE *stream, const char *in);
void jsonWriteString(FILE *stream, const char *in, bool lowercase);
#endif /* HEADER_CURL_TOOL_WRITEOUT_H */

View File

@ -58,14 +58,14 @@ Accept: */*
</protocol>
<stdout mode="text">
{"Date":["Tue, 09 Nov 2010 14:49:00 GMT"],
"Server":["test-server/fake"],
"Last-Modified":["Tue, 13 Jun 2000 12:10:00 GMT"],
"ETag":["\"21025-dc7-39462498\""],
"Accept-Ranges":["bytes"],
"Set-Cookie":["firstcookie=want1; path=/","2cookie=want2; path=/","cookie3=want3; path=/"],
"Content-Length":["6"],
"Connection":["close"]
{"date":["Tue, 09 Nov 2010 14:49:00 GMT"],
"server":["test-server/fake"],
"last-modified":["Tue, 13 Jun 2000 12:10:00 GMT"],
"etag":["\"21025-dc7-39462498\""],
"accept-ranges":["bytes"],
"set-cookie":["firstcookie=want1; path=/","2cookie=want2; path=/","cookie3=want3; path=/"],
"content-length":["6"],
"connection":["close"]
}
</stdout>
</verify>