Skip to content

Commit

Permalink
Refactor coro file (#611)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Jul 5, 2024
1 parent 43932dc commit acc4ae5
Show file tree
Hide file tree
Showing 9 changed files with 680 additions and 570 deletions.
2 changes: 1 addition & 1 deletion example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async_simple::coro::Lazy<resp_data> chunked_upload1(coro_http_client &client) {
create_file(filename, 1010);

coro_io::coro_file file{};
co_await file.async_open(filename, coro_io::flags::read_only);
file.open(filename, std::ios::in);

std::string buf;
cinatra::detail::resize(buf, 100);
Expand Down
28 changes: 15 additions & 13 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
std::string range = "") {
resp_data data{};
coro_io::coro_file file;
co_await file.async_open(filename, coro_io::flags::create_write);
file.open(filename, std::ios::trunc | std::ios::out);
if (!file.is_open()) {
data.net_err = std::make_error_code(std::errc::no_such_file_or_directory);
data.status = 404;
Expand Down Expand Up @@ -849,12 +849,12 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {

private:
async_simple::coro::Lazy<void> send_file_chunked_with_copy(
std::string source, std::error_code &ec) {
std::string_view source, std::error_code &ec) {
std::string file_data;
detail::resize(file_data, max_single_part_size_);
coro_io::coro_file file{};
bool ok = co_await file.async_open(source, coro_io::flags::read_only);
if (!ok) {
file.open(source, std::ios::in);
if (!file.is_open()) {
ec = std::make_error_code(std::errc::bad_file_descriptor);
co_return;
}
Expand All @@ -873,15 +873,15 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
}

async_simple::coro::Lazy<void> send_file_no_chunked_with_copy(
std::string source, std::error_code &ec, std::size_t length) {
std::string_view source, std::error_code &ec, std::size_t length) {
if (length <= 0) {
co_return;
}
std::string file_data;
detail::resize(file_data, std::min(max_single_part_size_, length));
coro_io::coro_file file{};
bool ok = co_await file.async_open(source, coro_io::flags::read_only);
if (!ok) {
file.open(source, std::ios::in);
if (!file.is_open()) {
ec = std::make_error_code(std::errc::bad_file_descriptor);
co_return;
}
Expand Down Expand Up @@ -1868,8 +1868,8 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {

if (is_ranges) {
if (ctx.resp_body_stream) {
auto ec =
co_await ctx.resp_body_stream->async_write(data_ptr, content_len);
auto [ec, size] = co_await ctx.resp_body_stream->async_write(
{data_ptr, content_len});
if (ec) {
data.net_err = ec;
co_return;
Expand Down Expand Up @@ -1955,8 +1955,9 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
auto part_body = co_await multipart.read_part_body(boundary);

if (ctx.resp_body_stream) {
ec = co_await ctx.resp_body_stream->async_write(part_body.data.data(),
part_body.data.size());
size_t size;
std::tie(ec, size) =
co_await ctx.resp_body_stream->async_write(part_body.data);
}
else {
resp_chunk_str_.append(part_body.data.data(), part_body.data.size());
Expand Down Expand Up @@ -2035,7 +2036,8 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {

data_ptr = asio::buffer_cast<const char *>(chunked_buf_.data());
if (ctx.resp_body_stream) {
ec = co_await ctx.resp_body_stream->async_write(data_ptr, chunk_size);
std::tie(ec, size) = co_await ctx.resp_body_stream->async_write(
{data_ptr, (size_t)chunk_size});
}
else {
resp_chunk_str_.append(data_ptr, chunk_size);
Expand Down Expand Up @@ -2141,7 +2143,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {

if (is_file) {
coro_io::coro_file file{};
co_await file.async_open(part.filename, coro_io::flags::read_only);
file.open(part.filename, std::ios::in);
assert(file.is_open());
std::string file_data;
detail::resize(file_data, max_single_part_size_);
Expand Down
18 changes: 15 additions & 3 deletions include/cinatra/coro_http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ class coro_http_server {
detail::resize(content, chunked_size_);

coro_io::coro_file in_file{};
co_await in_file.async_open(file_name, coro_io::flags::read_only);
in_file.open(file_name, std::ios::in);
if (!in_file.is_open()) {
resp.set_status_and_content(status_type::not_found,
file_name + "not found");
Expand Down Expand Up @@ -468,7 +468,13 @@ class coro_http_server {
if (ranges.size() == 1) {
// single part
auto [start, end] = ranges[0];
in_file.seek(start, SEEK_SET);
bool ok = in_file.seek(start, std::ios::beg);
if (!ok) {
resp.set_status_and_content(status_type::bad_request,
"invalid range");
co_await resp.get_conn()->reply();
co_return;
}
size_t part_size = end + 1 - start;
int status = (part_size == file_size) ? 200 : 206;
std::string content_range = "Content-Range: bytes ";
Expand Down Expand Up @@ -511,7 +517,13 @@ class coro_http_server {
}

auto [start, end] = ranges[i];
in_file.seek(start, SEEK_SET);
bool ok = in_file.seek(start, std::ios::beg);
if (!ok) {
resp.set_status_and_content(status_type::bad_request,
"invalid range");
co_await resp.get_conn()->reply();
co_return;
}
size_t part_size = end + 1 - start;

std::string_view more = CRCF;
Expand Down
Loading

0 comments on commit acc4ae5

Please sign in to comment.