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 11 Created Aug. 8, 2017, 2:48 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
Index: modules/adblockplus/files/mimeo.py
===================================================================
new file mode 100644
--- /dev/null
+++ b/modules/adblockplus/files/mimeo.py
@@ -0,0 +1,78 @@
+#!/usr/bin/env python3
+
+import argparse
+import logging
+import sys
+
+from http.server import BaseHTTPRequestHandler, HTTPServer
+from string import Template
+
+PUNCTUATION = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
+PROXY_TOKEN = 'http_'
+DEFAULT_LOG = '$remote_addr - - [$time_local] "$request" $status $bytes_sent'
+
+
+class Handler(BaseHTTPRequestHandler):
+ def get_header_values(self):
mathias 2017/08/08 07:51:46 Why all these string operations when a simple `re.
Vasily Kuznetsov 2017/08/08 11:30:51 This particular regexp doesn't quite work, I think
f.lopez 2017/08/09 19:50:47 Done.
+ values = {}
+ for var in self.log_format.split():
+ name = var.strip(PUNCTUATION)
+ if name.startswith(PROXY_TOKEN):
+ new_var = name[len(PROXY_TOKEN):].title()
+ values[name] = self.headers.get(new_var, '-')
+ return values
+
+ def log_info(self, args):
mathias 2017/08/08 07:51:46 While the mimeo output looks like an HTTPd log, it
f.lopez 2017/08/09 19:50:48 Done.
+ message = Template(self.log_format)
+ 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),
+ 'content_type': 'text/plain',
+ 'content_length': len(content),
+ }
+ values.update(self.get_header_values())
+ self.log_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)
+ self.log_request(200)
mathias 2017/08/08 07:51:46 Shouldn't this use the status variable from above
f.lopez 2017/08/09 19:50:47 Done.
+
+
+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('--log_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='?',
+ help='The file where the logs will be written')
+ args = parser.parse_args()
+ handler_class = Handler
+ server_class = HTTPServer
mathias 2017/08/08 07:51:45 Why the aliasing, additional variables?
f.lopez 2017/08/09 19:50:47 Done.
+ setattr(handler_class, 'log_format', args.log_format)
mathias 2017/08/08 07:51:45 Analogous to log_info/write_info abobve, this prop
f.lopez 2017/08/09 19:50:47 Done.
+ setattr(handler_class, 'response', args.response)
mathias 2017/08/08 07:51:45 Why invocations of setattr() instead of assignment
f.lopez 2017/08/09 19:50:48 The HTTPServer (server_class) receives a server ad
Vasily Kuznetsov 2017/08/14 10:59:55 I'm not sure what you mean here. Setting attribute
+ server_address = ('', args.port)
+ httpd = server_class(server_address, handler_class)
+ if args.log_file is None:
+ logging.basicConfig(stream=sys.stdout, level=logging.INFO)
+ else:
+ logging.basicConfig(filename=args.log_file, level=logging.INFO)
+ httpd.serve_forever()

Powered by Google App Engine
This is Rietveld