0


【在Linux世界中追寻伟大的One Piece】HTTP 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

  1. #pragma once
  2. #include <iostream>
  3. #include <string>
  4. #include <memory>
  5. #include <ctime>
  6. #include <unistd.h>
  7. #include <unordered_map>
  8. // 用来进行测试说明
  9. class Session
  10. {
  11. public:
  12. Session(const std::string& username, const std::string
  13. & status)
  14. :_username(username), _status(status)
  15. {
  16. _create_time = time(nullptr); // 获取时间戳就行了,后面实际需要,就转化就转换一下
  17. }
  18. ~Session()
  19. {}
  20. public:
  21. std::string _username;
  22. std::string _status;
  23. uint64_t _create_time;
  24. //当然还可以再加任何其他信息,看你的需求
  25. };
  26. using session_ptr = std::shared_ptr<Session>;
  27. class SessionManager
  28. {
  29. public:
  30. SessionManager()
  31. {
  32. srand(time(nullptr) ^ getpid());
  33. }
  34. std::string AddSession(session_ptr s)
  35. {
  36. uint32_t randomid = rand() + time(nullptr); // 随机数+时间戳,实际有形成 sessionid 的库,比如 boost uuid 库,或者其他第三方库等
  37. std::string sessionid = std::to_string(randomid);
  38. _sessions.insert(std::make_pair(sessionid, s));
  39. return sessionid;
  40. }
  41. session_ptr GetSession(const std::string sessionid)
  42. {
  43. if (_sessions.find(sessionid) == _sessions.end())
  44. return nullptr;
  45. return _sessions[sessionid];
  46. }
  47. ~SessionManager()
  48. {}
  49. private:
  50. std::unordered_map<std::string, session_ptr> _sessions;
  51. };

