Repository navigation
feat(webapp): 会话存储与安全 Cookie 中间件(#70 第一批) - #81
Merged
Merged
Conversation
新增 `uvcpp_session` / `uvcpp_session_store` / `uvcpp_memory_session_store` / `uvcpp_session_middleware`: - **每个请求都有会话**(首次访问建 + 下发 `Set-Cookie`),`sliding` 默认开; - Cookie 属性齐全:HttpOnly / Secure / SameSite / Path / Max-Age; - `uvcpp_session_of(req)` 取当前会话;`set/erase/clear` 在响应 `on_sent` 时 落库 —— 实测中间件链的 `next()` 会先返回、下游 handler 后跑,写在 `next()` 后面的 save 拿的是处理前的会话(登录后下一条请求读不到 uid); - 注销 `uvcpp_session_logout()`(删 store + 过期 Cookie);防会话固定 `session->regenerate()` + `uvcpp_session_set_cookie()`; - store 可插拔:内存实现默认上限 10000、按最早过期逐出,接口要求线程安全 (多循环共用);Redis/MySQL/SQLite 适配器、RBAC/限流/CSRF 留给 #70 后续; - `uvcpp_web_request` 新增请求级属性 API(`set_attribute/attribute/ attribute_as`,`take_from` 时清空),中间件与处理器之间传会话不用加基类。 用例 `web_app_session_func`:建会话(HttpOnly/SameSite/Path/Max-Age)、登录后 读 uid、注销后旧 Cookie 失效且 store 删净、ttl=1 过期、regenerate 后旧 id 立即 失效且新 id 带原数据、secure=true 带 Secure、store 边界(空 id/destroy/load)。 文档 `doc/webapp-guide.md` §22。 本地 build-all 167/167、build-h3/default 编过;五条文档门禁全绿。
Antruly
force-pushed
the
feat/session
branch
from
November 21, 2026 19:03
264a4aa to
5412d41
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #70(第一批:会话存储与安全 Cookie;Redis/RBAC/CSRF 等留后续)。
内容
uvcpp_session:id + 键值数据 + 创建/过期时间;regenerate()(防会话固定)。uvcpp_session_store:可插拔接口;uvcpp_memory_session_store(线程安全、默认上限 10000、按最早过期逐出)。uvcpp_session_middleware(store, opts):Set-Cookie);sliding默认开,每次请求续期;on_sent时落库 —— 实测中间件链的next()先返回、下游 handler 后执行,写在next()后面的 save 会漏掉处理器里的set();regenerate()时旧 id 在响应结束时删除、新 id 由uvcpp_session_set_cookie()下发。uvcpp_session_logout(req, resp, store, opts):删 store + 过期 Cookie。uvcpp_web_request新增请求级属性 API(set_attribute/attribute/attribute_as),take_from()时清空;中间件与处理器之间传对象不用加基类。用例
web_app_session_func(真端口 + 真 Cookie):Set-Cookie带sid、HttpOnly、SameSite=Lax、Path=/、Max-Age=ttl;set())后同 Cookie 读得到uid=42;Max-Age=0(第二条 Set-Cookie),旧 Cookie 立即失效、store 删净;ttl=1过期后旧 Cookie 被拒;regenerate()后旧 id 401、新 id 带原会话数据;secure=true时 Cookie 带 Secure;验证
build-all 167/167、build-h3 / build-default 编过;check_docs / check_doc_lines / check_doc_versions / check_ci_layout 全绿。文档
doc/webapp-guide.md§22。