Skip to content

feat(func): support virtual host#398

Open
PIKACHUIM wants to merge 1 commit intomainfrom
dev-vhost
Open

feat(func): support virtual host#398
PIKACHUIM wants to merge 1 commit intomainfrom
dev-vhost

Conversation

@PIKACHUIM
Copy link
Member

Description / 描述

实现虚拟主机(Virtual Host)功能的完整支持,包含两种工作模式:

模式一:路径重映射(Enabled,Web Hosting 关闭)

将指定域名的访问路径透明映射到后端真实路径,实现"伪静态"效果:

  • 访问 http://example.com/,地址栏保持不变,面包屑显示 🏠Home/
  • 后端 API(/api/fs/list/api/fs/get)自动将请求路径映射到 vhost.Path(如 /123pan/Downloads
  • 下载链接(/p//d/)自动去掉 vhost 路径前缀,保持前端路径一致性,避免路径重复叠加

模式二:Web Hosting(Enabled + Web Hosting 开启)

将指定域名作为静态网站托管,直接返回 index.html 等静态文件内容:

  • 通过 forceContentTypeWriter 包装器强制覆盖响应头中的 Content-Type,确保 HTML 文件在浏览器中正确渲染而非触发下载
  • 在处理前注入 guest 用户到请求 context,解决文件访问时的 401 权限问题

Motivation and Context / 背景

原有虚拟主机功能存在以下问题:

  1. 无限重定向:通过 302 跳转实现路径映射,导致 too many redirects 错误
  2. 地址栏变化:使用 history.replaceState 注入脚本修改地址栏,不符合"伪静态"语义
  3. Web Hosting 文件被下载:代理上游响应头中的 Content-Type 覆盖了正确的 MIME 类型,导致 index.html 被当作 application/octet-stream 下载
  4. 401 权限错误:Web Hosting 模式下调用 internalfs.Get/Link 时,context 中缺少用户信息
  5. 下载链接路径重复:前端拿到的是真实路径(含 vhost 前缀),生成的 /p/ 链接经过中间件再次映射后路径翻倍

How Has This Been Tested? / 测试

  • 配置虚拟主机绑定域名 localhost,映射路径 /123pan/Downloads
  • 访问 http://localhost:5244/,验证:
    • 地址栏保持 http://localhost:5244/ 不变 ✅
    • 面包屑显示 🏠Home/
    • 文件列表正确显示 /123pan/Downloads 的内容 ✅
    • 点击子目录,地址栏变为 /subdir,文件列表正确显示 /123pan/Downloads/subdir 的内容 ✅
    • 下载按钮链接为 /p/filename,不含 vhost 路径前缀 ✅
  • 开启 Web Hosting,上传 index.html 到映射路径:
    • 访问域名,浏览器直接渲染 HTML 页面,不触发下载 ✅
    • 无 401 权限错误 ✅

Checklist / 检查清单

  • I have read the CONTRIBUTING document.
    我已阅读 CONTRIBUTING 文档。
  • I have formatted my code with go fmt or prettier.
    我已使用 go fmtprettier 格式化提交的代码。
  • I have added appropriate labels to this PR (or mentioned needed labels in the description if lacking permissions).
    我已为此 PR 添加了适当的标签(如无权限或需要的标签不存在,请在描述中说明,管理员将后续处理)。
  • I have requested review from relevant code authors using the "Request review" feature when applicable.
    我已在适当情况下使用"Request review"功能请求相关代码作者进行审查。
  • I have updated the repository accordingly (If it's needed).
    我已相应更新了相关仓库(若适用)。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant