Haio 0.2.X
Homebrew Asset IO/CDN
Loading...
Searching...
No Matches
bucket.cpp
Go to the documentation of this file.
1#include <bucket/bucket.hpp>
2
3#include <boost/beast/core.hpp>
4
5#include <boost/system/system_error.hpp>
6
7namespace beast = boost::beast;
8
9namespace Haio::Cdn {
10
11/**
12 * picks a fetcher by the scheme the config already parsed, and turns whatever it
13 * throws into a Result, so nothing above this line has to catch anything.
14 */
15boost::asio::awaitable<Result<Blob>> fetchBucket(const Config& config, Bucket::ZipArchives& archives, std::string bucketName, std::string path) {
16 try {
17 const auto& security = config.security;
18 const auto it = config.buckets.find(bucketName);
19 if (it == config.buckets.end()) {
20 co_return std::unexpected(Error{ErrorCode::NotFound, "unknown bucket: " + bucketName});
21 }
22
23 const auto& bucket = it->second;
24
25 if (auto zipped = Bucket::splitZipPath(path)) {
26 if (!security.allowUnzip) {
27 co_return std::unexpected(Error{ErrorCode::InvalidInput,
28 "this path names a file inside a zip, and allow_unzip is off"});
29 }
30 co_return co_await Bucket::fetchInsideZip(bucket, *zipped, security, archives);
31 }
32
33 if (bucket.scheme == "file") co_return Bucket::fetchFile(bucket, std::move(path));
34 if (bucket.scheme == "s3") co_return co_await Bucket::fetchS3(bucket, std::move(path));
35 co_return co_await Bucket::fetchHttp(bucket, std::move(path));
36 } catch (const Bucket::Failure& err) {
37 co_return std::unexpected(Error{err.code, err.what()});
38 } catch (const boost::system::system_error& err) {
39 const bool timedOut = err.code() == beast::error::timeout;
40 if (timedOut) {
41 co_return std::unexpected(Error{ErrorCode::Timeout, "upstream timed out"});
42 }
43 co_return std::unexpected(Error{ErrorCode::Upstream, "upstream unreachable: " + std::string(err.code().message())});
44 } catch (const std::exception& err) {
45 co_return std::unexpected(Error{ErrorCode::Internal, err.what()});
46 }
47}
48
49}
std::optional< ZipPath > splitZipPath(std::string_view path)
Definition archives.cpp:6
boost::asio::awaitable< Blob > fetchS3(const BucketConfig &bucket, std::string path)
Definition s3.cpp:130
boost::asio::awaitable< Blob > fetchHttp(const BucketConfig &bucket, std::string path)
Definition http.cpp:253
boost::asio::awaitable< Blob > fetchInsideZip(const BucketConfig &bucket, const ZipPath &path, const SecurityConfig &security, ZipArchives &archives)
Definition fetch.cpp:16
Blob fetchFile(const BucketConfig &bucket, std::string path)
Definition file.cpp:54
SecurityConfig security
Definition haio_cdn.hpp:265
std::map< std::string, BucketConfig > buckets
Definition haio_cdn.hpp:267
boost::asio::awaitable< Result< Blob > > fetchBucket(const Config &config, Bucket::ZipArchives &archives, std::string bucket, std::string path)
Definition bucket.cpp:15
Haio::ErrorCode code
Definition bucket.hpp:29