Coverage Report

Created: 2025-05-09 16:48

/root/doris/be/src/util/s3_uri.cpp
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "util/s3_uri.h"
19
20
#include <absl/strings/ascii.h>
21
#include <absl/strings/str_split.h>
22
23
#include <vector>
24
25
namespace doris {
26
27
const std::string S3URI::_SCHEME_S3 = "s3";
28
const std::string S3URI::_SCHEME_HTTP = "http";
29
const std::string S3URI::_SCHEME_HTTPS = "https";
30
const std::string S3URI::_SCHEME_DELIM = "://";
31
const std::string S3URI::_PATH_DELIM = "/";
32
const std::string S3URI::_QUERY_DELIM = "?";
33
const std::string S3URI::_FRAGMENT_DELIM = "#";
34
35
/// eg:
36
///     s3://bucket1/path/to/file.txt
37
/// _schema: s3
38
/// _bucket: bucket1
39
/// _key:    path/to/file.txt
40
90
Status S3URI::parse() {
41
90
    if (_location.empty()) {
42
0
        return Status::InvalidArgument("location is empty");
43
0
    }
44
90
    std::vector<std::string> scheme_split = absl::StrSplit(_location, _SCHEME_DELIM);
45
90
    std::string rest;
46
90
    if (scheme_split.size() == 2) {
47
11
        if (scheme_split[0] == _SCHEME_S3) {
48
            // has scheme, eg: s3://bucket1/path/to/file.txt
49
5
            rest = scheme_split[1];
50
5
            std::vector<std::string> authority_split =
51
5
                    absl::StrSplit(rest, absl::MaxSplits(_PATH_DELIM, 1));
52
5
            if (authority_split.size() < 1) {
53
0
                return Status::InvalidArgument("Invalid S3 URI: {}", _location);
54
0
            }
55
5
            _bucket = authority_split[0];
56
            // support s3://bucket1
57
5
            _key = authority_split.size() == 1 ? "/" : authority_split[1];
58
6
        } else if (scheme_split[0] == _SCHEME_HTTP || scheme_split[0] == _SCHEME_HTTPS) {
59
            // has scheme, eg: http(s)://host/bucket1/path/to/file.txt
60
5
            rest = scheme_split[1];
61
5
            std::vector<std::string> authority_split =
62
5
                    absl::StrSplit(rest, absl::MaxSplits(_PATH_DELIM, 2));
63
5
            if (authority_split.size() != 3) {
64
2
                return Status::InvalidArgument("Invalid S3 HTTP URI: {}", _location);
65
2
            }
66
            // authority_split[1] is host
67
3
            _bucket = authority_split[1];
68
3
            _key = authority_split[2];
69
3
        } else {
70
1
            return Status::InvalidArgument("Invalid S3 URI: {}", _location);
71
1
        }
72
79
    } else if (scheme_split.size() == 1) {
73
        // no scheme, eg: path/to/file.txt
74
79
        _bucket = ""; // unknown
75
79
        _key = _location;
76
79
    } else {
77
0
        return Status::InvalidArgument("Invalid S3 URI: {}", _location);
78
0
    }
79
87
    absl::StripAsciiWhitespace(&_key);
80
87
    if (_key.empty()) {
81
2
        return Status::InvalidArgument("Invalid S3 key: {}", _location);
82
2
    }
83
    // Strip query and fragment if they exist
84
85
    std::vector<std::string> _query_split = absl::StrSplit(_key, _QUERY_DELIM);
85
85
    std::vector<std::string> _fragment_split = absl::StrSplit(_query_split[0], _FRAGMENT_DELIM);
86
85
    _key = _fragment_split[0];
87
85
    return Status::OK();
88
87
}
89
90
0
std::string S3URI::to_string() const {
91
0
    return _location;
92
0
}
93
94
} // end namespace doris