Browse Source

Bugfix and favicon addition

M. Ian Graham 17 years ago
parent
commit
97521dae7f
3 changed files with 38 additions and 4 deletions
  1. 30 1
      direct/src/http/LandingPage.py
  2. 8 3
      direct/src/http/WebRequest.py
  3. BIN
      direct/src/http/favicon.ico

+ 30 - 1
direct/src/http/LandingPage.py

@@ -1,4 +1,8 @@
+import os
 from direct.directnotify.DirectNotifyGlobal import directNotify
 from direct.directnotify.DirectNotifyGlobal import directNotify
+from pandac.PandaModules import VirtualFileSystem
+from pandac.PandaModules import Filename
+from pandac.PandaModules import DSearchPath
 import LandingPageHTML
 import LandingPageHTML
 
 
 class LandingPage:
 class LandingPage:
@@ -15,6 +19,26 @@ class LandingPage:
 
 
         self.addQuickStat("Pages Served", 0, 0)
         self.addQuickStat("Pages Served", 0, 0)
 
 
+        self.favicon = self.readFavIcon()
+        
+
+    def readFavIcon(self):
+        vfs = VirtualFileSystem.getGlobalPtr()
+        filename = Filename('favicon.ico')
+
+        searchPath = DSearchPath()
+        searchPath.appendDirectory(Filename('.'))
+        searchPath.appendDirectory(Filename('etc'))
+        searchPath.appendDirectory(Filename.fromOsSpecific(os.path.expandvars('$DIRECT/src/http')))
+        searchPath.appendDirectory(Filename.fromOsSpecific(os.path.expandvars('direct/src/http')))
+        searchPath.appendDirectory(Filename.fromOsSpecific(os.path.expandvars('direct/http')))
+        found = vfs.resolveFilename(filename,searchPath)
+        if not found:
+            raise "Couldn't find direct/http/favicon.ico"
+
+        return vfs.readFile(filename, 1)
+
+
     def addTab(self, title, uri):
     def addTab(self, title, uri):
         self.menu[title] = uri
         self.menu[title] = uri
         self.uriToTitle[uri] = title
         self.uriToTitle[uri] = title
@@ -46,10 +70,12 @@ class LandingPage:
         if "/services" in uriList:
         if "/services" in uriList:
             uriList.remove("/services")
             uriList.remove("/services")
             autoList.append("/services")
             autoList.append("/services")
-
         if "/default.css" in uriList:
         if "/default.css" in uriList:
             uriList.remove("/default.css")
             uriList.remove("/default.css")
             autoList.append("/default.css")
             autoList.append("/default.css")
+        if "/favicon.ico" in uriList:
+            uriList.remove("/favicon.ico")
+            autoList.append("/favicon.ico")
 
 
         output += LandingPageHTML.getURITable(title="Application",uriList=uriList,uriToHandler=uriToHandler)
         output += LandingPageHTML.getURITable(title="Application",uriList=uriList,uriToHandler=uriToHandler)
 
 
@@ -87,6 +113,9 @@ class LandingPage:
 
 
     def getStyleSheet(self):
     def getStyleSheet(self):
         return LandingPageHTML.stylesheet
         return LandingPageHTML.stylesheet
+
+    def getFavIcon(self):
+        return self.favicon
     
     
     def skin(self, body, uri):
     def skin(self, body, uri):
         title = self.uriToTitle.get(uri,"Services")
         title = self.uriToTitle.get(uri,"Services")

+ 8 - 3
direct/src/http/WebRequest.py

@@ -44,10 +44,10 @@ class WebRequest(object):
         self.connection.SendThisResponse(msg)
         self.connection.SendThisResponse(msg)
 
 
     def respondCustom(self,contentType,body):
     def respondCustom(self,contentType,body):
-        msg = "HTTP/1.0 200 OK\r\nContent-Type: %s\n" % contentType
+        msg = "HTTP/1.0 200 OK\r\nContent-Type: %s" % contentType
 
 
         if contentType in ["text/css",]:
         if contentType in ["text/css",]:
-            msg += "Cache-Control: max-age=313977290\nExpires: Tue, 02 May 2017 04:08:44 GMT\n"
+            msg += "\nCache-Control: max-age=313977290\nExpires: Tue, 02 May 2017 04:08:44 GMT\n"
 
 
         msg += "\r\n\r\n%s" % (body)
         msg += "\r\n\r\n%s" % (body)
         self.connection.SendThisResponse(msg)
         self.connection.SendThisResponse(msg)
@@ -225,6 +225,7 @@ class WebRequestDispatcher(object):
                 self.registerGETHandler("/", self._main, returnsResponse = True, autoSkin = True)
                 self.registerGETHandler("/", self._main, returnsResponse = True, autoSkin = True)
                 self.registerGETHandler("/services", self._services, returnsResponse = True, autoSkin = True)
                 self.registerGETHandler("/services", self._services, returnsResponse = True, autoSkin = True)
                 self.registerGETHandler("/default.css", self._stylesheet)
                 self.registerGETHandler("/default.css", self._stylesheet)
+                self.registerGETHandler("/favicon.ico", self._favicon)
                 self.landingPage.addTab("Main", "/")
                 self.landingPage.addTab("Main", "/")
                 self.landingPage.addTab("Services", "/services")
                 self.landingPage.addTab("Services", "/services")
         else:
         else:
@@ -245,4 +246,8 @@ class WebRequestDispatcher(object):
         body = self.landingPage.getStyleSheet()
         body = self.landingPage.getStyleSheet()
         replyTo.respondCustom("text/css",body)
         replyTo.respondCustom("text/css",body)
 
 
-        
+    def _favicon(self,**kw):
+        replyTo = kw.get("replyTo",None)
+        assert replyTo is not None
+        body = self.landingPage.getFavIcon()
+        replyTo.respondCustom("image/x-icon",body)

BIN
direct/src/http/favicon.ico