-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
155 lines (142 loc) · 3.25 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"bytes"
"crypto/md5"
"encoding/binary"
"fmt"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"net/http"
"net/url"
"os"
"time"
"unsafe"
base14 "github.com/fumiama/go-base16384"
"github.com/fumiama/image-classification-questionnaire-server/configo"
log "github.com/sirupsen/logrus"
)
func getuuid() string {
stamp := time.Now().Unix()
timestruct := [3]uintptr{uintptr(unsafe.Pointer(&stamp)), uintptr(8), uintptr(8)}
timebytes := *(*[]byte)(unsafe.Pointer(×truct))
ima := md5.Sum(timebytes)
uuid, _ := base14.UTF16be2utf8(base14.Encode(ima[:]))
return string(uuid)[:6]
}
// u82int 字节数(大端)组转成int(无符号的)
func u82int(s string) (int, error) {
b, err1 := base14.UTF82utf16be([]byte(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(pbfile string) error {
if exists(pbfile) {
f, err := os.Open(pbfile)
if err == nil {
data, err1 := io.ReadAll(f)
if err1 == nil {
if len(data) > 0 {
return conf.Unmarshal(data)
}
}
return err1
}
return err
}
conf.Upload = make(map[string]string)
conf.Users = make(map[string]*configo.DataVote)
return nil
}
func saveconf() error {
data, err := conf.Marshal()
if err == nil {
f, err1 := os.OpenFile(configfile, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err1 == nil {
defer f.Close()
_, err2 := f.Write(data)
return err2
}
return err1
}
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
}