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 28 to 30 Created Aug. 16, 2017, 4:14 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 import traceback
8 8
9 from http.server import BaseHTTPRequestHandler, HTTPServer 9 from http.server import BaseHTTPRequestHandler, HTTPServer
10 from string import Template 10 from string import Template
11 11
12 # The token used for the headers passed by nginx is: http_ 12 # The token used for the headers passed by nginx is: http_
13 REGEX = r'\$http_\w+\b' 13 REGEX = r'\$http_\w+\b'
14 DEFAULT_LOG = '$remote_addr - - [$time_local] "$request" $status $bytes_sent' 14 DEFAULT_LOG = '$remote_addr - - [$time_local] "$request" $status $bytes_sent'
15 _lock = threading.Lock() 15 _lock = threading.Lock()
16 16
17 17
18 class Handler(BaseHTTPRequestHandler): 18 class Handler(BaseHTTPRequestHandler):
19 def get_header_values(self): 19 def get_header_values(self):
20 values = {} 20 values = {}
21 headers = re.findall(REGEX, self.format) 21 headers = re.findall(REGEX, self.format)
22 for name in headers: 22 for name in headers:
23 new_var = name[6:] 23 new_var = name[6:].replace('_', '-')
24 values[name[1:]] = self.headers.get(new_var, '-') 24 values[name[1:]] = self.headers.get(new_var, '-')
mathias 2017/08/18 08:47:43 Does this understand translation between i.e. `X-F
f.lopez 2017/08/19 00:51:00 No, one has to match them manually. proxy_set_hea
mathias 2017/08/19 15:06:08 No idea what you mean. I was referring to this cod
25 return values 25 return values
26 26
27 def send_simple_response(self, status, response=None): 27 def send_simple_response(self, status, response=None):
28 self.send_response(status) 28 self.send_response(status)
29 self.end_headers() 29 self.end_headers()
30 if response is None: 30 if response is None:
31 response = bytes(self.responses[status][0], 'UTF-8') 31 response = bytes(self.responses[status][0], 'UTF-8')
32 self.wfile.write(response) 32 self.wfile.write(response)
33 33
34 def write_info(self, args): 34 def write_info(self, args):
35 message = Template(self.format).safe_substitute(args) + '\n'
35 with _lock: 36 with _lock:
36 message = Template(self.format) 37 self.output.write(message)
37 self.output.write(message.safe_substitute(args) + '\n')
mathias 2017/08/18 08:47:44 Shouldn't the template stuff happen before, so tha
f.lopez 2017/08/19 00:51:00 Done.
38 self.output.flush() 38 self.output.flush()
39 39
40 def do_POST(self): 40 def do_POST(self):
41 status = 200 41 status = 200
42 content = bytes(self.response, 'UTF-8') 42 content = bytes(self.response, 'UTF-8')
43 values = { 43 values = {
44 'remote_addr': self.address_string(), 44 'remote_addr': self.address_string(),
45 'time_local': self.log_date_time_string(), 45 'time_local': self.log_date_time_string(),
46 'request': self.requestline, 46 'request': self.requestline,
47 'status': status, 47 'status': status,
(...skipping 21 matching lines...) Expand all
69 type=str, nargs='?', 69 type=str, nargs='?',
70 default=DEFAULT_LOG, 70 default=DEFAULT_LOG,
71 help='Format of the log ouput') 71 help='Format of the log ouput')
72 parser.add_argument('output', action='store', 72 parser.add_argument('output', action='store',
73 type=str, nargs='?', default='-', 73 type=str, nargs='?', default='-',
74 help='The file where the logs will be written') 74 help='The file where the logs will be written')
75 args = parser.parse_args() 75 args = parser.parse_args()
76 if args.output and args.output != '-': 76 if args.output and args.output != '-':
77 fh = open(args.output, 'a') 77 fh = open(args.output, 'a')
78 else: 78 else:
79 fh = sys.stdout 79 fh = open(sys.stdout.fileno(), 'w', closefd=False)
mathias 2017/08/18 08:47:43 If you would `os.open(sys.stdout.fileno(), 'w', 0)
Vasily Kuznetsov 2017/08/18 12:49:57 This doesn't work (at least in Python 3.6), but wh
f.lopez 2017/08/19 00:51:00 Done.
80 Handler.output = fh
81 Handler.format = args.format
82 Handler.response = args.response
83 server_address = ('', args.port)
84 httpd = HTTPServer(server_address, Handler)
85 try: 80 try:
mathias 2017/08/18 08:47:44 This exception handling should enclose everything
f.lopez 2017/08/19 00:51:01 Done.
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() 86 httpd.serve_forever()
87 except: 87 finally:
88 if args.output != '-': 88 fh.close()
89 fh.close()
LEFTRIGHT

Powered by Google App Engine
This is Rietveld