Break up do_GET.

This commit is contained in:
ccd0 2015-08-01 12:25:07 -07:00
parent 55e3dd3889
commit 93f1b1442c

View File

@ -16,34 +16,40 @@ class ExtensionReplacer(http.server.BaseHTTPRequestHandler):
def do_GET(self):
if self.headers.get('Host', '').split(':')[0] == 'localhost':
if self.path in resources:
mimeType, data = resources[self.path]
self.send_response(200)
self.send_header('Content-Type', mimeType)
self.send_header('Content-Length', len(data))
self.end_headers()
if self.command != 'HEAD':
self.wfile.write(data)
else:
self.send_error(404)
self.local()
else:
del self.headers['Accept-Encoding']
try:
conn = http.client.HTTPConnection('boards.4chan.org')
conn.request('GET', self.path, headers=self.headers)
response = conn.getresponse()
body = response.read()
finally:
conn.close()
body = body.replace(b'<head>', b'<head><script src="https://ccd0.github.io/4chan-x/builds/4chan-X.user.js"></script>', 1)
self.send_response(response.status, response.reason)
for header, value in response.getheaders():
if header.lower() not in ('date', 'connection', 'transfer-encoding', 'content-length'):
self.send_header(header, value)
self.send_header('Content-Length', len(body))
self.proxy()
def local(self):
if self.path in resources:
mimeType, data = resources[self.path]
self.send_response(200)
self.send_header('Content-Type', mimeType)
self.send_header('Content-Length', len(data))
self.end_headers()
if self.command != 'HEAD':
self.wfile.write(body)
self.wfile.write(data)
else:
self.send_error(404)
def proxy(self):
del self.headers['Accept-Encoding']
try:
conn = http.client.HTTPConnection('boards.4chan.org')
conn.request('GET', self.path, headers=self.headers)
response = conn.getresponse()
body = response.read()
finally:
conn.close()
body = body.replace(b'<head>', b'<head><script src="https://ccd0.github.io/4chan-x/builds/4chan-X.user.js"></script>', 1)
self.send_response(response.status, response.reason)
for header, value in response.getheaders():
if header.lower() not in ('date', 'connection', 'transfer-encoding', 'content-length'):
self.send_header(header, value)
self.send_header('Content-Length', len(body))
self.end_headers()
if self.command != 'HEAD':
self.wfile.write(body)
class ThreadedHTTPServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
pass