diff options
author | AlexSm <alex@ydb.tech> | 2024-01-18 11:28:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-18 11:28:56 +0100 |
commit | 9d0a3761b3201e0d9db879a7adf91876ebdb0564 (patch) | |
tree | 541d11ac878c18efd7ebca81e35112aa0fef995b /contrib/libs/curl/src/tool_writeout_json.c | |
parent | 404ef8886ecc9736bc58ade6da2fbd83b486a408 (diff) | |
download | ydb-9d0a3761b3201e0d9db879a7adf91876ebdb0564.tar.gz |
Library import 8 (#1074)
* Library import 8
* Add contrib/libs/cxxsupp/libcxx/include/__verbose_abort
Diffstat (limited to 'contrib/libs/curl/src/tool_writeout_json.c')
-rw-r--r-- | contrib/libs/curl/src/tool_writeout_json.c | 82 |
1 files changed, 55 insertions, 27 deletions
diff --git a/contrib/libs/curl/src/tool_writeout_json.c b/contrib/libs/curl/src/tool_writeout_json.c index a36d60c4f1..4ed6b93fb7 100644 --- a/contrib/libs/curl/src/tool_writeout_json.c +++ b/contrib/libs/curl/src/tool_writeout_json.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 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 @@ -31,50 +31,73 @@ #include "tool_writeout_json.h" #include "tool_writeout.h" -void jsonWriteString(FILE *stream, const char *in, bool lowercase) +#define MAX_JSON_STRING 100000 + +/* provide the given string in dynbuf as a quoted json string, but without the + outer quotes. The buffer is not inited by this function. + + Return 0 on success, non-zero on error. +*/ +int jsonquoted(const char *in, size_t len, + struct curlx_dynbuf *out, bool lowercase) { - const char *i = in; - const char *in_end = in + strlen(in); + const unsigned char *i = (unsigned char *)in; + const unsigned char *in_end = &i[len]; + CURLcode result = CURLE_OK; - fputc('\"', stream); - for(; i < in_end; i++) { + for(; (i < in_end) && !result; i++) { switch(*i) { case '\\': - fputs("\\\\", stream); + result = curlx_dyn_addn(out, "\\\\", 2); break; case '\"': - fputs("\\\"", stream); + result = curlx_dyn_addn(out, "\\\"", 2); break; case '\b': - fputs("\\b", stream); + result = curlx_dyn_addn(out, "\\b", 2); break; case '\f': - fputs("\\f", stream); + result = curlx_dyn_addn(out, "\\f", 2); break; case '\n': - fputs("\\n", stream); + result = curlx_dyn_addn(out, "\\n", 2); break; case '\r': - fputs("\\r", stream); + result = curlx_dyn_addn(out, "\\r", 2); break; case '\t': - fputs("\\t", stream); + result = curlx_dyn_addn(out, "\\t", 2); break; default: - if (*i < 32) { - fprintf(stream, "u%04x", *i); - } + if(*i < 32) + result = curlx_dyn_addf(out, "\\u%04x", *i); else { - char out = *i; - if(lowercase && (out >= 'A' && out <= 'Z')) + char o = *i; + if(lowercase && (o >= 'A' && o <= 'Z')) /* do not use tolower() since that's locale specific */ - out |= ('a' - 'A'); - fputc(out, stream); + o |= ('a' - 'A'); + result = curlx_dyn_addn(out, &o, 1); } break; } } - fputc('\"', stream); + if(result) + return (int)result; + return 0; +} + +void jsonWriteString(FILE *stream, const char *in, bool lowercase) +{ + struct curlx_dynbuf out; + curlx_dyn_init(&out, MAX_JSON_STRING); + + if(!jsonquoted(in, strlen(in), &out, lowercase)) { + fputc('\"', stream); + if(curlx_dyn_len(&out)) + fputs(curlx_dyn_ptr(&out), stream); + fputc('\"', stream); + } + curlx_dyn_free(&out); } void ourWriteOutJSON(FILE *stream, const struct writeoutvar mappings[], @@ -110,11 +133,6 @@ void headerJSON(FILE *stream, struct per_transfer *per) fputc('{', stream); while((header = curl_easy_nextheader(per->curl, CURLH_HEADER, -1, prev))) { - if(prev) - fputs(",\n", stream); - jsonWriteString(stream, header->name, TRUE); - 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 @@ -122,6 +140,11 @@ void headerJSON(FILE *stream, struct per_transfer *per) size_t a = header->amount; size_t i = 0; char *name = header->name; + if(prev) + fputs(",\n", stream); + jsonWriteString(stream, header->name, TRUE); + fputc(':', stream); + prev = header; fputc('[', stream); do { jsonWriteString(stream, header->value, FALSE); @@ -132,13 +155,18 @@ void headerJSON(FILE *stream, struct per_transfer *per) -1, &header)) break; } while(1); + fputc(']', stream); } - fputc(']', stream); } else { + if(prev) + fputs(",\n", stream); + jsonWriteString(stream, header->name, TRUE); + fputc(':', stream); fputc('[', stream); jsonWriteString(stream, header->value, FALSE); fputc(']', stream); + prev = header; } } fputs("\n}", stream); |