1 Star 0 Fork 0

Aurora/Linux

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
HttpServer.cc 1.98 KB
一键复制 编辑 原始数据 按行查看 历史
Aurora 提交于 2025-07-13 00:44 +08:00 . 完整版httpserver
#include "HttpServer.hpp"
#include "Session.hpp"
using namespace LogModule;
void Login(HttpRequest &req,HttpResponse &resp)
{
// 根据req,动态构建resp
LOG(LogLevel::DEBUG) << "进入到了登录模块" << req.Path() << ", " << req.Args();
std::string req_args = req.Args();
// 1.解析参数格式,得到要的参数
// 2.访问数据库,验证对应的用户是否是合法的用户,其他工作....
// SessionManager m;
// session_id = m.CreateSession(xxxx);
// 3.登录成功
// std::string body = "<html><body><p>Login Success!</p></body></html>";
std::string body = req.GetContent("wwwroot/success.html");
resp.SetCode(200);
resp.SetHeader("Content-Length",std::to_string(body.size()));
resp.SetHeader("Content-Type","text/html");
resp.SetHeader("Set-Cookie","username=zhangsan&passwd=hahahaha");
resp.SetBody(body);
// resp.SetCode(302);
// resp.SetHeader("Location","/");
}
void Register(HttpRequest &req,HttpResponse &resp)
{
// 根据req,动态构建resp
LOG(LogLevel::DEBUG) << "进入到了注册模块" << req.Path() << ", " << req.Args();
}
void Search(HttpRequest &req,HttpResponse &resp)
{
// 根据req,动态构建resp
LOG(LogLevel::DEBUG) << "进入到了搜索模块" << req.Path() << ", " << req.Args();
}
void Test(HttpRequest &req,HttpResponse &resp)
{
// 根据req,动态构建resp
LOG(LogLevel::DEBUG) << "进入到了测试模块" << req.Path() << ", " << req.Args();
}
int main(int argc,char *argv[])
{
if(argc != 2)
{
std::cout << "Usage: " << argv[0] << " port" << std::endl;
return 1;
}
auto httpserver = std::make_unique<HttpServer>(std::stoi(argv[1]));
// 要让服务器具有登录功能
httpserver->Resgiter("/login",Login); // restful风格的网络请求接口
httpserver->Resgiter("/Register",Register);
httpserver->Resgiter("/Search",Search);
httpserver->Resgiter("/Test",Test);
httpserver->Start();
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/Axurea/linux.git
git@gitee.com:Axurea/linux.git
Axurea
linux
Linux
master

搜索帮助