- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
在 https://www.alexedwards.net/blog/serving-static-sites-with-go ,有一个静态文件服务器在单个目录中为站点提供服务的示例:static
。
File: app.go
...
func main() {
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
log.Println("Listening...")
http.ListenAndServe(":3000", nil)
}
但是,我发现我可以通过以下方式获得相同的结果。
func main() {
fs := http.FileServer(http.Dir(".")) // root at the root directory.
http.Handle("/static/", fs) //leave off the StripPrefix call.
log.Println("Listening...")
http.ListenAndServe(":3000", nil)
}
这样做有任何(性能或安全性)缺点吗?我可以看到,如果我在文件系统上的文件位置与它们所服务的 URL 不匹配,我将不得不使用 StripPrefix
,但在这种情况下,似乎调用了 StripPrefix
是不必要的。
编辑:我忘记说了,但我自己已经研究过了。在性能方面,这似乎不是问题,因为对 FileServer
的调用实际上并没有将文件加载到内存中;它只是存储地址。在安全方面,这似乎完全相同:我尝试使用类似以下内容的目录遍历攻击。
$ curl -i --path-as-is 'http://localhost:3000/static/../sensitive.txt'
但是我对这两个版本都得到了 301 响应,这让我有点吃惊。
最佳答案
当你使用http.ServeMux
handler时是一样的
http.ServeMux
匹配路径前会调用cleanPath函数
https://github.com/golang/go/blob/master/src/net/http/server.go#L2305
func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) {
// snip
path := cleanPath(r.URL.Path)
//snip
}
cleanPath 函数返回 p 的规范路径,消除 .和..元素。
https://github.com/golang/go/blob/master/src/net/http/server.go#L2174-L2193
// cleanPath returns the canonical path for p, eliminating . and .. elements.
func cleanPath(p string) string {
if p == "" {
return "/"
}
if p[0] != '/' {
p = "/" + p
}
np := path.Clean(p)
// path.Clean removes trailing slash except for root;
// put the trailing slash back if necessary.
if p[len(p)-1] == '/' && np != "/" {
// Fast path for common case of p being the string we want:
if len(p) == len(np)+1 && strings.HasPrefix(p, np) {
np = p
} else {
np += "/"
}
}
return np
}
关于go - 我在 FileServer 的根目录有什么不同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50768343/
我正在使用 prestashop,在主题中我添加了自己的目录,里面有一个样式表 然后我在 header.tpl 中链接了样式表 例如 但是,如果我在子目录中安装 prestashop,例如 www.
我有以下目录结构: C:\mywebsites \site_1 \site_2 \site_n 在“site_2”中,我安装了 joomla,但是,我在“额外”文件夹之一中有其他文件夹,其
我有一个家庭作业,要求我使用 bash 脚本在 Linux 终端中打印某些内容。我已经完成了其中的大部分,但我被困在我需要做的最后一件事上......这就是我的教授的措辞 Checks to see
通过 Windows Batch,删除文件夹的所有子目录和子文件而不删除/删除所述父/根文件夹的命令是什么? 到目前为止,这是我尝试过的: ECHO "Good riddance, cache! Mu
我正在将 OpenCV 用于各种对象检测器,但我发现很难编写可移植代码。 例如,要在通过自制软件安装了 OpenCV 的 Mac 上加载人脸检测器,我必须这样写: haar=cv.Load('/usr
目录树的主要部分有root(/)、/USR、/var、/home等等。下面是一个典型的linux目录结构如下: / 根目录 /bin 存放必要的命令 /boot
我被这个非常愚蠢的错误所困扰。我正在尝试使用 bluepy 在 Raspberry Pi 上运行 pytest。 pi@pi:~/bluepy/bluepy $ pytest test_asdf.py
我在 Ubuntu 14 上安装了 MediaWiki,该站点的 URL 为:www.wiki.example.com/mediawiki/ 但是我想将位置更改为 www.wiki.example.c
我已经阅读了文档,但有些事情仍然让我感到困惑,主要与同步文件夹和数据库数据有关。 我想在我的主机上使用以下文件夹结构 ROOT |- workFolder ||- project1 |||- proj
我想在我的 Azure webrole 启动时授予网络服务帐户修改权限(根项目目录),有人知道这样做的方法吗?修改后,我会重置该值以确保安全。主要目的是我需要修改 webconfig 以根据多个节点上
我需要作为自定义构建器的一部分按顺序运行两个程序。 其中一个程序我被困住了,无法处理绝对/相对路径,因此我必须使用构建器的 chdir=1 选项才能运行其操作与目标位于同一目录中。 第二个是位于项目的
Cmake的安装命令 install(TARGETS MyTarget LIBRARY DESTINATION lib) 要求我将共享库安装到子文件夹中。似乎 LIBRARY DESTINATION
我正在尝试运行这个: string webRoot = "http://www.dev/api"; string apiRoot = "http://api.dev"; string path = "
在 android 中,我可以使用以下方法获取手机的可移动外部存储: for (File f : context.getExternalFilesDirs("/")) if (Environm
当我启动 tomcat 时,它从 localhost:8080/开始服务。我希望它从 localhost:8080/aaa 开始服务。我不想用“aaa”webapp 替换“root”webapp,我希
我想在按下按钮时向页面顶部的根 html 标记添加一个类。我遇到的唯一代码是将类添加到具有 id 的 div 元素。 Untitled Document 开始演示 我尝试修改这段代码,但没
我希望了解 ROOT 的 TTreeReader 和 TVector3 类的人可以帮助我。 我正在尝试使用 TTreeReader 读取包含 TVector3 的 TTree。 class MuseS
我已经从已经运行并安装在我本地的服务器上下载了 prestashop 文件选择了默认主题,但未应用任何样式表例如,当我通过 Firebug 检查时,样式表路径是错误的我有 http://localho
我有一个脚本,用于搜索包含特定文件的目录,从当前目录开始向上爬(想想试图找出 .git 目录所在的位置)。 我的方法是这样的: def getDir(self,cwd): path = os.pa
我需要 检查当前目录是否在Git版本控制下 如果是 Git 根目录 在单行 shell 脚本中执行上述操作,成功时应以 0 退出 最佳答案 使用 git-rev-parse 是可行的方法。只需确保将其
我是一名优秀的程序员,十分优秀!