-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
79 lines (70 loc) · 1.76 KB
/
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
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
package magic
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"reflect"
"strings"
"unsafe"
"github.com/klauspost/compress/gzhttp"
)
type Empty = struct{}
type Nothing = Empty
// Must panics if the passed error is not nil
func Must[T any](val T, err error) T {
if err != nil {
panic(err)
}
return val
}
// Compresses the given http.Handler with gzip
func Compressor(h http.Handler) http.HandlerFunc {
return gzhttp.GzipHandler(h)
}
func socketid(id uintptr) json.RawMessage {
v, _ := json.Marshal(fmt.Sprintf("%v", id))
return v
}
func unsafeStringToBytes(s string) (b []byte) {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
bh.Data = sh.Data
bh.Cap = sh.Len
bh.Len = sh.Len
return b
}
func urlToStringWithoutSchemeAndHost(u *url.URL) string {
var buf strings.Builder
if u.Opaque != "" {
buf.WriteString(u.Opaque)
} else {
path := u.EscapedPath()
if path != "" && path[0] != '/' && u.Host != "" {
buf.WriteByte('/')
}
if buf.Len() == 0 {
// RFC 3986 §4.2
// A path segment that contains a colon character (e.g., "this:that")
// cannot be used as the first segment of a relative-path reference, as
// it would be mistaken for a scheme name. Such a segment must be
// preceded by a dot-segment (e.g., "./this:that") to make a relative-
// path reference.
if segment, _, _ := strings.Cut(path, "/"); strings.Contains(segment, ":") {
buf.WriteString("./")
}
}
buf.WriteString(path)
}
if u.ForceQuery || u.RawQuery != "" {
buf.WriteByte('?')
buf.WriteString(u.RawQuery)
}
if u.Fragment != "" {
buf.WriteByte('#')
buf.WriteString(u.EscapedFragment())
}
return buf.String()
}
//go:linkname acceptsGzip gzhttp.acceptsGzip
func acceptsGzip(r *http.Request) bool