-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·168 lines (130 loc) · 5.2 KB
/
server.py
File metadata and controls
executable file
·168 lines (130 loc) · 5.2 KB
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
from __future__ import unicode_literals
try:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
except ImportError:
from http.server import BaseHTTPRequestHandler, HTTPServer
try:
from HTMLParser import HTMLParser
except ImportError:
from html.parser import HTMLParser
try:
from StringIO import StringIO
except ImportError:
from io import BytesIO as StringIO
import gzip
try:
from urllib2 import urlopen, Request
except ImportError:
from urllib.request import urlopen, Request
import collections
try:
import urlparse
except ImportError:
import urllib.parse as urlparse
import webbrowser
import re
import config
class Transformer(object):
search_word = re.compile(r'\b([\S]{6})\b', re.IGNORECASE | re.UNICODE)
replace_word = r'\1™'
def __call__(self, *args, **kwargs):
return self.transform_func(*args, **kwargs)
def transform_func(self, text):
return self.search_word.sub(self.replace_word, text)
transformer = Transformer()
class ContentParser(HTMLParser):
# content in those tags excluded from replacement
exclude_text_in_tags = config.EXCLUDE_TAGS
content_tag = config.CONTENT_TAG_FOR_PARSING[0]
content_tag_params = list(config.CONTENT_TAG_FOR_PARSING[1:])
def __init__(self, transform_func):
self.transform_func = transform_func
self.no_replace = False
self.tags_fifo = collections.deque()
self.content_tag_counter = 0
self.buffer = collections.deque()
HTMLParser.__init__(self)
@property
def result_html(self):
return u''.join(self.buffer)
def append_text(self, text):
self.buffer.append(text)
def handle_startendtag(self, tag, attrs):
self.append_text(self.get_starttag_text())
def handle_starttag(self, tag, attrs):
self.append_text(self.get_starttag_text())
if tag == self.content_tag:
self.content_tag_counter += 1
if not self.content_tag_params or attrs == self.content_tag_params:
self.tags_fifo.append({'attrs': attrs, 'depth': self.content_tag_counter})
self.no_replace = tag in self.exclude_text_in_tags
def handle_comment(self, data):
self.append_text(u'<!--%s-->' % data)
def handle_entityref(self, name):
self.append_text(u'&%s;' % name)
def handle_decl(self, decl):
self.append_text(u'<!%s>' % decl)
def handle_charref(self, name):
self.append_text(u'&#%s;' % name)
def handle_endtag(self, tag):
if tag == self.content_tag:
if len(self.tags_fifo) > 0:
last_tag = self.tags_fifo[-1]
if (not self.content_tag_params or last_tag['attrs'] == self.content_tag_params) and \
last_tag['depth'] == self.content_tag_counter:
self.tags_fifo.pop()
self.content_tag_counter -= 1
self.append_text(u'</%s>' % tag)
def handle_data(self, data):
if not self.no_replace and self.tags_fifo:
data = self.transform_func(data)
self.append_text(data)
class HttpProcessor(BaseHTTPRequestHandler):
def send_headers(self, code, params):
self.send_response(code)
for key, value in params.items():
self.send_header(key, value)
self.end_headers()
@staticmethod
def get_encoding(headers):
content_type = headers['content-type']
enc_list = content_type.split('charset=')
return enc_list[-1] if len(enc_list) > 1 else config.DEFAULT_ENCODING
@staticmethod
def unzip_response(response):
if response.info().get('Content-Encoding') == 'gzip':
buffer = StringIO(response.read())
return gzip.GzipFile(fileobj=buffer)
return response
@staticmethod
def create_request(url):
request = Request(url)
request.add_header('Accept-Encoding', 'gzip')
return request
def read_data_and_replace_content(self, response, encoding):
html = response.read().decode(encoding)
content_parser = ContentParser(transformer)
content_parser.feed(html)
return content_parser.result_html.encode(encoding)
def redirect_to_original_site(self):
url_parts = urlparse.urlparse(config.SOURCE_URL)
self.send_headers(302, {
'Location': urlparse.urlunparse([url_parts.scheme, url_parts.netloc, self.path, '', '', ''])
})
def do_GET(self):
if self.path != '/':
# forward all other requests to the original site (images, scripts, etc.)
self.redirect_to_original_site()
return
response = urlopen(self.create_request(config.SOURCE_URL))
encoding = self.get_encoding(response.headers)
response = self.unzip_response(response)
self.send_headers(200, {'Content-Type': 'text/html'})
self.wfile.write(self.read_data_and_replace_content(response, encoding))
server = HTTPServer((config.SERVER_ADDRESS, config.SERVER_PORT), HttpProcessor)
if __name__ == '__main__':
url = 'http://%s:%d/' % (config.SERVER_ADDRESS, config.SERVER_PORT)
print('Open {} in your browser.'.format(url))
if config.OPEN_AT_STARTUP:
webbrowser.open(url)
server.serve_forever()