aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/service/format.h
blob: b95f504c05b37a1f2d530b9b47424ed1b0f61179 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#pragma once

#include <library/cpp/monlib/encode/format.h>

#include <util/string/ascii.h>
#include <util/generic/yexception.h>
#include <util/generic/typetraits.h>

namespace NMonitoring {
    namespace NPrivate {
        Y_HAS_MEMBER(Name, Name);
        Y_HAS_MEMBER(second, Second);
    } // namespace NPrivate

    template <typename TRequest>
    ECompression ParseCompression(const TRequest& req) {
        auto&& headers = req.GetHeaders();

        constexpr auto isPlainPair = NPrivate::THasSecond<std::decay_t<decltype(*headers.begin())>>::value;

        auto it = FindIf(std::begin(headers), std::end(headers),
            [=] (const auto& h) {
                if constexpr (NPrivate::THasName<std::decay_t<decltype(h)>>::value) {
                    return AsciiEqualsIgnoreCase(h.Name(), TStringBuf("accept-encoding"));
                } else if constexpr (isPlainPair) {
                    return AsciiEqualsIgnoreCase(h.first, TStringBuf("accept-encoding"));
                } else {
                    static_assert(TDependentFalse<decltype(h)>);
                }
            });

        if (it == std::end(headers)) {
            return NMonitoring::ECompression::IDENTITY;
        }

        NMonitoring::ECompression val{};
        if constexpr (isPlainPair) {
            val = CompressionFromAcceptEncodingHeader(it->second);
        } else {
            val = CompressionFromAcceptEncodingHeader(it->Value());
        }

        return val != NMonitoring::ECompression::UNKNOWN
            ? val
            : NMonitoring::ECompression::IDENTITY;
    }

    template <typename TRequest>
    NMonitoring::EFormat ParseFormat(const TRequest& req) {
        auto&& formatStr = req.GetParams()
            .Get(TStringBuf("format"));

        if (!formatStr.empty()) {
            if (formatStr == TStringBuf("SPACK")) {
                return EFormat::SPACK;
            } else if (formatStr == TStringBuf("TEXT")) {
                return EFormat::TEXT;
            } else if (formatStr == TStringBuf("JSON")) {
                return EFormat::JSON;
            } else {
                ythrow yexception() << "unknown format: " << formatStr << ". Only spack is supported here";
            }
        }

        auto&& headers = req.GetHeaders();
        constexpr auto isPlainPair = NPrivate::THasSecond<std::decay_t<decltype(*headers.begin())>>::value;

        auto it = FindIf(std::begin(headers), std::end(headers),
            [=] (const auto& h) {
                if constexpr (NPrivate::THasName<std::decay_t<decltype(h)>>::value) {
                    return AsciiEqualsIgnoreCase(h.Name(), TStringBuf("accept"));
                } else if constexpr (isPlainPair) {
                    return AsciiEqualsIgnoreCase(h.first, TStringBuf("accept"));
                } else {
                    static_assert(TDependentFalse<decltype(h)>);
                }
            });

        if (it != std::end(headers)) {
            if constexpr (isPlainPair) {
                return FormatFromAcceptHeader(it->second);
            } else {
                return FormatFromAcceptHeader(it->Value());
            }
        }

        return EFormat::UNKNOWN;
    }

} // namespace NMonitoring