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

fix sendfile #615

Merged
merged 2 commits into from
Jul 9, 2024
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
37 changes: 16 additions & 21 deletions include/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
32 changes: 25 additions & 7 deletions include/cinatra/ylt/coro_io/coro_io.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
/*
* Copyright (c) 2023, Alibaba Group Holding Limited;
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include <async_simple/Executor.h>
#include <async_simple/coro/Collect.h>
#include <async_simple/coro/Lazy.h>
#include <async_simple/coro/Sleep.h>
#include <async_simple/coro/SyncAwait.h>

#include "async_simple/coro/Collect.h"

#if defined(YLT_ENABLE_SSL) || defined(CINATRA_ENABLE_SSL)
#include <asio/ssl.hpp>
#endif

#include <async_simple/coro/Lazy.h>

#include <asio/connect.hpp>
#include <asio/experimental/channel.hpp>
#include <asio/ip/tcp.hpp>
Expand All @@ -23,8 +36,12 @@
#include <chrono>
#include <deque>

#include "../util/type_traits.h"
#include "io_context_pool.hpp"
#if __has_include("ylt/util/type_traits.h")
#include "ylt/util/type_traits.h"
#else
#include "../util/type_traits.h"
#endif
#ifdef __linux__
#include <sys/sendfile.h>
#endif
Expand Down Expand Up @@ -254,7 +271,7 @@ inline async_simple::coro::Lazy<void> async_close(Socket &socket) noexcept {
});
}

#if defined(ENABLE_SSL) || defined(CINATRA_ENABLE_SSL)
#if defined(YLT_ENABLE_SSL) || defined(CINATRA_ENABLE_SSL)
inline async_simple::coro::Lazy<std::error_code> async_handshake(
auto &ssl_stream, asio::ssl::stream_base::handshake_type type) noexcept {
callback_awaitor<std::error_code> awaitor;
Expand Down Expand Up @@ -524,6 +541,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 @@ -559,7 +578,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
Loading