From d46b17f673c1b1d2f342bcd0e4436bbecb3c4d59 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 26 Aug 2018 21:36:39 +0800 Subject: [PATCH] =?UTF-8?q?gspath=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/net/ghttp/ghttp_server_handler.go | 5 ++++- g/os/gspath/gspath.go | 17 +++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/g/net/ghttp/ghttp_server_handler.go b/g/net/ghttp/ghttp_server_handler.go index c23f5b47c..546b46e4b 100644 --- a/g/net/ghttp/ghttp_server_handler.go +++ b/g/net/ghttp/ghttp_server_handler.go @@ -142,7 +142,10 @@ func (s *Server)callServeHandler(h *handlerItem, r *Request) { // http server静态文件处理,path可以为相对路径也可以为绝对路径 func (s *Server)serveFile(r *Request, path string) { - path = s.paths.Search(path) + // 首先判断是否给定的path已经是一个绝对路径 + if !gfile.Exists(path) { + path = s.paths.Search(path) + } if path == "" { r.Response.WriteStatus(http.StatusNotFound) return diff --git a/g/os/gspath/gspath.go b/g/os/gspath/gspath.go index 345e7fc2b..bd7b2cbd5 100644 --- a/g/os/gspath/gspath.go +++ b/g/os/gspath/gspath.go @@ -69,21 +69,18 @@ func (sp *SPath) Add(path string) error { } // 按照优先级搜索文件,返回搜索到的文件绝对路径,找不到该文件时,返回空字符串 +// 给定的name只是相对文件路径,或者只是一个文件名 func (sp *SPath) Search(name string) string { path := sp.cache.Get(name) if path == "" { - // 优先判断是否给定是已经是一个绝对路径 - path = gfile.RealPath(name) - if path == "" { - sp.mu.RLock() - for _, v := range sp.paths { - path = gfile.RealPath(v + gfile.Separator + name) - if path != "" && gfile.Exists(path) { - break - } + sp.mu.RLock() + for _, v := range sp.paths { + path = gfile.RealPath(v + gfile.Separator + name) + if path != "" && gfile.Exists(path) { + break } - sp.mu.RUnlock() } + sp.mu.RUnlock() if path != "" { sp.cache.Set(name, path) sp.addMonitor(name, path)