|
| 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 | +namespace NKikimr::NColumnShard::NTiers { |
| 10 | + |
| 11 | +class TS3Uri { |
| 12 | +private: |
| 13 | + YDB_READONLY_DEF(std::optional<NKikimrSchemeOp::TS3Settings_EScheme>, Scheme); |
| 14 | + YDB_READONLY_DEF(TString, Bucket); |
| 15 | + YDB_READONLY_DEF(TString, Host); |
| 16 | + YDB_READONLY_DEF(std::optional<ui16>, Port); |
| 17 | + YDB_READONLY_DEF(std::optional<TString>, Folder); |
| 18 | + |
| 19 | + enum TUriStyle { |
| 20 | + PATH_STYLE = 1, |
| 21 | + VIRTUAL_HOSTED_STYLE = 2, |
| 22 | + }; |
| 23 | + |
| 24 | + inline static const std::vector<TString> BucketHostSeparators = { ".s3.", ".s3-" }; |
| 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 | + const bool hasSubdomain = std::count(uri.GetHost().begin(), uri.GetHost().end(), '.') >= 2; |
| 38 | + const bool hasPath = !StripPath(uri.GetField(NUri::TField::FieldPath)).Empty(); |
| 39 | + if (hasSubdomain && !hasPath) { |
| 40 | + return VIRTUAL_HOSTED_STYLE; |
| 41 | + } |
| 42 | + if (!hasSubdomain && hasPath) { |
| 43 | + return PATH_STYLE; |
| 44 | + } |
| 45 | + |
| 46 | + // URI style deduction copied from AWS SDK for Java |
| 47 | + for (const TString& sep : BucketHostSeparators) { |
| 48 | + if (uri.GetHost().StartsWith(sep.substr(1))) { |
| 49 | + return PATH_STYLE; |
| 50 | + } |
| 51 | + if (uri.GetHost().Contains(sep)) { |
| 52 | + return VIRTUAL_HOSTED_STYLE; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return std::nullopt; |
| 57 | + } |
| 58 | + |
| 59 | + static TConclusion<TS3Uri> ParsePathStyleUri(const NUri::TUri& input) { |
| 60 | + TS3Uri result; |
| 61 | + |
| 62 | + TStringBuf path = StripPath(input.GetField(NUri::TField::FieldPath)); |
| 63 | + |
| 64 | + if (path.Empty()) { |
| 65 | + return TConclusionStatus::Fail(TStringBuilder() << "Missing bucket in path-style S3 uri: " << input.Serialize()); |
| 66 | + } |
| 67 | + |
| 68 | + TStringBuf folder; |
| 69 | + TStringBuf bucket; |
| 70 | + if (path.TryRSplit('/', folder, bucket)) { |
| 71 | + result.Folder = folder; |
| 72 | + result.Bucket = bucket; |
| 73 | + } else { |
| 74 | + result.Bucket = path; |
| 75 | + } |
| 76 | + |
| 77 | + result.Host = input.GetHost(); |
| 78 | + |
| 79 | + if (auto status = result.FillStyleAgnosticFields(input); status.IsFail()) { |
| 80 | + return status; |
| 81 | + } |
| 82 | + return result; |
| 83 | + } |
| 84 | + |
| 85 | + static TConclusion<TS3Uri> ParseVirtualHostedStyleUri(const NUri::TUri& input) { |
| 86 | + TS3Uri result; |
| 87 | + |
| 88 | + for (const TString& sep : BucketHostSeparators) { |
| 89 | + if (const ui64 findSep = input.GetHost().find(sep); findSep != TStringBuf::npos) { |
| 90 | + result.Bucket = input.GetHost().SubStr(0, findSep); |
| 91 | + result.Host = input.GetHost().SubStr(findSep + 1); |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + if (result.Host.empty()) { |
| 96 | + TStringBuf host; |
| 97 | + TStringBuf bucket; |
| 98 | + if (input.GetHost().TrySplit('.', bucket, host)) { |
| 99 | + result.Host = host; |
| 100 | + result.Bucket = bucket; |
| 101 | + } else { |
| 102 | + return TConclusionStatus::Fail(TStringBuilder() << "Missing bucket in virtual-hosted style S3 uri: " << input.Serialize()); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if (TStringBuf path = StripPath(input.GetField(NUri::TField::FieldPath))) { |
| 107 | + result.Folder = path; |
| 108 | + } |
| 109 | + |
| 110 | + if (auto status = result.FillStyleAgnosticFields(input); status.IsFail()) { |
| 111 | + return status; |
| 112 | + } |
| 113 | + return result; |
| 114 | + } |
| 115 | + |
| 116 | + TConclusionStatus FillStyleAgnosticFields(const NUri::TUri& from) { |
| 117 | + if (from.GetField(NUri::TField::FieldPort)) { |
| 118 | + Port = from.GetPort(); |
| 119 | + } |
| 120 | + |
| 121 | + switch (from.GetScheme()) { |
| 122 | + case NUri::TScheme::SchemeEmpty: |
| 123 | + break; |
| 124 | + case NUri::TScheme::SchemeHTTP: |
| 125 | + Scheme = NKikimrSchemeOp::TS3Settings_EScheme_HTTP; |
| 126 | + break; |
| 127 | + case NUri::TScheme::SchemeHTTPS: |
| 128 | + Scheme = NKikimrSchemeOp::TS3Settings_EScheme_HTTPS; |
| 129 | + break; |
| 130 | + default: |
| 131 | + return TConclusionStatus::Fail(TStringBuilder() << "Unexpected scheme in url: " << from.Serialize()); |
| 132 | + } |
| 133 | + |
| 134 | + return TConclusionStatus::Success(); |
| 135 | + } |
| 136 | + |
| 137 | +public: |
| 138 | + static TConclusion<TS3Uri> ParseUri(const TString& input) { |
| 139 | + NUri::TUri uri; |
| 140 | + if (uri.Parse(input, NUri::TFeature::NewFeaturesRecommended) != NUri::TState::EParsed::ParsedOK) { |
| 141 | + return TConclusionStatus::Fail("Cannot parse URI: " + input); |
| 142 | + } |
| 143 | + |
| 144 | + TUriStyle uriStyle; |
| 145 | + if (const auto deducedStyle = DeduceUriStyle(uri)) { |
| 146 | + uriStyle = *deducedStyle; |
| 147 | + } else { |
| 148 | + uriStyle = PATH_STYLE; |
| 149 | + } |
| 150 | + |
| 151 | + switch (uriStyle) { |
| 152 | + case PATH_STYLE: |
| 153 | + return ParsePathStyleUri(uri); |
| 154 | + case VIRTUAL_HOSTED_STYLE: |
| 155 | + return ParseVirtualHostedStyleUri(uri); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + TString GetEndpoint() const { |
| 160 | + TString endpoint = Host; |
| 161 | + if (Port) { |
| 162 | + endpoint += TStringBuilder() << ':' << *Port; |
| 163 | + } |
| 164 | + if (Folder) { |
| 165 | + endpoint += TStringBuilder() << '/' << *Folder; |
| 166 | + } |
| 167 | + return endpoint; |
| 168 | + } |
| 169 | + |
| 170 | + void FillSettings(NKikimrSchemeOp::TS3Settings& settings) const { |
| 171 | + settings.SetEndpoint(GetEndpoint()); |
| 172 | + settings.SetBucket(Bucket); |
| 173 | + if (Scheme) { |
| 174 | + settings.SetScheme(*Scheme); |
| 175 | + } |
| 176 | + } |
| 177 | +}; |
| 178 | + |
| 179 | +} // namespace NKikimr::NColumnShard::NTiers |
0 commit comments