From 032d73d016c9815a65fb5aaceebc8544b34d62ca Mon Sep 17 00:00:00 2001 From: John Date: Sun, 26 Aug 2018 20:29:28 +0800 Subject: [PATCH] =?UTF-8?q?ghttp.Server/gspath=E5=8C=85=E9=9D=99=E6=80=81?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=A3=80=E7=B4=A2=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO | 2 +- g/net/ghttp/ghttp_server.go | 2 +- g/net/ghttp/ghttp_server_handler.go | 13 +++++++++---- g/os/gspath/gspath.go | 18 +++++++++++------- geg/other/test.go | 6 +++--- 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/TODO b/TODO index 9dbebd207..3860d4d9c 100644 --- a/TODO +++ b/TODO @@ -23,7 +23,7 @@ ghttp增加返回数据压缩机制; 检查windows下的平滑重启失效问题; gview中的template标签失效问题; gfile文件stat信息使用gfsnotify进行缓存更新改进; - +ghttp.Server增加proxy功能特性,本地proxy和远程proxy,本地即将路由规则映射;远程即反向代理; DONE: diff --git a/g/net/ghttp/ghttp_server.go b/g/net/ghttp/ghttp_server.go index 0a46a3722..ce3156d01 100644 --- a/g/net/ghttp/ghttp_server.go +++ b/g/net/ghttp/ghttp_server.go @@ -59,7 +59,7 @@ const ( type Server struct { // 基本属性变量 name string // 服务名称,方便识别 - paths *gspath.SPath // 静态文件检索对象 + paths *gspath.SPath // 静态文件检索对象(类似nginx tryfile功能) config ServerConfig // 配置对象 servers []*gracefulServer // 底层http.Server列表 methodsMap map[string]struct{} // 所有支持的HTTP Method(初始化时自动填充) diff --git a/g/net/ghttp/ghttp_server_handler.go b/g/net/ghttp/ghttp_server_handler.go index 42c368d30..c23f5b47c 100644 --- a/g/net/ghttp/ghttp_server_handler.go +++ b/g/net/ghttp/ghttp_server_handler.go @@ -140,13 +140,19 @@ func (s *Server)callServeHandler(h *handlerItem, r *Request) { } } -// http server静态文件处理,path必须为完整的**绝对路径** +// http server静态文件处理,path可以为相对路径也可以为绝对路径 func (s *Server)serveFile(r *Request, path string) { - f, err := os.Open(path) - if os.IsExist(err) { + path = s.paths.Search(path) + if path == "" { r.Response.WriteStatus(http.StatusNotFound) return } + f, err := os.Open(path) + if err != nil { + r.Response.WriteStatus(http.StatusForbidden) + return + } + defer f.Close() info, _ := f.Stat() if info.IsDir() { if s.config.IndexFolder { @@ -158,7 +164,6 @@ func (s *Server)serveFile(r *Request, path string) { // 读取文件内容返回, no buffer http.ServeContent(r.Response.Writer, &r.Request, info.Name(), info.ModTime(), f) } - f.Close() } // 目录列表 diff --git a/g/os/gspath/gspath.go b/g/os/gspath/gspath.go index d9718a605..345e7fc2b 100644 --- a/g/os/gspath/gspath.go +++ b/g/os/gspath/gspath.go @@ -68,18 +68,22 @@ func (sp *SPath) Add(path string) error { return errors.New("invalid path:" + path) } -// 按照优先级搜索文件,返回搜索到的文件绝对路径 +// 按照优先级搜索文件,返回搜索到的文件绝对路径,找不到该文件时,返回空字符串 func (sp *SPath) Search(name string) string { path := sp.cache.Get(name) if path == "" { - sp.mu.RLock() - for _, v := range sp.paths { - path = gfile.RealPath(v + gfile.Separator + name) - if path != "" && gfile.Exists(path) { - break + // 优先判断是否给定是已经是一个绝对路径 + 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.RUnlock() } - sp.mu.RUnlock() if path != "" { sp.cache.Set(name, path) sp.addMonitor(name, path) diff --git a/geg/other/test.go b/geg/other/test.go index ceaafd29e..f0df82ecf 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,10 +1,10 @@ package main import ( - "gitee.com/johng/gf/g/util/gutil" - "gitee.com/johng/gf/g/os/genv" + "fmt" + "gitee.com/johng/gf/g/os/gfile" ) func main() { - gutil.Dump(genv.All()) + fmt.Println(gfile.RealPath()) } \ No newline at end of file