-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhtml2canvasproxy.py
313 lines (252 loc) · 10 KB
/
html2canvasproxy.py
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# html2canvas-python-proxy 0.1.0
# Copyright (c) 2017 Guilherme Nascimento (brcontainer@yahoo.com.br)
#
# Released under the MIT license
import random
import time
import hashlib
import urllib2
import urlparse
import base64
import json
import re
import os
class html2canvasproxy:
#Config (If need)
folder = 'images'
timeout = 30
mimetype = 'application/javascript'
#commons
ua = ''
host = ''
scheme = ''
ref = ''
url = ''
http_username = ''
http_password = ''
callback = ''
data = ''
response = ''
status = 0
route_path = '/'
save_path = '/'
prefix = 'htc_'
real_extension = ''
real_mimetype = ''
real_charset = ''
init_exec = time.time()
ccache = 60 * 5 * 1000
cross_domain = False
mimes = [
'image/bmp', 'image/windows-bmp', 'image/ms-bmp',
'image/jpeg', 'image/jpg', 'image/png', 'image/gif',
'text/html', 'application/xhtml', 'application/xhtml+xml',
'image/svg+xml', 'image/svg-xml'
]
def __init__(self, callback, url):
if callback is not None and re.match('[^A-Za-z0-9_[.]\\[\\]]', callback) is not None:
self.set_response('error:Parameter "callback" contains invalid characters (' + callback + ')')
elif url == '' or url is None:
self.set_response('error:No such parameter "url"')
elif html2canvasproxy.is_http_url(url) == False:
self.set_response('error:Only http scheme and https scheme are allowed (' + url + ')')
else:
self.callback = callback
o = urlparse.urlparse(url)
if o.username is not None:
self.http_username = o.username
if o.password is not None:
self.http_password = o.password
if self.http_username != '' or self.http_password != '':
uri = (o.netloc.split('@'))[1]
else:
uri = o.netloc
self.url = o.scheme + '://' + uri + o.path
if o.query != '':
self.url += '?' + o.query
def enable_crossdomain(self):
self.cross_domain = True;
def initiate(self):
if self.status != 0:
return None
self.download_source()
def download_source(self):
headers = { 'User-Agent' : self.ua }
if self.ref != '':
o = urlparse.urlparse(self.ref)
self.scheme = o.scheme
self.host = o.netloc
headers['Referer'] = self.ref
if self.http_username != '' or self.http_password != '':
auth = self.http_username + ':' + self.http_password
auth = auth.encode('ascii')
auth = base64.b64encode(auth)
headers['Authorization'] = 'Basic ' + auth
try:
req = urllib2.Request(self.url, None, headers)
r = urllib2.urlopen(req)
h = r.info()
if h['Content-Type'] != '' and h['Content-Type'] != None:
if re.match('^(image|text|application)\/', h['Content-Type']) is None:
self.set_response('error:Invalid mime-type: ' + h['Content-Type'])
else:
mime = str(re.sub('[;]([\s\S]+)$', '', h['Content-Type'])).strip().lower()
mime = re.sub('/x-', '/', mime)
if mime in self.mimes:
self.data = r.read()
extension = re.sub('^(image|text|application)\/', '', mime)
extension = re.sub('(windows[-]bmp|ms[-]bmp)', 'bmp', extension)
extension = re.sub('(svg[+]xml|svg[-]xml)', 'svg', extension)
extension = extension.replace('xhtml[+]xml', 'xhtml')
extension = extension.replace('jpeg', 'jpg')
self.real_extension = extension
self.real_mimetype = mime
cp = h['Content-Type'].find(';');
if cp != -1:
cp = cp + 1
charset = h['Content-Type']
self.real_charset = ';' + charset[cp:].strip()
self.save_file()
else:
self.set_response('error:Invalid mime-type: ' + h['Content-Type'])
else:
self.set_response('error:No mime-type defined')
r.close()
except urllib2.URLError, e:
self.set_response('error:SOCKET: ' + str(e.reason))
def remove_old_files(self):
a = []
for f in os.listdir(self.save_path):
if f.find(self.prefix) == 0 and os.path.isfile(self.save_path + f) and ((self.init_exec - os.path.getctime(self.save_path + f))) > (self.ccache * 2):
os.unlink(self.save_path + f)
def save_file(self):
file_name = hashlib.sha1(self.url).hexdigest()
tmp_ext = str(random.randrange(1000)) + '_' + str(self.init_exec)
if os.path.isfile(self.save_path + file_name + '.' + tmp_ext):
self.save_file() #try again
else:
f = open(self.save_path + file_name + '.' + tmp_ext, 'wb')
f.write(self.data)
f.close()
if os.path.isfile(self.save_path + file_name + '.' + self.real_extension):
os.remove(self.save_path + file_name + '.' + self.real_extension)
os.rename(self.save_path + file_name + '.' + tmp_ext, self.save_path + file_name + '.' + self.real_extension)
self.set_response(self.scheme + '://' + self.host + self.route_path + file_name + '.' + self.real_extension)
def hostname(self, url):
if url == '' or url is None:
self.set_response('error:No such host in html2canvasproxy.hostname("url")')
if html2canvasproxy.is_http_url(url) == False:
self.set_response('error:Only http scheme and https scheme are allowed in html2canvasproxy.hostname(' + url + ')')
elif self.ref == '':
o = urlparse.urlparse(url)
self.scheme = o.scheme
self.host = o.netloc
def referer(self, url):
if url == '' or url is None:
self.set_response('error:No such referer in html2canvasproxy.referer("url")')
if html2canvasproxy.is_http_url(url) == False:
self.set_response('error:Only http scheme and https scheme are allowed in html2canvasproxy.referer(' + url + ')')
else:
self.ref = url
o = urlparse.urlparse(url)
self.scheme = o.scheme
self.host = o.netloc
def route(self, current_path, route):
if re.match('(^/|[a-zA-Z][:])', current_path) is None:
self.set_response('error:Invalid current_path (' + current_path + ')')
else:
current_path = current_path.replace('\\', '/')
if not os.path.isdir(current_path):
self.set_response('error:Not found ' + current_path)
else:
self.save_path = html2canvasproxy.fix_path(current_path)
self.route_path = '/' + route.strip('/') + '/';
def useragent(self, ua):
self.ua = ua
def result(self):
if self.response == '':
self.initiate()
self.remove_old_files()
if self.callback is None:
return {
'mime': self.real_mimetype,
'data': self.data
}
elif self.cross_domain:
duheader = re.sub('(^"|"$)', '',
json.dumps(self.real_mimetype + self.real_charset)
)
if self.real_extension == 'svg' or re.match('^image/', self.real_mimetype) is None:
return {
'mime': self.mimetype,
'data': self.callback + '("data:' + duheader + ',' + html2canvasproxy.ascii_to_inline(self.data) + '");'
}
else:
return {
'mime': self.mimetype,
'data': self.callback + '("data:' + duheader + ';base64,' + base64.b64encode(self.data) + '");'
}
else:
return {
'mime': self.mimetype,
'data': self.callback + '(' + json.dumps(self.response) + ');'
}
def set_response(self, resp):
if self.response == '':
self.status = 2
self.response = str(resp)
def debug_vars(self):
return self.__dict__
@staticmethod
def ascii_to_inline(str):
i = 0;
x = {
'\n': '%0A',
'\r': '%0D',
' ': '%20',
'"': '%22',
'#': '%23',
'&': '%26',
'\/': '%2F',
'\\': '%5C',
':': '%3A',
'?': '%3F',
'\0': '%00',
'\b': '',
'\t': '%09'
}
for (k, v) in x.items():
str = str.replace(k, v)
return str;
@staticmethod
def fix_path(path):
path = re.sub('(\/|\\\\)$', '', path)
if re.match('[a-zA-Z][:]\\\\', path) is not None:
return path + '\\'
else:
return path + '/'
@staticmethod
def resource(current_path, f):
current_path = html2canvasproxy.fix_path(current_path)
if os.path.isfile(current_path + f):
fileName, ext = os.path.splitext(f)
mime = 'application/octet-stream'
ext = re.sub('^[.]', '', ext)
if re.match('^(jp|bm|gi|pn)', ext) is not None:
mime = 'image/' + ext
elif ext == 'xhtml':
mime = 'application/xml+xhtml'
elif ext == 'svg':
mime = 'image/svg+xml'
elif ext == 'html':
mime = 'text/html'
fullpath = current_path + '/' + f
f = open(fullpath, 'rb')
data = f.read(os.stat(fullpath).st_size)
f.close()
return { 'mime': mime, 'data': data }
else:
return None
@staticmethod
def is_http_url(url):
return re.match('^http(|s)[:][/][/][a-zA-Z0-9]', url) is not None