Skip to content

Commit

Permalink
begin replace
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Jul 5, 2024
1 parent 196ef33 commit f4f10ad
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 43 deletions.
34 changes: 18 additions & 16 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ struct req_context {
req_content_type content_type = req_content_type::none;
std::string req_header; /*header string*/
String content; /*body*/
coro_io::coro_file *resp_body_stream = nullptr;
coro_io::coro_file0 *resp_body_stream = nullptr;
};

struct multipart_t {
Expand Down Expand Up @@ -777,8 +777,8 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
std::string filename,
std::string range = "") {
resp_data data{};
coro_io::coro_file file;
co_await file.async_open(filename, coro_io::flags::create_write);
coro_io::coro_file0 file;
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 @@ -852,9 +852,9 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
std::string 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) {
coro_io::coro_file0 file{};
file.open(source, std::ios::in);
if (!file.is_open()) {
ec = std::make_error_code(std::errc::bad_file_descriptor);
co_return;
}
Expand All @@ -879,9 +879,9 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
}
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) {
coro_io::coro_file0 file{};
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 @@ -2140,8 +2142,8 @@ 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);
coro_io::coro_file0 file{};
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
20 changes: 16 additions & 4 deletions include/cinatra/coro_http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ class coro_http_server {
std::string content;
detail::resize(content, chunked_size_);

coro_io::coro_file in_file{};
co_await in_file.async_open(file_name, coro_io::flags::read_only);
coro_io::coro_file0 in_file{};
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
13 changes: 6 additions & 7 deletions lang/coroutine_based_http_lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ coro_http_client client{};
std::string filename = "test.txt";
create_file(filename, 1010);

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

std::string buf;
detail::resize(buf, 100);
Expand Down Expand Up @@ -536,10 +536,10 @@ async_simple::coro::Lazy<void> byte_ranges_download() {
std::cout << part_head.name << "\n";
std::cout << part_head.filename << "\n";

std::shared_ptr<coro_io::coro_file> file;
std::shared_ptr<coro_io::coro_file0> file;
std::string filename;
if (!part_head.filename.empty()) {
file = std::make_shared<coro_io::coro_file>();
file = std::make_shared<coro_io::coro_file0>();
filename = std::to_string(
std::chrono::system_clock::now().time_since_epoch().count());

Expand All @@ -550,7 +550,7 @@ async_simple::coro::Lazy<void> byte_ranges_download() {
}

std::cout << filename << "\n";
co_await file->async_open(filename, coro_io::flags::create_write);
file->open(filename, std::ios::trunc|std::ios::out);
if (!file->is_open()) {
resp.set_status_and_content(status_type::internal_server_error,
"file open failed");
Expand All @@ -564,8 +564,7 @@ async_simple::coro::Lazy<void> byte_ranges_download() {
}

if (!filename.empty()) {
auto ec = co_await file->async_write(part_body.data.data(),
part_body.data.size());
auto ec = co_await file->async_write(part_body.data);
if (ec) {
co_return;
}
Expand Down
25 changes: 11 additions & 14 deletions tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,10 +862,10 @@ TEST_CASE("test upload file") {
std::cout << part_head.name << "\n";
std::cout << part_head.filename << "\n";

std::shared_ptr<coro_io::coro_file> file;
std::shared_ptr<coro_io::coro_file0> file;
std::string filename;
if (!part_head.filename.empty()) {
file = std::make_shared<coro_io::coro_file>();
file = std::make_shared<coro_io::coro_file0>();
filename = std::to_string(
std::chrono::system_clock::now().time_since_epoch().count());

Expand All @@ -876,7 +876,7 @@ TEST_CASE("test upload file") {
}

std::cout << filename << "\n";
co_await file->async_open(filename, coro_io::flags::create_write);
file->open(filename, std::ios::trunc | std::ios::out);
if (!file->is_open()) {
resp.set_status_and_content(status_type::internal_server_error,
"file open failed");
Expand All @@ -890,8 +890,7 @@ TEST_CASE("test upload file") {
}

if (!filename.empty()) {
auto ec = co_await file->async_write(part_body.data.data(),
part_body.data.size());
auto [ec, sz] = co_await file->async_write(part_body.data);
if (ec) {
co_return;
}
Expand Down Expand Up @@ -1072,10 +1071,10 @@ TEST_CASE("test coro_http_client multipart upload") {
std::cout << part_head.name << "\n";
std::cout << part_head.filename << "\n";

std::shared_ptr<coro_io::coro_file> file;
std::shared_ptr<coro_io::coro_file0> file;
std::string filename;
if (!part_head.filename.empty()) {
file = std::make_shared<coro_io::coro_file>();
file = std::make_shared<coro_io::coro_file0>();
filename = std::to_string(
std::chrono::system_clock::now().time_since_epoch().count());

Expand All @@ -1086,7 +1085,7 @@ TEST_CASE("test coro_http_client multipart upload") {
}

std::cout << filename << "\n";
co_await file->async_open(filename, coro_io::flags::create_write);
file->open(filename, std::ios::trunc | std::ios::out);
if (!file->is_open()) {
resp.set_status_and_content(status_type::internal_server_error,
"file open failed");
Expand All @@ -1100,8 +1099,7 @@ TEST_CASE("test coro_http_client multipart upload") {
}

if (!filename.empty()) {
auto ec = co_await file->async_write(part_body.data.data(),
part_body.data.size());
auto [ec, sz] = co_await file->async_write(part_body.data);
if (ec) {
co_return;
}
Expand Down Expand Up @@ -1184,10 +1182,9 @@ TEST_CASE("test coro_http_client upload") {
if (r_size != SIZE_MAX)
client.add_header("filesize", std::to_string(r_size));
std::string uri = "http://127.0.0.1:8090/upload";
coro_io::coro_file file;
auto res = async_simple::coro::syncAwait(
file.async_open(filename, coro_io::flags::read_only));
CHECK(res);
coro_io::coro_file0 file;
file.open(filename, std::ios::in);
CHECK(file.is_open());
std::string buf;
buf.resize(1'000'000);
auto async_read =
Expand Down
4 changes: 2 additions & 2 deletions tests/test_coro_http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,8 @@ async_simple::coro::Lazy<resp_data> chunked_upload1(coro_http_client &client) {
std::string filename = "test.txt";
create_file(filename, 1010);

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

std::string buf;
detail::resize(buf, 100);
Expand Down
3 changes: 3 additions & 0 deletions tests/test_corofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ async_simple::coro::Lazy<void> read_seek(std::string filename) {
CHECK(pair.second == 5);
CHECK(file.eof());
}

bool ok = file.seek(100, std::ios::beg);
CHECK(!ok);
}

async_simple::coro::Lazy<void> write_seek(std::string filename) {
Expand Down

0 comments on commit f4f10ad

Please sign in to comment.