aboutsummaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/http/helpers.cpp
blob: 233a565f2058f30309e0778a869b42686a8084a5 (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
#include "helpers.h"

#include "context.h"
#include "requests.h"

#include <yt/cpp/mapreduce/interface/logging/yt_log.h>

#include <library/cpp/yson/node/node_io.h>

namespace NYT {

///////////////////////////////////////////////////////////////////////////////

TString CreateHostNameWithPort(const TString& hostName, const TClientContext& context)
{
    static constexpr int HttpProxyPort = 80;
    static constexpr int HttpsProxyPort = 443;

    static constexpr int TvmOnlyHttpProxyPort = 9026;
    static constexpr int TvmOnlyHttpsProxyPort = 9443;

    if (hostName.find(':') == TString::npos) {
        int port;
        if (context.TvmOnly) {
            port = context.UseTLS
                ? TvmOnlyHttpsProxyPort
                : TvmOnlyHttpProxyPort;
        } else {
            port = context.UseTLS
                ? HttpsProxyPort
                : HttpProxyPort;
        }
        return Format("%v:%v", hostName, port);
    }
    return hostName;
}

TString GetFullUrl(const TString& hostName, const TClientContext& context, THttpHeader& header)
{
    Y_UNUSED(context);
    return Format("http://%v%v", hostName, header.GetUrl());
}

static TString GetParametersDebugString(const THttpHeader& header)
{
    const auto& parameters = header.GetParameters();
    if (parameters.Empty()) {
        return "<empty>";
    } else {
        return NodeToYsonString(parameters);
    }
}

TString TruncateForLogs(const TString& text, size_t maxSize)
{
    Y_VERIFY(maxSize > 10);
    if (text.empty()) {
        static TString empty = "empty";
        return empty;
    } else if (text.size() > maxSize) {
        TStringStream out;
        out << text.substr(0, maxSize) + "... ("  << text.size() << " bytes total)";
        return out.Str();
    } else {
        return text;
    }
}

TString GetLoggedAttributes(const THttpHeader& header, const TString& url, bool includeParameters, size_t sizeLimit)
{
    const auto parametersDebugString = GetParametersDebugString(header);
    TStringStream out;
    out << "Method: " << url << "; "
        << "X-YT-Parameters (sent in " << (includeParameters ? "header" : "body") << "): " << TruncateForLogs(parametersDebugString, sizeLimit);
    return out.Str();
}

void LogRequest(const THttpHeader& header, const TString& url, bool includeParameters, const TString& requestId, const TString& hostName)
{
    YT_LOG_DEBUG("REQ %v - sending request (HostName: %v; %v)",
        requestId,
        hostName,
        GetLoggedAttributes(header, url, includeParameters, Max<size_t>()));
}

///////////////////////////////////////////////////////////////////////////////

} // namespace NYT