aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/skpdiff
diff options
context:
space:
mode:
authorGravatar zachr@google.com <zachr@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-07-25 19:49:17 +0000
committerGravatar zachr@google.com <zachr@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-07-25 19:49:17 +0000
commit48b8891194e4abab68c3b233b876a5710a41ecc3 (patch)
treecd1e51194ff2f8b450df3463c69bba8c16921b1a /tools/skpdiff
parente2cf805e9ae57f259307ab0834dd23dfe468c316 (diff)
add basic HTTP server for viewing skpdiff results
R=epoger@google.com Review URL: https://codereview.chromium.org/19929007 git-svn-id: http://skia.googlecode.com/svn/trunk@10371 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'tools/skpdiff')
-rw-r--r--tools/skpdiff/skpdiff_server.py87
1 files changed, 87 insertions, 0 deletions
diff --git a/tools/skpdiff/skpdiff_server.py b/tools/skpdiff/skpdiff_server.py
new file mode 100644
index 0000000000..07f0e53af3
--- /dev/null
+++ b/tools/skpdiff/skpdiff_server.py
@@ -0,0 +1,87 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+from __future__ import print_function
+import BaseHTTPServer
+import os
+import os.path
+
+# A simple dictionary of file name extensions to MIME types. The empty string
+# entry is used as the default when no extension was given or if the extension
+# has no entry in this dictionary.
+MIME_TYPE_MAP = {'': 'application/octet-stream',
+ 'html': 'text/html',
+ 'css': 'text/css',
+ 'png': 'image/png',
+ 'js': 'application/javascript'
+ }
+
+
+class SkPDiffHandler(BaseHTTPServer.BaseHTTPRequestHandler):
+ def send_file(self, file_path):
+ # Grab the extension if there is one
+ extension = os.path.splitext(file_path)[1]
+ if len(extension) >= 1:
+ extension = extension[1:]
+
+ # Determine the MIME type of the file from its extension
+ mime_type = MIME_TYPE_MAP.get(extension, MIME_TYPE_MAP[''])
+
+ # Open the file and send it over HTTP
+ sending_file = open(file_path, 'rb')
+ self.send_response(200)
+ self.send_header('Content-type', mime_type)
+ self.end_headers()
+ self.wfile.write(sending_file.read())
+ sending_file.close()
+
+ def serve_if_in_dir(self, dir_path, file_path):
+ # Determine if the file exists relative to the given dir_path AND exists
+ # under the dir_path. This is to prevent accidentally serving files
+ # outside the directory intended using symlinks, or '../'.
+ real_path = os.path.normpath(os.path.join(dir_path, file_path))
+ print(repr(real_path))
+ if os.path.commonprefix([real_path, dir_path]) == dir_path:
+ if os.path.isfile(real_path):
+ self.send_file(real_path)
+ return True
+ return False
+
+ def do_GET(self):
+ # Grab the script path because that is where all the static assets are
+ script_dir = os.path.dirname(os.path.abspath(__file__))
+
+ # Simple rewrite rule of the root path to 'viewer.html'
+ if self.path == '' or self.path == '/':
+ self.path = '/viewer.html'
+
+ # The [1:] chops off the leading '/'
+ file_path = self.path[1:]
+
+ # Attempt to send static asset files first
+ if self.serve_if_in_dir(script_dir, file_path):
+ return
+
+ # WARNING: Using the root is a big ol' hack. Incredibly insecure. Only
+ # allow serving to localhost unless you want the network to be able to
+ # see ALL of the files you can.
+ # Attempt to send gm image files
+ if self.serve_if_in_dir('/', file_path):
+ return
+
+ # If no file to send was found, just give the standard 404
+ self.send_error(404)
+
+
+def main():
+ # Do not bind to interfaces other than localhost because the server will
+ # attempt to serve files relative to the root directory as a last resort
+ # before 404ing. This means all of your files can be accessed from this
+ # server, so DO NOT let this server listen to anything but localhost.
+ server_address = ('127.0.0.1', 8080)
+ http_server = BaseHTTPServer.HTTPServer(server_address, SkPDiffHandler)
+ print('Navigate thine browser to: {}:{}'.format(*server_address))
+ http_server.serve_forever()
+
+if __name__ == '__main__':
+ main()