-
Notifications
You must be signed in to change notification settings - Fork 2
/
fst_handler.go
55 lines (48 loc) · 1.81 KB
/
fst_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2021 GoFast Author(http://chende.ren). All rights reserved.
// Use of this source code is governed by a MIT license
package fst
import (
"github.com/qinchende/gofast/logx"
"net/http"
"path"
)
// 如果没有配置,添加默认的处理函数
func (gft *GoFast) initDefaultHandlers() {
if len(gft.allRoutes[1].eHds) == 0 {
gft.Reg404(func(c *Context) { c.AbortDirect(http.StatusNotFound, "404 (Not Found)") })
}
if len(gft.allRoutes[2].eHds) == 0 {
gft.Reg405(func(c *Context) { c.AbortDirect(http.StatusMethodNotAllowed, "405 (Method Not Allowed)") })
}
}
// 每次设置都会替换掉以前设置好的方法
// NoRoute adds handlers for NoRoute. It return a 404 code by default.
func (gft *GoFast) Reg404(hds ...CtxHandler) {
gft.regSpecialHandlers(hds, 1)
}
// 每次设置都会替换掉以前设置好的方法
// NoMethod sets the handlers called when...
func (gft *GoFast) Reg405(hds ...CtxHandler) {
gft.regSpecialHandlers(hds, 2)
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// 请求结尾的 '/' 取消或者添加之后重定向,看是否能够匹配到相应路由
func redirectTrailingSlash(c *Context) {
req := c.ReqRaw
p := req.URL.Path
if prefix := path.Clean(c.ReqRaw.Header.Get("X-Forwarded-Prefix")); prefix != "." {
p = prefix + "/" + req.URL.Path
}
req.URL.Path = p + "/"
if length := len(p); length > 1 && p[length-1] == '/' {
req.URL.Path = p[:length-1]
}
// GET 和 非GET 请求重定向状态不一样
code := http.StatusMovedPermanently // Permanent redirect, request with GET method
if req.Method != http.MethodGet {
code = http.StatusTemporaryRedirect
}
rURL := req.URL.String()
logx.InfoF("redirecting request %d: %s --> %s", code, req.URL.Path, rURL)
c.AbortRedirect(code, rURL)
}