| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python3 | |
| 2 | |
| 3 import argparse | |
| 4 import re | |
| 5 import sys | |
| 6 import threading | |
| 7 | |
| 8 from http.server import BaseHTTPRequestHandler, HTTPServer | |
| 9 from string import Template | |
| 10 | |
| 11 PROXY_TOKEN = 'http_' | |
| 12 REGEX = '\\$' + PROXY_TOKEN + '[a-z_0-9]+\\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 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 | |
| 17 class Handler(BaseHTTPRequestHandler): | |
| 18 def get_header_values(self): | |
| 19 values = {} | |
| 20 headers = re.findall(REGEX, self.format) | |
| 21 for name in headers: | |
| 22 new_var = name[len(PROXY_TOKEN):].title() | |
|
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 return values | |
| 25 | |
| 26 def write_info(self, args): | |
| 27 LOCK.acquire() | |
| 28 message = Template(self.format) | |
| 29 if self.log_file and self.log_file is not sys.stdout: | |
|
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') | |
|
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 | |
| 42 def do_POST(self): | |
| 43 status = 200 | |
| 44 content = bytes(self.response, 'UTF-8') | |
| 45 values = { | |
| 46 'remote_addr': self.address_string(), | |
| 47 'time_local': self.log_date_time_string(), | |
| 48 'request': self.requestline, | |
| 49 'status': status, | |
| 50 '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 } | |
| 54 values.update(self.get_header_values()) | |
| 55 self.write_info(values) | |
| 56 self.send_response(status) | |
| 57 self.send_header('Content-Type', values['content_type']) | |
| 58 self.send_header('Content-Length', values['content_length']) | |
| 59 self.end_headers() | |
| 60 self.wfile.write(content) | |
| 61 | |
| 62 | |
| 63 if __name__ == '__main__': | |
| 64 parser = argparse.ArgumentParser() | |
| 65 parser.add_argument('--port', action='store', | |
| 66 default=8000, type=int, | |
| 67 nargs='?', | |
| 68 help='Port to use [default: 8000]') | |
| 69 parser.add_argument('--response', action='store', | |
| 70 type=str, nargs='?', default='OK', | |
| 71 help='The response send to the client') | |
| 72 parser.add_argument('--format', action='store', | |
| 73 type=str, nargs='?', | |
| 74 default=DEFAULT_LOG, | |
| 75 help='Format of the log ouput') | |
| 76 parser.add_argument('log_file', action='store', | |
| 77 type=str, nargs='?', default=sys.stdout, | |
| 78 help='The file where the logs will be written') | |
| 79 args = parser.parse_args() | |
| 80 setattr(Handler, 'format', args.format) | |
| 81 setattr(Handler, 'response', args.response) | |
| 82 setattr(Handler, 'log_file', args.log_file) | |
|
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) | |
| 84 httpd = HTTPServer(server_address, Handler) | |
| 85 httpd.serve_forever() | |
| OLD | NEW |