-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_http_web.go
149 lines (132 loc) · 3.45 KB
/
handler_http_web.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"expvar"
"net"
"net/http"
"net/http/pprof"
"net/netip"
"strings"
"text/template"
"github.com/phuslu/log"
)
type HTTPWebHandler struct {
Config HTTPConfig
Transport *http.Transport
Functions template.FuncMap
wildcards []struct {
location string
handler HTTPHandler
}
mux *http.ServeMux
}
func (h *HTTPWebHandler) Load() error {
type router struct {
location string
handler HTTPHandler
}
var routers []router
for _, web := range h.Config.Web {
switch {
case web.Cgi.Enabled:
routers = append(routers, router{
web.Location,
&HTTPWebCgiHandler{
Location: web.Location,
Root: web.Cgi.Root,
DefaultApp: web.Cgi.DefaultAPP,
},
})
case web.Dav.Enabled:
routers = append(routers, router{
web.Location,
&HTTPWebDavHandler{
Root: web.Dav.Root,
AuthBasicUserFile: web.Dav.AuthBasicUserFile,
},
})
case web.Index.Root != "" || web.Index.Body != "" || web.Index.File != "":
routers = append(routers, router{
web.Location,
&HTTPWebIndexHandler{
Functions: h.Functions,
Location: web.Location,
Root: web.Index.Root,
Headers: web.Index.Headers,
Body: web.Index.Body,
File: web.Index.File,
},
})
case web.Proxy.Pass != "":
routers = append(routers, router{
web.Location,
&HTTPWebProxyHandler{
Transport: h.Transport,
Functions: h.Functions,
Pass: web.Proxy.Pass,
AuthBasicUserFile: web.Proxy.AuthBasicUserFile,
SetHeaders: web.Proxy.SetHeaders,
DumpFailure: web.Proxy.DumpFailure,
},
})
}
}
var root HTTPHandler
h.mux = http.NewServeMux()
for _, x := range routers {
err := x.handler.Load()
if err != nil {
log.Fatal().Err(err).Str("web_location", x.location).Msgf("%T.Load() return error: %+v", x.handler, err)
}
log.Info().Str("web_location", x.location).Msgf("%T.Load() ok", x.handler)
if x.location == "/" {
root = x.handler
continue
}
if strings.ContainsAny(x.location, "*?[]") {
h.wildcards = append(h.wildcards, x)
continue
}
h.mux.Handle(x.location, x.handler)
}
h.mux.HandleFunc("/debug/", func(rw http.ResponseWriter, req *http.Request) {
if ap, err := netip.ParseAddrPort(req.RemoteAddr); err == nil && !ap.Addr().IsLoopback() && !ap.Addr().IsPrivate() {
http.Error(rw, "403 forbidden", http.StatusForbidden)
return
}
switch req.URL.Path {
case "/debug/vars":
expvar.Handler().ServeHTTP(rw, req)
case "/debug/pprof/cmdline":
pprof.Cmdline(rw, req)
case "/debug/pprof/profile":
pprof.Profile(rw, req)
case "/debug/pprof/symbol":
pprof.Symbol(rw, req)
case "/debug/pprof/trace":
pprof.Trace(rw, req)
default:
pprof.Index(rw, req)
}
})
h.mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
if root != nil {
root.ServeHTTP(rw, req)
return
}
http.NotFound(rw, req)
})
return nil
}
func (h *HTTPWebHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if config, _ := h.Config.ServerConfig[req.Host]; !config.DisableHttp3 && req.ProtoMajor != 3 {
_, port, _ := net.SplitHostPort(req.Context().Value(http.LocalAddrContextKey).(net.Addr).String())
rw.Header().Add("Alt-Svc", `h3=":`+port+`"; ma=2592000,h3-29=":`+port+`"; ma=2592000`)
}
for _, x := range h.wildcards {
if WildcardMatch(x.location, req.URL.Path) {
x.handler.ServeHTTP(rw, req)
return
}
}
h.mux.ServeHTTP(rw, req)
}