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
/
Copy pathwarc_record.go
244 lines (211 loc) · 6.14 KB
/
warc_record.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package rewrite
import (
"github.com/datatogether/cdxj"
"github.com/datatogether/warc"
"strings"
)
type WarcRecordRewriter struct {
Index cdxj.Writer
Urlrw *UrlRewriter
Cookierw *CookieRewriter
rules map[string]RewriteRule
rewriters map[RewriterType]Rewriter
rewriteTypes map[string]string
contentTypes map[string]string
}
// NewWarcRecordRewriter allocates a Rewriter, config funcs are optional.
// NewWarcRecordRewriter(urlstr) will return a default rewriter that rewrites
// content urls that match the domain of urlstr to relative urls
func NewWarcRecordRewriter(urlstr string, config ...func(o *Config)) *WarcRecordRewriter {
// c := makeConfig(config...)
return &WarcRecordRewriter{
// rewriters: o.Rewriters,
Urlrw: NewHostRelativeUrlRewriter(urlstr),
}
}
// Rewrite exists to conform WarcRecordRewriter to the rewriter interface,
// but doesn't handle malformed data very well. If you're confident that the
// supplied bytes represents a valid warc record, this'll work just fine,
// for better error reporting, use RewriteRecord
func (wrr *WarcRecordRewriter) Rewrite(in []byte) (out []byte) {
inrec, err := warc.UnmarshalRecord(in)
if err != nil {
return in
}
outrec, err := wrr.RewriteRecord(&inrec)
if err != nil {
return in
}
out, err = outrec.Bytes()
if err != nil {
return in
}
return out
}
// RewriteRecord takes a record and rewrites it according to rules defined on the
// Rewriter.
func (wrr *WarcRecordRewriter) RewriteRecord(rec *warc.Record) (*warc.Record, error) {
rwinfo := wrr.rewriteInfo(rec)
// var contentRewriter Rewriter
if !rwinfo.shouldRewriteContent() {
return rec, nil
}
switch rec.Headers[warc.FieldNameWARCIdentifiedPayloadType] {
case "text/html; charset=utf-8":
rw := NewHtmlRewriter(wrr.Urlrw)
body, err := rec.Body()
if err != nil {
return nil, err
}
rec.SetBody(rw.Rewrite(body))
default:
// TODO - lots to finish here
}
return rec, nil
// rule := wrr.rule(wrr.Index)
// contentRewriter := wrr.createRewriter(textType, rule, rwinfo, cdx)
}
func (wrr *WarcRecordRewriter) rewriteInfo(rec *warc.Record) rewriteInfo {
return rewriteInfo{
record: rec,
urlRw: wrr.Urlrw,
cookieRw: wrr.Cookierw,
isContentRw: false, // TODO - determine this value
rewriteTypes: wrr.rewriteTypes,
defaultContentTypes: wrr.contentTypes,
}
}
func (wrr *WarcRecordRewriter) rule(rec cdxj.Writer) RewriteRule {
// TODO - huh?
return Keep
}
// func (wrr *WarcRecordRewriter) createRewriter(textType string) {
// }
// func (wrr *WarcRecordRewriter) rwClass(rule, textType string) (string, string) {
// if textType == "js" {
// }
// }
type rewriteInfo struct {
record *warc.Record
urlRw Rewriter
cookieRw Rewriter
isContentRw bool
rewriteTypes map[string]string
defaultContentTypes map[string]string
}
func (rwi rewriteInfo) shouldRewriteContent() bool {
return true
}
func (rwi rewriteInfo) textTypeAndCharset() (string, string) {
ct := rwi.record.Headers.Get(warc.FieldNameContentType)
parts := strings.Split(ct, ";")
mime := parts[0]
ogTextType := rwi.rewriteTypes[strings.ToLower(mime)]
textType := rwi.resolveTextType(ogTextType)
charset := ""
if textType == "guess-text" || textType == "guess-bin" {
textType = ""
}
if textType == "js" {
// if 'callback=jQuery' in self.url_rewriter.wburl.url or '.json?' in self.url_rewriter.wburl.url:
// text_type = 'json'
}
if (textType != "" && ogTextType != textType) || textType == "html" {
newMime := rwi.defaultContentTypes[textType]
if newMime != "" && newMime != mime {
// newContentType := ct.
// new_content_type = content_type.replace(mime, new_mime)
// self.record.http_headers.replace_header('Content-Type', new_content_type)
}
if len(parts) == 2 {
parts = strings.Split(strings.ToLower(parts[1]), "charset=")
if len(parts) == 2 {
charset = strings.TrimSpace(parts[1])
}
}
}
return textType, charset
}
func (rwi rewriteInfo) resolveTextType(textType string) string {
// mod = self.url_rewriter.wburl.mod
mod := ""
if textType == "css" && mod == "js_" {
textType = "css"
}
isCssOrJs := mod == "js_" || mod == "cs_"
if textType == "guess-text" {
if !isCssOrJs && !(mod == "if_" || mod == "mp_" || mod == "") {
return ""
}
} else if textType == "guess-bin" || textType == "html" {
if !isCssOrJs {
return textType
}
}
return textType
// http.DetectContentType(data)
}
func (rwi rewriteInfo) IsUrlRw() bool {
return true
}
var DefaultWarcRecordRewriters = map[string]Rewriter{
// "header": DefaultHeaderRewriter,
"header": NoopRewriter,
// "cookie": HostScopeCookieRewriter,
"cookie": NoopRewriter,
// "html": HTMLRewriter,
"html": NoopRewriter,
// "html-banner-only": HTMLInsertOnlyRewriter,
"html-banner-only": NoopRewriter,
// "css": CSSRewriter,
"css": NoopRewriter,
// "js": JSLocationOnlyRewriter,
"js": NoopRewriter,
// "js-proxy": JSNoneRewriter,
"js-proxy": NoopRewriter,
// "json": JSONPRewriter,
"json": NoopRewriter,
// "xml": XMLRewriter,
"xml": NoopRewriter,
// "dash": RewriteDASH,
"dash": NoopRewriter,
// "hls": RewriteHLS,
"hls": NoopRewriter,
// "amf": RewriteAMF,
"amf": NoopRewriter,
}
var RewriteTypes = map[string]string{
// HTML
"text/html": "html",
"application/xhtml": "html",
"application/xhtml+xml": "html",
// CSS
"text/css": "css",
// JS
"text/javascript": "js",
"application/javascript": "js",
"application/x-javascript": "js",
// JSON
"application/json": "json",
// HLS
"application/x-mpegURL": "hls",
"application/vnd.apple.mpegurl": "hls",
// DASH
"application/dash+xml": "dash",
// AMF
"application/x-amf": "amf",
// XML -- don"t rewrite xml
//"text/xml": "xml",
//"application/xml": "xml",
//"application/rss+xml": "xml",
// PLAIN
"text/plain": "guess-text",
// DEFAULT or octet-stream
"": "guess-text",
"application/octet-stream": "guess-bin",
}
var defaultContentTypes = map[string]string{
"html": "text/html",
"css": "text/css",
"js": "text/javascript",
}