Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch return code of OPTIONS request method between 200 and 204 depending on cmake option #951

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -498,7 +498,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 @@ -521,7 +525,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 @@ -533,7 +541,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
Loading