Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: modules/adblockplus/files/mimeo.py

Issue 29504594: #2687 - Include mimeo python module (Closed)
Patch Set: For comments 20 and 21 Created Aug. 10, 2017, 7:06 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « hiera/roles/web/adblockbrowser.yaml ('k') | modules/adblockplus/manifests/web/mimeo.pp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: modules/adblockplus/files/mimeo.py
===================================================================
new file mode 100644
--- /dev/null
+++ b/modules/adblockplus/files/mimeo.py
@@ -0,0 +1,83 @@
+#!/usr/bin/env python3
+
+import argparse
+import re
+import sys
+import threading
+
+from http.server import BaseHTTPRequestHandler, HTTPServer
+from string import Template
+
+PROXY_TOKEN = 'http_'
+REGEX = '\\$' + PROXY_TOKEN + '[a-z_0-9]+\\b'
+DEFAULT_LOG = '$remote_addr - - [$time_local] "$request" $status $bytes_sent'
+_lock = threading.Lock()
+
+
+class Handler(BaseHTTPRequestHandler):
+ def get_header_values(self):
+ values = {}
+ headers = re.findall(REGEX, self.format)
+ for name in headers:
+ new_var = name[len(PROXY_TOKEN)+1:]
+ values[name[1:]] = self.headers.get(new_var, '-')
+ return values
+
+ def write_info(self, args):
+ _lock.acquire()
+ message = Template(self.format)
Vasily Kuznetsov 2017/08/11 17:31:47 You should probably use try/finally here with the
+ self.info_file.write(message.safe_substitute(args))
+ if self.info_file is sys.stdout:
+ self.info_file.flush()
+ _lock.release()
+
+ def do_POST(self):
+ status = 200
+ content = bytes(self.response, 'UTF-8')
+ values = {
+ 'remote_addr': self.address_string(),
+ 'time_local': self.log_date_time_string(),
+ 'request': self.requestline,
+ 'status': status,
+ 'bytes_sent': len(content),
+ }
+ values.update(self.get_header_values())
+ self.write_info(values)
+ self.send_response(status)
+ self.send_header('Content-Type', values['content_type'])
+ self.send_header('Content-Length', values['content_length'])
+ self.end_headers()
+ self.wfile.write(content)
+
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--port', action='store',
+ default=8000, type=int,
+ nargs='?',
+ help='Port to use [default: 8000]')
+ parser.add_argument('--response', action='store',
+ type=str, nargs='?', default='OK',
+ help='The response send to the client')
+ parser.add_argument('--format', action='store',
+ type=str, nargs='?',
+ default=DEFAULT_LOG,
+ help='Format of the log ouput')
+ parser.add_argument('info_file', action='store',
Vasily Kuznetsov 2017/08/11 17:31:47 I'm not sure if "info_file" is the best name, it s
+ type=str, nargs='?', default='-',
+ help='The file where the logs will be written')
+ args = parser.parse_args()
+ setattr(Handler, 'format', args.format)
+ setattr(Handler, 'response', args.response)
+ server_address = ('', args.port)
+ httpd = HTTPServer(server_address, Handler)
+ if args.info_file and args.info_file != '-':
+ fh = open(args.info_file, 'a')
+ else:
+ fh = sys.stdout
+ setattr(Handler, 'info_file', fh)
Vasily Kuznetsov 2017/08/11 17:31:47 I think it would be more clear to order the steps
+ try:
+ httpd.serve_forever()
+ except:
+ if args.info_file != '-':
+ fh.close()
« no previous file with comments | « hiera/roles/web/adblockbrowser.yaml ('k') | modules/adblockplus/manifests/web/mimeo.pp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld