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 關注
- 163 瀏覽
添加回答
舉報
