-
Notifications
You must be signed in to change notification settings - Fork 8
/
routing.go
119 lines (107 loc) · 4.09 KB
/
routing.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
package webshell
import (
"log"
"net/http"
"path/filepath"
)
// Simple name for a function capable of handling a route.
type RouteHandler func(w http.ResponseWriter, r *http.Request)
// Simple name for a function that handles basic errors.
type ErrorHandlerFn func(string, string, http.ResponseWriter, *http.Request)
type TemplateErrorHandlerFn func(interface{}, http.ResponseWriter, *http.Request)
// Generic error handlers. They take a message and content-type as a string,
// as well as the HTTP response writer and request, and respond with the
// named error. The http.StatusText function may be used to return the
// text for the error code. Note that you have to explicitly call these; the
// design of Go's http server means it will respond with its own 404 handler
// if a route is not found or if a static file server cannot find the file.
var (
Error400 func(msg, ctype string, w http.ResponseWriter, r *http.Request)
Error401 func(msg, ctype string, w http.ResponseWriter, r *http.Request)
Error403 func(msg, ctype string, w http.ResponseWriter, r *http.Request)
Error404 func(msg, ctype string, w http.ResponseWriter, r *http.Request)
Error405 func(msg, ctype string, w http.ResponseWriter, r *http.Request)
Error429 func(msg, ctype string, w http.ResponseWriter, r *http.Request)
Error500 func(msg, ctype string, w http.ResponseWriter, r *http.Request)
Error501 func(msg, ctype string, w http.ResponseWriter, r *http.Request)
Error502 func(msg, ctype string, w http.ResponseWriter, r *http.Request)
Error503 func(msg, ctype string, w http.ResponseWriter, r *http.Request)
)
// AddRoute adds a route to the app. The route handler needs to match
// the net/http specifications.
func (app *WebApp) AddRoute(path string, handler RouteHandler) {
app.mux.HandleFunc(path, handler)
log.Printf("[+] route %s added\n", path)
}
// AddConditionalRoute adds the route and handler if the condition is
// true.
func (app *WebApp) AddConditionalRoute(condition bool, path string, handler RouteHandler) {
if condition {
app.AddRoute(path, handler)
}
}
// StaticRoute adds a new file server route for static assets.
func (app *WebApp) StaticRoute(route string, path string) {
var err error
if len(route) == 0 {
panic("Invalid route: " + route + " -> " + path)
}
if len(path) == 0 {
panic("Invalid path:" + route + " -> " + path)
} else {
path, err = filepath.Abs(path)
if err != nil {
panic(err)
}
}
app.mux.Handle(route, http.StripPrefix(route, http.FileServer(http.Dir(path))))
log.Printf("static route %s -> %s added\n", route, path)
}
// GenerateErrorHandler returns a RouteHandler function
func GenerateErrorHandler(status int) ErrorHandlerFn {
return func(msg, ctype string, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(status)
w.Header().Add("content-type", ctype)
w.Write([]byte(msg))
}
}
// GenerateTemplateErrorHandler returns a function serving a templated error
func GenerateTemplateErrorHandler(status int, filename string) (hdlr TemplateErrorHandlerFn, err error) {
tpl, err := CompileTemplate(filename)
if err != nil {
return
}
hdlr = func(in interface{}, w http.ResponseWriter, r *http.Request) {
msg, err := BuildTemplate(tpl, in)
if err != nil {
log.Printf("error serving template %d %s: %s\n",
status, filename, err.Error())
return
}
w.WriteHeader(status)
w.Write(msg)
}
return
}
func init() {
Error400 = GenerateErrorHandler(http.StatusBadRequest)
Error401 = GenerateErrorHandler(http.StatusUnauthorized)
Error403 = GenerateErrorHandler(http.StatusForbidden)
Error404 = GenerateErrorHandler(http.StatusNotFound)
Error429 = GenerateErrorHandler(429)
Error500 = GenerateErrorHandler(http.StatusInternalServerError)
Error501 = GenerateErrorHandler(http.StatusNotImplemented)
Error502 = GenerateErrorHandler(http.StatusBadGateway)
Error503 = GenerateErrorHandler(http.StatusServiceUnavailable)
}
// ContentResponder returns the appropriate content-type for the
// request.
func ContentResponder(r *http.Request) string {
accept := r.Header["Accept"][0]
if accept == "" {
return "text/plain"
} else if accept == "*/*" {
return "text/plain"
}
return accept
}