Permalink
Browse files

Improved icon validation & test page

git-svn-id: https://potato.codebasehq.com/getfavicon/getfavicon.svn/trunk@28 0c1c7bd3-9391-4859-b94e-4e2dcdc6a516
  • Loading branch information...
1 parent af69831 commit 5a59cc9eca95a451887102c33b944fc3c826cde3 jason committed Sep 4, 2010
Showing with 67 additions and 27 deletions.
  1. +24 −0 globals.py
  2. +42 −26 main.py
  3. +1 −1 templates/test.html
View
@@ -9,6 +9,30 @@
MC_CACHE_TIME = 2419200 #seconds (28 days)
DS_CACHE_TIME = 90 #days
+MIN_ICON_LENGTH = 100
+MAX_ICON_LENGTH = 20000
+EMPTY_ICON_LENGTH = 1150
+
+ICON_MIMETYPES = [
+ "image/x-icon",
+ "image/vnd.microsoft.icon",
+ "image/ico",
+ "image/icon",
+ "text/ico",
+ "application/ico",
+ "image/x-ms-bmp",
+ "image/x-bmp",
+ "image/gif",
+ "image/png",
+ "image/jpeg",
+ None,
+]
+
+ICON_MIMETYPE_BLACKLIST = [
+ "application/xml",
+ "text/html",
+]
+
# Surpresses the index and test pages
HEADLESS = False
View
68 main.py
@@ -138,12 +138,41 @@ def get(self):
self.printTemplate("test",{
"isHomepage":False,
- "topSites":topSites
+ "topSites":topSites,
+ "isDev":self.isDev()
})
class PrintFavicon(BaseHandler):
+ def isValidIconResponse(self,iconResponse):
+
+ iconLength = len(iconResponse.content)
+
+ iconContentType = iconResponse.headers.get("Content-Type")
+ if iconContentType:
+ iconContentType = iconContentType.split(";")[0]
+
+ invalidIconReason = []
+
+ if not iconResponse.status_code == 200:
+ invalidIconReason.append("Status code isn't 200")
+
+ if iconContentType in ICON_MIMETYPE_BLACKLIST:
+ invalidIconReason.append("Content-Type in ICON_MIMETYPE_BLACKLIST")
+
+ if iconLength < MIN_ICON_LENGTH:
+ invalidIconReason.append("Length below MIN_ICON_LENGTH")
+
+ if iconLength > MAX_ICON_LENGTH:
+ invalidIconReason.append("Length greater than MAX_ICON_LENGTH")
+
+ if len(invalidIconReason) > 0:
+ inf("Invalid icon because: %s" % invalidIconReason)
+ return False
+ else:
+ return True
+
def iconInMC(self):
@@ -219,28 +248,17 @@ def iconAtRoot(self):
inf("Failed to retrieve iconAtRoot")
return False
-
-
- if rootDomainFaviconResult.status_code == 200:
-
- inf("Got iconAtRoot, length %d bytes" % (len(rootDomainFaviconResult.content)))
-
- if len(rootDomainFaviconResult.content) > 0:
-
- self.icon = rootDomainFaviconResult.content
- self.cacheIcon()
- self.writeIcon()
+
+ if self.isValidIconResponse(rootDomainFaviconResult):
+
+ self.icon = rootDomainFaviconResult.content
+ self.cacheIcon()
+ self.writeIcon()
- return True
+ return True
- else:
-
- return False
-
else:
-
- inf("No iconAtRoot")
-
+
return False
@@ -297,16 +315,14 @@ def iconInPage(self):
inf("Failed to retrieve icon to found in page")
return False
-
- if pagePathFaviconResult.status_code == 200 and len(pagePathFaviconResult.content) > 100 and len(pagePathFaviconResult.content) < 20000:
-
- inf("Got iconInPage, length %d bytes" % (len(pagePathFaviconResult.content)))
+
+ if self.isValidIconResponse(pagePathFaviconResult):
self.icon = pagePathFaviconResult.content
self.cacheIcon()
self.writeIcon()
-
- return True
+
+ return True
return False
View
@@ -6,7 +6,7 @@
<ul class="icons">
{% for topSite in topSites %}
- <li><a href="{{ topSite }}"><img src="//{{ topSite.11 }}.getfavicon.appspot.com/{{ topSite }}" alt="{{ topSite }}"/></a></li>
+ <li><a href="{{ topSite }}"><img src="{% if not isDev %}//{{ topSite.11 }}.getfavicon.appspot.com{% endif %}/{{ topSite }}" alt="{{ topSite }}"/></a></li>
{% endfor %}
</ul>

0 comments on commit 5a59cc9

Please sign in to comment.