> 技术文档 > 【在Linux世界中追寻伟大的One Piece】HTTP Session

【在Linux世界中追寻伟大的One Piece】HTTP Session

目录

1 -> 引入HTTP Session

1.1 -> 定义

1.2 -> 工作原理

1.3 -> 安全性

1.4 -> 超时和失效

1.5 -> 用途

2 -> 模拟session行为

3 -> 实验测试session


1 -> 引入HTTP Session

1.1 -> 定义

HTTP Session是服务器用来跟踪用户与服务器交互期间用户状态的机制。由于HTTP协议是无状态的(每个请求都是独立的),因此服务器需要通过Session来记住用户的信息。

1.2 -> 工作原理

当用户首次访问网站时,服务器会为用户创建一个唯一的Session ID,并通过Cookie将其发送到客户端。

客户端在之后的请求中会携带这个Session ID,服务器通过Session ID来识别用户,从而获取用户的会话信息。

服务器通常会将Session信息存储在内存、数据库或缓存中。

1.3 -> 安全性

与Cookie相似,由于Session ID是在客户端和服务器之间传递的,因此也存在被窃取的风险。

但是一般虽然Cookie被盗取了,但是用户只泄漏了一个Session ID,私密信息暂时没有被泄露的风险。

Session ID便于服务端进行客户端有效性的管理,比如异地登录。

可以通过HTTPS和设置合适的Cookie属性(如HttpOnly和Secure)来增强安全性。

1.4 -> 超时和失效

Session可以设置超时时间,当超过这个时间后,Session会自动失效。

服务器也可以主动使Session失效,例如当用户登出时。

1.5 -> 用途

  • 用户认证和会话管理
  • 存储用户的临时数据(如购物车内容)
  • 实现分布式系统的会话共享(通过将会话数据存储在共享数据库或缓存中)

2 -> 模拟session行为

代码文件结构

Comm.hpp HttpProtocol.hpp InetAddr.hpp LockGuard.hpp Log.hpp Main.cc Makefile Session.hpp Socket.hpp TcpServer.hpp Thread.hpp ThreadPool.hpp

部分核心代码:

Session.hpp

#pragma once#include #include #include #include #include #include // 用来进行测试说明class Session{public:Session(const std::string& username, const std::string& status):_username(username), _status(status){_create_time = time(nullptr); // 获取时间戳就行了,后面实际需要,就转化就转换一下}~Session(){}public:std::string _username;std::string _status;uint64_t _create_time;//当然还可以再加任何其他信息,看你的需求};using session_ptr = std::shared_ptr;class SessionManager{public:SessionManager(){srand(time(nullptr) ^ getpid());}std::string AddSession(session_ptr s){uint32_t randomid = rand() + time(nullptr); // 随机数+时间戳,实际有形成 sessionid 的库,比如 boost uuid 库,或者其他第三方库等std::string sessionid = std::to_string(randomid);_sessions.insert(std::make_pair(sessionid, s));return sessionid;}session_ptr GetSession(const std::string sessionid){if (_sessions.find(sessionid) == _sessions.end()) return nullptr;return _sessions[sessionid];}~SessionManager(){}private:std::unordered_map _sessions;};

HttpProtocol.hpp

