forked from igm/sockjs-go
/
iframe.go
48 lines (43 loc) · 1.23 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
43
44
45
46
47
48
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()
if _, err := hash.Write([]byte(iframeBody)); err!=nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
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)
if err := tmpl.Execute(rw, h.options.SockJSURL); err!=nil {
http.Error(rw, "could not render iframe content: "+err.Error(), http.StatusInternalServerError)
return
}
}
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>`