forked from igm/sockjs-go
/
htmlfile.go
64 lines (56 loc) · 1.64 KB
/
htmlfile.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
package sockjs
import (
"fmt"
"io"
"net/http"
"regexp"
"strings"
)
var iframeTemplate = `<!doctype html>
<html><head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head><body><h2>Don't panic!</h2>
<script>
document.domain = document.domain;
var c = parent.%s;
c.start();
function p(d) {c.message(d);};
window.onload = function() {c.stop();};
</script>
`
var invalidCallback = regexp.MustCompile("[^a-zA-Z0-9\\_\\.]")
func init() {
iframeTemplate += strings.Repeat(" ", 1024-len(iframeTemplate)+14)
iframeTemplate += "\r\n\r\n"
}
func (h *handler) htmlFile(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("content-type", "text/html; charset=UTF-8")
req.ParseForm()
callback := req.Form.Get("c")
if callback == "" {
http.Error(rw, `"callback" parameter required`, http.StatusInternalServerError)
return
} else if invalidCallback.MatchString(callback) {
http.Error(rw, `invalid character in "callback" parameter`, http.StatusBadRequest)
return
}
rw.WriteHeader(http.StatusOK)
fmt.Fprintf(rw, iframeTemplate, callback)
rw.(http.Flusher).Flush()
sess, _ := h.sessionByRequest(req)
recv := newHTTPReceiver(rw, h.options.ResponseLimit, new(htmlfileFrameWriter))
if err := sess.attachReceiver(recv); err != nil {
recv.sendFrame(cFrame)
recv.close()
return
}
select {
case <-recv.doneNotify():
case <-recv.interruptedNotify():
}
}
type htmlfileFrameWriter struct{}
func (*htmlfileFrameWriter) write(w io.Writer, frame string) (int, error) {
return fmt.Fprintf(w, "<script>\np(%s);\n</script>\r\n", quote(frame))
}