Files
gf/g/net/ghttp/http_server_handler.go

153 lines
4.4 KiB
Go
Raw Normal View History

2017-12-29 16:03:30 +08:00
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
2018-04-13 15:19:31 +08:00
// 请求处理.
2017-12-31 18:19:58 +08:00
2017-11-23 10:21:28 +08:00
package ghttp
import (
"os"
"fmt"
"sort"
"reflect"
"strings"
2017-11-23 10:21:28 +08:00
"net/url"
"net/http"
"path/filepath"
"gitee.com/johng/gf/g/os/gfile"
2018-04-11 16:06:45 +08:00
"gitee.com/johng/gf/g/encoding/ghtml"
2017-11-23 10:21:28 +08:00
)
2017-12-08 12:03:21 +08:00
// 默认HTTP Server处理入口http包底层默认使用了gorutine异步处理请求所以这里不再异步执行
2017-11-23 10:21:28 +08:00
func (s *Server)defaultHttpHandle(w http.ResponseWriter, r *http.Request) {
2017-12-08 12:03:21 +08:00
s.handleRequest(w, r)
}
// 执行处理HTTP请求
// 首先,查找是否有对应域名的处理接口配置;
// 其次,如果没有对应的自定义处理接口配置,那么走默认的域名处理接口配置;
// 最后,如果以上都没有找到处理接口,那么进行文件处理;
2017-12-08 12:03:21 +08:00
func (s *Server)handleRequest(w http.ResponseWriter, r *http.Request) {
2018-04-11 16:06:45 +08:00
request := newRequest(s, r, w)
2018-04-11 12:05:25 +08:00
if h := s.getHandler(request); h != nil {
s.callHandler(h, request)
2017-11-23 10:21:28 +08:00
} else {
2018-04-11 12:05:25 +08:00
s.serveFile(w, r)
}
}
// 初始化控制器
func (s *Server)callHandler(h *HandlerItem, r *Request) {
2017-12-31 12:15:04 +08:00
// 会话处理
2018-04-10 10:32:37 +08:00
r.Cookie = GetCookie(r)
r.Session = GetSession(r)
2017-12-31 12:15:04 +08:00
// 请求处理
2018-04-09 00:06:09 +08:00
s.callHookHandler(r, "BeforeServe")
2017-12-26 10:13:49 +08:00
if h.faddr == nil {
// 新建一个控制器对象处理请求
2017-12-26 10:13:49 +08:00
c := reflect.New(h.ctype)
c.MethodByName("Init").Call([]reflect.Value{reflect.ValueOf(r)})
2017-12-26 10:13:49 +08:00
c.MethodByName(h.fname).Call(nil)
c.MethodByName("Shut").Call([]reflect.Value{reflect.ValueOf(r)})
2017-12-26 10:13:49 +08:00
} else {
h.faddr(r)
2017-12-26 10:13:49 +08:00
}
2018-04-09 00:06:09 +08:00
s.callHookHandler(r, "AfterServe")
2018-04-09 18:51:46 +08:00
s.callHookHandler(r, "BeforeOutput")
// 输出Cookie
2017-12-31 12:15:04 +08:00
r.Cookie.Output()
2017-12-28 15:21:25 +08:00
// 输出缓冲区
r.Response.OutputBuffer()
2018-04-09 18:51:46 +08:00
s.callHookHandler(r, "AfterOutput")
// 将Request对象指针丢到队列中异步处理
s.closeQueue.PushBack(r)
}
2017-11-23 10:21:28 +08:00
// 处理静态文件请求
func (s *Server)serveFile(w http.ResponseWriter, r *http.Request) {
uri := r.URL.String()
if s.config.ServerRoot != "" {
// 获取文件的绝对路径
path := strings.TrimRight(s.config.ServerRoot, string(filepath.Separator))
path = path + uri
path = gfile.RealPath(path)
2017-12-28 15:21:25 +08:00
if path != "" {
2017-11-23 10:21:28 +08:00
s.doServeFile(w, r, path)
} else {
s.NotFound(w, r)
}
} else {
s.NotFound(w, r)
}
}
// http server静态文件处理
func (s *Server)doServeFile(w http.ResponseWriter, r *http.Request, path string) {
f, err := os.Open(path)
if err != nil {
return
}
info, _ := f.Stat()
if info.IsDir() {
if len(s.config.IndexFiles) > 0 {
for _, file := range s.config.IndexFiles {
fpath := path + "/" + file
if gfile.Exists(fpath) {
f.Close()
s.doServeFile(w, r, fpath)
return
}
}
}
if s.config.IndexFolder {
s.listDir(w, f)
} else {
s.ResponseStatus(w, http.StatusForbidden)
}
} else {
http.ServeContent(w, r, info.Name(), info.ModTime(), f)
}
f.Close()
}
// 目录列表
func (s *Server)listDir(w http.ResponseWriter, f http.File) {
dirs, err := f.Readdir(-1)
if err != nil {
http.Error(w, "Error reading directory", http.StatusInternalServerError)
return
}
sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() })
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "<pre>\n")
for _, d := range dirs {
name := d.Name()
if d.IsDir() {
name += "/"
}
2017-12-07 17:34:51 +08:00
u := url.URL{Path: name}
fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", u.String(), ghtml.SpecialChars(name))
2017-11-23 10:21:28 +08:00
}
fmt.Fprintf(w, "</pre>\n")
}
// 返回http状态码并使用默认配置的字符串返回信息
func (s *Server)ResponseStatus(w http.ResponseWriter, code int) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code)
2017-12-07 17:34:51 +08:00
w.Write([]byte(http.StatusText(code)))
2017-11-23 10:21:28 +08:00
}
// 404
func (s *Server)NotFound(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
}