tool_writeout_json.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "tool_setup.h"
  25. #define ENABLE_CURLX_PRINTF
  26. /* use our own printf() functions */
  27. #include "curlx.h"
  28. #include "tool_cfgable.h"
  29. #include "tool_writeout_json.h"
  30. #include "tool_writeout.h"
  31. #define MAX_JSON_STRING 100000
  32. /* provide the given string in dynbuf as a quoted json string, but without the
  33. outer quotes. The buffer is not inited by this function.
  34. Return 0 on success, non-zero on error.
  35. */
  36. int jsonquoted(const char *in, size_t len,
  37. struct curlx_dynbuf *out, bool lowercase)
  38. {
  39. const unsigned char *i = (unsigned char *)in;
  40. const unsigned char *in_end = &i[len];
  41. CURLcode result = CURLE_OK;
  42. for(; (i < in_end) && !result; i++) {
  43. switch(*i) {
  44. case '\\':
  45. result = curlx_dyn_addn(out, "\\\\", 2);
  46. break;
  47. case '\"':
  48. result = curlx_dyn_addn(out, "\\\"", 2);
  49. break;
  50. case '\b':
  51. result = curlx_dyn_addn(out, "\\b", 2);
  52. break;
  53. case '\f':
  54. result = curlx_dyn_addn(out, "\\f", 2);
  55. break;
  56. case '\n':
  57. result = curlx_dyn_addn(out, "\\n", 2);
  58. break;
  59. case '\r':
  60. result = curlx_dyn_addn(out, "\\r", 2);
  61. break;
  62. case '\t':
  63. result = curlx_dyn_addn(out, "\\t", 2);
  64. break;
  65. default:
  66. if(*i < 32)
  67. result = curlx_dyn_addf(out, "\\u%04x", *i);
  68. else {
  69. char o = *i;
  70. if(lowercase && (o >= 'A' && o <= 'Z'))
  71. /* do not use tolower() since that's locale specific */
  72. o |= ('a' - 'A');
  73. result = curlx_dyn_addn(out, &o, 1);
  74. }
  75. break;
  76. }
  77. }
  78. if(result)
  79. return (int)result;
  80. return 0;
  81. }
  82. void jsonWriteString(FILE *stream, const char *in, bool lowercase)
  83. {
  84. struct curlx_dynbuf out;
  85. curlx_dyn_init(&out, MAX_JSON_STRING);
  86. if(!jsonquoted(in, strlen(in), &out, lowercase)) {
  87. fputc('\"', stream);
  88. if(curlx_dyn_len(&out))
  89. fputs(curlx_dyn_ptr(&out), stream);
  90. fputc('\"', stream);
  91. }
  92. curlx_dyn_free(&out);
  93. }
  94. void ourWriteOutJSON(FILE *stream, const struct writeoutvar mappings[],
  95. struct per_transfer *per, CURLcode per_result)
  96. {
  97. int i;
  98. fputs("{", stream);
  99. for(i = 0; mappings[i].name != NULL; i++) {
  100. if(mappings[i].writefunc &&
  101. mappings[i].writefunc(stream, &mappings[i], per, per_result, true))
  102. fputs(",", stream);
  103. }
  104. /* The variables are sorted in alphabetical order but as a special case
  105. curl_version (which is not actually a --write-out variable) is last. */
  106. fprintf(stream, "\"curl_version\":");
  107. jsonWriteString(stream, curl_version(), FALSE);
  108. fprintf(stream, "}");
  109. }
  110. #ifdef _MSC_VER
  111. /* warning C4706: assignment within conditional expression */
  112. #pragma warning(disable:4706)
  113. #endif
  114. void headerJSON(FILE *stream, struct per_transfer *per)
  115. {
  116. struct curl_header *header;
  117. struct curl_header *prev = NULL;
  118. fputc('{', stream);
  119. while((header = curl_easy_nextheader(per->curl, CURLH_HEADER, -1,
  120. prev))) {
  121. if(header->amount > 1) {
  122. if(!header->index) {
  123. /* act on the 0-index entry and pull the others in, then output in a
  124. JSON list */
  125. size_t a = header->amount;
  126. size_t i = 0;
  127. char *name = header->name;
  128. if(prev)
  129. fputs(",\n", stream);
  130. jsonWriteString(stream, header->name, TRUE);
  131. fputc(':', stream);
  132. prev = header;
  133. fputc('[', stream);
  134. do {
  135. jsonWriteString(stream, header->value, FALSE);
  136. if(++i >= a)
  137. break;
  138. fputc(',', stream);
  139. if(curl_easy_header(per->curl, name, i, CURLH_HEADER,
  140. -1, &header))
  141. break;
  142. } while(1);
  143. fputc(']', stream);
  144. }
  145. }
  146. else {
  147. if(prev)
  148. fputs(",\n", stream);
  149. jsonWriteString(stream, header->name, TRUE);
  150. fputc(':', stream);
  151. fputc('[', stream);
  152. jsonWriteString(stream, header->value, FALSE);
  153. fputc(']', stream);
  154. prev = header;
  155. }
  156. }
  157. fputs("\n}", stream);
  158. }