Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: modules/adblockplus/files/mimeo.py

Issue 29504594: #2687 - Include mimeo python module (Closed)
Left Patch Set: For comments 14 to 17 Created Aug. 9, 2017, 7:51 p.m.
Right Patch Set: For comments 47 and 48 Created Aug. 22, 2017, 8:33 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « hiera/roles/web/adblockbrowser.yaml ('k') | modules/adblockplus/manifests/web/mimeo.pp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2 2
3 import argparse 3 import argparse
4 import re 4 import re
5 import sys 5 import sys
6 import threading 6 import threading
7 import traceback
7 8
8 from http.server import BaseHTTPRequestHandler, HTTPServer 9 from http.server import BaseHTTPRequestHandler, HTTPServer
9 from string import Template 10 from string import Template
10 11
11 PROXY_TOKEN = 'http_' 12 # The token used for the headers passed by nginx is: http_
12 REGEX = '\\$' + PROXY_TOKEN + '[a-z_0-9]+\\b' 13 REGEX = r'\$http_\w+\b'
Vasily Kuznetsov 2017/08/10 15:05:57 How about making the regex r'\$http_(\w+)\b'? Then
Vasily Kuznetsov 2017/08/11 17:31:47 You don't like my idea? Wouldn't the code become m
13 DEFAULT_LOG = '$remote_addr - - [$time_local] "$request" $status $bytes_sent' 14 DEFAULT_LOG = '$remote_addr - - [$time_local] "$request" $status $bytes_sent'
14 LOCK = threading.Lock() 15 _lock = threading.Lock()
mathias 2017/08/09 21:09:07 Not sure if an upper-case name is appropriate for
Vasily Kuznetsov 2017/08/10 15:05:57 Yeah, this is not a constant in my book either.
f.lopez 2017/08/10 19:06:34 Done.
15 16
16 17
17 class Handler(BaseHTTPRequestHandler): 18 class Handler(BaseHTTPRequestHandler):
18 def get_header_values(self): 19 def get_header_values(self):
19 values = {} 20 values = {}
20 headers = re.findall(REGEX, self.format) 21 headers = re.findall(REGEX, self.format)
21 for name in headers: 22 for name in headers:
22 new_var = name[len(PROXY_TOKEN):].title() 23 new_var = name[6:].replace('_', '-')
mathias 2017/08/09 21:09:06 Shouldn't this take the `$` into account, because
f.lopez 2017/08/10 19:06:33 Done.
23 values[name] = self.headers.get(new_var, '-') 24 values[name[1:]] = self.headers.get(new_var, '-')
24 return values 25 return values
25 26
27 def send_simple_response(self, status, response=None):
28 self.send_response(status)
29 self.end_headers()
30 if response is None:
31 response = bytes(self.responses[status][0], 'UTF-8')
32 self.wfile.write(response)
33
26 def write_info(self, args): 34 def write_info(self, args):
27 LOCK.acquire() 35 message = Template(self.format).safe_substitute(args) + '\n'
28 message = Template(self.format) 36 with _lock:
29 if self.log_file and self.log_file is not sys.stdout: 37 self.output.write(message)
mathias 2017/08/09 21:09:06 This should probably check for `not in ('/dev/stdo
Vasily Kuznetsov 2017/08/10 15:05:57 If it's '/dev/stdout', then you just open it, no?
f.lopez 2017/08/10 19:06:34 Done.
30 fh = open(self.log_file, 'a') 38 self.output.flush()
mathias 2017/08/09 21:09:06 Why open and close the file handle with every invo
Vasily Kuznetsov 2017/08/10 15:05:58 Just checked, and apparently this class is instant
f.lopez 2017/08/10 19:06:34 Done.
31 else:
32 fh = sys.stdout
33 fh.write(message.safe_substitute(args))
34
35 if self.log_file is not sys.stdout:
36 fh.close()
37 else:
38 fh.flush()
39
40 LOCK.release()
41 39
42 def do_POST(self): 40 def do_POST(self):
43 status = 200 41 status = 200
44 content = bytes(self.response, 'UTF-8') 42 content = bytes(self.response, 'UTF-8')
45 values = { 43 values = {
46 'remote_addr': self.address_string(), 44 'remote_addr': self.address_string(),
47 'time_local': self.log_date_time_string(), 45 'time_local': self.log_date_time_string(),
48 'request': self.requestline, 46 'request': self.requestline,
49 'status': status, 47 'status': status,
50 'bytes_sent': len(content), 48 'bytes_sent': len(content),
51 'content_type': 'text/plain',
52 'content_length': len(content),
mathias 2017/08/09 21:09:07 Since we're emulating Nginx behavior this should a
f.lopez 2017/08/10 19:06:33 Done.
53 } 49 }
54 values.update(self.get_header_values()) 50 values.update(self.get_header_values())
55 self.write_info(values) 51 try:
56 self.send_response(status) 52 self.write_info(values)
57 self.send_header('Content-Type', values['content_type']) 53 self.send_simple_response(status, content)
58 self.send_header('Content-Length', values['content_length']) 54 except:
59 self.end_headers() 55 traceback.print_exc(file=sys.stderr)
60 self.wfile.write(content) 56 self.send_simple_response(500)
61 57
62 58
63 if __name__ == '__main__': 59 if __name__ == '__main__':
64 parser = argparse.ArgumentParser() 60 parser = argparse.ArgumentParser()
65 parser.add_argument('--port', action='store', 61 parser.add_argument('--port', action='store',
66 default=8000, type=int, 62 default=8000, type=int,
67 nargs='?', 63 nargs='?',
68 help='Port to use [default: 8000]') 64 help='Port to use [default: 8000]')
69 parser.add_argument('--response', action='store', 65 parser.add_argument('--response', action='store',
70 type=str, nargs='?', default='OK', 66 type=str, nargs='?', default='OK',
71 help='The response send to the client') 67 help='The response send to the client')
72 parser.add_argument('--format', action='store', 68 parser.add_argument('--format', action='store',
73 type=str, nargs='?', 69 type=str, nargs='?',
74 default=DEFAULT_LOG, 70 default=DEFAULT_LOG,
75 help='Format of the log ouput') 71 help='Format of the log ouput')
76 parser.add_argument('log_file', action='store', 72 parser.add_argument('output', action='store',
77 type=str, nargs='?', default=sys.stdout, 73 type=str, nargs='?', default='-',
78 help='The file where the logs will be written') 74 help='The file where the logs will be written')
79 args = parser.parse_args() 75 args = parser.parse_args()
80 setattr(Handler, 'format', args.format) 76 if args.output and args.output != '-':
81 setattr(Handler, 'response', args.response) 77 fh = open(args.output, 'a')
82 setattr(Handler, 'log_file', args.log_file) 78 else:
mathias 2017/08/09 21:09:06 Again this is not a log, but regular 'output' or a
f.lopez 2017/08/10 19:06:34 Done.
83 server_address = ('', args.port) 79 fh = open(sys.stdout.fileno(), 'w', closefd=False)
84 httpd = HTTPServer(server_address, Handler) 80 try:
85 httpd.serve_forever() 81 Handler.output = fh
82 Handler.format = args.format
83 Handler.response = args.response
84 server_address = ('', args.port)
85 httpd = HTTPServer(server_address, Handler)
86 httpd.serve_forever()
87 finally:
88 fh.close()
LEFTRIGHT

Powered by Google App Engine
This is Rietveld