|
| 1 | +#include <ydb/core/protos/flat_scheme_op.pb.h> |
| 2 | + |
| 3 | +#include <ydb/library/accessor/accessor.h> |
| 4 | +#include <ydb/library/conclusion/result.h> |
| 5 | + |
| 6 | +#include <library/cpp/uri/uri.h> |
| 7 | +#include <util/string/builder.h> |
| 8 | + |
| 9 | +#include <regex> |
| 10 | + |
| 11 | +namespace NKikimr::NColumnShard::NTiers { |
| 12 | + |
| 13 | +class TS3Uri { |
| 14 | +private: |
| 15 | + YDB_READONLY_DEF(TString, Host); |
| 16 | + YDB_READONLY_DEF(TString, Bucket); |
| 17 | + YDB_READONLY_DEF(std::optional<TString>, Folder) |
| 18 | + YDB_READONLY_DEF(std::optional<ui16>, Port); |
| 19 | + YDB_READONLY_DEF(std::optional<NKikimrSchemeOp::TS3Settings_EScheme>, Scheme); |
| 20 | + |
| 21 | + enum TUriStyle { |
| 22 | + PATH_STYLE = 1, |
| 23 | + VIRTUAL_HOSTED_STYLE = 2, |
| 24 | + }; |
| 25 | + |
| 26 | +private: |
| 27 | + static TStringBuf StripPath(const TStringBuf& path) { |
| 28 | + TStringBuf stripped = path; |
| 29 | + while (stripped.SkipPrefix("/")) { |
| 30 | + } |
| 31 | + while (stripped.ChopSuffix("/")) { |
| 32 | + } |
| 33 | + return stripped; |
| 34 | + } |
| 35 | + |
| 36 | + static std::optional<TUriStyle> DeduceUriStyle(const NUri::TUri& uri) { |
| 37 | + { |
| 38 | + static const std::regex StrictEndpointPattern = std::regex{ "^(.+\\.)?s3[.-].*" }; |
| 39 | + std::match_results<TString::const_iterator> match; |
| 40 | + if (std::regex_match(uri.GetHost().begin(), uri.GetHost().end(), match, StrictEndpointPattern)) { |
| 41 | + if (match[1].length()) { |
| 42 | + return VIRTUAL_HOSTED_STYLE; |
| 43 | + } else { |
| 44 | + return PATH_STYLE; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + const bool hasSubdomain = std::count(uri.GetHost().begin(), uri.GetHost().end(), '.') >= 2; |
| 50 | + const bool hasPath = !StripPath(uri.GetField(NUri::TField::FieldPath)).Empty(); |
| 51 | + if (hasSubdomain && !hasPath) { |
| 52 | + return VIRTUAL_HOSTED_STYLE; |
| 53 | + } |
| 54 | + if (!hasSubdomain && hasPath) { |
| 55 | + return PATH_STYLE; |
| 56 | + } |
| 57 | + |
| 58 | + return std::nullopt; |
| 59 | + } |
| 60 | + |
| 61 | + static TConclusion<TS3Uri> ParsePathStyleUri(const NUri::TUri& input) { |
| 62 | + TS3Uri result; |
| 63 | + |
| 64 | + TStringBuf path = StripPath(input.GetField(NUri::TField::FieldPath)); |
| 65 | + |
| 66 | + if (path.Empty()) { |
| 67 | + return TConclusionStatus::Fail(TStringBuilder() << "Missing bucket in path-style S3 uri: " << input.Serialize()); |
| 68 | + } |
| 69 | + |
| 70 | + TStringBuf folder; |
| 71 | + TStringBuf bucket; |
| 72 | + if (path.TryRSplit('/', folder, bucket)) { |
| 73 | + result.Folder = folder; |
| 74 | + result.Bucket = bucket; |
| 75 | + } else { |
| 76 | + result.Bucket = path; |
| 77 | + } |
| 78 | + |
| 79 | + result.Host = input.GetHost(); |
| 80 | + |
| 81 | + if (auto status = result.FillStyleAgnosticFields(input); status.IsFail()) { |
| 82 | + return status; |
| 83 | + } |
| 84 | + return result; |
| 85 | + } |
| 86 | + |
| 87 | + static TConclusion<TS3Uri> ParseVirtualHostedStyleUri(const NUri::TUri& input) { |
| 88 | + TS3Uri result; |
| 89 | + |
| 90 | + { |
| 91 | + TStringBuf host; |
| 92 | + TStringBuf bucket; |
| 93 | + if (input.GetHost().TrySplit('.', bucket, host)) { |
| 94 | + result.Host = host; |
| 95 | + result.Bucket = bucket; |
| 96 | + } else { |
| 97 | + return TConclusionStatus::Fail(TStringBuilder() << "Missing bucket in virtual-hosted style S3 uri: " << input.Serialize()); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (TStringBuf path = StripPath(input.GetField(NUri::TField::FieldPath))) { |
| 102 | + result.Folder = path; |
| 103 | + } |
| 104 | + |
| 105 | + if (auto status = result.FillStyleAgnosticFields(input); status.IsFail()) { |
| 106 | + return status; |
| 107 | + } |
| 108 | + return result; |
| 109 | + } |
| 110 | + |
| 111 | + TConclusionStatus FillStyleAgnosticFields(const NUri::TUri& from) { |
| 112 | + if (from.GetField(NUri::TField::FieldPort)) { |
| 113 | + Port = from.GetPort(); |
| 114 | + } |
| 115 | + |
| 116 | + switch (from.GetScheme()) { |
| 117 | + case NUri::TScheme::SchemeEmpty: |
| 118 | + break; |
| 119 | + case NUri::TScheme::SchemeHTTP: |
| 120 | + Scheme = NKikimrSchemeOp::TS3Settings_EScheme_HTTP; |
| 121 | + break; |
| 122 | + case NUri::TScheme::SchemeHTTPS: |
| 123 | + Scheme = NKikimrSchemeOp::TS3Settings_EScheme_HTTPS; |
| 124 | + break; |
| 125 | + default: |
| 126 | + return TConclusionStatus::Fail(TStringBuilder() << "Unexpected scheme in url: " << from.Serialize()); |
| 127 | + } |
| 128 | + |
| 129 | + return TConclusionStatus::Success(); |
| 130 | + } |
| 131 | + |
| 132 | +public: |
| 133 | + static TConclusion<TS3Uri> ParseUri(const TString& input) { |
| 134 | + NUri::TUri uri; |
| 135 | + if (uri.Parse(input, NUri::TFeature::NewFeaturesRecommended) != NUri::TState::EParsed::ParsedOK) { |
| 136 | + return TConclusionStatus::Fail("Cannot parse URI: " + input); |
| 137 | + } |
| 138 | + |
| 139 | + TUriStyle uriStyle; |
| 140 | + if (const auto deducedStyle = DeduceUriStyle(uri)) { |
| 141 | + uriStyle = *deducedStyle; |
| 142 | + } else { |
| 143 | + uriStyle = PATH_STYLE; |
| 144 | + } |
| 145 | + |
| 146 | + switch (uriStyle) { |
| 147 | + case PATH_STYLE: |
| 148 | + return ParsePathStyleUri(uri); |
| 149 | + case VIRTUAL_HOSTED_STYLE: |
| 150 | + return ParseVirtualHostedStyleUri(uri); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + TString GetEndpoint() const { |
| 155 | + TString endpoint = Host; |
| 156 | + if (Port) { |
| 157 | + endpoint += TStringBuilder() << ':' << *Port; |
| 158 | + } |
| 159 | + if (Folder) { |
| 160 | + endpoint += TStringBuilder() << '/' << *Folder; |
| 161 | + } |
| 162 | + return endpoint; |
| 163 | + } |
| 164 | + |
| 165 | + void FillSettings(NKikimrSchemeOp::TS3Settings& settings) const { |
| 166 | + settings.SetEndpoint(GetEndpoint()); |
| 167 | + settings.SetBucket(Bucket); |
| 168 | + if (Scheme) { |
| 169 | + settings.SetScheme(*Scheme); |
| 170 | + } |
| 171 | + } |
| 172 | +}; |
| 173 | + |
| 174 | +} // namespace NKikimr::NColumnShard::NTiers |
0 commit comments