-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
114 lines (101 loc) · 2.91 KB
/
http.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
package main
import (
"fmt"
"log"
"net"
"net/http"
"strconv"
"github.com/NYTimes/gziphandler"
)
func redirect_handler(to string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logit(r)
http.Redirect(w, r, to, 302)
}
}
func Redirect(from, to string) {
http.Handle(from, redirect_handler(to))
}
func ServeDir(prefix, path string) {
handler := http.StripPrefix(prefix, http.FileServer(http.Dir(path)))
_handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Server", "https://github.com/weaming/s")
handler.ServeHTTP(w, r)
})
http.Handle(prefix, gziphandler.GzipHandler(_handler))
}
func ServeFile(path, fp string) {
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
logit(r)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Server", "https://github.com/weaming/s")
http.ServeFile(w, r, fp)
})
}
func logit(r *http.Request) {
log.Printf(`%v "%v %v %v"`, r.RemoteAddr, r.Method, r.RequestURI, r.Proto)
}
func getQuery(r *http.Request, name string) (v string) {
v = r.URL.Query().Get("page")
return
}
func getQueryInt(r *http.Request, name string) (v int, err error) {
v_str := getQuery(r, name)
v, err = strconv.Atoi(v_str)
return
}
// BasicAuth wraps a handler requiring HTTP basic auth for it using the given
// username and password and the specified realm, which shouldn't contain quotes.
//
// Most web browser display a dialog with something like:
//
// The website says: "<realm>"
//
// Which is really stupid so you may want to set the realm to a message rather than
// an actual realm.
func BasicAuth(handler http.HandlerFunc, realm string, check func(string, string) bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if !ok || !check(user, pass) {
w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
w.WriteHeader(401)
w.Write([]byte("Unauthorised.\n"))
return
}
handler(w, r)
}
}
func mybasicAuth(handler http.HandlerFunc, username, password string) http.HandlerFunc {
return BasicAuth(handler, "hello", func(user, pass string) bool {
if user == username && pass == password {
//green(fmt.Sprintf("Auth success: name: [%v]; password: [%v]", user, pass))
return true
} else {
red(fmt.Sprintf("Auth fail: name: [%v]; password: [%v]", user, pass))
return false
}
})
}
func GetIntranetIP() (rv []string) {
addrs, err := net.InterfaceAddrs()
if err != nil {
fmt.Println(err.Error())
return
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
rv = append(rv, ipnet.IP.String())
}
}
}
return
}
func HTTPGetQuery(req *http.Request, name, dft string) string {
if values, ok := req.URL.Query()[name]; ok {
return values[len(values)-1]
} else {
return dft
}
}