Haio
0.2.X
Homebrew Asset IO/CDN
Toggle main menu visibility
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
7
namespace
beast = boost::beast;
8
9
namespace
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
*/
15
boost::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
}
bucket.hpp
Haio::Cdn::Bucket::ZipArchives
Definition
bucket.hpp:98
Haio::Cdn::Bucket::splitZipPath
std::optional< ZipPath > splitZipPath(std::string_view path)
Definition
archives.cpp:6
Haio::Cdn::Bucket::fetchS3
boost::asio::awaitable< Blob > fetchS3(const BucketConfig &bucket, std::string path)
Definition
s3.cpp:130
Haio::Cdn::Bucket::fetchHttp
boost::asio::awaitable< Blob > fetchHttp(const BucketConfig &bucket, std::string path)
Definition
http.cpp:253
Haio::Cdn::Bucket::fetchInsideZip
boost::asio::awaitable< Blob > fetchInsideZip(const BucketConfig &bucket, const ZipPath &path, const SecurityConfig &security, ZipArchives &archives)
Definition
fetch.cpp:16
Haio::Cdn::Bucket::fetchFile
Blob fetchFile(const BucketConfig &bucket, std::string path)
Definition
file.cpp:54
Haio::Cdn
Definition
haio_cache.hpp:16
Haio::Cdn::Config::security
SecurityConfig security
Definition
haio_cdn.hpp:265
Haio::Cdn::Config::buckets
std::map< std::string, BucketConfig > buckets
Definition
haio_cdn.hpp:267
Haio::Cdn::fetchBucket
boost::asio::awaitable< Result< Blob > > fetchBucket(const Config &config, Bucket::ZipArchives &archives, std::string bucket, std::string path)
Definition
bucket.cpp:15
Haio::Cdn::Config
Definition
haio_cdn.hpp:261
Haio::ErrorCode::NotFound
@ NotFound
Definition
haio_codec.hpp:20
Haio::ErrorCode::Upstream
@ Upstream
Definition
haio_codec.hpp:22
Haio::ErrorCode::Internal
@ Internal
Definition
haio_codec.hpp:17
Haio::ErrorCode::Timeout
@ Timeout
Definition
haio_codec.hpp:23
Haio::ErrorCode::InvalidInput
@ InvalidInput
Definition
haio_codec.hpp:18
Haio::Error
Definition
haio_codec.hpp:26
Haio::Cdn::Bucket::Failure
Definition
bucket.hpp:28
Haio::Cdn::Bucket::Failure::code
Haio::ErrorCode code
Definition
bucket.hpp:29
library
frontend
cdn
bucket
bucket.cpp
Generated by
1.18.0