From b20063dcf630eb4c28571b4f3933fea1923104c1 Mon Sep 17 00:00:00 2001 From: DavidPetkovsek Date: Wed, 16 Jul 2025 18:27:01 -0400 Subject: [PATCH 1/5] Handle headers for HTTPMethod::Options --- include/crow/http_connection.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/include/crow/http_connection.h b/include/crow/http_connection.h index f7f52beb4..96aa11ccd 100644 --- a/include/crow/http_connection.h +++ b/include/crow/http_connection.h @@ -113,7 +113,7 @@ namespace crow { routing_handle_result_ = handler_->handle_initial(req_, res); // if no route is found for the request method, return the response without parsing or processing anything further. - if (!routing_handle_result_->rule_index) + if (!routing_handle_result_->rule_index && req_.method != HTTPMethod::Options) { parser_.done(); need_to_call_after_handlers_ = true; @@ -132,6 +132,12 @@ namespace crow buffers_.emplace_back(expect_100_continue.data(), expect_100_continue.size()); do_write_sync(buffers_); } + if (!routing_handle_result_->rule_index && req_.method == HTTPMethod::Options) + { + parser_.done(); + need_to_call_after_handlers_ = true; + complete_request(); + } } void handle() From f6254ed826f68c42a17cfaefc1943b9227491a8a Mon Sep 17 00:00:00 2001 From: DavidPetkovsek Date: Wed, 16 Jul 2025 18:56:19 -0400 Subject: [PATCH 2/5] Prevent memory leak from Options requests trying to upgrade into websockets for websocket endpoints --- include/crow/http_connection.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/crow/http_connection.h b/include/crow/http_connection.h index 96aa11ccd..b09b07892 100644 --- a/include/crow/http_connection.h +++ b/include/crow/http_connection.h @@ -163,7 +163,7 @@ namespace crow is_invalid_request = true; res = response(400); } - else if (req_.upgrade) + else if (req_.upgrade && req_.method != HTTPMethod::Options) { // h2 or h2c headers if (req_.get_header_value("upgrade").find("h2")==0) From ed38db916ba6fb9874d00f0e6545bf869d488d83 Mon Sep 17 00:00:00 2001 From: DavidPetkovsek Date: Sun, 27 Jul 2025 15:58:50 -0400 Subject: [PATCH 3/5] Fix erroneous unit test --- tests/unittest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unittest.cpp b/tests/unittest.cpp index 52a7ca3f2..5095123ca 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -2045,7 +2045,7 @@ TEST_CASE("middleware_cors") CHECK(resp.find("Access-Control-Allow-Credentials: true") != std::string::npos); resp = HttpClient::request(LOCALHOST_ADDRESS, port, - "OPTIONS /auth-origin / HTTP/1.1 \r\n\r\n"); + "OPTIONS /auth-origin HTTP/1.1\r\n\r\n"); CHECK(resp.find("Access-Control-Allow-Origin: *") != std::string::npos); CHECK(resp.find("Access-Control-Allow-Credentials: true") == std::string::npos); @@ -2289,7 +2289,7 @@ TEST_CASE("simple_url_params") // check multiple value, multiple types HttpClient::request(LOCALHOST_ADDRESS, 45451, "GET /params?int=100&double=123.45&boolean=1\r\n\r\n"); - + CHECK(utility::lexical_cast(last_url_params.get("int")) == 100); REQUIRE_THAT(123.45, Catch::Matchers::WithinAbs(utility::lexical_cast(last_url_params.get("double")), 1e-9)); CHECK(utility::lexical_cast(last_url_params.get("boolean"))); From 7faa008b242faf42733be08ad57ad171d107b1f4 Mon Sep 17 00:00:00 2001 From: DavidPetkovsek Date: Sun, 27 Jul 2025 18:53:55 -0400 Subject: [PATCH 4/5] Fix erroneous unit test 2 --- tests/unittest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unittest.cpp b/tests/unittest.cpp index 5095123ca..f45db0d31 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -4171,7 +4171,7 @@ TEST_CASE("option_header_passed_in_full") }; std::string request = - "OPTIONS /echo HTTP/1.1\r\n"; + "OPTIONS /echo HTTP/1.1\r\n\r\n"; auto res = make_request(request); CHECK(res.find(ServerName) != std::string::npos); From 22fba82ac3c422642d8df6cc09f63f7e3a48fab5 Mon Sep 17 00:00:00 2001 From: DavidPetkovsek Date: Sun, 27 Jul 2025 18:54:28 -0400 Subject: [PATCH 5/5] Exit early for requests with no route for optional too --- include/crow/http_connection.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/crow/http_connection.h b/include/crow/http_connection.h index b09b07892..dfd71b81b 100644 --- a/include/crow/http_connection.h +++ b/include/crow/http_connection.h @@ -113,7 +113,7 @@ namespace crow { routing_handle_result_ = handler_->handle_initial(req_, res); // if no route is found for the request method, return the response without parsing or processing anything further. - if (!routing_handle_result_->rule_index && req_.method != HTTPMethod::Options) + if (!routing_handle_result_->rule_index && (req_.method != HTTPMethod::Options || routing_handle_result_->method == HTTPMethod::InternalMethodCount)) { parser_.done(); need_to_call_after_handlers_ = true;