Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(412)

Side by Side Diff: tools/skpdiff/skpdiff_server.py

Issue 19929007: add basic HTTP server for viewing skpdiff results (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: warn localhost Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
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
10 # entry is used as the default when no extension was given or if the extension
11 # has no entry in this dictionary.
12 MIME_TYPE_MAP = {'': 'application/octet-stream',
13 'html': 'text/html',
14 'css': 'text/css',
15 'png': 'image/png',
16 'js': 'application/javascript'
17 }
18
19
20 class SkPDiffHandler(BaseHTTPServer.BaseHTTPRequestHandler):
21 def send_file(self, file_path):
22 # Grab the extension if there is one
23 extension = os.path.splitext(file_path)[1]
24 if len(extension) >= 1:
25 extension = extension[1:]
26
27 # Determine the MIME type of the file from its extension
28 mime_type = MIME_TYPE_MAP.get(extension, MIME_TYPE_MAP[''])
29
30 # Open the file and send it over HTTP
31 sending_file = open(file_path, 'rb')
32 self.send_response(200)
33 self.send_header('Content-type', mime_type)
34 self.end_headers()
35 self.wfile.write(sending_file.read())
36 sending_file.close()
37
38 def serve_if_in_dir(self, dir_path, file_path):
39 # Determine if the file exists relative to the given dir_path AND exists
40 # under the dir_path. This is to prevent accidentally serving files
41 # outside the directory intended using symlinks, or '../'.
42 real_path = os.path.normpath(os.path.join(dir_path, file_path))
43 print(repr(real_path))
44 if os.path.commonprefix([real_path, dir_path]) == dir_path:
45 if os.path.isfile(real_path):
46 self.send_file(real_path)
47 return True
48 return False
49
50 def do_GET(self):
51 # Grab the script path because that is where all the static assets are
52 script_dir = os.path.dirname(os.path.abspath(__file__))
53
54 # Simple rewrite rule of the root path to 'viewer.html'
55 if self.path == '' or self.path == '/':
56 self.path = '/viewer.html'
57
58 # The [1:] chops off the leading '/'
59 file_path = self.path[1:]
60
61 # Attempt to send static asset files first
62 if self.serve_if_in_dir(script_dir, file_path):
63 return
64
65 # WARNING: Using the root is a big ol' hack. Incredibly insecure. Only
66 # allow serving to localhost unless you want the network to be able to
67 # see ALL of the files you can.
68 # Attempt to send gm image files
69 if self.serve_if_in_dir('/', file_path):
70 return
71
72 # If no file to send was found, just give the standard 404
73 self.send_error(404)
74
75
76 def main():
77 # Do not bind to interfaces other than localhost because the server will
78 # attempt to serve files relative to the root directory as a last resort
79 # before 404ing. This means all of your files can be accessed from this
80 # server, so DO NOT let this server listen to anything but localhost.
81 server_address = ('127.0.0.1', 8080)
82 http_server = BaseHTTPServer.HTTPServer(server_address, SkPDiffHandler)
83 print('Navigate thine browser to: {}:{}'.format(*server_address))
84 http_server.serve_forever()
85
86 if __name__ == '__main__':
87 main()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698