Skip to content

Commit

Permalink
correct close client when sendfile error
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle committed Jul 9, 2024
1 parent 0c88765 commit 1a5a218
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
5 changes: 4 additions & 1 deletion include/ylt/coro_io/coro_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <async_simple/coro/Sleep.h>
#include <async_simple/coro/SyncAwait.h>

#include "asio/error.hpp"

#if defined(YLT_ENABLE_SSL) || defined(CINATRA_ENABLE_SSL)
#include <asio/ssl.hpp>
#endif
Expand Down Expand Up @@ -513,6 +515,8 @@ inline auto pipe_signal_handler = [] {
return 0;
}();

// FIXME: this function may not thread-safe if it not running in socket's
// executor
inline async_simple::coro::Lazy<std::pair<std::error_code, std::size_t>>
async_sendfile(asio::ip::tcp::socket &socket, int fd, off_t offset,
size_t size) noexcept {
Expand Down Expand Up @@ -548,7 +552,6 @@ async_sendfile(asio::ip::tcp::socket &socket, int fd, off_t offset,
handler.set_value_then_resume(ec);
});
});
continue;
}
if (ec || n == 0 || least_bytes == 0) [[unlikely]] { // End of File
break;
Expand Down
37 changes: 16 additions & 21 deletions include/ylt/standalone/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,13 +893,11 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
file_data.data(), std::min(file_data.size(), length));
ec) {
// bad request, file may smaller than content-length
close();
break;
}
length -= size;
if (length > 0 && file.eof()) {
// bad request, file may smaller than content-length
close();
ec = std::make_error_code(std::errc::invalid_argument);
break;
}
Expand Down Expand Up @@ -936,7 +934,6 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
}
if (actual_len != length) [[unlikely]] {
// bad request, file is smaller than content-length
close();
ec = std::make_error_code(std::errc::invalid_argument);
co_return;
}
Expand Down Expand Up @@ -972,7 +969,6 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
}
if (actual_len != len) [[unlikely]] {
// bad request, file is smaller than content-length
close();
ec = std::make_error_code(std::errc::invalid_argument);
co_return;
}
Expand Down Expand Up @@ -1013,14 +1009,19 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
int64_t content_length = -1,
req_content_type content_type = req_content_type::text,
std::unordered_map<std::string, std::string> headers = {}) {
std::shared_ptr<int> guard(nullptr, [this](auto) {
std::error_code ec{};
size_t size = 0;
bool is_keep_alive = true;
req_context<> ctx{content_type};
resp_data data{};

std::shared_ptr<void> guard(nullptr, [&, this](auto) {
if (!req_headers_.empty()) {
req_headers_.clear();
}
handle_result(data, ec, is_keep_alive);
});

req_context<> ctx{content_type};
resp_data data{};
auto [ok, u] = handle_uri(data, uri);
if (!ok) {
co_return resp_data{std::make_error_code(std::errc::protocol_error), 404};
Expand Down Expand Up @@ -1064,9 +1065,6 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
std::string header_str =
build_request_header(u, method, ctx, true, std::move(headers));

std::error_code ec{};
size_t size = 0;

if (socket_->has_closed_) {
{
auto guard = timer_guard(this, conn_timeout_duration_, "connect timer");
Expand Down Expand Up @@ -1109,7 +1107,6 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
if (!ec && content_length > 0) {
// bad request, file is smaller than content-length
ec = std::make_error_code(std::errc::invalid_argument);
close();
}
}
else if constexpr (std::is_same_v<Source, std::string> ||
Expand Down Expand Up @@ -1147,7 +1144,6 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
else if (result.eof) [[unlikely]] {
// bad request, file is smaller than content-length
ec = std::make_error_code(std::errc::invalid_argument);
close();
break;
}
}
Expand All @@ -1159,13 +1155,11 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
co_return resp_data{ec, 404};
}

bool is_keep_alive = true;
data = co_await handle_read(ec, size, is_keep_alive, std::move(ctx),
http_method::POST);
if (ec && socket_->is_timeout_) {
ec = std::make_error_code(std::errc::timed_out);
}
handle_result(data, ec, is_keep_alive);
co_return data;
}

Expand All @@ -1174,18 +1168,23 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
S uri, http_method method, Source source,
req_content_type content_type = req_content_type::text,
std::unordered_map<std::string, std::string> headers = {}) {
std::shared_ptr<int> guard(nullptr, [this](auto) {
req_context<> ctx{content_type};
resp_data data{};
std::error_code ec{};
size_t size = 0;
bool is_keep_alive = true;

std::shared_ptr<void> guard(nullptr, [&, this](auto) {
if (!req_headers_.empty()) {
req_headers_.clear();
}
handle_result(data, ec, is_keep_alive);
});

if (!resp_chunk_str_.empty()) {
resp_chunk_str_.clear();
}

req_context<> ctx{content_type};
resp_data data{};
auto [ok, u] = handle_uri(data, uri);
if (!ok) {
co_return resp_data{std::make_error_code(std::errc::protocol_error), 404};
Expand Down Expand Up @@ -1216,9 +1215,6 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
std::string header_str =
build_request_header(u, method, ctx, true, std::move(headers));

std::error_code ec{};
size_t size = 0;

if (socket_->has_closed_) {
{
auto guard = timer_guard(this, conn_timeout_duration_, "connect timer");
Expand Down Expand Up @@ -1296,7 +1292,6 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
co_return resp_data{ec, 404};
}

bool is_keep_alive = true;
data = co_await handle_read(ec, size, is_keep_alive, std::move(ctx),
http_method::POST);
if (ec && socket_->is_timeout_) {
Expand Down

0 comments on commit 1a5a218

Please sign in to comment.