-
Notifications
You must be signed in to change notification settings - Fork 288
/
origin.go
58 lines (51 loc) · 1.14 KB
/
origin.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
package server
import (
"net/http"
"net/url"
"unicode/utf8"
)
// Code forked from
// https://github.com/gorilla/websocket
// for doing cross-domain origin checks to work with our dev server.
// checkSameOrigin returns true if the origin is
// 1) not set or
// 2) is equal to the request host or
// 3) is equal to the dev server host
func checkWebsocketOrigin(r *http.Request) bool {
origin := r.Header["Origin"]
if len(origin) == 0 {
return true
}
u, err := url.Parse(origin[0])
if err != nil {
return false
}
dev, err := url.Parse(devServerOrigin)
if err != nil {
return false
}
return equalASCIIFold(u.Host, dev.Host) || equalASCIIFold(u.Host, r.Host)
}
// equalASCIIFold returns true if s is equal to t with ASCII case folding as
// defined in RFC 4790.
func equalASCIIFold(s, t string) bool {
for s != "" && t != "" {
sr, size := utf8.DecodeRuneInString(s)
s = s[size:]
tr, size := utf8.DecodeRuneInString(t)
t = t[size:]
if sr == tr {
continue
}
if 'A' <= sr && sr <= 'Z' {
sr = sr + 'a' - 'A'
}
if 'A' <= tr && tr <= 'Z' {
tr = tr + 'a' - 'A'
}
if sr != tr {
return false
}
}
return s == t
}