tool_writeout_json.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. void jsonWriteString(FILE *stream, const char *in, bool lowercase)
  32. {
  33. const char *i = in;
  34. const char *in_end = in + strlen(in);
  35. fputc('\"', stream);
  36. for(; i < in_end; i++) {
  37. switch(*i) {
  38. case '\\':
  39. fputs("\\\\", stream);
  40. break;
  41. case '\"':
  42. fputs("\\\"", stream);
  43. break;
  44. case '\b':
  45. fputs("\\b", stream);
  46. break;
  47. case '\f':
  48. fputs("\\f", stream);
  49. break;
  50. case '\n':
  51. fputs("\\n", stream);
  52. break;
  53. case '\r':
  54. fputs("\\r", stream);
  55. break;
  56. case '\t':
  57. fputs("\\t", stream);
  58. break;
  59. default:
  60. if (*i < 32) {
  61. fprintf(stream, "u%04x", *i);
  62. }
  63. else {
  64. char out = *i;
  65. if(lowercase && (out >= 'A' && out <= 'Z'))
  66. /* do not use tolower() since that's locale specific */
  67. out |= ('a' - 'A');
  68. fputc(out, stream);
  69. }
  70. break;
  71. }
  72. }
  73. fputc('\"', stream);
  74. }
  75. void ourWriteOutJSON(FILE *stream, const struct writeoutvar mappings[],
  76. struct per_transfer *per, CURLcode per_result)
  77. {
  78. int i;
  79. fputs("{", stream);
  80. for(i = 0; mappings[i].name != NULL; i++) {
  81. if(mappings[i].writefunc &&
  82. mappings[i].writefunc(stream, &mappings[i], per, per_result, true))
  83. fputs(",", stream);
  84. }
  85. /* The variables are sorted in alphabetical order but as a special case
  86. curl_version (which is not actually a --write-out variable) is last. */
  87. fprintf(stream, "\"curl_version\":");
  88. jsonWriteString(stream, curl_version(), FALSE);
  89. fprintf(stream, "}");
  90. }
  91. #ifdef _MSC_VER
  92. /* warning C4706: assignment within conditional expression */
  93. #pragma warning(disable:4706)
  94. #endif
  95. void headerJSON(FILE *stream, struct per_transfer *per)
  96. {
  97. struct curl_header *header;
  98. struct curl_header *prev = NULL;
  99. fputc('{', stream);
  100. while((header = curl_easy_nextheader(per->curl, CURLH_HEADER, -1,
  101. prev))) {
  102. if(prev)
  103. fputs(",\n", stream);
  104. jsonWriteString(stream, header->name, TRUE);
  105. fputc(':', stream);
  106. prev = header;
  107. if(header->amount > 1) {
  108. if(!header->index) {
  109. /* act on the 0-index entry and pull the others in, then output in a
  110. JSON list */
  111. size_t a = header->amount;
  112. size_t i = 0;
  113. char *name = header->name;
  114. fputc('[', stream);
  115. do {
  116. jsonWriteString(stream, header->value, FALSE);
  117. if(++i >= a)
  118. break;
  119. fputc(',', stream);
  120. if(curl_easy_header(per->curl, name, i, CURLH_HEADER,
  121. -1, &header))
  122. break;
  123. } while(1);
  124. }
  125. fputc(']', stream);
  126. }
  127. else {
  128. fputc('[', stream);
  129. jsonWriteString(stream, header->value, FALSE);
  130. fputc(']', stream);
  131. }
  132. }
  133. fputs("\n}", stream);
  134. }