forked from dgrr/fastws
-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.go
54 lines (47 loc) · 1.04 KB
/
utils.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
package fastws
import (
"net/http"
"reflect"
"unsafe"
"github.com/valyala/fasthttp"
)
// Upgrade returns a RequestHandler for fasthttp doing the upgrading process easier.
func Upgrade(handler RequestHandler) func(ctx *fasthttp.RequestCtx) {
upgr := Upgrader{
Handler: handler,
Compress: true,
}
return upgr.Upgrade
}
// NetUpgrade returns a RequestHandler for net/http doing the upgrading process easier.
func NetUpgrade(handler RequestHandler) func(http.ResponseWriter, *http.Request) {
upgr := NetUpgrader{
Handler: handler,
Compress: true,
}
return upgr.Upgrade
}
func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func s2b(s string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{
Data: sh.Data,
Len: sh.Len,
Cap: sh.Len,
}
return *(*[]byte)(unsafe.Pointer(&bh))
}
func equalsFold(b, s []byte) (equals bool) {
n := len(b)
equals = n == len(s)
if equals {
for i := 0; i < n; i++ {
if equals = b[i]|0x20 == s[i]|0x20; !equals {
break
}
}
}
return
}