From b3f96c587e92c6d6d49ca191c5ad22fb39b69b30 Mon Sep 17 00:00:00 2001 From: John Date: Mon, 23 Apr 2018 17:06:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3web=20server=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E5=AE=89=E5=85=A8=E6=80=A7=E6=A0=A1=E9=AA=8C=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=88=86=E9=9A=94=E7=AC=A6=E5=8F=B7=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO | 3 ++- g/net/ghttp/http_server_handler.go | 4 ++-- g/net/ghttp/http_server_options.go | 12 ++++++++---- g/os/gview/gview.go | 2 +- geg/net/ghttp/server1.go | 2 +- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/TODO b/TODO index e7f41f9f3..686b18c5e 100644 --- a/TODO +++ b/TODO @@ -3,7 +3,8 @@ ON THE WAY: 2. ghttp.Server平滑重启方案; 3. 更多数据库的ORM支持; 4. 验证码包支持; - +5. https支持; +6. FAQ DONE: 1. gconv完善针对不同类型的判断,例如:尽量减少sprintf("%v", xxx)来执行string类型的转换; diff --git a/g/net/ghttp/http_server_handler.go b/g/net/ghttp/http_server_handler.go index 6c4e576c0..7c87ecdf7 100644 --- a/g/net/ghttp/http_server_handler.go +++ b/g/net/ghttp/http_server_handler.go @@ -93,8 +93,8 @@ func (s *Server)serveFile(r *Request) { if gfile.Separator != "/" { uri = strings.Replace(uri, "/", gfile.Separator, -1) } - path = path + uri - path = gfile.RealPath(path) + path = path + uri + path = gfile.RealPath(path) if path != "" { // 文件/目录访问安全限制:服务的路径必须在ServerRoot下,否则会报错 if len(path) >= len(s.config.ServerRoot) && strings.EqualFold(path[0 : len(s.config.ServerRoot)], s.config.ServerRoot) { diff --git a/g/net/ghttp/http_server_options.go b/g/net/ghttp/http_server_options.go index e591c88e4..cb6f4102d 100644 --- a/g/net/ghttp/http_server_options.go +++ b/g/net/ghttp/http_server_options.go @@ -14,7 +14,6 @@ import ( "strings" "net/http" "crypto/tls" - "path/filepath" "gitee.com/johng/gf/g/os/gfile" ) @@ -105,7 +104,7 @@ func (s *Server)SetMaxHeaderBytes(b int) error { return nil } -// 设置http server参数 - IndexFiles +// 设置http server参数 - IndexFiles,默认展示文件,如:index.html, index.htm func (s *Server)SetIndexFiles(index []string) error { if s.status == 1 { return errors.New("server config cannot be changed while running") @@ -114,7 +113,7 @@ func (s *Server)SetIndexFiles(index []string) error { return nil } -// 设置http server参数 - IndexFolder +// 允许展示访问目录的文件列表 func (s *Server)SetIndexFolder(index bool) error { if s.status == 1 { return errors.New("server config cannot be changed while running") @@ -137,7 +136,12 @@ func (s *Server)SetServerRoot(root string) error { if s.status == 1 { return errors.New("server config cannot be changed while running") } - s.config.ServerRoot = strings.TrimRight(root, string(filepath.Separator)) + // RealPath的作用除了校验地址正确性以外,还转换分隔符号为当前系统正确的文件分隔符号 + path := gfile.RealPath(root) + if path == "" { + return errors.New("invalid root path \"" + root + "\"") + } + s.config.ServerRoot = strings.TrimRight(path, string(gfile.Separator)) return nil } diff --git a/g/os/gview/gview.go b/g/os/gview/gview.go index 4f894efc8..ecf6f3f53 100644 --- a/g/os/gview/gview.go +++ b/g/os/gview/gview.go @@ -90,7 +90,7 @@ func (view *View) Parse(file string, params map[string]interface{}) ([]byte, err } } if content == "" { - return nil, errors.New("invalid tpl \"" + file + "\"") + return nil, errors.New("tpl \"" + file + "\" not found") } // 执行模板解析 view.mu.RLock() diff --git a/geg/net/ghttp/server1.go b/geg/net/ghttp/server1.go index 7474247b4..459152731 100644 --- a/geg/net/ghttp/server1.go +++ b/geg/net/ghttp/server1.go @@ -7,7 +7,7 @@ import ( func main() { s := ghttp.GetServer() s.SetIndexFolder(true) - s.SetServerRoot("C:\\Documents and Settings\\Claymore\\桌面\\gf.test") + s.SetServerRoot("/home/john/Workspace/view") s.SetPort(8199) s.Run() }