-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
49 lines (45 loc) · 897 Bytes
/
util.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
package cproxy
import (
"net/http"
"os"
"path"
"strings"
)
func IsWebSocketRequest(r *http.Request) bool {
contains := func(key, val string) bool {
vv := strings.Split(r.Header.Get(key), ",")
for _, v := range vv {
if val == strings.ToLower(strings.TrimSpace(v)) {
return true
}
}
return false
}
if !contains("Connection", "upgrade") {
return false
}
if !contains("Upgrade", "websocket") {
return false
}
return true
}
func getSplitHostPort(host string) (ip, port string) {
sps := strings.Split(host, ":")
if len(sps) > 1 {
return sps[0], sps[1]
}
return host, ""
}
func WriteToFile(filePath string, data []byte) error {
err := os.MkdirAll(path.Dir(filePath), os.ModePerm)
if err != nil {
return err
}
f, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
return err
}
f.Write(data)
f.Close()
return nil
}