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 2 to 4 Created Aug. 7, 2017, 3:11 a.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,74 @@
+#!/usr/bin/env python3
+
+import argparse
+import logging
+import sys
+
+from http.server import BaseHTTPRequestHandler, HTTPServer
+from string import Template
+
+PUNCTUATION = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
+PROXY_TOKEN = 'http_'
+
+
+class Handler(BaseHTTPRequestHandler):
+ def get_header_values(self, log_format):
Vasily Kuznetsov 2017/08/07 13:06:41 What about not passing `log_format`? You can alway
f.lopez 2017/08/07 18:40:01 Done.
+ values = {}
+ for var in log_format.split():
+ name = var.strip()
Vasily Kuznetsov 2017/08/07 13:06:41 Shouldn't you remove punctuation here instead of s
f.lopez 2017/08/07 18:40:01 Done.
+ if name.startswith(PROXY_TOKEN):
+ name = var.strip(PUNCTUATION)
+ new_var = name[len(PROXY_TOKEN):].title()
+ values[name] = self.headers.get(new_var, '-')
+ return values
+
+ def message_log(self, form, args):
Vasily Kuznetsov 2017/08/07 13:06:40 This name is somewhat confusing, looks like a meth
f.lopez 2017/08/07 18:40:02 Done.
+ message = Template(form)
+ logging.info(message.safe_substitute(args))
+
+ 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),
Vasily Kuznetsov 2017/08/07 13:06:40 Oops, double quote :)
f.lopez 2017/08/07 18:40:01 Done.
+ 'Content-Type': 'text/plain',
+ 'Content-Length': len(content),
+ }
+ values.update(self.get_header_values(self.log_format))
+ self.message_log(self.log_format, 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)
+ self.log_request(200)
+
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--port', action='store',
+ default=8000, type=int,
+ nargs='?',
+ help='Specify alternate port [default: 8000]')
Vasily Kuznetsov 2017/08/07 13:06:40 I don't think you need "Specify" here either.
f.lopez 2017/08/07 18:40:02 Done.
+ parser.add_argument('--response', action='store',
+ type=str, nargs='?', default='OK',
+ help='The response send to the client')
+ parser.add_argument('--log_format', action='store',
Vasily Kuznetsov 2017/08/07 13:06:40 Probably having some kind of default log format wo
f.lopez 2017/08/07 18:40:01 Done.
+ type=str, nargs='?',
+ help='Format of the log ouput')
+ parser.add_argument('log_file', action='store',
+ type=str, nargs='?', default=sys.stdout,
Vasily Kuznetsov 2017/08/07 13:06:40 Can you pass `sys.stdout` as `filename` to `loggin
f.lopez 2017/08/07 18:40:02 You are right, I got the error as well, I'm pretty
+ help='The file where the logs will be written')
+ args = parser.parse_args()
+ handler_class = Handler
+ server_class = HTTPServer
+ setattr(handler_class, 'log_format', args.log_format)
+ setattr(handler_class, 'response', args.response)
+ server_address = ('', args.port)
+ httpd = server_class(server_address, handler_class)
+ logging.basicConfig(filename=args.log_file, level=logging.INFO)
+ httpd.serve_forever()
« 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