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 28 to 30 Created Aug. 16, 2017, 4:14 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 100755
--- /dev/null
+++ b/modules/adblockplus/files/mimeo.py
@@ -0,0 +1,89 @@
+#!/usr/bin/env python3
+
+import argparse
+import re
+import sys
+import threading
+import traceback
+
+from http.server import BaseHTTPRequestHandler, HTTPServer
+from string import Template
+
+# The token used for the headers passed by nginx is: http_
+REGEX = r'\$http_\w+\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[6:]
+ values[name[1:]] = self.headers.get(new_var, '-')
mathias 2017/08/18 08:47:43 Does this understand translation between i.e. `X-F
f.lopez 2017/08/19 00:51:00 No, one has to match them manually. proxy_set_hea
mathias 2017/08/19 15:06:08 No idea what you mean. I was referring to this cod
+ return values
+
+ def send_simple_response(self, status, response=None):
+ self.send_response(status)
+ self.end_headers()
+ if response is None:
+ response = bytes(self.responses[status][0], 'UTF-8')
+ self.wfile.write(response)
+
+ def write_info(self, args):
+ with _lock:
+ message = Template(self.format)
+ self.output.write(message.safe_substitute(args) + '\n')
mathias 2017/08/18 08:47:44 Shouldn't the template stuff happen before, so tha
f.lopez 2017/08/19 00:51:00 Done.
+ self.output.flush()
+
+ 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())
+ try:
+ self.write_info(values)
+ self.send_simple_response(status, content)
+ except:
+ traceback.print_exc(file=sys.stderr)
+ self.send_simple_response(500)
+
+
+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('output', action='store',
+ type=str, nargs='?', default='-',
+ help='The file where the logs will be written')
+ args = parser.parse_args()
+ if args.output and args.output != '-':
+ fh = open(args.output, 'a')
+ else:
+ fh = sys.stdout
mathias 2017/08/18 08:47:43 If you would `os.open(sys.stdout.fileno(), 'w', 0)
Vasily Kuznetsov 2017/08/18 12:49:57 This doesn't work (at least in Python 3.6), but wh
f.lopez 2017/08/19 00:51:00 Done.
+ Handler.output = fh
+ Handler.format = args.format
+ Handler.response = args.response
+ server_address = ('', args.port)
+ httpd = HTTPServer(server_address, Handler)
+ try:
mathias 2017/08/18 08:47:44 This exception handling should enclose everything
f.lopez 2017/08/19 00:51:01 Done.
+ httpd.serve_forever()
+ except:
+ if args.output != '-':
+ fh.close()

Powered by Google App Engine
This is Rietveld