Haio
0.2.X
Homebrew Asset IO/CDN
Toggle main menu visibility
Loading...
Searching...
No Matches
fetch.cpp
Go to the documentation of this file.
1
#include <
bucket/bucket.hpp
>
2
3
#include <boost/asio/use_awaitable.hpp>
4
5
namespace
asio = boost::asio;
6
7
namespace
Haio::Cdn::Bucket
{
8
9
/**
10
* reads one file out of an archive the bucket holds.
11
*
12
* the archive is fetched the ordinary way and then kept, so a second picture out of
13
* the same zip costs an inflate and no fetch. the same picture twice never arrives
14
* here at all: the response cache answered before the bucket was asked.
15
*/
16
asio::awaitable<Blob>
fetchInsideZip
(
const
BucketConfig
& bucket,
const
ZipPath
& path,
17
const
SecurityConfig
& security,
ZipArchives
& archives) {
18
const
auto
key = bucket.
name
+
"/"
+ path.
archive
;
19
20
auto
archive = archives.
find
(key);
21
if
(!archive) {
22
auto
blob = bucket.
scheme
==
"file"
?
fetchFile
(bucket, path.
archive
)
23
: bucket.
scheme
==
"s3"
?
co_await
fetchS3
(bucket, path.
archive
)
24
:
co_await
fetchHttp
(bucket, path.
archive
);
25
26
auto
index =
readZipIndex
(blob.data);
27
if
(!index)
throw
Failure
(index.error().code, index.error().message);
28
29
archive = archives.
keep
(key, std::move(blob.data), *std::move(index));
30
}
31
32
const
auto
found = archive->index.entries.find(path.
inside
);
33
if
(found == archive->index.entries.end()) {
34
// both halves came from the request, so repeating them tells the caller
35
// nothing it did not already write
36
throw
Failure
(
ErrorCode::NotFound
,
"no \""
+ path.
inside
+
"\" inside "
+ path.
archive
);
37
}
38
39
auto
data =
readZipEntry
(archive->data, found->second, security.
maxUnzip
);
40
if
(!data)
throw
Failure
(data.error().code, data.error().message);
41
42
const
auto
detected =
Haio::Detect
(*data);
43
const
auto
format = detected ? detected.format :
Haio::formatFromExtension
(path.
inside
);
44
co_return
Blob
{format, detected.color, std::string(
Haio::contentTypeFor
(format)), path.
inside
, *std::move(data)};
45
}
46
47
}
bucket.hpp
Haio::Cdn::Bucket::ZipArchives
Definition
bucket.hpp:98
Haio::Cdn::Bucket::ZipArchives::keep
std::shared_ptr< const Archive > keep(const std::string &key, std::vector< uint8_t > data, ZipIndex index)
Definition
archives.cpp:28
Haio::Cdn::Bucket::ZipArchives::find
std::shared_ptr< const Archive > find(const std::string &key)
Definition
archives.cpp:20
Haio::Cdn::Bucket
Definition
haio_cdn.hpp:281
Haio::Cdn::Bucket::readZipEntry
Result< std::vector< uint8_t > > readZipEntry(Bytes data, const ZipEntry &entry, size_t maxSize)
Definition
reader.cpp:106
Haio::Cdn::Bucket::ZipPath::inside
std::string inside
Definition
bucket.hpp:84
Haio::Cdn::Bucket::fetchS3
boost::asio::awaitable< Blob > fetchS3(const BucketConfig &bucket, std::string path)
Definition
s3.cpp:130
Haio::Cdn::Bucket::ZipPath::archive
std::string archive
Definition
bucket.hpp:83
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::readZipIndex
Result< ZipIndex > readZipIndex(Bytes data)
Definition
reader.cpp:57
Haio::Cdn::Bucket::fetchFile
Blob fetchFile(const BucketConfig &bucket, std::string path)
Definition
file.cpp:54
Haio::Cdn::Bucket::ZipPath
Definition
bucket.hpp:82
Haio::Cdn::BucketConfig::scheme
std::string scheme
Definition
haio_cdn.hpp:206
Haio::Cdn::BucketConfig::name
std::string name
Definition
haio_cdn.hpp:202
Haio::Cdn::SecurityConfig::maxUnzip
size_t maxUnzip
Definition
haio_cdn.hpp:258
Haio::Cdn::BucketConfig
Definition
haio_cdn.hpp:201
Haio::Cdn::SecurityConfig
Definition
haio_cdn.hpp:229
Haio::Detect
Found Detect(Bytes data)
Definition
haio_registry.hpp:84
Haio::formatFromExtension
Format formatFromExtension(std::string_view path)
Definition
format.cpp:33
Haio::contentTypeFor
std::string_view contentTypeFor(Format format)
Definition
format.cpp:62
Haio::ErrorCode::NotFound
@ NotFound
Definition
haio_codec.hpp:20
Haio::Blob
Definition
haio_codec.hpp:122
Haio::Cdn::Bucket::Failure::Failure
Failure(Haio::ErrorCode why, const std::string &what)
Definition
bucket.hpp:30
library
frontend
cdn
bucket
zip
fetch.cpp
Generated by
1.18.0