This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
139 lines (125 loc) · 2.94 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
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
package main
import (
"bytes"
"crypto/md5"
"encoding/binary"
"fmt"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"net/http"
"net/url"
"os"
"time"
base14 "github.com/fumiama/go-base16384"
"github.com/fumiama/imago"
log "github.com/sirupsen/logrus"
"github.com/fumiama/image-classification-questionnaire-server/configo"
)
func getuuid() string {
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], uint64(time.Now().Unix()))
ima := md5.Sum(buf[:])
uuid, _ := base14.UTF16BE2UTF8(base14.Encode(ima[:]))
return imago.BytesToString(uuid)[:6]
}
// u82int 字节数(大端)组转成int(无符号的)
func u82int(s string) (int, error) {
b, err1 := base14.UTF82UTF16BE(imago.StringToBytes(s))
if err1 != nil {
return 0, err1
}
if len(b) == 3 {
b = append([]byte{0}, b...)
}
bytesBuffer := bytes.NewBuffer(b)
switch len(b) {
case 1:
var tmp uint8
err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
return int(tmp), err
case 2:
var tmp uint16
err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
return int(tmp), err
case 4:
var tmp uint32
err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
return int(tmp), err
default:
return 0, fmt.Errorf("%s", "u82Int bytes lenth is invaild!")
}
}
func exists(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
func userexists(uid string) bool {
_, ok := conf.Users[uid]
return ok
}
func flushconf() {
timer := time.NewTicker(time.Minute)
defer timer.Stop()
for range timer.C {
if confchanged {
err := saveconf()
if err != nil {
log.Errorln("[saveconf] error:", err)
} else {
log.Println("[saveconf] success.")
}
confchanged = false
} else {
log.Debugln("[saveconf] vote not change.")
}
}
}
func loadconf() error {
data, err := storage.GetConf()
if err != nil || len(data) == 0 {
conf.Upload = make(map[string]string)
conf.Users = make(map[string]*configo.DataVote)
return nil
}
return conf.Unmarshal(data)
}
func saveconf() error {
data, err := conf.Marshal()
if err == nil {
err = storage.SaveConf(data)
}
return err
}
func getkeys(m map[string]uint32) []string {
// 数组默认长度为map长度,后面append时,不需要重新申请内存和拷贝,效率很高
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
func getfirst(key string, q *url.Values) string {
keys, ok := (*q)[key]
if ok {
log.Debugln("[getfirst] get query", key, "=", keys[0], ".")
return keys[0]
}
log.Debugln("[getfirst]", key, "has no query.")
return ""
}
func getIP(r *http.Request) string {
forwarded := r.Header.Get("X-FORWARDED-FOR")
if forwarded != "" {
return forwarded
}
return r.RemoteAddr
}
func methodis(m string, resp http.ResponseWriter, req *http.Request) bool {
log.Debugf("[methodis] %v from %v.", req.Method, getIP(req))
if req.Method != m {
http.Error(resp, "405 Method Not Allowed", http.StatusMethodNotAllowed)
return false
}
return true
}