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 comment 26 Created Aug. 14, 2017, 7:01 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 100755
--- /dev/null
+++ b/modules/adblockplus/files/mimeo.py
@@ -0,0 +1,91 @@
+#!/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'
Vasily Kuznetsov 2017/08/15 18:56:25 Square brackets are not necessary around '\w', it
f.lopez 2017/08/16 00:54:31 Done.
+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, '-')
+ 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):
+ _lock.acquire()
+ try:
+ message = Template(self.format)
+ self.output.write(message.safe_substitute(args) + '\n')
+ self.output.flush()
+ except Exception:
Vasily Kuznetsov 2017/08/15 18:56:25 You can just write `except` instead of `except Exc
f.lopez 2017/08/16 00:54:31 Done.
+ traceback.print_exc(file=sys.stderr)
+ self.send_simple_response(500)
Vasily Kuznetsov 2017/08/15 18:56:25 When `write_info` returns, the code will try to se
f.lopez 2017/08/16 00:54:32 What if I use this function to return the code ins
Vasily Kuznetsov 2017/08/16 11:32:27 Yes, this approach can work too, although you woul
+ finally:
+ _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_simple_response(status, 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('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
+ Handler.output = fh
+ Handler.format = args.format
+ Handler.response = args.response
+ server_address = ('', args.port)
+ httpd = HTTPServer(server_address, Handler)
+ try:
+ httpd.serve_forever()
+ except:
+ if args.output != '-':
+ 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