helpers.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "helpers.h"
  2. #include "context.h"
  3. #include "requests.h"
  4. #include <yt/cpp/mapreduce/interface/logging/yt_log.h>
  5. #include <library/cpp/yson/node/node_io.h>
  6. namespace NYT {
  7. ///////////////////////////////////////////////////////////////////////////////
  8. TString CreateHostNameWithPort(const TString& hostName, const TClientContext& context)
  9. {
  10. static constexpr int HttpProxyPort = 80;
  11. static constexpr int HttpsProxyPort = 443;
  12. static constexpr int TvmOnlyHttpProxyPort = 9026;
  13. static constexpr int TvmOnlyHttpsProxyPort = 9443;
  14. if (hostName.find(':') == TString::npos) {
  15. int port;
  16. if (context.TvmOnly) {
  17. port = context.UseTLS
  18. ? TvmOnlyHttpsProxyPort
  19. : TvmOnlyHttpProxyPort;
  20. } else {
  21. port = context.UseTLS
  22. ? HttpsProxyPort
  23. : HttpProxyPort;
  24. }
  25. return Format("%v:%v", hostName, port);
  26. }
  27. return hostName;
  28. }
  29. TString GetFullUrl(const TString& hostName, const TClientContext& context, THttpHeader& header)
  30. {
  31. Y_UNUSED(context);
  32. return Format("http://%v%v", hostName, header.GetUrl());
  33. }
  34. void UpdateHeaderForProxyIfNeed(const TString& hostName, const TClientContext& context, THttpHeader& header)
  35. {
  36. if (context.ProxyAddress) {
  37. header.SetHostPort(Format("http://%v", hostName));
  38. header.SetProxyAddress(*context.ProxyAddress);
  39. }
  40. }
  41. TString GetFullUrlForProxy(const TString& hostName, const TClientContext& context, THttpHeader& header)
  42. {
  43. if (context.ProxyAddress) {
  44. THttpHeader emptyHeader(header.GetMethod(), "", false);
  45. return GetFullUrl(*context.ProxyAddress, context, emptyHeader);
  46. }
  47. return GetFullUrl(hostName, context, header);
  48. }
  49. static TString GetParametersDebugString(const THttpHeader& header)
  50. {
  51. const auto& parameters = header.GetParameters();
  52. if (parameters.Empty()) {
  53. return "<empty>";
  54. } else {
  55. return NodeToYsonString(parameters);
  56. }
  57. }
  58. TString TruncateForLogs(const TString& text, size_t maxSize)
  59. {
  60. Y_ABORT_UNLESS(maxSize > 10);
  61. if (text.empty()) {
  62. static TString empty = "empty";
  63. return empty;
  64. } else if (text.size() > maxSize) {
  65. TStringStream out;
  66. out << text.substr(0, maxSize) + "... (" << text.size() << " bytes total)";
  67. return out.Str();
  68. } else {
  69. return text;
  70. }
  71. }
  72. TString GetLoggedAttributes(const THttpHeader& header, const TString& url, bool includeParameters, size_t sizeLimit)
  73. {
  74. const auto parametersDebugString = GetParametersDebugString(header);
  75. TStringStream out;
  76. out << "Method: " << url << "; "
  77. << "X-YT-Parameters (sent in " << (includeParameters ? "header" : "body") << "): " << TruncateForLogs(parametersDebugString, sizeLimit);
  78. return out.Str();
  79. }
  80. void LogRequest(const THttpHeader& header, const TString& url, bool includeParameters, const TString& requestId, const TString& hostName)
  81. {
  82. YT_LOG_DEBUG("REQ %v - sending request (HostName: %v; %v)",
  83. requestId,
  84. hostName,
  85. GetLoggedAttributes(header, url, includeParameters, Max<size_t>()));
  86. }
  87. ///////////////////////////////////////////////////////////////////////////////
  88. } // namespace NYT