Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[coro_http][feat]update coro_http_server #526

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions include/ylt/coro_io/coro_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ class coro_file {
async_simple::coro::Lazy<std::pair<std::error_code, size_t>> async_read_at(
uint64_t offset, char* data, size_t size) {
assert(stream_file_);
assert(type_ == read_type::uring_random);

auto [ec, read_size] = co_await coro_io::async_read_at(
offset,
Expand All @@ -288,6 +289,7 @@ class coro_file {
const char* data,
size_t size) {
assert(stream_file_);
assert(type_ == read_type::uring_random);

auto [ec, write_size] = co_await coro_io::async_write_at(
offset,
Expand All @@ -299,6 +301,7 @@ class coro_file {
async_simple::coro::Lazy<std::pair<std::error_code, size_t>> async_read(
char* data, size_t size) {
assert(stream_file_);
assert(type_ == read_type::uring);

auto [ec, read_size] = co_await coro_io::async_read(
*reinterpret_cast<asio::stream_file*>(stream_file_.get()),
Expand All @@ -314,6 +317,7 @@ class coro_file {
async_simple::coro::Lazy<std::error_code> async_write(const char* data,
size_t size) {
assert(stream_file_);
assert(type_ == read_type::uring);

auto [ec, write_size] = co_await coro_io::async_write(
*reinterpret_cast<asio::stream_file*>(stream_file_.get()),
Expand All @@ -325,20 +329,20 @@ class coro_file {
std::string str_mode(int open_mode) {
switch (open_mode) {
case flags::read_only:
return "r";
return "rb";
case flags::create_write:
case flags::write_only:
return "w";
case flags::read_write:
return "r+";
return "rb+";
case flags::append:
return "a";
case flags::create_read_write_append:
return "a+";
return "ab+";
case flags::truncate:
return "w+";
default:
return "r+";
return "rb+";
}
}

Expand Down
52 changes: 49 additions & 3 deletions include/ylt/thirdparty/cinatra/coro_http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#include "ylt/coro_io/io_context_pool.hpp"

namespace cinatra {
enum class file_resp_format_type {
chunked,
range,
};
class coro_http_server {
public:
coro_http_server(asio::io_context &ctx, unsigned short port)
Expand Down Expand Up @@ -151,6 +155,35 @@ class coro_http_server {
}
}

void set_max_size_of_cache_files(size_t max_size = 3 * 1024 * 1024) {
std::error_code ec;
for (const auto &file :
std::filesystem::recursive_directory_iterator(static_dir_, ec)) {
if (ec) {
continue;
}

if (!file.is_directory()) {
size_t filesize = fs::file_size(file, ec);
if (ec || filesize > max_size) {
continue;
}

std::ifstream ifs(file.path(), std::ios::binary);
if (ifs.is_open()) {
std::string content;
detail::resize(content, filesize);
ifs.read(content.data(), content.size());
static_file_cache_.emplace(file.path().string(), std::move(content));
}
}
}
}

void set_file_resp_format_type(file_resp_format_type type) {
format_type_ = type;
}

void set_transfer_chunked_size(size_t size) { chunked_size_ = size; }

void set_static_res_handler(std::string_view uri_suffix = "",
Expand Down Expand Up @@ -202,11 +235,21 @@ class coro_http_server {
[this, file_name = file](
coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
bool is_ranges = req.is_req_ranges();

std::string_view extension = get_extension(file_name);
std::string_view mime = get_mime_type(extension);

if (auto it = static_file_cache_.find(file_name);
it != static_file_cache_.end()) {
auto range_header =
build_range_header(mime, file_name, fs::file_size(file_name));
resp.set_delay(true);
std::string &body = it->second;
std::array<asio::const_buffer, 2> arr{asio::buffer(range_header),
asio::buffer(body)};
co_await req.get_conn()->async_write(arr);
co_return;
}

std::string content;
detail::resize(content, chunked_size_);

Expand All @@ -218,7 +261,7 @@ class coro_http_server {
co_return;
}

if (!is_ranges) {
if (format_type_ == file_resp_format_type::chunked) {
resp.set_format_type(format_type::chunked);
bool ok;
if (ok = co_await resp.get_conn()->begin_chunked(); !ok) {
Expand Down Expand Up @@ -473,6 +516,9 @@ class coro_http_server {
std::string static_dir_ = "";
std::vector<std::string> files_;
size_t chunked_size_ = 1024 * 10;

std::unordered_map<std::string, std::string> static_file_cache_;
file_resp_format_type format_type_ = file_resp_format_type::chunked;
#ifdef CINATRA_ENABLE_SSL
std::string cert_file_;
std::string key_file_;
Expand Down
Loading