diff --git a/include/crow/http_connection.h b/include/crow/http_connection.h index f7f52beb4..dfd71b81b 100644 --- a/include/crow/http_connection.h +++ b/include/crow/http_connection.h @@ -113,7 +113,7 @@ namespace crow { routing_handle_result_ = handler_->handle_initial(req_, res); // if no route is found for the request method, return the response without parsing or processing anything further. - if (!routing_handle_result_->rule_index) + if (!routing_handle_result_->rule_index && (req_.method != HTTPMethod::Options || routing_handle_result_->method == HTTPMethod::InternalMethodCount)) { parser_.done(); need_to_call_after_handlers_ = true; @@ -132,6 +132,12 @@ namespace crow buffers_.emplace_back(expect_100_continue.data(), expect_100_continue.size()); do_write_sync(buffers_); } + if (!routing_handle_result_->rule_index && req_.method == HTTPMethod::Options) + { + parser_.done(); + need_to_call_after_handlers_ = true; + complete_request(); + } } void handle() @@ -157,7 +163,7 @@ namespace crow is_invalid_request = true; res = response(400); } - else if (req_.upgrade) + else if (req_.upgrade && req_.method != HTTPMethod::Options) { // h2 or h2c headers if (req_.get_header_value("upgrade").find("h2")==0) diff --git a/tests/unittest.cpp b/tests/unittest.cpp index 52a7ca3f2..f45db0d31 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -2045,7 +2045,7 @@ TEST_CASE("middleware_cors") CHECK(resp.find("Access-Control-Allow-Credentials: true") != std::string::npos); resp = HttpClient::request(LOCALHOST_ADDRESS, port, - "OPTIONS /auth-origin / HTTP/1.1 \r\n\r\n"); + "OPTIONS /auth-origin HTTP/1.1\r\n\r\n"); CHECK(resp.find("Access-Control-Allow-Origin: *") != std::string::npos); CHECK(resp.find("Access-Control-Allow-Credentials: true") == std::string::npos); @@ -2289,7 +2289,7 @@ TEST_CASE("simple_url_params") // check multiple value, multiple types HttpClient::request(LOCALHOST_ADDRESS, 45451, "GET /params?int=100&double=123.45&boolean=1\r\n\r\n"); - + CHECK(utility::lexical_cast(last_url_params.get("int")) == 100); REQUIRE_THAT(123.45, Catch::Matchers::WithinAbs(utility::lexical_cast(last_url_params.get("double")), 1e-9)); CHECK(utility::lexical_cast(last_url_params.get("boolean"))); @@ -4171,7 +4171,7 @@ TEST_CASE("option_header_passed_in_full") }; std::string request = - "OPTIONS /echo HTTP/1.1\r\n"; + "OPTIONS /echo HTTP/1.1\r\n\r\n"; auto res = make_request(request); CHECK(res.find(ServerName) != std::string::npos);