format.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #include <library/cpp/monlib/encode/format.h>
  3. #include <util/string/ascii.h>
  4. #include <util/generic/yexception.h>
  5. #include <util/generic/typetraits.h>
  6. namespace NMonitoring {
  7. namespace NPrivate {
  8. Y_HAS_MEMBER(Name, Name);
  9. Y_HAS_MEMBER(second, Second);
  10. } // namespace NPrivate
  11. template <typename TRequest>
  12. ECompression ParseCompression(const TRequest& req) {
  13. auto&& headers = req.GetHeaders();
  14. constexpr auto isPlainPair = NPrivate::THasSecond<std::decay_t<decltype(*headers.begin())>>::value;
  15. auto it = FindIf(std::begin(headers), std::end(headers),
  16. [=] (const auto& h) {
  17. if constexpr (NPrivate::THasName<std::decay_t<decltype(h)>>::value) {
  18. return AsciiEqualsIgnoreCase(h.Name(), TStringBuf("accept-encoding"));
  19. } else if constexpr (isPlainPair) {
  20. return AsciiEqualsIgnoreCase(h.first, TStringBuf("accept-encoding"));
  21. } else {
  22. static_assert(TDependentFalse<decltype(h)>);
  23. }
  24. });
  25. if (it == std::end(headers)) {
  26. return NMonitoring::ECompression::IDENTITY;
  27. }
  28. NMonitoring::ECompression val{};
  29. if constexpr (isPlainPair) {
  30. val = CompressionFromAcceptEncodingHeader(it->second);
  31. } else {
  32. val = CompressionFromAcceptEncodingHeader(it->Value());
  33. }
  34. return val != NMonitoring::ECompression::UNKNOWN
  35. ? val
  36. : NMonitoring::ECompression::IDENTITY;
  37. }
  38. template <typename TRequest>
  39. NMonitoring::EFormat ParseFormat(const TRequest& req) {
  40. auto&& formatStr = req.GetParams()
  41. .Get(TStringBuf("format"));
  42. if (!formatStr.empty()) {
  43. if (formatStr == TStringBuf("SPACK")) {
  44. return EFormat::SPACK;
  45. } else if (formatStr == TStringBuf("TEXT")) {
  46. return EFormat::TEXT;
  47. } else if (formatStr == TStringBuf("JSON")) {
  48. return EFormat::JSON;
  49. } else {
  50. ythrow yexception() << "unknown format: " << formatStr << ". Only spack is supported here";
  51. }
  52. }
  53. auto&& headers = req.GetHeaders();
  54. constexpr auto isPlainPair = NPrivate::THasSecond<std::decay_t<decltype(*headers.begin())>>::value;
  55. auto it = FindIf(std::begin(headers), std::end(headers),
  56. [=] (const auto& h) {
  57. if constexpr (NPrivate::THasName<std::decay_t<decltype(h)>>::value) {
  58. return AsciiEqualsIgnoreCase(h.Name(), TStringBuf("accept"));
  59. } else if constexpr (isPlainPair) {
  60. return AsciiEqualsIgnoreCase(h.first, TStringBuf("accept"));
  61. } else {
  62. static_assert(TDependentFalse<decltype(h)>);
  63. }
  64. });
  65. if (it != std::end(headers)) {
  66. if constexpr (isPlainPair) {
  67. return FormatFromAcceptHeader(it->second);
  68. } else {
  69. return FormatFromAcceptHeader(it->Value());
  70. }
  71. }
  72. return EFormat::UNKNOWN;
  73. }
  74. } // namespace NMonitoring