/
info.go
41 lines (36 loc) · 982 Bytes
/
info.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
package sockjs
import (
"encoding/json"
"math/rand"
"net/http"
)
type infoData struct {
Websocket bool `json:"websocket"`
CookieNeeded bool `json:"cookie_needed"`
Origins []string `json:"origins"`
Entropy int32 `json:"entropy"`
}
func createInfoData(ctx *context) infoData {
return infoData{
Websocket: ctx.Websocket,
CookieNeeded: ctx.CookieNeeded,
Origins: []string{"*:*"},
Entropy: rand.Int31(),
}
}
func (ctx *context) infoHandler(rw http.ResponseWriter, req *http.Request) {
header := rw.Header()
setCors(header, req)
setContentType(header, "application/json; charset=UTF-8")
disableCache(header)
rw.WriteHeader(http.StatusOK)
json, _ := json.Marshal(createInfoData(ctx))
rw.Write(json)
}
func infoOptionsHandler(rw http.ResponseWriter, req *http.Request) {
header := rw.Header()
setCors(header, req)
setAllowedMethods(header, req, "OPTIONS, GET")
setExpires(header)
rw.WriteHeader(http.StatusNoContent)
}