You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
2.6 KiB
C++
118 lines
2.6 KiB
C++
4 months ago
|
#include "HttplibServer.h"
|
||
|
#include <filesystem>
|
||
|
#include <vector>
|
||
|
#include <direct.h>
|
||
|
#include <iostream>
|
||
|
|
||
|
|
||
|
#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<string> NameVec = get_file_names(m_SvrFilesDir);
|
||
|
string sRespon;
|
||
|
for (auto const & Name: NameVec)
|
||
|
{
|
||
|
sRespon += Name;
|
||
|
sRespon += ",";
|
||
|
}
|
||
|
res.set_content(sRespon, "text/plain");
|
||
|
}
|
||
|
|
||
|
vector<string> CHttplibServer::get_file_names(const string& Dir)
|
||
|
{
|
||
|
vector<string> 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;
|
||
|
}
|
||
|
|