登录
首页 >  Golang >  Go问答

过滤 embed.FS 会导致 HTTP 服务器上出现 ERR_TOO_MANY_REDIRECTS

来源:stackoverflow

时间:2024-04-09 20:18:34 114浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《过滤 embed.FS 会导致 HTTP 服务器上出现 ERR_TOO_MANY_REDIRECTS》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。

问题内容

我的应用程序运行一个 http 服务器,该服务器提供一些静态文件。大多数文件都可以在 /static/ 下访问,但有些文件(例如 index.html)必须在根目录下访问。

此代码尝试通过将文件嵌入到 embed.fs 中来实现这一点(为了演示,我仅在此处嵌入 index.html):

package main

import (
    "net/http"
    "embed"
    "io/fs"
    "log"
)

//go:embed index.html
var files embed.fs

type primaryfiles struct {}

func (pf *primaryfiles) open(name string) (fs.file, error) {
    // name will be "." for paths / and /index.html, i guess that's a feature
    if name == "." {
        return files.open("index.html")
    }
    return nil, fs.errnotexist
}

func main() {
    http.handle("/", http.fileserver(http.fs(&primaryfiles{})))
    http.handle("/static/", http.stripprefix("/static/", http.fileserver(http.fs(files))))
    log.fatal(http.listenandserve(":8080", nil))
}

现在运行代码时,我可以在 http://localhost:8080/static/http://localhost:8080/static/index.html 上很好地查询 index.html 。但是,在 http://localhost:8080/http://localhost:8080/index.html,浏览器会给我 err_too_many_redirects。为什么会发生这种情况?我该如何修复它?

我已经尝试传递 ".",它会生成一个文件列表,而不是 index.html 内容。我使用的是 go 版本 go1.17.3 darwin/arm64。我还试图弄清楚 curl 发生了什么:

$ curl -v http://localhost:8080/index.html
*   Trying ::1:8080...
* Connected to localhost (::1) port 8080 (#0)
> GET /index.html HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.77.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Location: ./
< Date: Mon, 06 Dec 2021 22:05:50 GMT
< Content-Length: 0
<
* Connection #0 to host localhost left intact

$ curl -v http://localhost:8080/
*   Trying ::1:8080...
* Connected to localhost (::1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.77.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Location: ..//
< Date: Mon, 06 Dec 2021 22:05:12 GMT
< Content-Length: 0
<
* Connection #0 to host localhost left intact

这并不能帮助我理解正在发生的事情 - 好吧。 /index.html 被重定向到 ./,这似乎是有道理的。但是 / 被重定向到 ..// ...我不知道该怎么做。


正确答案


发生的事情部分记录在 http package 中:

作为一种特殊情况,返回的文件服务器会将任何以“/index.html”结尾的请求重定向到同一路径,而没有最终的“index.html”。

因此,在我的 open fn 中看不到任何对 index.html 的请求,因为它重定向到 .

文档没有说明对 . 的请求似乎按如下方式处理:

  • 通过 open 查询 . 的目录。这应该返回一个目录,而我的原始代码没有返回该目录并导致了错误。
  • 如果返回目录,则会在其中搜索文件 index.html,如果存在,则向 open 发出另一个请求。这正是我所错过的。

因此,要修复代码,我需要将 .index.html 的请求通过管道传输到实际文件:

func (pf *primaryFiles) Open(name string) (fs.File, error) {
    if name == "." || name == "index.html" {
        return files.Open(name)
    }
    return nil, fs.ErrNotExist
}

当给定 "." 时,您的 primaryfiles.open 实现返回一个文件而不是目录。这是一个错误。

关于 fs.fs.open 的文档应该引导您到 fs.validpath,其 godoc 声明如下。

net/http.fileserver 依赖于 ../ 上的递归重定向应该为 eventually get to some directory 的事实,但是根据 primaryfiles.open 的工作方式找不到目录。可以说这是增强 net/http.fileserver 的机会,但目前还不清楚。

到这里,我们也就讲完了《过滤 embed.FS 会导致 HTTP 服务器上出现 ERR_TOO_MANY_REDIRECTS》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>