-
Notifications
You must be signed in to change notification settings - Fork 1
/
padder.go
198 lines (178 loc) · 4.51 KB
/
padder.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) 2014 SameGoal LLC. All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package wc
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"net/http"
"unicode/utf16"
"unicode/utf8"
)
type paddingType int
const (
none paddingType = iota
length
script
)
var (
scriptStart = template.Must(template.New("").Parse("<html><body>" +
"{{if .Domain}}<script>try{document.domain='{{.Domain}}'}catch(e){}" +
"</script>{{end}}7cca69475363026330a0d99468e88d23ce95e22259112644301" +
"5f5f462d9a177186c8701fb45a6ffee0daf1a178fc0f58cd309308fba7e6f011ac38c9c" +
"dd4580760f1d4560a84d5ca0355ecbbed2ab715a3350fe0c479050640bd0e77acec90c5" +
"8c4d3dd0f5cf8d4510e68c8b12e087bd88cad349aafd2ab16b07b0b1b8276091217a44a" +
"9fe92fedacffff48092ee693af\n"))
scriptMessage = template.Must(template.New("").Parse(
"<script>try{parent.m('{{.Message}}')}catch(e){}</script>\n"))
scriptEnd = template.Must(template.New("").Parse(
"<script>try{parent.d()}catch(e){}</script>"))
)
type padder struct {
w http.ResponseWriter
f http.Flusher
t paddingType
setup bool
domain string
}
type startData struct {
Domain string
}
type messageData struct {
Message string
}
func jsonArray(vals []interface{}) string {
replyJSON, err := json.Marshal(vals)
if err != nil {
panic(err)
}
return string(replyJSON)
}
func jsonObject(vals map[string]interface{}) string {
replyJSON, err := json.Marshal(vals)
if err != nil {
panic(err)
}
return string(replyJSON)
}
func guessType(r *http.Request) paddingType {
if r.FormValue("TYPE") == "html" {
return script
}
return length
}
func newPadder(w http.ResponseWriter, r *http.Request) *padder {
f, ok := w.(http.Flusher)
if !ok {
panic("webserver doesn't support flushing")
}
t := guessType(r)
return &padder{w, f, t, false, r.FormValue("DOMAIN")}
}
func (p *padder) start() error {
p.setup = true
header := p.w.Header()
// All WebChannel traffic must not be cached by the browser or proxies
header.Set("Expires", "Fri, 01 Jan 1990 00:00:00 GMT")
header.Set("Cache-Control", "max-age=0, must-revalidate, private")
// X-Content-Type-Options is required on Chrome for incremental
// XMLHttpRequest HTTP chunk processing with Content-Type text/plain.
header.Set("X-Content-Type-Options", "nosniff")
switch p.t {
case script:
header.Set("Content-Type", "text/html; charset=utf-8")
d := startData{p.domain}
if err := scriptStart.Execute(p.w, d); err != nil {
return err
}
default:
header.Set("Content-Type", "text/plain; charset=utf-8")
}
return nil
}
func (p *padder) prepMessages(msgs []*Message) string {
buf := new(bytes.Buffer)
buf.Write([]byte("["))
for i, msg := range msgs {
if i != 0 {
buf.Write([]byte(","))
}
fmt.Fprintf(buf, "[%d,%s]", msg.ID, msg.Body)
}
buf.Write([]byte("]"))
return buf.String()
}
func (p *padder) chunkMessages(msgs []*Message) error {
return p.chunk(p.prepMessages(msgs))
}
func (p *padder) writeMessages(msgs []*Message) error {
return p.write(p.prepMessages(msgs))
}
func (p *padder) chunk(b string) error {
if err := p.writeInternal(b); err != nil {
return err
}
p.f.Flush()
return nil
}
func (p *padder) write(b string) error {
if err := p.writeInternal(b); err != nil {
return err
}
if err := p.end(); err != nil {
return err
}
return nil
}
func (p *padder) writeInternal(b string) error {
if !p.setup {
if err := p.start(); err != nil {
return err
}
}
switch p.t {
case script:
d := messageData{b}
if err := scriptMessage.Execute(p.w, d); err != nil {
return err
}
case length:
runeCount := utf8.RuneCountInString(b)
jsLength := runeCount
for _, r := range []rune(b) {
// Internally js uses utf-16 for strings (after parsing them out of a
// utf-8 context). In utf-16, non-bmp characters (code points >= U+10000)
// are represented as surrogate pairs (length 2, not 1). Double count
// code points represented as surrogate pairs in JS
// http://mathiasbynens.be/notes/javascript-encoding
if r1, r2 := utf16.EncodeRune(r); r1 != '\uFFFD' && r2 != '\uFFFD' {
jsLength++
}
}
if _, err := fmt.Fprintf(p.w, "%d\n%s", jsLength, b); err != nil {
return err
}
default:
if _, err := p.w.Write([]byte(b)); err != nil {
return err
}
}
return nil
}
func (p *padder) end() error {
if !p.setup {
if err := p.start(); err != nil {
return err
}
}
if p.t == script {
d := struct{}{}
if err := scriptEnd.Execute(p.w, d); err != nil {
return err
}
p.f.Flush()
}
return nil
}