forked from igm/sockjs-go
/
web.go
68 lines (59 loc) · 1.92 KB
/
web.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
package sockjs
import (
"fmt"
"net/http"
"time"
)
func xhrCorsFactory(opts Options) func(rw http.ResponseWriter, req *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
header := rw.Header()
var corsEnabled bool
var corsOrigin string
if opts.CheckOrigin != nil {
corsEnabled = opts.CheckOrigin(req)
if corsEnabled {
corsOrigin = req.Header.Get("origin")
if corsOrigin == "" {
corsOrigin = "*"
}
}
} else {
corsEnabled = true
corsOrigin = opts.Origin
if corsOrigin == "" {
corsOrigin = req.Header.Get("origin")
}
if corsOrigin == "" || corsOrigin == "null" {
corsOrigin = "*"
}
}
if corsEnabled {
header.Set("Access-Control-Allow-Origin", corsOrigin)
if allowHeaders := req.Header.Get("Access-Control-Request-Headers"); allowHeaders != "" && allowHeaders != "null" {
header.Add("Access-Control-Allow-Headers", allowHeaders)
}
header.Set("Access-Control-Allow-Credentials", "true")
}
}
}
func xhrOptions(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Access-Control-Allow-Methods", "OPTIONS, POST")
rw.WriteHeader(http.StatusNoContent) // 204
}
func cacheFor(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", 365*24*60*60))
rw.Header().Set("Expires", time.Now().AddDate(1, 0, 0).Format(time.RFC1123))
rw.Header().Set("Access-Control-Max-Age", fmt.Sprintf("%d", 365*24*60*60))
}
func noCache(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
}
func welcomeHandler(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("content-type", "text/plain;charset=UTF-8")
fmt.Fprintf(rw, "Welcome to SockJS!\n")
}
func httpError(w http.ResponseWriter, error string, code int) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(code)
fmt.Fprintf(w, error)
}