Skip to content

Commit

Permalink
based on CMake option the return code for OPTION request method is sw…
Browse files Browse the repository at this point in the history
…itched between 200 (OK) and 204 (no content)
  • Loading branch information
gittiver committed Dec 30, 2024
1 parent 856f92a commit 4f66ac0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions include/crow/routing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions tests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand All @@ -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"));
}

Expand All @@ -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
Expand Down

0 comments on commit 4f66ac0

Please sign in to comment.