#pragma once#include #include #include #include #include #include #include #include \"TcpServer.hpp\"#include \"Session.hpp\" // 引入 sessionconst std::string HttpSep = \"\\r\\n\";// 可以配置的const std::string homepage = \"index.html\";const std::string wwwroot = \"./wwwroot\";class HttpRequest{public:HttpRequest() : _req_blank(HttpSep), _path(wwwroot){}bool GetLine(std::string& str, std::string* line){auto pos = str.find(HttpSep);if (pos == std::string::npos)return false;*line = str.substr(0, pos); // \\r\\nstr.erase(0, pos + HttpSep.size());return true;}void Parse(){// 解析出来 urlstd::stringstream ss(_req_line);ss >> _method >> _url >> _http_version;// 查找 cookiestd::string prefix = \"Cookie: \";for (auto& line : _req_header){std::string cookie;if (strncmp(line.c_str(), prefix.c_str(),prefix.size()) == 0) // 找到了{cookie = line.substr(prefix.size()); // 截取\"Cookie: \"之后的就行了_cookies.emplace_back(cookie);break;}}// 查找 sessionidprefix = \"sessionid=\";for (const auto& cookie : _cookies){if (strncmp(cookie.c_str(), prefix.c_str(),prefix.size()) == 0){_sessionid = cookie.substr(prefix.size()); // 截取\"sessionid=\"之后的就行了// std::cout << \"_sessionid: \" << _sessionid << std::endl;}}}std::string Url(){return _url;}std::string SessionId(){return _sessionid;}bool Deserialize(std::string & request){std::string line;bool ok = GetLine(request, &line);if (!ok)return false;_req_line = line;while (true){bool ok = GetLine(request, &line);if (ok && line.empty()){_req_content = request;break;}else if (ok && !line.empty()){_req_header.push_back(line);}else{break;}}return true;}void DebugHttp(){std::cout << \"_req_line: \" << _req_line << std::endl;for (auto& line : _req_header){std::cout < \" << line << std::endl;}}~HttpRequest(){}private:// http 报文自动std::string _req_line; // method url http_versionstd::vector _req_header;std::string _req_blank;std::string _req_content;// 解析之后的内容std::string _method;std::string _url; // / /dira/dirb/x.html / dira / dirb / XX ? usrname = 100 && password = 1234 / dira / dirbstd::string _http_version;std::string _path; // \"./wwwroot\"std::string _suffix; // 请求资源的后缀std::vector _cookies; // 其实 cookie 可以有多个,因为 Set - Cookie 可以被写多条,测试,一条够了。std::string _sessionid; // 请求携带的 sessionid,仅仅用来测试};const std::string BlankSep = \" \";const std::string LineSep = \"\\r\\n\";class HttpResponse{public:HttpResponse() : _http_version(\"HTTP/1.0\"), _status_code(200),_status_code_desc(\"OK\"), _resp_blank(LineSep){}void SetCode(int code){_status_code = code;}void SetDesc(const std::string& desc){_status_code_desc = desc;}void MakeStatusLine(){_status_line = _http_version + BlankSep +std::to_string(_status_code) + BlankSep + _status_code_desc +LineSep;}void AddHeader(const std::string& header){_resp_header.push_back(header + LineSep);}void AddContent(const std::string & content){_resp_content = content;}std::string Serialize(){MakeStatusLine();std::string response_str = _status_line;for (auto& header : _resp_header){response_str += header;}response_str += _resp_blank;response_str += _resp_content;return response_str;}~HttpResponse() {}private:std::string _status_line;std::vector _resp_header;std::string _resp_blank;std::string _resp_content; // body// httpversion StatusCode StatusCodeDescstd::string _http_version;int _status_code;std::string _status_code_desc;};class Http{private:std::string GetMonthName(int month){std::vector months = { \"Jan\", \"Feb\", \"Mar\",\"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\" };return months[month];}std::string GetWeekDayName(int day){std::vector weekdays = { \"Sun\", \"Mon\", \"Tue\",\"Wed\", \"Thu\", \"Fri\", \"Sat\" };return weekdays[day];}std::string ExpireTimeUseRfc1123(int t) // 秒级别的未来 UTC 时间{time_t timeout = time(nullptr) + t;struct tm* tm = gmtime(&timeout); // 这里不能用 localtime,因为 localtime 是默认带了时区的.gmtime 获取的就是 UTC 统一时间char timebuffer[1024];// 时间格式如: expires=Thu, 18 Dec 2024 12:00:00 UTCsnprintf(timebuffer, sizeof(timebuffer),\"%s, %02d %s %d %02d:%02d:%02d UTC\",GetWeekDayName(tm->tm_wday).c_str(),tm->tm_mday,GetMonthName(tm->tm_mon).c_str(),tm->tm_year + 1900,tm->tm_hour,tm->tm_min,tm->tm_sec);return timebuffer;}public:Http(uint16_t port){_tsvr = std::make_unique(port,std::bind(&Http::HandlerHttp, this, std::placeholders::_1));_tsvr->Init();_session_manager = std::make_unique();}std::string ProveCookieWrite() // 证明 cookie 能被写入浏览器{return \"Set-Cookie: username=zhangsan;\";}std::string ProveCookieTimeOut(){return \"Set-Cookie: username=zhangsan; expires=\" +ExpireTimeUseRfc1123(60) + \";\"; // 让 cookie 1min 后过期}std::string ProvePath(){return \"Set-Cookie: username=zhangsan; path=/a/b;\";}std::string ProveSession(const std::string& session_id){return \"Set-Cookie: sessionid=\" + session_id + \";\";}std::string HandlerHttp(std::string request){HttpRequest req;HttpResponse resp;req.Deserialize(request);req.Parse();// req.DebugHttp();// std::cout << req.Url() << std::endl;// // 下面的代码就用来测试,如果你想更优雅,可以回调出去处理static int number = 0;if (req.Url() == \"/login\") // 用/login path 向指定浏览器写入sessionid,并在服务器维护对应的 session 对象{std::string sessionid = req.SessionId();if (sessionid.empty()) // 说明历史没有登陆过{std::string user = \"user-\" + std::to_string(number++);session_ptr s = std::make_shared(user, \"logined\");std::string sessionid = _session_manager->AddSession(s);lg.LogMessage(Debug, \"%s 被添加, sessionid是: % s\\n\", user.c_str(), sessionid.c_str());resp.AddHeader(ProveSession(sessionid));}}else{// 当浏览器在本站点任何路径中活跃,都会自动提交 sessionid, 我们就能知道谁活跃了.std::string sessionid = req.SessionId();if (!sessionid.empty()){session_ptr s = _session_manager->GetSession(sessionid);// 这个地方有坑,一定要判断服务器端 session 对象是否存在,因为可能测试的时候// 浏览器还有历史 sessionid,但是服务器重启之后,session 对象没有了.if (s != nullptr)lg.LogMessage(Debug, \"%s 正在活跃.\\n\", s->_username.c_str());elselg.LogMessage(Debug, \"cookie : %s 已经过期, 需要清理\\n\", sessionid.c_str());}}resp.SetCode(200);resp.SetDesc(\"OK\");resp.AddHeader(\"Content-Type: text/html\");// resp.AddHeader(ProveCookieWrite()); //测试 cookie 被写入与自动提交// resp.AddHeader(ProveCookieTimeOut()); //测试过期时间的写入// resp.AddHeader(ProvePath()); // 测试路径resp.AddContent(\"

helloworld

\");return resp.Serialize();}void Run(){_tsvr->Start();}~Http(){}private:std::unique_ptr _tsvr;std::unique_ptr _session_manager;};

3 -> 实验测试session

  • 准备两个浏览器:Google Chrome和Microsoft Edge(windows自带的)

1. 删除浏览器中指定的服务器上的所有的cookie

  • 如果历史上没有做过测试,就不删了。
  • chrome的cookie有些特殊,实验不出来,尝试打印chrome浏览器发过来的http请求,观察cookie部分,你就能知道为什么要删除历史cookie。

Microsoft Edge

Google Chrome

2. 访问/login, 模拟登录

Microsoft Edge

Google Chrome

3. 两个浏览器访问任意的站点资源

服务器端已经能识别是哪一个浏览器了。

总结:

HTTP Cookie和Session都是用于在Web应用中跟踪用户状态的机制。Cookie是存储在客户端的,而Session是存储在服务器端的。它们各有优缺点,通常在实际应用中会结合使用,以达到最佳的用户体验和安全性。


感谢各位大佬支持!!!

互三啦!!!