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 some #454

Merged
merged 3 commits into from
Dec 12, 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
52 changes: 29 additions & 23 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,14 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {

auto [ok, u] = handle_uri(data, no_schema ? append_uri : uri);
if (!ok) {
co_return resp_data{{}, 404};
co_return resp_data{std::make_error_code(std::errc::protocol_error), 404};
}

auto future = start_timer(req_timeout_duration_, "connect timer");

data = co_await connect(u);
if (auto ec = co_await wait_timer(std::move(future)); ec) {
co_return resp_data{{}, 404};
co_return resp_data{ec, 404};
}
if (!data.net_err) {
data.status = 200;
Expand Down Expand Up @@ -659,14 +659,15 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
});
if (form_data_.empty()) {
CINATRA_LOG_WARNING << "no multipart";
co_return resp_data{{}, 404};
co_return resp_data{std::make_error_code(std::errc::invalid_argument),
404};
}

req_context<> ctx{req_content_type::multipart, "", ""};
resp_data data{};
auto [ok, u] = handle_uri(data, uri);
if (!ok) {
co_return resp_data{{}, 404};
co_return resp_data{std::make_error_code(std::errc::protocol_error), 404};
}

size_t content_len = multipart_content_len();
Expand All @@ -678,17 +679,19 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
std::error_code ec{};
size_t size = 0;

auto future = start_timer(req_timeout_duration_, "connect timer");
if (socket_->has_closed_) {
auto future = start_timer(req_timeout_duration_, "connect timer");

data = co_await connect(u);
if (ec = co_await wait_timer(std::move(future)); ec) {
co_return resp_data{{}, 404};
}
if (data.net_err) {
co_return data;
data = co_await connect(u);
if (ec = co_await wait_timer(std::move(future)); ec) {
co_return resp_data{ec, 404};
}
if (data.net_err) {
co_return data;
}
}

future = start_timer(req_timeout_duration_, "upload timer");
auto future = start_timer(req_timeout_duration_, "upload timer");
std::tie(ec, size) = co_await async_write(asio::buffer(header_str));
#ifdef INJECT_FOR_HTTP_CLIENT_TEST
if (inject_write_failed == ClientInjectAction::write_failed) {
Expand Down Expand Up @@ -733,7 +736,8 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
std::string uri, std::string name, std::string filename) {
if (!add_file_part(std::move(name), std::move(filename))) {
CINATRA_LOG_WARNING << "open file failed or duplicate test names";
co_return resp_data{{}, 404};
co_return resp_data{std::make_error_code(std::errc::invalid_argument),
404};
}
co_return co_await async_upload_multipart(std::move(uri));
}
Expand Down Expand Up @@ -832,7 +836,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
resp_data data{};
auto [ok, u] = handle_uri(data, uri);
if (!ok) {
co_return resp_data{{}, 404};
co_return resp_data{std::make_error_code(std::errc::protocol_error), 404};
}

constexpr bool is_stream_file = is_stream_ptr_v<Source>;
Expand Down Expand Up @@ -863,17 +867,19 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
std::error_code ec{};
size_t size = 0;

auto future = start_timer(req_timeout_duration_, "connect timer");
if (socket_->has_closed_) {
auto future = start_timer(req_timeout_duration_, "connect timer");

data = co_await connect(u);
if (ec = co_await wait_timer(std::move(future)); ec) {
co_return resp_data{{}, 404};
}
if (data.net_err) {
co_return data;
data = co_await connect(u);
if (ec = co_await wait_timer(std::move(future)); ec) {
co_return resp_data{ec, 404};
}
if (data.net_err) {
co_return data;
}
}

future = start_timer(req_timeout_duration_, "upload timer");
auto future = start_timer(req_timeout_duration_, "upload timer");
std::tie(ec, size) = co_await async_write(asio::buffer(header_str));
if (ec) {
co_return resp_data{ec, 404};
Expand Down Expand Up @@ -1322,7 +1328,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
co_return data;
}

bool is_ranges = parser_.is_ranges();
bool is_ranges = parser_.is_resp_ranges();
if (is_ranges) {
is_keep_alive = true;
}
Expand Down
4 changes: 3 additions & 1 deletion include/cinatra/coro_http_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class coro_http_request {
return is_chunk;
}

bool is_ranges() { return parser_.is_ranges(); }
bool is_resp_ranges() { return parser_.is_resp_ranges(); }

bool is_req_ranges() { return parser_.is_req_ranges(); }

content_type get_content_type() {
static content_type thread_local content_type = get_content_type_impl();
Expand Down
11 changes: 3 additions & 8 deletions include/cinatra/coro_http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,7 @@ class coro_http_server {
[this, file_name = file](
coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
bool is_chunked = req.is_chunked();
bool is_ranges = req.is_ranges();
if (!is_chunked && !is_ranges) {
resp.set_status(status_type::not_implemented);
co_return;
}
bool is_ranges = req.is_req_ranges();

std::string_view extension = get_extension(file_name);
std::string_view mime = get_mime_type(extension);
Expand All @@ -225,7 +220,7 @@ class coro_http_server {
co_return;
}

if (is_chunked) {
if (!is_ranges) {
resp.set_format_type(format_type::chunked);
bool ok;
if (ok = co_await resp.get_conn()->begin_chunked(); !ok) {
Expand Down Expand Up @@ -253,7 +248,7 @@ class coro_http_server {
}
}
}
else if (is_ranges) {
else {
auto range_header = build_range_header(
mime, file_name, coro_io::coro_file::file_size(file_name));
resp.set_delay(true);
Expand Down
11 changes: 8 additions & 3 deletions include/cinatra/http_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,14 @@ class http_parser {
return false;
}

bool is_ranges() const {
auto transfer_encoding = this->get_header_value("Range"sv);
return !transfer_encoding.empty();
bool is_req_ranges() const {
auto value = this->get_header_value("Range"sv);
return !value.empty();
}

bool is_resp_ranges() const {
auto value = this->get_header_value("Accept-Ranges"sv);
return !value.empty();
}

bool is_websocket() const {
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 @@ -949,11 +949,11 @@ TEST_CASE("test http download server") {
{
coro_http_client client{};
auto result = async_simple::coro::syncAwait(client.async_download(
"http://127.0.0.1:9001/download/test_download.txt", "download.txt",
"http://127.0.0.1:9001/download/test_download.txt", "download1.txt",
"0-"));

CHECK(result.status == 200);
std::string download_file = fs::absolute("download.txt").string();
std::string download_file = fs::absolute("download1.txt").string();
std::ifstream ifs(download_file, std::ios::binary);
std::string content((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
Expand Down
Loading