This repository has been archived by the owner on Oct 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
html.go
187 lines (168 loc) · 5.09 KB
/
html.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
package rewrite
import (
"bytes"
"fmt"
"regexp"
"golang.org/x/net/html"
)
var headTags = []string{"html", "head", "base", "link", "meta", "title", "style", "script", "object", "bgsound"}
var beforeHeadTags = []string{"html", "head"}
var dataRwProtocols = []string{"http://", "https://", "//"}
type HtmlRewriter struct {
urlrw Rewriter
jsRewriter Rewriter
cssRewriter Rewriter
url string
defmod Rewriter
parseComments bool
rewriteTags map[string]map[string]Rewriter
}
func NewHtmlRewriter(urlrw Rewriter, configs ...func(*Config)) *HtmlRewriter {
// c := makeConfig(configs...)
return &HtmlRewriter{
urlrw: urlrw,
rewriteTags: rewriteTags(urlrw),
}
}
func (hrw *HtmlRewriter) Rewrite(p []byte) []byte {
rdr := bytes.NewReader(p)
tokenizer := html.NewTokenizer(rdr)
w := &bytes.Buffer{}
var token html.Token
for {
tt := tokenizer.Next()
// token := tokenizer.Token()
switch tt {
// case html.TextToken:
// case html.CommentToken:
case html.ErrorToken:
// ErrorToken means that an error occurred during tokenization.
// most common is end-of-file (EOF)
if tokenizer.Err().Error() == "EOF" {
return w.Bytes()
}
fmt.Println(tokenizer.Err().Error())
return p
case html.StartTagToken:
name, hasAttr := tokenizer.TagName()
token := html.Token{
Type: html.StartTagToken,
Data: string(name),
}
if hasAttr {
hrw.rewriteToken(&token, tokenizer)
}
w.WriteString(token.String())
continue
case html.SelfClosingTagToken:
name, hasAttr := tokenizer.TagName()
token := html.Token{
Type: html.SelfClosingTagToken,
Data: string(name),
}
if hasAttr {
hrw.rewriteToken(&token, tokenizer)
}
w.WriteString(token.String())
continue
}
token = tokenizer.Token()
w.WriteString(token.String())
}
return w.Bytes()
}
func (hrw *HtmlRewriter) rewriteMetaRefresh(p []byte, metaRefresh *regexp.Regexp) {
}
func (hrw *HtmlRewriter) rewriteToken(t *html.Token, tok *html.Tokenizer) {
attrs := hrw.rewriteTags[t.Data]
for {
key, val, more := tok.TagAttr()
repl := attrs[string(bytes.ToLower(key))]
if repl != nil {
val = repl.Rewrite(val)
}
t.Attr = append(t.Attr, html.Attribute{
Key: string(key),
Val: string(val),
})
if !more {
return
}
}
return
}
func rewriteTags(defmod Rewriter) map[string]map[string]Rewriter {
oe := PrefixRewriter{Prefix: []byte("oe_")}
im := PrefixRewriter{Prefix: []byte("im_")}
if_ := PrefixRewriter{Prefix: []byte("if_")}
fr_ := PrefixRewriter{Prefix: []byte("fr_")}
// js_ := PrefixRewriter{Prefix: []byte("js_")}
return map[string]map[string]Rewriter{
"a": {"href": defmod},
"applet": {"codebase": oe, "archive": oe},
"area": {"href": defmod},
"audio": {"src": oe},
"base": {"href": defmod},
"blockquote": {"cite": defmod},
"body": {"background": im},
"button": {"formaction": defmod},
"command": {"icon": im},
"del": {"cite": defmod},
"embed": {"src": oe},
"head": {"": defmod}, // for head rewriting
"iframe": {"src": if_},
"image": {"src": defmod, "xlink:href": im},
"img": {"src": defmod, "srcset": im},
"ins": {"cite": defmod},
"input": {"src": im, "formaction": defmod},
"form": {"action": defmod},
"frame": {"src": fr_},
"link": {"href": defmod},
"meta": {"content": defmod},
"object": {"codebase": oe, "data": oe},
"param": {"value": oe},
"q": {"cite": defmod},
"ref": {"href": oe},
"script": {"src": defmod},
"source": {"src": oe},
"video": {"src": oe, "poster": im},
}
}
// TODO - resolve older verion:
// func rewriteTags(defmod Rewriter) map[string]map[string]Rewriter {
// oe := PrefixRewriter{Prefix: []byte("oe_")}
// im := PrefixRewriter{Prefix: []byte("im_")}
// if_ := PrefixRewriter{Prefix: []byte("if_")}
// fr_ := PrefixRewriter{Prefix: []byte("fr_")}
// js_ := PrefixRewriter{Prefix: []byte("js_")}
// return map[string]map[string]Rewriter{
// "a": {"href": defmod},
// "applet": {"codebase": oe, "archive": oe},
// "area": {"href": defmod},
// "audio": {"src": oe},
// "base": {"href": defmod},
// "blockquote": {"cite": defmod},
// "body": {"background": im},
// "button": {"formaction": defmod},
// "command": {"icon": im},
// "del": {"cite": defmod},
// "embed": {"src": oe},
// "head": {"": defmod}, // for head rewriting
// "iframe": {"src": if_},
// "image": {"src": im, "xlink:href": im},
// "img": {"src": im, "srcset": im},
// "ins": {"cite": defmod},
// "input": {"src": im, "formaction": defmod},
// "form": {"action": defmod},
// "frame": {"src": fr_},
// "link": {"href": oe},
// "meta": {"content": defmod},
// "object": {"codebase": oe, "data": oe},
// "param": {"value": oe},
// "q": {"cite": defmod},
// "ref": {"href": oe},
// "script": {"src": js_},
// "source": {"src": oe},
// "video": {"src": oe, "poster": im},
// }
// }