From 047e9ff7e27e69e074d6ee067749e5c5a27bfa16 Mon Sep 17 00:00:00 2001 From: June Han Date: Wed, 21 Feb 2024 14:55:47 +0900 Subject: [PATCH] fix: call after_handlers for legitimate requests without body part --- include/crow/http_connection.h | 1 + tests/unittest.cpp | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/include/crow/http_connection.h b/include/crow/http_connection.h index c059ce8d9..8142d1707 100644 --- a/include/crow/http_connection.h +++ b/include/crow/http_connection.h @@ -103,6 +103,7 @@ namespace crow if (!routing_handle_result_->rule_index) { parser_.done(); + need_to_call_after_handlers_ = true; complete_request(); } } diff --git a/tests/unittest.cpp b/tests/unittest.cpp index 06345cecc..2e9028d62 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -1869,7 +1869,8 @@ TEST_CASE("middleware_cors") return "-"; }); - auto _ = app.bindaddr(LOCALHOST_ADDRESS).port(45451).run_async(); + const auto port = 33333; + auto _ = app.bindaddr(LOCALHOST_ADDRESS).port(port).run_async(); app.wait_for_server_start(); asio::io_service is; @@ -1877,7 +1878,20 @@ TEST_CASE("middleware_cors") { asio::ip::tcp::socket c(is); c.connect(asio::ip::tcp::endpoint( - asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); + asio::ip::address::from_string(LOCALHOST_ADDRESS), port)); + + c.send(asio::buffer("OPTIONS / HTTP/1.1\r\n\r\n")); + + c.receive(asio::buffer(buf, 2048)); + c.close(); + + CHECK(std::string(buf).find("Access-Control-Allow-Origin: *") != std::string::npos); + } + + { + asio::ip::tcp::socket c(is); + c.connect(asio::ip::tcp::endpoint( + asio::ip::address::from_string(LOCALHOST_ADDRESS), port)); c.send(asio::buffer("GET /\r\n\r\n")); @@ -1890,7 +1904,7 @@ TEST_CASE("middleware_cors") { asio::ip::tcp::socket c(is); c.connect(asio::ip::tcp::endpoint( - asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); + asio::ip::address::from_string(LOCALHOST_ADDRESS), port)); c.send(asio::buffer("GET /origin\r\n\r\n")); @@ -1903,7 +1917,7 @@ TEST_CASE("middleware_cors") { asio::ip::tcp::socket c(is); c.connect(asio::ip::tcp::endpoint( - asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); + asio::ip::address::from_string(LOCALHOST_ADDRESS), port)); c.send(asio::buffer("GET /nocors/path\r\n\r\n"));