OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
epoger
2013/07/24 17:46:53
I don't remember where this is documented, but for
Zach Reizner
2013/07/24 20:16:36
According to http://www.python.org/dev/peps/pep-00
| |
2 # -*- coding: utf-8 -*- | |
3 | |
4 from __future__ import print_function | |
5 import BaseHTTPServer | |
6 import os | |
7 import os.path | |
8 | |
9 # A simple dictionary of file name extensions to MIME types. The empty string en try is used as the | |
10 # default when no extension was given or if the extension has no entry in this d ictionary. | |
11 MIME_TYPE_MAP = {'': 'application/octet-stream', | |
12 'html': 'text/html', | |
13 'css': 'text/css', | |
14 'png': 'image/png', | |
15 'js': 'application/javascript' | |
16 } | |
17 | |
18 | |
19 class SkPDiffHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
20 def send_file(self, file_path): | |
21 # Grab the extension if there is one | |
22 extension = os.path.splitext(file_path)[1] | |
23 if len(extension) >= 1: | |
epoger
2013/07/24 17:46:53
FYI: I don't care one way or the other, but I thin
| |
24 extension = extension[1:] | |
25 | |
26 # Determine the MIME type of the file from its extension | |
27 mime_type = MIME_TYPE_MAP.get(extension, MIME_TYPE_MAP['']) | |
28 | |
29 # Open the file and send it over HTTP | |
30 sending_file = open(file_path, 'rb') | |
31 self.send_response(200) | |
32 self.send_header('Content-type', mime_type) | |
33 self.end_headers() | |
34 self.wfile.write(sending_file.read()) | |
35 sending_file.close() | |
36 | |
37 def serve_if_in_dir(self, dir_path, file_path): | |
38 # Determine if the file exists relative to the given dir_path AND exists under the dir_path | |
epoger
2013/07/24 17:46:53
Please add a note indicating the security reasons
| |
39 real_path = os.path.normpath(os.path.join(dir_path, file_path)) | |
40 print(repr(real_path)) | |
41 if os.path.commonprefix([real_path, dir_path]) == dir_path: | |
42 if os.path.isfile(real_path): | |
43 self.send_file(real_path) | |
44 return True | |
45 return False | |
46 | |
47 def do_GET(self): | |
48 # Grab the script path because that is where all the static assets are | |
49 script_dir = os.path.dirname(os.path.abspath(__file__)) | |
50 | |
51 # Simple rewrite rule of the root path to 'viewer.html' | |
52 if self.path == '' or self.path == '/': | |
53 self.path = '/viewer.html' | |
54 | |
55 # The [1:] chops of the leading '/' | |
epoger
2013/07/24 17:46:53
of -> off
| |
56 file_path = self.path[1:] | |
57 | |
58 # Attempt to send static asset files first | |
59 if self.serve_if_in_dir(script_dir, file_path): | |
60 return | |
61 | |
62 # WARNING: Using the root is a big ol' hack. Incredibly insecure. Only a llow serving to | |
epoger
2013/07/24 17:46:53
Indeed it is. Is there some other way we can hand
| |
63 # localhost unless you want the network to be able to see ALL of the fil es you can. | |
64 # Attempt to send gm image files | |
65 if self.serve_if_in_dir('/', file_path): | |
66 return | |
67 | |
68 # If no file to send was found, just give the standard 404 | |
69 self.send_error(404) | |
70 | |
71 | |
72 def main(): | |
73 server_address = ('127.0.0.1', 8080) | |
74 http_server = BaseHTTPServer.HTTPServer(server_address, SkPDiffHandler) | |
75 print('Navigate thine browser to: {}:{}'.format(*server_address)) | |
76 http_server.serve_forever() | |
77 | |
78 if __name__ == '__main__': | |
79 main() | |
OLD | NEW |