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 number 8 Created Aug. 7, 2017, 6:42 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,77 @@
+#!/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):
+ values = {}
+ for var in self.log_format.split():
+ name = var.strip(PUNCTUATION)
Vasily Kuznetsov 2017/08/07 20:46:01 You've added a space to PUNCTUATION above, but do
f.lopez 2017/08/08 02:48:17 Done.
+ 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):
+ 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',
Vasily Kuznetsov 2017/08/07 20:46:01 Some other variables, like 'time_local' and 'bytes
f.lopez 2017/08/08 02:48:17 Done.
+ '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)
+
+
+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='$remote_addr - - [$time_local] "$request" $status $bytes_sent',
Vasily Kuznetsov 2017/08/07 20:46:01 This line is too long now, PEP8 (and our style gui
f.lopez 2017/08/08 02:48:17 Done.
+ 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
+ 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)
+ if args.log_file == None:
+ logging.basicConfig(stream=sys.stdout, level=logging.INFO)
+ else:
+ 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