diff --git a/include/crow/http_connection.h b/include/crow/http_connection.h index fe748ec93..2ebf72a95 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 && !routing_handle_result_->catch_all) + if (!routing_handle_result_->rule_index && !routing_handle_result_->catch_all && (req_.method != HTTPMethod::Options || routing_handle_result_->method == HTTPMethod::InternalMethodCount)) { parser_.done(); need_to_call_after_handlers_ = true; @@ -136,6 +136,12 @@ namespace crow CROW_LOG_ERROR << ec << " buffer write error happened while handling sending continuation buffer header"; } } + if (!routing_handle_result_->rule_index && !routing_handle_result_->catch_all && req_.method == HTTPMethod::Options) + { + parser_.done(); + need_to_call_after_handlers_ = true; + complete_request(); + } } void handle() @@ -161,7 +167,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 88f730894..19bd722d5 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -1291,7 +1291,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); @@ -2800,7 +2800,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);