Skip to content

Commit

Permalink
Writing to socket synchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Zaytsev committed Jan 9, 2025
1 parent 77eda0d commit 41d4bd7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/crow/http_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
}
}

Expand Down Expand Up @@ -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_)
{
Expand Down
63 changes: 63 additions & 0 deletions tests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CROW_LOG_LEVEL 0
#include <sys/stat.h>

#include <exception>
#include <iostream>
#include <vector>
#include <thread>
Expand Down Expand Up @@ -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<crow::CORSHandler>
app;

app.get_middleware<crow::CORSHandler>() //
.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();
}

0 comments on commit 41d4bd7

Please sign in to comment.