format.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. } else if (AsciiEqualsIgnoreCase(value, NFormatContenType::UNISTAT)) {
  38. return EFormat::UNISTAT;
  39. }
  40. return EFormat::UNKNOWN;
  41. }
  42. EFormat FormatFromAcceptHeader(TStringBuf value) {
  43. EFormat result{EFormat::UNKNOWN};
  44. for (const auto& it : StringSplitter(value).Split(',').SkipEmpty()) {
  45. TStringBuf token = StripString(it.Token()).Before(';');
  46. result = FormatFromHttpMedia(token);
  47. if (result != EFormat::UNKNOWN) {
  48. break;
  49. }
  50. }
  51. return result;
  52. }
  53. EFormat FormatFromContentType(TStringBuf value) {
  54. value = value.NextTok(';');
  55. return FormatFromHttpMedia(value);
  56. }
  57. TStringBuf ContentTypeByFormat(EFormat format) {
  58. switch (format) {
  59. case EFormat::SPACK:
  60. return NFormatContenType::SPACK;
  61. case EFormat::JSON:
  62. return NFormatContenType::JSON;
  63. case EFormat::PROTOBUF:
  64. return NFormatContenType::PROTOBUF;
  65. case EFormat::TEXT:
  66. return NFormatContenType::TEXT;
  67. case EFormat::PROMETHEUS:
  68. return NFormatContenType::PROMETHEUS;
  69. case EFormat::UNISTAT:
  70. return NFormatContenType::UNISTAT;
  71. case EFormat::UNKNOWN:
  72. return TStringBuf();
  73. }
  74. Y_ABORT(); // for GCC
  75. }
  76. ECompression CompressionFromAcceptEncodingHeader(TStringBuf value) {
  77. return CompressionFromHeader(value);
  78. }
  79. ECompression CompressionFromContentEncodingHeader(TStringBuf value) {
  80. return CompressionFromHeader(value);
  81. }
  82. TStringBuf ContentEncodingByCompression(ECompression compression) {
  83. switch (compression) {
  84. case ECompression::IDENTITY:
  85. return NFormatContentEncoding::IDENTITY;
  86. case ECompression::ZLIB:
  87. return NFormatContentEncoding::ZLIB;
  88. case ECompression::LZ4:
  89. return NFormatContentEncoding::LZ4;
  90. case ECompression::ZSTD:
  91. return NFormatContentEncoding::ZSTD;
  92. case ECompression::UNKNOWN:
  93. return TStringBuf();
  94. }
  95. Y_ABORT(); // for GCC
  96. }
  97. }
  98. template <>
  99. NMonitoring::EFormat FromStringImpl<NMonitoring::EFormat>(const char* str, size_t len) {
  100. using NMonitoring::EFormat;
  101. TStringBuf value(str, len);
  102. if (value == TStringBuf("SPACK")) {
  103. return EFormat::SPACK;
  104. } else if (value == TStringBuf("JSON")) {
  105. return EFormat::JSON;
  106. } else if (value == TStringBuf("PROTOBUF")) {
  107. return EFormat::PROTOBUF;
  108. } else if (value == TStringBuf("TEXT")) {
  109. return EFormat::TEXT;
  110. } else if (value == TStringBuf("PROMETHEUS")) {
  111. return EFormat::PROMETHEUS;
  112. } else if (value == TStringBuf("UNISTAT")) {
  113. return EFormat::UNISTAT;
  114. } else if (value == TStringBuf("UNKNOWN")) {
  115. return EFormat::UNKNOWN;
  116. }
  117. ythrow yexception() << "unknown format: " << value;
  118. }
  119. template <>
  120. void Out<NMonitoring::EFormat>(IOutputStream& o, NMonitoring::EFormat f) {
  121. using NMonitoring::EFormat;
  122. switch (f) {
  123. case EFormat::SPACK:
  124. o << TStringBuf("SPACK");
  125. return;
  126. case EFormat::JSON:
  127. o << TStringBuf("JSON");
  128. return;
  129. case EFormat::PROTOBUF:
  130. o << TStringBuf("PROTOBUF");
  131. return;
  132. case EFormat::TEXT:
  133. o << TStringBuf("TEXT");
  134. return;
  135. case EFormat::PROMETHEUS:
  136. o << TStringBuf("PROMETHEUS");
  137. return;
  138. case EFormat::UNISTAT:
  139. o << TStringBuf("UNISTAT");
  140. return;
  141. case EFormat::UNKNOWN:
  142. o << TStringBuf("UNKNOWN");
  143. return;
  144. }
  145. Y_ABORT(); // for GCC
  146. }
  147. template <>
  148. NMonitoring::ECompression FromStringImpl<NMonitoring::ECompression>(const char* str, size_t len) {
  149. using NMonitoring::ECompression;
  150. TStringBuf value(str, len);
  151. if (value == TStringBuf("IDENTITY")) {
  152. return ECompression::IDENTITY;
  153. } else if (value == TStringBuf("ZLIB")) {
  154. return ECompression::ZLIB;
  155. } else if (value == TStringBuf("LZ4")) {
  156. return ECompression::LZ4;
  157. } else if (value == TStringBuf("ZSTD")) {
  158. return ECompression::ZSTD;
  159. } else if (value == TStringBuf("UNKNOWN")) {
  160. return ECompression::UNKNOWN;
  161. }
  162. ythrow yexception() << "unknown compression: " << value;
  163. }
  164. template <>
  165. void Out<NMonitoring::ECompression>(IOutputStream& o, NMonitoring::ECompression c) {
  166. using NMonitoring::ECompression;
  167. switch (c) {
  168. case ECompression::IDENTITY:
  169. o << TStringBuf("IDENTITY");
  170. return;
  171. case ECompression::ZLIB:
  172. o << TStringBuf("ZLIB");
  173. return;
  174. case ECompression::LZ4:
  175. o << TStringBuf("LZ4");
  176. return;
  177. case ECompression::ZSTD:
  178. o << TStringBuf("ZSTD");
  179. return;
  180. case ECompression::UNKNOWN:
  181. o << TStringBuf("UNKNOWN");
  182. return;
  183. }
  184. Y_ABORT(); // for GCC
  185. }