aboutsummaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/http/helpers.cpp
blob: 18f74b595841f6695e3831fc387157ef3a4f4edf (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#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());
}

void UpdateHeaderForProxyIfNeed(const TString& hostName, const TClientContext& context, THttpHeader& header)
{
    if (context.ProxyAddress) {
        header.SetHostPort(Format("http://%v", hostName));
        header.SetProxyAddress(*context.ProxyAddress);
    }
}

TString GetFullUrlForProxy(const TString& hostName, const TClientContext& context, THttpHeader& header)
{
    if (context.ProxyAddress) {
        THttpHeader emptyHeader(header.GetMethod(), "", false);
        return GetFullUrl(*context.ProxyAddress, context, emptyHeader);
    }
    return GetFullUrl(hostName, context, header);
}

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_ABORT_UNLESS(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