forked from igm/sockjs-go
/
iframe.go
42 lines (37 loc) · 1 KB
/
iframe.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
package sockjs
import (
"crypto/md5"
"fmt"
"net/http"
"text/template"
)
var tmpl = template.Must(template.New("iframe").Parse(iframeBody))
func (h *handler) iframe(rw http.ResponseWriter, req *http.Request) {
etagReq := req.Header.Get("If-None-Match")
hash := md5.New()
hash.Write([]byte(iframeBody))
etag := fmt.Sprintf("%x", hash.Sum(nil))
if etag == etagReq {
rw.WriteHeader(http.StatusNotModified)
return
}
rw.Header().Set("Content-Type", "text/html; charset=UTF-8")
rw.Header().Add("ETag", etag)
tmpl.Execute(rw, h.options.SockJSURL)
}
var iframeBody = `<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script>
document.domain = document.domain;
_sockjs_onload = function(){SockJS.bootstrap_iframe();};
</script>
<script src="{{.}}"></script>
</head>
<body>
<h2>Don't panic!</h2>
<p>This is a SockJS hidden iframe. It's used for cross domain magic.</p>
</body>
</html>`