forked from centrifugal/centrifugo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
47 lines (39 loc) · 1.44 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
package sockjs
import (
"fmt"
"net/http"
"time"
)
func xhrCors(rw http.ResponseWriter, req *http.Request) {
header := rw.Header()
origin := req.Header.Get("origin")
if origin == "" || origin == "null" {
origin = "*"
}
header.Set("Access-Control-Allow-Origin", origin)
if allowHeaders := req.Header.Get("Access-Control-Request-Headers"); allowHeaders != "" && allowHeaders != "null" {
header.Add("Access-Control-Allow-Headers", allowHeaders)
}
header.Add("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)
}