Skip to content

Commit 1f1b7fe

Browse files
committed
based on CMake option the return code for OPTION request method is switched between 200 (OK) and 204 (no content)
1 parent 3c82cda commit 1f1b7fe

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ option(CROW_BUILD_FUZZER "Instrument and build Crow fuzzer" OFF)
4848
option(CROW_AMALGAMATE "Combine all headers into one" OFF)
4949
option(CROW_INSTALL "Add install step for Crow" ON )
5050
option(CROW_USE_BOOST "Use Boost.Asio for Crow" OFF)
51+
option( CROW_RETURNS_OK_ON_HTTP_OPTIONS_REQUEST
52+
"Returns HTTP status code OK (200) instead of 204 for OPTIONS request"
53+
OFF )
5154

5255
option(CROW_ENABLE_SSL "Enable Crow's SSL feature for supporting https" OFF)
5356
option(CROW_ENABLE_COMPRESSION "Enable Crow's Compression feature for supporting compressed http content" OFF)

include/crow/routing.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,7 +1596,12 @@ namespace crow // NOTE: Already documented in "crow/app.h"
15961596
allow.append(method_name(static_cast<HTTPMethod>(i)));
15971597
}
15981598
}
1599-
res = response(204);
1599+
#ifdef CROW_RETURNS_OK_ON_HTTP_OPTIONS_REQUEST
1600+
res = response(crow::status::OK);
1601+
#else
1602+
res = response(crow::status::NO_CONTENT);
1603+
#endif
1604+
16001605
res.set_header("Allow", allow);
16011606
res.end();
16021607
found->method = method_actual;
@@ -1620,7 +1625,11 @@ namespace crow // NOTE: Already documented in "crow/app.h"
16201625
}
16211626
if (rules_matched)
16221627
{
1623-
res = response(204);
1628+
#ifdef CROW_RETURNS_OK_ON_HTTP_OPTIONS_REQUEST
1629+
res = response(crow::status::OK);
1630+
#else
1631+
res = response(crow::status::NO_CONTENT);
1632+
#endif
16241633
res.set_header("Allow", allow);
16251634
res.end();
16261635
found->method = method_actual;

tests/unittest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,11 @@ TEST_CASE("http_method")
549549
req.method = "OPTIONS"_method;
550550
app.handle_full(req, res);
551551

552+
#ifdef CROW_RETURNS_OK_ON_HTTP_OPTIONS_REQUEST
553+
CHECK(200 == res.code);
554+
#else
552555
CHECK(204 == res.code);
556+
#endif
553557
CHECK("OPTIONS, HEAD, GET, POST" == res.get_header_value("Allow"));
554558
}
555559

@@ -572,7 +576,11 @@ TEST_CASE("http_method")
572576
req.method = "OPTIONS"_method;
573577
app.handle_full(req, res);
574578

579+
#ifdef CROW_RETURNS_OK_ON_HTTP_OPTIONS_REQUEST
580+
CHECK(200 == res.code);
581+
#else
575582
CHECK(204 == res.code);
583+
#endif
576584
CHECK("OPTIONS, HEAD, GET, POST, PATCH, PURGE" == res.get_header_value("Allow"));
577585
}
578586

@@ -584,7 +592,11 @@ TEST_CASE("http_method")
584592
req.method = "OPTIONS"_method;
585593
app.handle_full(req, res);
586594

595+
#ifdef CROW_RETURNS_OK_ON_HTTP_OPTIONS_REQUEST
596+
CHECK(200 == res.code);
597+
#else
587598
CHECK(204 == res.code);
599+
#endif
588600
CHECK("OPTIONS, HEAD" == res.get_header_value("Allow"));
589601
}
590602
} // http_method

0 commit comments

Comments
 (0)