From 334edbddfd60d21bd1581fd16e9fccdb89010fbf Mon Sep 17 00:00:00 2001 From: amass <168062547@qq.com> Date: Fri, 14 Mar 2025 18:17:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0http=E6=96=87=E4=BB=B6get?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=20=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Application.cpp | 1 + CMakeLists.txt | 4 ++++ ServiceLogic.cpp | 43 ++++++++++++++++++++++++++++++++++++++ ServiceLogic.h | 9 ++------ WebRTC/SignalServer.cpp | 4 +++- resources/conf/server.conf | 7 +------ 6 files changed, 54 insertions(+), 14 deletions(-) diff --git a/Application.cpp b/Application.cpp index cf585c3..12b1ac1 100644 --- a/Application.cpp +++ b/Application.cpp @@ -90,6 +90,7 @@ int Application::exec() { ServiceLogic::live2dBackend(); ServiceLogic::visitAnalysis(); ServiceLogic::userAccount(); + ServiceLogic::staticFilesDeploy(); startAcceptHttpConnections(settings->server(), settings->port()); m_ioContext->run(); return 0; diff --git a/CMakeLists.txt b/CMakeLists.txt index a3236d7..ddde7df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,7 @@ +cmake_minimum_required(VERSION 3.16) + +project(Older VERSION 0.1 LANGUAGES C CXX) + set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/ServiceLogic.cpp b/ServiceLogic.cpp index 53f102e..f85a021 100644 --- a/ServiceLogic.cpp +++ b/ServiceLogic.cpp @@ -52,6 +52,49 @@ http::response badRequest(const http::request::instance(); + // clang-format off + application->insertUrl("/{path*}", [](Older::HttpSession &session, const Older::Application::Request &request, const matches &matches) { + using namespace boost::beast; + url_view view(request.target()); + auto target = view.path(); + LOG(info) << target; + if (target.find("..") != boost::beast::string_view::npos) { + session.reply(ServiceLogic::badRequest(request, "Illegal request-target")); + return; + } + auto settings = Singleton::instance(); + std::string path = ResponseUtility::pathCat(settings->documentRoot(), target); + if (target.back() == '/') path.append("index.html"); + if (std::filesystem::is_directory(path)) path.append("/index.html"); + boost::beast::error_code ec; + http::file_body::value_type body; + body.open(path.c_str(), boost::beast::file_mode::scan, ec); + if (ec == boost::beast::errc::no_such_file_or_directory) { + std::ostringstream oss; + oss << "The resource '" << target << "' was not found."; + LOG(error) << oss.str(); + session.errorReply(request, http::status::not_found, oss.str()); + return; + } else if (ec) { + session.reply(ServiceLogic::serverError(request, ec.message())); + return; + } + auto const size = body.size(); + http::response res{std::piecewise_construct, std::make_tuple(std::move(body)), + std::make_tuple(http::status::ok, request.version())}; + res.set(http::field::server, BOOST_BEAST_VERSION_STRING); + res.set(http::field::content_type, ResponseUtility::mimeType(path)); + res.content_length(size); + res.keep_alive(request.keep_alive()); + session.reply(std::move(res)); + }); + // clang-format on +} + void visitAnalysis() { using namespace Core; // clang-format off diff --git a/ServiceLogic.h b/ServiceLogic.h index 8f2bbe2..2e90258 100644 --- a/ServiceLogic.h +++ b/ServiceLogic.h @@ -13,8 +13,6 @@ using StringRequest = boost::beast::http::request; - - namespace ServiceLogic { template @@ -29,8 +27,7 @@ badRequest(const boost::beast::http::request &r template boost::beast::http::response make_200(const boost::beast::http::request &request, - typename ResponseBody::value_type body, - boost::beast::string_view content) { + typename ResponseBody::value_type body, boost::beast::string_view content) { boost::beast::http::response response{boost::beast::http::status::ok, request.version()}; response.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING); response.set(boost::beast::http::field::content_type, content); @@ -41,13 +38,11 @@ boost::beast::http::response make_200(const boost::beast::http::re return response; } +void staticFilesDeploy(); void live2dBackend(); void visitAnalysis(); void userAccount(); - - - }; // namespace ServiceLogic #include "ServiceLogic.inl" diff --git a/WebRTC/SignalServer.cpp b/WebRTC/SignalServer.cpp index 970ecb6..9735cdd 100644 --- a/WebRTC/SignalServer.cpp +++ b/WebRTC/SignalServer.cpp @@ -14,6 +14,8 @@ SignalServer::SignalServer(Application &app) { if (boost::beast::websocket::is_upgrade(request)) { auto ws = std::make_shared(session.releaseSocket(), *this, id); ws->run(request); + } else { + LOG(error) << "webrtc client[" << id << "] not upgrade connection, request: " << std::endl << request; } }); // clang-format on @@ -36,4 +38,4 @@ WebSocketSignalSession *SignalServer::client(const std::string &id) { } return ret; } -} \ No newline at end of file +} // namespace Older \ No newline at end of file diff --git a/resources/conf/server.conf b/resources/conf/server.conf index f41f78f..b7b3e6c 100644 --- a/resources/conf/server.conf +++ b/resources/conf/server.conf @@ -31,15 +31,10 @@ location ~ ^/api/v1/.*$ { proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Port $server_port; + proxy_http_version 1.1; proxy_pass http://local; } -location ~ ^/lvgl/.+$ { - root amass_blog; - index index.html index.htm; - try_files /lvgl/index.html =404; -} - location ^~ /api/v1/freedom { if ($http_upgrade != "websocket") { # WebSocket协商失败时返回404 return 404;