整理HttpServer
							parent
							
								
									486ed28c56
								
							
						
					
					
						commit
						9c2146ac04
					
				@ -0,0 +1,117 @@
 | 
				
			|||||||
 | 
					#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;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -0,0 +1,29 @@
 | 
				
			|||||||
 | 
					#pragma once
 | 
				
			||||||
 | 
					#include "../cpp-httplib/httplib.h"
 | 
				
			||||||
 | 
					#include <string>
 | 
				
			||||||
 | 
					#include <windows.h>
 | 
				
			||||||
 | 
					#include <vector>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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<string> get_file_names(const string & Dir);//获取文件夹内的所有文件名
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -1,75 +1,8 @@
 | 
				
			|||||||
#include <iostream>
 | 
					#include "HttplibServer.h"
 | 
				
			||||||
#include <fstream>
 | 
					 | 
				
			||||||
#include "../cpp-httplib/httplib.h"
 | 
					 | 
				
			||||||
#include <filesystem>
 | 
					 | 
				
			||||||
#include <vector>
 | 
					 | 
				
			||||||
#include <string>
 | 
					 | 
				
			||||||
#include <direct.h>
 | 
					 | 
				
			||||||
#include <iostream>
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int main() 
 | 
				
			||||||
using namespace std;
 | 
					 | 
				
			||||||
using namespace httplib;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const string SERVER_FILE_DIR = "/ServerFiles/";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
string GetExeDir()
 | 
					 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	char buff[_MAX_PATH];
 | 
						CHttplibServer svr;
 | 
				
			||||||
	_getcwd(buff, _MAX_PATH);
 | 
						svr.StartHttpServer();
 | 
				
			||||||
	string currPath(buff);
 | 
						getchar();
 | 
				
			||||||
	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<char> 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);
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
					Loading…
					
					
				
		Reference in New Issue