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 14 to 17 Created Aug. 9, 2017, 7:51 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
Index: modules/adblockplus/files/mimeo.py
===================================================================
new file mode 100644
--- /dev/null
+++ b/modules/adblockplus/files/mimeo.py
@@ -0,0 +1,85 @@
+#!/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'
Vasily Kuznetsov 2017/08/10 15:05:57 How about making the regex r'\$http_(\w+)\b'? Then
Vasily Kuznetsov 2017/08/11 17:31:47 You don't like my idea? Wouldn't the code become m
+DEFAULT_LOG = '$remote_addr - - [$time_local] "$request" $status $bytes_sent'
+LOCK = threading.Lock()
mathias 2017/08/09 21:09:07 Not sure if an upper-case name is appropriate for
Vasily Kuznetsov 2017/08/10 15:05:57 Yeah, this is not a constant in my book either.
f.lopez 2017/08/10 19:06:34 Done.
+
+
+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):].title()
mathias 2017/08/09 21:09:06 Shouldn't this take the `$` into account, because
f.lopez 2017/08/10 19:06:33 Done.
+ values[name] = self.headers.get(new_var, '-')
+ return values
+
+ def write_info(self, args):
+ LOCK.acquire()
+ message = Template(self.format)
+ if self.log_file and self.log_file is not sys.stdout:
mathias 2017/08/09 21:09:06 This should probably check for `not in ('/dev/stdo
Vasily Kuznetsov 2017/08/10 15:05:57 If it's '/dev/stdout', then you just open it, no?
f.lopez 2017/08/10 19:06:34 Done.
+ fh = open(self.log_file, 'a')
mathias 2017/08/09 21:09:06 Why open and close the file handle with every invo
Vasily Kuznetsov 2017/08/10 15:05:58 Just checked, and apparently this class is instant
f.lopez 2017/08/10 19:06:34 Done.
+ else:
+ fh = sys.stdout
+ fh.write(message.safe_substitute(args))
+
+ if self.log_file is not sys.stdout:
+ fh.close()
+ else:
+ fh.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),
+ 'content_type': 'text/plain',
+ 'content_length': len(content),
mathias 2017/08/09 21:09:07 Since we're emulating Nginx behavior this should a
f.lopez 2017/08/10 19:06:33 Done.
+ }
+ 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('log_file', action='store',
+ type=str, nargs='?', default=sys.stdout,
+ help='The file where the logs will be written')
+ args = parser.parse_args()
+ setattr(Handler, 'format', args.format)
+ setattr(Handler, 'response', args.response)
+ setattr(Handler, 'log_file', args.log_file)
mathias 2017/08/09 21:09:06 Again this is not a log, but regular 'output' or a
f.lopez 2017/08/10 19:06:34 Done.
+ server_address = ('', args.port)
+ httpd = HTTPServer(server_address, Handler)
+ httpd.serve_forever()

Powered by Google App Engine
This is Rietveld