From 6c90ddac8b3896d6faefa9d065c6fd0e33abeefb Mon Sep 17 00:00:00 2001 From: Djenad Razic Date: Wed, 12 Feb 2025 18:06:40 +0100 Subject: [PATCH 1/3] - Fix for the OPTIONS route not adding Access-Control-Allow-Origin header --- include/crow/middlewares/cors.h | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/include/crow/middlewares/cors.h b/include/crow/middlewares/cors.h index 15e7d4213..f490a6209 100644 --- a/include/crow/middlewares/cors.h +++ b/include/crow/middlewares/cors.h @@ -130,16 +130,13 @@ namespace crow bool origin_set = false; - if (req.method != HTTPMethod::Options) + if (allow_credentials_) { - if (allow_credentials_) + set_header_no_override("Access-Control-Allow-Credentials", "true", res); + if (origin_ == "*") { - set_header_no_override("Access-Control-Allow-Credentials", "true", res); - if (origin_ == "*") - { - set_header_no_override("Access-Control-Allow-Origin", req.get_header_value("Origin"), res); - origin_set = true; - } + set_header_no_override("Access-Control-Allow-Origin", req.get_header_value("Origin"), res); + origin_set = true; } } From 823bd372875b0d443131775310031a39ba3c470c Mon Sep 17 00:00:00 2001 From: Djenad Razic Date: Wed, 12 Feb 2025 22:07:54 +0100 Subject: [PATCH 2/3] - Fix for the headers not processed with OPTION request, and not passed to the middleware --- include/crow/http_connection.h | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/include/crow/http_connection.h b/include/crow/http_connection.h index edc90bcc5..ff432fe74 100644 --- a/include/crow/http_connection.h +++ b/include/crow/http_connection.h @@ -113,9 +113,16 @@ namespace crow // if no route is found for the request method, return the response without parsing or processing anything further. if (!routing_handle_result_->rule_index) { - parser_.done(); - need_to_call_after_handlers_ = true; - complete_request(); + if (req_.method == HTTPMethod::Options) + { + need_to_call_after_handlers_ = true; + } + else + { + parser_.done(); + need_to_call_after_handlers_ = true; // Not sure if the order of this and the above line matters + complete_request(); + } } } @@ -180,7 +187,11 @@ namespace crow CROW_LOG_INFO << "Request: " << utility::lexical_cast(adaptor_.remote_endpoint()) << " " << this << " HTTP/" << (char)(req_.http_ver_major + '0') << "." << (char)(req_.http_ver_minor + '0') << ' ' << method_name(req_.method) << " " << req_.url; - need_to_call_after_handlers_ = false; + if (req_.method != HTTPMethod::Options) + { + need_to_call_after_handlers_ = false; + } + if (!is_invalid_request) { res.complete_request_handler_ = nullptr; From 3ec1779317d594aed7fcc21b8709418c4b9a687a Mon Sep 17 00:00:00 2001 From: gittiver Date: Wed, 16 Apr 2025 00:12:46 +0200 Subject: [PATCH 3/3] Update http_connection.h