From 9c2146ac04caad23462fa2a8b686b096ccf27222 Mon Sep 17 00:00:00 2001 From: bestlqiang Date: Tue, 18 Mar 2025 15:26:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86HttpServer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/HttplibServer.cpp | 117 ++++++++++++++++++++++++++++++++++ server/HttplibServer.h | 29 +++++++++ server/server.cpp | 77 ++-------------------- server/server.vcxproj | 2 + server/server.vcxproj.filters | 6 ++ 5 files changed, 159 insertions(+), 72 deletions(-) create mode 100644 server/HttplibServer.cpp create mode 100644 server/HttplibServer.h diff --git a/server/HttplibServer.cpp b/server/HttplibServer.cpp new file mode 100644 index 0000000..461bea5 --- /dev/null +++ b/server/HttplibServer.cpp @@ -0,0 +1,117 @@ +#include "HttplibServer.h" +#include +#include +#include +#include + + +#define SERVER_FILE_DIR "/ServerFiles" + +#define CMD_GetFileList "GetFileList" + +string static GetExeDir() +{ + char buff[_MAX_PATH]; + _getcwd(buff, _MAX_PATH); + string currPath(buff); + return string(buff); +} + +bool static bFileExist(const string& path) +{ + ifstream file(path, ios::binary); + return file.good(); +} + +CHttplibServer::CHttplibServer() +{ +} + + +CHttplibServer::~CHttplibServer() +{ +} + +bool CHttplibServer::StartHttpServer(string SvrFilesDir /*= ""*/, bool bAsync /*= true*/) +{ + m_SvrFilesDir = SvrFilesDir; + auto fStart = [&] + { + httplib::Server svr; + svr.set_payload_max_length(1024 * 1024 * 512); // 512MB + svr.Get(R"(/download/(.+))", [&](const Request& req, Response& res) {OnDownloadFile(req, res);}); + svr.Get(R"(/command/(.+))", [&](const Request& req, Response& res) {OnCommandBase(req, res);}); + std::cout << "Server started at http://localhost:1234\n"; + svr.listen(m_SvrIP, m_Port); + }; + bAsync? thread(fStart).detach(): fStart(); + return true; +} + +void CHttplibServer::OnCommandBase(const Request & req, Response & res) +{ + string cmd = req.matches[1]; + std::cout << "Rcved Command:" << cmd << endl; + + if (cmd.find(CMD_GetFileList) != string::npos) + OnCommand_GetFileList(req, res); + else + OnCommand(cmd, res); +} + +void CHttplibServer::OnCommand(string cmd, Response & res) +{ + cmd += "Rcved"; + res.set_content(cmd, "text/plain"); +} + +void CHttplibServer::OnDownloadFile(const Request& req, Response& res) +{ + if (m_SvrFilesDir.empty()) + m_SvrFilesDir = GetExeDir() + SERVER_FILE_DIR; + + string FileName = req.matches[1]; + string file_path = m_SvrFilesDir+"/"+FileName; + + if (!bFileExist(file_path)) + { + res.status = 404; + res.set_content("File not found", "text/plain"); + return; + } + res.set_file_content(file_path); + return; +} + +void CHttplibServer::OnCommand_GetFileList(const Request& req, Response& res) +{ + if (m_SvrFilesDir.empty()) + m_SvrFilesDir = GetExeDir() + SERVER_FILE_DIR; + + vector NameVec = get_file_names(m_SvrFilesDir); + string sRespon; + for (auto const & Name: NameVec) + { + sRespon += Name; + sRespon += ","; + } + res.set_content(sRespon, "text/plain"); +} + +vector CHttplibServer::get_file_names(const string& Dir) +{ + vector NamesVec; + string search_path = Dir + "\\*"; + WIN32_FIND_DATAA find_data; + HANDLE hFind = FindFirstFileA(search_path.c_str(), &find_data); + if (hFind != INVALID_HANDLE_VALUE) { + do { + if (!(find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { + NamesVec.push_back(find_data.cFileName); + } + } while (FindNextFileA(hFind, &find_data)); + FindClose(hFind); + } + return NamesVec; +} + diff --git a/server/HttplibServer.h b/server/HttplibServer.h new file mode 100644 index 0000000..ee45b49 --- /dev/null +++ b/server/HttplibServer.h @@ -0,0 +1,29 @@ +#pragma once +#include "../cpp-httplib/httplib.h" +#include +#include +#include + +using namespace std; +using namespace httplib; + +class CHttplibServer +{ +public: + CHttplibServer(); + virtual ~CHttplibServer(); + bool StartHttpServer(string SvrFilesDir="",bool bAsync=true); + void SetFilesDir(string Dir) { m_SvrFilesDir = Dir; } +private: + string m_SvrFilesDir;//服务器文件所在文件夹 + string m_SvrIP = "0.0.0.0"; + int m_Port = 1234; +public: + virtual void OnCommand(string cmd, Response& res); +private: + void OnDownloadFile(const Request& req, Response& res); + void OnCommandBase(const Request& req, Response& res); + void OnCommand_GetFileList(const Request& req, Response& res);//获取服务器文件夹的所有文件名 + vector get_file_names(const string & Dir);//获取文件夹内的所有文件名 +}; + diff --git a/server/server.cpp b/server/server.cpp index cbca3e9..05f54dc 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -1,75 +1,8 @@ -#include -#include -#include "../cpp-httplib/httplib.h" -#include -#include -#include -#include -#include +#include "HttplibServer.h" - -using namespace std; -using namespace httplib; - -const string SERVER_FILE_DIR = "/ServerFiles/"; - -string GetExeDir() +int main() { - char buff[_MAX_PATH]; - _getcwd(buff, _MAX_PATH); - string currPath(buff); - return string(buff); -} - -bool file_exists(const string& path) { - ifstream file(path, ios::binary); - return file.good(); -} - -void send_file(const string& file_path, Response& res) { - ifstream file(file_path, ios::binary | ios::ate); - if (!file) { - res.status = 500; // 服务器错误 - res.set_content("Failed to open file", "text/plain"); - return; - } - - // 获取文件大小 - streamsize file_size = file.tellg(); - file.seekg(0, ios::beg); - - res.set_content_provider( - file_size, "application/octet-stream", - [file_path](size_t offset, size_t length, DataSink& sink) { - ifstream file(file_path, ios::binary); - if (!file) return false; - - file.seekg(offset, ios::beg); - vector buffer(length); - file.read(buffer.data(), length); - sink.write(buffer.data(), file.gcount()); // 发送数据 - - return true; // 继续传输 - } - ); -} - -int main() { - Server svr; - - svr.Get(R"(/download/(.+))", [&](const Request& req, Response& res) { - string filename = req.matches[1]; - string file_path = GetExeDir()+SERVER_FILE_DIR + filename; - - if (!file_exists(file_path)) { - res.status = 404; - res.set_content("File not found", "text/plain"); - return; - } - - send_file(file_path, res); - }); - - cout << "Server started at http://localhost:1234\n"; - svr.listen("localhost", 1234); + CHttplibServer svr; + svr.StartHttpServer(); + getchar(); } diff --git a/server/server.vcxproj b/server/server.vcxproj index 01052d0..e256e7e 100644 --- a/server/server.vcxproj +++ b/server/server.vcxproj @@ -147,10 +147,12 @@ + + diff --git a/server/server.vcxproj.filters b/server/server.vcxproj.filters index 2457d82..12f7a23 100644 --- a/server/server.vcxproj.filters +++ b/server/server.vcxproj.filters @@ -18,10 +18,16 @@ 婧愭枃浠 + + 婧愭枃浠 + 澶存枃浠 + + 澶存枃浠 + \ No newline at end of file