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 21 and 24 Created Aug. 13, 2017, 4:13 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,84 @@
+#!/usr/bin/env python3
+
+import argparse
+import re
+import sys
+import threading
+
+from http.server import BaseHTTPRequestHandler, HTTPServer
+from string import Template
+
+# The token used for the headers passed by nginx is: http_
+REGEX = '\\$http_[a-z_0-9]+\\b'
Vasily Kuznetsov 2017/08/14 10:59:56 It's a bit more elegant to use a raw string here:
+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 write_info(self, args):
+ _lock.acquire()
+ try:
+ message = Template(self.format)
+ self.output.write(message.safe_substitute(args))
+ self.output.flush()
+ except Exception as e:
+ sys.stderr.write(e)
Vasily Kuznetsov 2017/08/14 10:59:56 I've just tested this and it seems to raise a Type
+ 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_response(status)
+ 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('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
+ setattr(Handler, 'output', fh)
+ setattr(Handler, 'format', args.format)
+ setattr(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