From 41d4bd79f0ff4c78d716e09fac893fcbaf89b2b3 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaytsev Date: Thu, 9 Jan 2025 18:37:14 +0300 Subject: [PATCH] Writing to socket synchronously --- include/crow/http_connection.h | 4 +-- tests/unittest.cpp | 63 ++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/include/crow/http_connection.h b/include/crow/http_connection.h index 04909453e..76c82f014 100644 --- a/include/crow/http_connection.h +++ b/include/crow/http_connection.h @@ -128,7 +128,7 @@ namespace crow buffers_.clear(); static std::string expect_100_continue = "HTTP/1.1 100 Continue\r\n\r\n"; buffers_.emplace_back(expect_100_continue.data(), expect_100_continue.size()); - do_write(); + do_write_sync(buffers_); } } @@ -427,7 +427,7 @@ namespace crow res_body_copy_.swap(res.body); buffers_.emplace_back(res_body_copy_.data(), res_body_copy_.size()); - do_write(); + do_write_sync(buffers_); if (need_to_start_read_after_complete_) { diff --git a/tests/unittest.cpp b/tests/unittest.cpp index 2d3945dd2..fd9dd3854 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -2,6 +2,7 @@ #define CROW_LOG_LEVEL 0 #include +#include #include #include #include @@ -3942,3 +3943,65 @@ TEST_CASE("http2_upgrade_is_ignored") CHECK(res.find("http2 upgrade is not supported so body is parsed") != std::string::npos); app.stop(); } + +TEST_CASE("option_header_passed_in_full") +{ + const std::string ServerName = "AN_EXTREMELY_UNIQUE_SERVER_NAME"; + + crow::App + app; + + app.get_middleware() // + .global() + .allow_credentials() + .expose("X-Total-Pages", "X-Total-Entries", "Content-Disposition"); + + app.server_name(ServerName); + + + CROW_ROUTE(app, "/echo").methods(crow::HTTPMethod::Options)([]() { + crow::response response{}; + response.add_header("Key", "Value"); + return response; + }); + + auto _ = app.bindaddr(LOCALHOST_ADDRESS).port(45451).run_async(); + + app.wait_for_server_start(); + + asio::io_service is; + + auto make_request = [&](const std::string& rq) { + asio::ip::tcp::socket c(is); + c.connect(asio::ip::tcp::endpoint( + asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); + c.send(asio::buffer(rq)); + std::string fullString{}; + asio::error_code error; + char buffer[1024]; + while (true) + { + size_t len = c.read_some(asio::buffer(buffer), error); + + if (error == asio::error::eof) + { + break; + } + else if (error) + { + throw system_error(error); + } + + fullString.append(buffer, len); + } + c.close(); + return fullString; + }; + + std::string request = + "OPTIONS /echo HTTP/1.1\r\n"; + + auto res = make_request(request); + CHECK(res.find(ServerName) != std::string::npos); + app.stop(); +}