Skip to content

Commit

Permalink
support earlier return (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Dec 13, 2024
1 parent 01fe5b3 commit 9c27357
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
17 changes: 8 additions & 9 deletions include/cinatra/coro_http_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,9 @@ class coro_http_connection
}
}
// not found
if (!is_matched_regex_router)
if (!is_matched_regex_router) {
response_.set_status(status_type::not_found);
}
}
}
}
Expand All @@ -305,10 +306,12 @@ class coro_http_connection

if (!response_.get_delay()) {
if (head_buf_.size()) {
if (type == content_type::multipart) {
response_.set_status_and_content(
status_type::not_implemented,
"mutipart handler not implemented or incorrect implemented");
if (type == content_type::multipart ||
type == content_type::chunked) {
if (response_.content().empty())
response_.set_status_and_content(
status_type::not_implemented,
"mutipart handler not implemented or incorrect implemented");
co_await reply();
close();
CINATRA_LOG_ERROR
Expand Down Expand Up @@ -405,10 +408,6 @@ class coro_http_connection
if (need_to_bufffer) {
response_.to_buffers(buffers_, chunk_size_str_);
}
int64_t send_size = 0;
for (auto &buf : buffers_) {
send_size += buf.size();
}
std::tie(ec, size) = co_await async_write(buffers_);
}
else {
Expand Down
2 changes: 2 additions & 0 deletions include/cinatra/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class session {
return std::nullopt;
}

const auto &get_all_data() const { return data_; }

const std::string &get_session_id() {
std::unique_lock<std::mutex> lock(mtx_);
return session_id_;
Expand Down
53 changes: 53 additions & 0 deletions tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,9 @@ TEST_CASE("test pipeline") {
coro_http_server server(1, 9001);
server.set_http_handler<GET, POST>(
"/test", [](coro_http_request &req, coro_http_response &res) {
if (req.get_content_type() == content_type::multipart) {
return;
}
res.set_status_and_content(status_type::ok, "hello world");
});
server.set_http_handler<GET, POST>(
Expand Down Expand Up @@ -2313,6 +2316,54 @@ TEST_CASE("test coro_http_client chunked upload and download") {
}
}

TEST_CASE("test multipart and chunked return error") {
coro_http_server server(1, 8090);
server.set_http_handler<cinatra::PUT, cinatra::POST>(
"/multipart",
[](request &req, response &resp) -> async_simple::coro::Lazy<void> {
resp.set_status_and_content(status_type::bad_request,
"invalid headers");
co_return;
});
server.set_http_handler<cinatra::PUT, cinatra::POST>(
"/chunked",
[](request &req, response &resp) -> async_simple::coro::Lazy<void> {
resp.set_status_and_content(status_type::bad_request,
"invalid headers");
co_return;
});
server.async_start();

std::string filename = "small_test_file.txt";
create_file(filename, 10);
{
coro_http_client client{};
std::string uri1 = "http://127.0.0.1:8090/chunked";
auto result = async_simple::coro::syncAwait(
client.async_upload_chunked(uri1, http_method::PUT, filename));
CHECK(result.resp_body == "invalid headers");
}

{
coro_http_client client{};
std::string uri2 = "http://127.0.0.1:8090/multipart";
client.add_str_part("test", "test value");
auto result =
async_simple::coro::syncAwait(client.async_upload_multipart(uri2));
CHECK(result.resp_body == "invalid headers");
}

{
coro_http_client client{};
std::string uri1 = "http://127.0.0.1:8090/no_such";
auto result = async_simple::coro::syncAwait(
client.async_upload_chunked(uri1, http_method::PUT, filename));
CHECK(result.status != 200);
}
std::error_code ec;
fs::remove(filename, ec);
}

TEST_CASE("test coro_http_client get") {
coro_http_client client{};
auto r = client.get("http://www.baidu.com");
Expand Down Expand Up @@ -3086,6 +3137,8 @@ TEST_CASE("test session") {
session_id_check_login = session->get_session_id();
bool login = session->get_data<bool>("login").value_or(false);
CHECK(login == true);
auto &all = session->get_all_data();
CHECK(all.size() > 0);
res.set_status(status_type::ok);
});
server.set_http_handler<GET>(
Expand Down

0 comments on commit 9c27357

Please sign in to comment.