HttpProtocol.hpp

  1. #pragma once
  2. #include <iostream>
  3. #include <string>
  4. #include <sstream>
  5. #include <vector>
  6. #include <memory>
  7. #include <ctime>
  8. #include <functional>
  9. #include "TcpServer.hpp"
  10. #include "Session.hpp" // 引入 session
  11. const std::string HttpSep = "\r\n";
  12. // 可以配置的
  13. const std::string homepage = "index.html";
  14. const std::string wwwroot = "./wwwroot";
  15. class HttpRequest
  16. {
  17. public:
  18. HttpRequest() : _req_blank(HttpSep), _path(wwwroot)
  19. {
  20. }
  21. bool GetLine(std::string& str, std::string* line)
  22. {
  23. auto pos = str.find(HttpSep);
  24. if (pos == std::string::npos)
  25. return false;
  26. *line = str.substr(0, pos); // \r\n
  27. str.erase(0, pos + HttpSep.size());
  28. return true;
  29. }
  30. void Parse()
  31. {
  32. // 解析出来 url
  33. std::stringstream ss(_req_line);
  34. ss >> _method >> _url >> _http_version;
  35. // 查找 cookie
  36. std::string prefix = "Cookie: ";
  37. for (auto& line : _req_header)
  38. {
  39. std::string cookie;
  40. if (strncmp(line.c_str(), prefix.c_str(),
  41. prefix.size()) == 0) // 找到了
  42. {
  43. cookie = line.substr(prefix.size()); // 截取"Cookie: "之后的就行了
  44. _cookies.emplace_back(cookie);
  45. break;
  46. }
  47. }
  48. // 查找 sessionid
  49. prefix = "sessionid=";
  50. for (const auto& cookie : _cookies)
  51. {
  52. if (strncmp(cookie.c_str(), prefix.c_str(),
  53. prefix.size()) == 0)
  54. {
  55. _sessionid = cookie.substr(prefix.size()); // 截取"sessionid="之后的就行了
  56. // std::cout << "_sessionid: " << _sessionid << std::endl;
  57. }
  58. }
  59. }
  60. std::string Url()
  61. {
  62. return _url;
  63. }
  64. std::string SessionId()
  65. {
  66. return _sessionid;
  67. }
  68. bool Deserialize(std::string & request)
  69. {
  70. std::string line;
  71. bool ok = GetLine(request, &line);
  72. if (!ok)
  73. return false;
  74. _req_line = line;
  75. while (true)
  76. {
  77. bool ok = GetLine(request, &line);
  78. if (ok && line.empty())
  79. {
  80. _req_content = request;
  81. break;
  82. }
  83. else if (ok && !line.empty())
  84. {
  85. _req_header.push_back(line);
  86. }
  87. else
  88. {
  89. break;
  90. }
  91. }
  92. return true;
  93. }
  94. void DebugHttp()
  95. {
  96. std::cout << "_req_line: " << _req_line << std::endl;
  97. for (auto& line : _req_header)
  98. {
  99. std::cout << "---> " << line << std::endl;
  100. }
  101. }
  102. ~HttpRequest()
  103. {
  104. }
  105. private:
  106. // http 报文自动
  107. std::string _req_line; // method url http_version
  108. std::vector<std::string> _req_header;
  109. std::string _req_blank;
  110. std::string _req_content;
  111. // 解析之后的内容
  112. std::string _method;
  113. std::string _url; // / /dira/dirb/x.html / dira / dirb / XX ? usrname = 100 && password = 1234 / dira / dirb
  114. std::string _http_version;
  115. std::string _path; // "./wwwroot"
  116. std::string _suffix; // 请求资源的后缀
  117. std::vector<std::string> _cookies; // 其实 cookie 可以有多个,因为 Set - Cookie 可以被写多条,测试,一条够了。
  118. std::string _sessionid; // 请求携带的 sessionid,仅仅用来测试
  119. };
  120. const std::string BlankSep = " ";
  121. const std::string LineSep = "\r\n";
  122. class HttpResponse
  123. {
  124. public:
  125. HttpResponse() : _http_version("HTTP/1.0"), _status_code(200),
  126. _status_code_desc("OK"), _resp_blank(LineSep)
  127. {
  128. }
  129. void SetCode(int code)
  130. {
  131. _status_code = code;
  132. }
  133. void SetDesc(const std::string& desc)
  134. {
  135. _status_code_desc = desc;
  136. }
  137. void MakeStatusLine()
  138. {
  139. _status_line = _http_version + BlankSep +
  140. std::to_string(_status_code) + BlankSep + _status_code_desc +
  141. LineSep;
  142. }
  143. void AddHeader(const std::string& header)
  144. {
  145. _resp_header.push_back(header + LineSep);
  146. }
  147. void AddContent(const std::string & content)
  148. {
  149. _resp_content = content;
  150. }
  151. std::string Serialize()
  152. {
  153. MakeStatusLine();
  154. std::string response_str = _status_line;
  155. for (auto& header : _resp_header)
  156. {
  157. response_str += header;
  158. }
  159. response_str += _resp_blank;
  160. response_str += _resp_content;
  161. return response_str;
  162. }
  163. ~HttpResponse() {}
  164. private:
  165. std::string _status_line;
  166. std::vector<std::string> _resp_header;
  167. std::string _resp_blank;
  168. std::string _resp_content; // body
  169. // httpversion StatusCode StatusCodeDesc
  170. std::string _http_version;
  171. int _status_code;
  172. std::string _status_code_desc;
  173. };
  174. class Http
  175. {
  176. private:
  177. std::string GetMonthName(int month)
  178. {
  179. std::vector<std::string> months = { "Jan", "Feb", "Mar",
  180. "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  181. return months[month];
  182. }
  183. std::string GetWeekDayName(int day)
  184. {
  185. std::vector<std::string> weekdays = { "Sun", "Mon", "Tue",
  186. "Wed", "Thu", "Fri", "Sat" };
  187. return weekdays[day];
  188. }
  189. std::string ExpireTimeUseRfc1123(int t) // 秒级别的未来 UTC 时间
  190. {
  191. time_t timeout = time(nullptr) + t;
  192. struct tm* tm = gmtime(&timeout); // 这里不能用 localtime,因为 localtime 是默认带了时区的.gmtime 获取的就是 UTC 统一时间
  193. char timebuffer[1024];
  194. // 时间格式如: expires=Thu, 18 Dec 2024 12:00:00 UTC
  195. snprintf(timebuffer, sizeof(timebuffer),
  196. "%s, %02d %s %d %02d:%02d:%02d UTC",
  197. GetWeekDayName(tm->tm_wday).c_str(),
  198. tm->tm_mday,
  199. GetMonthName(tm->tm_mon).c_str(),
  200. tm->tm_year + 1900,
  201. tm->tm_hour,
  202. tm->tm_min,
  203. tm->tm_sec);
  204. return timebuffer;
  205. }
  206. public:
  207. Http(uint16_t port)
  208. {
  209. _tsvr = std::make_unique<TcpServer>(port,
  210. std::bind(&Http::HandlerHttp, this, std::placeholders::_1));
  211. _tsvr->Init();
  212. _session_manager = std::make_unique<SessionManager>();
  213. }
  214. std::string ProveCookieWrite() // 证明 cookie 能被写入浏览器
  215. {
  216. return "Set-Cookie: username=zhangsan;";
  217. }
  218. std::string ProveCookieTimeOut()
  219. {
  220. return "Set-Cookie: username=zhangsan; expires=" +
  221. ExpireTimeUseRfc1123(60) + ";"; // 让 cookie 1min 后过期
  222. }
  223. std::string ProvePath()
  224. {
  225. return "Set-Cookie: username=zhangsan; path=/a/b;";
  226. }
  227. std::string ProveSession(const std::string& session_id)
  228. {
  229. return "Set-Cookie: sessionid=" + session_id + ";";
  230. }
  231. std::string HandlerHttp(std::string request)
  232. {
  233. HttpRequest req;
  234. HttpResponse resp;
  235. req.Deserialize(request);
  236. req.Parse();
  237. // req.DebugHttp();
  238. // std::cout << req.Url() << std::endl;
  239. // // 下面的代码就用来测试,如果你想更优雅,可以回调出去处理
  240. static int number = 0;
  241. if (req.Url() == "/login") // 用/login path 向指定浏览器写入sessionid,并在服务器维护对应的 session 对象
  242. {
  243. std::string sessionid = req.SessionId();
  244. if (sessionid.empty()) // 说明历史没有登陆过
  245. {
  246. std::string user = "user-" + std::to_string(number++);
  247. session_ptr s = std::make_shared<Session>(user, "logined");
  248. std::string sessionid = _session_manager->AddSession(s);
  249. lg.LogMessage(Debug, "%s 被添加, sessionid是: % s\n", user.c_str(), sessionid.c_str());
  250. resp.AddHeader(ProveSession(sessionid));
  251. }
  252. }
  253. else
  254. {
  255. // 当浏览器在本站点任何路径中活跃,都会自动提交 sessionid, 我们就能知道谁活跃了.
  256. std::string sessionid = req.SessionId();
  257. if (!sessionid.empty())
  258. {
  259. session_ptr s = _session_manager->GetSession(sessionid);
  260. // 这个地方有坑,一定要判断服务器端 session 对象是否存在,因为可能测试的时候
  261. // 浏览器还有历史 sessionid,但是服务器重启之后,session 对象没有了.
  262. if (s != nullptr)
  263. lg.LogMessage(Debug, "%s 正在活跃.\n", s->_username.c_str());
  264. else
  265. lg.LogMessage(Debug, "cookie : %s 已经过期, 需要清理\n", sessionid.c_str());
  266. }
  267. }
  268. resp.SetCode(200);
  269. resp.SetDesc("OK");
  270. resp.AddHeader("Content-Type: text/html");
  271. // resp.AddHeader(ProveCookieWrite()); //测试 cookie 被写入与自动提交
  272. // resp.AddHeader(ProveCookieTimeOut()); //测试过期时间的写入
  273. // resp.AddHeader(ProvePath()); // 测试路径
  274. resp.AddContent("<html><h1>helloworld</h1></html>");
  275. return resp.Serialize();
  276. }
  277. void Run()
  278. {
  279. _tsvr->Start();
  280. }
  281. ~Http()
  282. {
  283. }
  284. private:
  285. std::unique_ptr<TcpServer> _tsvr;
  286. std::unique_ptr<SessionManager> _session_manager;
  287. };

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是存储在服务器端的。它们各有优缺点,通常在实际应用中会结合使用,以达到最佳的用户体验和安全性。


感谢各位大佬支持!!!

互三啦!!!


本文转载自: https://blog.csdn.net/weixin_74809706/article/details/144328796
版权归原作者 枫叶丹4 所有, 如有侵权,请联系我们删除。

“【在Linux世界中追寻伟大的One Piece】HTTP Session”的评论:

还没有评论