From 4cba4eb3f122c7a1a49b629b1c0656d7f817f001 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Fri, 24 Oct 2025 19:32:01 +0200 Subject: move cpr in-tree (#605) * added cpr 1.10.5 in-tree to allow updates to vcpkg without breaking the build * added asio 1.29.0 in-tree to remove one more vcpkg dependency * bumped vcpkg to 2024.06.15 to address failure to build due to use of deprecated binaries in vcpkg (404 error: `https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-pkgconf-1~2.1.0-1-any.pkg.tar.zst` during build) --- thirdparty/cpr/test/httpsServer.cpp | 65 +++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 thirdparty/cpr/test/httpsServer.cpp (limited to 'thirdparty/cpr/test/httpsServer.cpp') diff --git a/thirdparty/cpr/test/httpsServer.cpp b/thirdparty/cpr/test/httpsServer.cpp new file mode 100644 index 000000000..401a47032 --- /dev/null +++ b/thirdparty/cpr/test/httpsServer.cpp @@ -0,0 +1,65 @@ +#include "httpsServer.hpp" +#include + +namespace cpr { +HttpsServer::HttpsServer(fs::path&& baseDirPath, fs::path&& sslCertFileName, fs::path&& sslKeyFileName) : baseDirPath(baseDirPath.make_preferred().string()), sslCertFileName(sslCertFileName.make_preferred().string()), sslKeyFileName(sslKeyFileName.make_preferred().string()) { + // See https://mongoose.ws/tutorials/tls/ + memset(static_cast(&tlsOpts), 0, sizeof(tlsOpts)); + tlsOpts.cert = this->sslCertFileName.c_str(); + tlsOpts.certkey = this->sslKeyFileName.c_str(); +} + +std::string HttpsServer::GetBaseUrl() { + return "https://127.0.0.1:" + std::to_string(GetPort()); +} + +uint16_t HttpsServer::GetPort() { + // Unassigned port in the ephemeral range + return 61937; +} + +mg_connection* HttpsServer::initServer(mg_mgr* mgr, mg_event_handler_t event_handler) { + mg_mgr_init(mgr); + + std::string port = std::to_string(GetPort()); + mg_connection* c = mg_http_listen(mgr, GetBaseUrl().c_str(), event_handler, this); + return c; +} + +void HttpsServer::acceptConnection(mg_connection* conn) { + // See https://mongoose.ws/tutorials/tls/ + mg_tls_init(conn, &tlsOpts); +} + +void HttpsServer::OnRequest(mg_connection* conn, mg_http_message* msg) { + std::string uri = std::string(msg->uri.ptr, msg->uri.len); + if (uri == "/hello.html") { + OnRequestHello(conn, msg); + } else { + OnRequestNotFound(conn, msg); + } +} + +void HttpsServer::OnRequestNotFound(mg_connection* conn, mg_http_message* /*msg*/) { + mg_http_reply(conn, 404, nullptr, "Not Found"); +} + +void HttpsServer::OnRequestHello(mg_connection* conn, mg_http_message* /*msg*/) { + std::string response{"Hello world!"}; + std::string headers{"Content-Type: text/html\r\n"}; + mg_http_reply(conn, 200, headers.c_str(), response.c_str()); +} + +const std::string& HttpsServer::getBaseDirPath() const { + return baseDirPath; +} + +const std::string& HttpsServer::getSslCertFileName() const { + return sslCertFileName; +} + +const std::string& HttpsServer::getSslKeyFileName() const { + return sslKeyFileName; +} + +} // namespace cpr -- cgit v1.2.3