From ff1f3ed882d93eb8262de30284c82c8a29e514e5 Mon Sep 17 00:00:00 2001
From: notmasteryet <async.processingjs@yahoo.com>
Date: Sat, 27 Aug 2011 10:10:36 -0500
Subject: [PATCH] Adds PDF index for "make server"

---
 test/test.py | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/test/test.py b/test/test.py
index 4801c4b38..a6b01ec3d 100644
--- a/test/test.py
+++ b/test/test.py
@@ -103,6 +103,33 @@ class PDFTestHandler(BaseHTTPRequestHandler):
         with open(path, "rb") as f:
             self.wfile.write(f.read())
 
+    def sendIndex(self, path, query):
+        if not path.endswith("/"):
+          # we need trailing slash
+          self.send_response(301)
+          redirectLocation = path + "/"
+          if query:
+            redirectLocation += "?" + query
+          self.send_header("Location",  redirectLocation)
+          self.end_headers()
+          return
+
+        self.send_response(200)
+        self.send_header("Content-Type", "text/html")
+        self.end_headers()
+        if query == "frame":
+          self.wfile.write("<html><frameset cols=*,200><frame name=pdf>" +
+            "<frame src='" + path + "'></frameset></html>")
+          return
+
+        location = os.path.abspath(os.path.realpath(DOC_ROOT + os.sep + path))
+        self.wfile.write("<html><body><h1>PDFs of " + path + "</h1>\n")
+        for filename in os.listdir(location):
+          if filename.lower().endswith('.pdf'):
+            self.wfile.write("<a href='/web/viewer.html?file=" + path + filename + "' target=pdf>" +
+              filename + "</a><br>\n")
+        self.wfile.write("</body></html>")
+
     def do_GET(self):
         url = urlparse(self.path)
         # Ignore query string
@@ -115,6 +142,10 @@ class PDFTestHandler(BaseHTTPRequestHandler):
             self.sendFile(os.path.join(DOC_ROOT, "test", "resources", "favicon.ico"), ext)
             return
 
+        if os.path.isdir(path):
+            self.sendIndex(url.path, url.query)
+            return
+
         if not (prefix == DOC_ROOT
                 and os.path.isfile(path) 
                 and ext in MIMEs):