Skip to content

Commit 9c27357

Browse files
authored
support earlier return (#671)
1 parent 01fe5b3 commit 9c27357

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

include/cinatra/coro_http_connection.hpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,9 @@ class coro_http_connection
295295
}
296296
}
297297
// not found
298-
if (!is_matched_regex_router)
298+
if (!is_matched_regex_router) {
299299
response_.set_status(status_type::not_found);
300+
}
300301
}
301302
}
302303
}
@@ -305,10 +306,12 @@ class coro_http_connection
305306

306307
if (!response_.get_delay()) {
307308
if (head_buf_.size()) {
308-
if (type == content_type::multipart) {
309-
response_.set_status_and_content(
310-
status_type::not_implemented,
311-
"mutipart handler not implemented or incorrect implemented");
309+
if (type == content_type::multipart ||
310+
type == content_type::chunked) {
311+
if (response_.content().empty())
312+
response_.set_status_and_content(
313+
status_type::not_implemented,
314+
"mutipart handler not implemented or incorrect implemented");
312315
co_await reply();
313316
close();
314317
CINATRA_LOG_ERROR
@@ -405,10 +408,6 @@ class coro_http_connection
405408
if (need_to_bufffer) {
406409
response_.to_buffers(buffers_, chunk_size_str_);
407410
}
408-
int64_t send_size = 0;
409-
for (auto &buf : buffers_) {
410-
send_size += buf.size();
411-
}
412411
std::tie(ec, size) = co_await async_write(buffers_);
413412
}
414413
else {

include/cinatra/session.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class session {
6262
return std::nullopt;
6363
}
6464

65+
const auto &get_all_data() const { return data_; }
66+
6567
const std::string &get_session_id() {
6668
std::unique_lock<std::mutex> lock(mtx_);
6769
return session_id_;

tests/test_cinatra.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,9 @@ TEST_CASE("test pipeline") {
738738
coro_http_server server(1, 9001);
739739
server.set_http_handler<GET, POST>(
740740
"/test", [](coro_http_request &req, coro_http_response &res) {
741+
if (req.get_content_type() == content_type::multipart) {
742+
return;
743+
}
741744
res.set_status_and_content(status_type::ok, "hello world");
742745
});
743746
server.set_http_handler<GET, POST>(
@@ -2313,6 +2316,54 @@ TEST_CASE("test coro_http_client chunked upload and download") {
23132316
}
23142317
}
23152318

2319+
TEST_CASE("test multipart and chunked return error") {
2320+
coro_http_server server(1, 8090);
2321+
server.set_http_handler<cinatra::PUT, cinatra::POST>(
2322+
"/multipart",
2323+
[](request &req, response &resp) -> async_simple::coro::Lazy<void> {
2324+
resp.set_status_and_content(status_type::bad_request,
2325+
"invalid headers");
2326+
co_return;
2327+
});
2328+
server.set_http_handler<cinatra::PUT, cinatra::POST>(
2329+
"/chunked",
2330+
[](request &req, response &resp) -> async_simple::coro::Lazy<void> {
2331+
resp.set_status_and_content(status_type::bad_request,
2332+
"invalid headers");
2333+
co_return;
2334+
});
2335+
server.async_start();
2336+
2337+
std::string filename = "small_test_file.txt";
2338+
create_file(filename, 10);
2339+
{
2340+
coro_http_client client{};
2341+
std::string uri1 = "http://127.0.0.1:8090/chunked";
2342+
auto result = async_simple::coro::syncAwait(
2343+
client.async_upload_chunked(uri1, http_method::PUT, filename));
2344+
CHECK(result.resp_body == "invalid headers");
2345+
}
2346+
2347+
{
2348+
coro_http_client client{};
2349+
std::string uri2 = "http://127.0.0.1:8090/multipart";
2350+
client.add_str_part("test", "test value");
2351+
auto result =
2352+
async_simple::coro::syncAwait(client.async_upload_multipart(uri2));
2353+
CHECK(result.resp_body == "invalid headers");
2354+
}
2355+
2356+
{
2357+
coro_http_client client{};
2358+
std::string uri1 = "http://127.0.0.1:8090/no_such";
2359+
auto result = async_simple::coro::syncAwait(
2360+
client.async_upload_chunked(uri1, http_method::PUT, filename));
2361+
CHECK(result.status != 200);
2362+
}
2363+
std::error_code ec;
2364+
fs::remove(filename, ec);
2365+
}
2366+
23162367
TEST_CASE("test coro_http_client get") {
23172368
coro_http_client client{};
23182369
auto r = client.get("http://www.baidu.com");
@@ -3086,6 +3137,8 @@ TEST_CASE("test session") {
30863137
session_id_check_login = session->get_session_id();
30873138
bool login = session->get_data<bool>("login").value_or(false);
30883139
CHECK(login == true);
3140+
auto &all = session->get_all_data();
3141+
CHECK(all.size() > 0);
30893142
res.set_status(status_type::ok);
30903143
});
30913144
server.set_http_handler<GET>(

0 commit comments

Comments
 (0)