/
interface.go
78 lines (69 loc) · 1.66 KB
/
interface.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
// Copyright © 2014 Alienero. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
// The pack use default mux
import (
"encoding/base64"
"net/http"
"strings"
"github.com/Alienero/quick-know/store"
"github.com/golang/glog"
)
type user struct {
ID string
isBreak bool
isOK bool
}
type handler func(w http.ResponseWriter, r *http.Request, uu *user)
type handle struct {
f handler
method string
}
func (h *handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != h.method {
glog.Info("Undefine Method.")
http.Error(w, "", http.StatusMethodNotAllowed)
}
u := new(user)
h.prepare(w, r, u)
if u.isBreak {
glog.Info("Login fail")
http.Error(w, "", http.StatusForbidden)
return
}
glog.Infof("Do the %v method,path:%v", r.Method, r.RequestURI)
h.f(w, r, u)
}
func (h *handle) prepare(w http.ResponseWriter, r *http.Request, u *user) {
// Check the use name and password
temp := r.Header.Get("Authorization")
if len(temp) < 7 {
u.isBreak = true
return
}
auth := strings.TrimLeft(temp, " ")[6:]
buf, err := base64.StdEncoding.DecodeString(auth)
if err != nil {
u.isBreak = true
glog.Errorf("Decode string(%v) from base64 error:%v", auth, err)
return
}
kp := string(buf)
index := strings.Index(kp, ":")
if index < 0 || index > len(kp) {
u.isBreak = true
glog.Error("auth basic string out of range")
return
}
if b, id := store.Manager.Ctrl_login(kp[:index], kp[index+1:]); !b {
u.isBreak = true
} else {
u.ID = id
}
}
func Handle(path, method string, h handler) {
http.Handle(path, &handle{
f: h,
method: method})
}