From 52db412e1bc34cfce50fa92769c175a5f5f42267 Mon Sep 17 00:00:00 2001 From: Gulliver Date: Tue, 26 Nov 2024 11:39:20 +0100 Subject: [PATCH] based on CMake option the return code for OPTION request method is switched between 200 (OK) and 204 (no content) --- CMakeLists.txt | 3 +++ include/crow/routing.h | 13 +++++++++++-- tests/unittest.cpp | 12 ++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 94a23336b..db2311c15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,9 @@ option(CROW_BUILD_FUZZER "Instrument and build Crow fuzzer" OFF) option(CROW_AMALGAMATE "Combine all headers into one" OFF) option(CROW_INSTALL "Add install step for Crow" ON ) option(CROW_USE_BOOST "Use Boost.Asio for Crow" OFF) +option( CROW_RETURNS_OK_ON_HTTP_OPTIONS_REQUEST + "Returns HTTP status code OK (200) instead of 204 for OPTIONS request" + OFF ) option(CROW_ENABLE_SSL "Enable Crow's SSL feature for supporting https" OFF) option(CROW_ENABLE_COMPRESSION "Enable Crow's Compression feature for supporting compressed http content" OFF) diff --git a/include/crow/routing.h b/include/crow/routing.h index 18bd7cf2b..1cd1daf31 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -1619,7 +1619,12 @@ namespace crow // NOTE: Already documented in "crow/app.h" } } allow = allow.substr(0, allow.size() - 2); - res = response(204); +#ifdef CROW_RETURNS_OK_ON_HTTP_OPTIONS_REQUEST + res = response(crow::status::OK); +#else + res = response(crow::status::NO_CONTENT); +#endif + res.set_header("Allow", allow); res.end(); found->method = method_actual; @@ -1642,8 +1647,12 @@ namespace crow // NOTE: Already documented in "crow/app.h" } if (rules_matched) { +#ifdef CROW_RETURNS_OK_ON_HTTP_OPTIONS_REQUEST + res = response(crow::status::OK); +#else + res = response(crow::status::NO_CONTENT); +#endif allow = allow.substr(0, allow.size() - 2); - res = response(204); res.set_header("Allow", allow); res.end(); found->method = method_actual; diff --git a/tests/unittest.cpp b/tests/unittest.cpp index a029c514f..b7db523a4 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -548,7 +548,11 @@ TEST_CASE("http_method") req.method = "OPTIONS"_method; app.handle_full(req, res); +#ifdef CROW_RETURNS_OK_ON_HTTP_OPTIONS_REQUEST + CHECK(200 == res.code); +#else CHECK(204 == res.code); +#endif CHECK("OPTIONS, HEAD, GET, POST" == res.get_header_value("Allow")); } @@ -571,7 +575,11 @@ TEST_CASE("http_method") req.method = "OPTIONS"_method; app.handle_full(req, res); +#ifdef CROW_RETURNS_OK_ON_HTTP_OPTIONS_REQUEST + CHECK(200 == res.code); +#else CHECK(204 == res.code); +#endif CHECK("OPTIONS, HEAD, GET, POST, PATCH, PURGE" == res.get_header_value("Allow")); } @@ -583,7 +591,11 @@ TEST_CASE("http_method") req.method = "OPTIONS"_method; app.handle_full(req, res); +#ifdef CROW_RETURNS_OK_ON_HTTP_OPTIONS_REQUEST + CHECK(200 == res.code); +#else CHECK(204 == res.code); +#endif CHECK("OPTIONS, HEAD" == res.get_header_value("Allow")); } } // http_method