1 回答
TA贡献1848条经验 获得超10个赞
从net/http源代码(fs.go):
// toHTTPError returns a non-specific HTTP error message and status code
// for a given non-nil error value. It's important that toHTTPError does not
// actually return err.Error(), since msg and httpStatus are returned to users,
// and historically Go's ServeContent always returned just "404 Not Found" for
// all errors. We don't want to start leaking information in error messages.
func toHTTPError(err error) (msg string, httpStatus int) {
if os.IsNotExist(err) {
return "404 page not found", StatusNotFound
}
if os.IsPermission(err) {
return "403 Forbidden", StatusForbidden
}
// Default:
return "500 Internal Server Error", StatusInternalServerError
}
文件服务器返回 200 和 404 错误的纯文本文件。浏览器尝试将此纯文本错误页面解释为 CSS 文件,并引发错误。
这种返回纯文本文件的行为不能被FileServer().
正如已经指出的那样,这并不是net/http.
如果由于某种原因您不希望这种行为,您可以探索为 404 响应创建自定义处理程序,这已在此线程中进行了探索。您还可以使用像 Gorilla 这样的路由库,它对未找到的页面具有可覆盖的行为。
- 1 回答
- 0 关注
- 177 浏览
添加回答
举报
