format.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "format.h"
  2. #include <util/string/ascii.h>
  3. #include <util/string/split.h>
  4. #include <util/string/strip.h>
  5. #include <util/stream/output.h>
  6. #include <util/string/cast.h>
  7. namespace NMonitoring {
  8. static ECompression CompressionFromHeader(TStringBuf value) {
  9. if (value.empty()) {
  10. return ECompression::UNKNOWN;
  11. }
  12. for (const auto& it : StringSplitter(value).Split(',').SkipEmpty()) {
  13. TStringBuf token = StripString(it.Token());
  14. if (AsciiEqualsIgnoreCase(token, NFormatContentEncoding::IDENTITY)) {
  15. return ECompression::IDENTITY;
  16. } else if (AsciiEqualsIgnoreCase(token, NFormatContentEncoding::ZLIB)) {
  17. return ECompression::ZLIB;
  18. } else if (AsciiEqualsIgnoreCase(token, NFormatContentEncoding::LZ4)) {
  19. return ECompression::LZ4;
  20. } else if (AsciiEqualsIgnoreCase(token, NFormatContentEncoding::ZSTD)) {
  21. return ECompression::ZSTD;
  22. }
  23. }
  24. return ECompression::UNKNOWN;
  25. }
  26. static EFormat FormatFromHttpMedia(TStringBuf value) {
  27. if (AsciiEqualsIgnoreCase(value, NFormatContenType::SPACK)) {
  28. return EFormat::SPACK;
  29. } else if (AsciiEqualsIgnoreCase(value, NFormatContenType::JSON)) {
  30. return EFormat::JSON;
  31. } else if (AsciiEqualsIgnoreCase(value, NFormatContenType::PROTOBUF)) {
  32. return EFormat::PROTOBUF;
  33. } else if (AsciiEqualsIgnoreCase(value, NFormatContenType::TEXT)) {
  34. return EFormat::TEXT;
  35. } else if (AsciiEqualsIgnoreCase(value, NFormatContenType::PROMETHEUS)) {
  36. return EFormat::PROMETHEUS;
  37. }
  38. return EFormat::UNKNOWN;
  39. }
  40. EFormat FormatFromAcceptHeader(TStringBuf value) {
  41. EFormat result{EFormat::UNKNOWN};
  42. for (const auto& it : StringSplitter(value).Split(',').SkipEmpty()) {
  43. TStringBuf token = StripString(it.Token()).Before(';');
  44. result = FormatFromHttpMedia(token);
  45. if (result != EFormat::UNKNOWN) {
  46. break;
  47. }
  48. }
  49. return result;
  50. }
  51. EFormat FormatFromContentType(TStringBuf value) {
  52. value = value.NextTok(';');
  53. return FormatFromHttpMedia(value);
  54. }
  55. TStringBuf ContentTypeByFormat(EFormat format) {
  56. switch (format) {
  57. case EFormat::SPACK:
  58. return NFormatContenType::SPACK;
  59. case EFormat::JSON:
  60. return NFormatContenType::JSON;
  61. case EFormat::PROTOBUF:
  62. return NFormatContenType::PROTOBUF;
  63. case EFormat::TEXT:
  64. return NFormatContenType::TEXT;
  65. case EFormat::PROMETHEUS:
  66. return NFormatContenType::PROMETHEUS;
  67. case EFormat::UNKNOWN:
  68. return TStringBuf();
  69. }
  70. Y_FAIL(); // for GCC
  71. }
  72. ECompression CompressionFromAcceptEncodingHeader(TStringBuf value) {
  73. return CompressionFromHeader(value);
  74. }
  75. ECompression CompressionFromContentEncodingHeader(TStringBuf value) {
  76. return CompressionFromHeader(value);
  77. }
  78. TStringBuf ContentEncodingByCompression(ECompression compression) {
  79. switch (compression) {
  80. case ECompression::IDENTITY:
  81. return NFormatContentEncoding::IDENTITY;
  82. case ECompression::ZLIB:
  83. return NFormatContentEncoding::ZLIB;
  84. case ECompression::LZ4:
  85. return NFormatContentEncoding::LZ4;
  86. case ECompression::ZSTD:
  87. return NFormatContentEncoding::ZSTD;
  88. case ECompression::UNKNOWN:
  89. return TStringBuf();
  90. }
  91. Y_FAIL(); // for GCC
  92. }
  93. }
  94. template <>
  95. NMonitoring::EFormat FromStringImpl<NMonitoring::EFormat>(const char* str, size_t len) {
  96. using NMonitoring::EFormat;
  97. TStringBuf value(str, len);
  98. if (value == TStringBuf("SPACK")) {
  99. return EFormat::SPACK;
  100. } else if (value == TStringBuf("JSON")) {
  101. return EFormat::JSON;
  102. } else if (value == TStringBuf("PROTOBUF")) {
  103. return EFormat::PROTOBUF;
  104. } else if (value == TStringBuf("TEXT")) {
  105. return EFormat::TEXT;
  106. } else if (value == TStringBuf("PROMETHEUS")) {
  107. return EFormat::PROMETHEUS;
  108. } else if (value == TStringBuf("UNKNOWN")) {
  109. return EFormat::UNKNOWN;
  110. }
  111. ythrow yexception() << "unknown format: " << value;
  112. }
  113. template <>
  114. void Out<NMonitoring::EFormat>(IOutputStream& o, NMonitoring::EFormat f) {
  115. using NMonitoring::EFormat;
  116. switch (f) {
  117. case EFormat::SPACK:
  118. o << TStringBuf("SPACK");
  119. return;
  120. case EFormat::JSON:
  121. o << TStringBuf("JSON");
  122. return;
  123. case EFormat::PROTOBUF:
  124. o << TStringBuf("PROTOBUF");
  125. return;
  126. case EFormat::TEXT:
  127. o << TStringBuf("TEXT");
  128. return;
  129. case EFormat::PROMETHEUS:
  130. o << TStringBuf("PROMETHEUS");
  131. return;
  132. case EFormat::UNKNOWN:
  133. o << TStringBuf("UNKNOWN");
  134. return;
  135. }
  136. Y_FAIL(); // for GCC
  137. }
  138. template <>
  139. NMonitoring::ECompression FromStringImpl<NMonitoring::ECompression>(const char* str, size_t len) {
  140. using NMonitoring::ECompression;
  141. TStringBuf value(str, len);
  142. if (value == TStringBuf("IDENTITY")) {
  143. return ECompression::IDENTITY;
  144. } else if (value == TStringBuf("ZLIB")) {
  145. return ECompression::ZLIB;
  146. } else if (value == TStringBuf("LZ4")) {
  147. return ECompression::LZ4;
  148. } else if (value == TStringBuf("ZSTD")) {
  149. return ECompression::ZSTD;
  150. } else if (value == TStringBuf("UNKNOWN")) {
  151. return ECompression::UNKNOWN;
  152. }
  153. ythrow yexception() << "unknown compression: " << value;
  154. }
  155. template <>
  156. void Out<NMonitoring::ECompression>(IOutputStream& o, NMonitoring::ECompression c) {
  157. using NMonitoring::ECompression;
  158. switch (c) {
  159. case ECompression::IDENTITY:
  160. o << TStringBuf("IDENTITY");
  161. return;
  162. case ECompression::ZLIB:
  163. o << TStringBuf("ZLIB");
  164. return;
  165. case ECompression::LZ4:
  166. o << TStringBuf("LZ4");
  167. return;
  168. case ECompression::ZSTD:
  169. o << TStringBuf("ZSTD");
  170. return;
  171. case ECompression::UNKNOWN:
  172. o << TStringBuf("UNKNOWN");
  173. return;
  174. }
  175. Y_FAIL(); // for GCC
  176. }