httpserver: Add favicon support.

This commit is contained in:
Valentin Lorentz 2012-09-03 10:35:54 +02:00
parent e5e2db15b2
commit eaf6877700
2 changed files with 40 additions and 1 deletions

View File

@ -1097,6 +1097,9 @@ registerGlobalValue(supybot.servers.http, 'keepAlive',
registerGlobalValue(supybot.servers.http, 'robots',
registry.String('', _("""Determines the content of the robots.txt file,
served on the server to search engine.""")))
registerGlobalValue(supybot.servers.http, 'favicon',
registry.String('', _("""Determines the path of the file served as
favicon to browsers.""")))
###

View File

@ -31,6 +31,7 @@
An embedded and centralized HTTP server for Supybot's plugins.
"""
import os
import cgi
from threading import Event, Thread
from cStringIO import StringIO
@ -86,6 +87,8 @@ class SupyHTTPRequestHandler(BaseHTTPRequestHandler):
callback = SupyIndex()
elif self.path == '/robots.txt':
callback = RobotsTxt()
elif self.path == '/favicon.ico':
callback = Favicon()
else:
subdir = self.path.split('/')[1]
try:
@ -200,7 +203,40 @@ class RobotsTxt(SupyHTTPServerCallback):
def doGet(self, handler, path):
response = conf.supybot.servers.http.robots().replace('\\n', '\n')
handler.send_response(200)
self.send_header('Content_type', 'text/html')
self.send_header('Content-type', 'text/plain')
self.send_header('Content-Length', len(response))
self.end_headers()
self.wfile.write(response)
class Favicon(SupyHTTPServerCallback):
"""Services the favicon.ico file to browsers."""
name = 'favicon'
defaultResponse = _('Request not handled')
def doGet(self, handler, path):
file_path = conf.supybot.servers.http.favicon()
found = False
if file_path:
try:
icon = open(file_path, 'r')
found = True
except IOError:
pass
if found:
response = icon.read()
filename = file_path.rsplit(os.sep, 1)[1]
if '.' in filename:
ext = filename.rsplit('.', 1)[1]
else:
ext = 'ico'
# I have no idea why, but this headers are already sent.
# self.send_header('Content-Length', len(response))
# self.send_header('Content-type', 'image/' + ext)
# self.end_headers()
self.wfile.write(response)
else:
response = _('No favicon set.')
handler.send_response(404)
self.send_header('Content-type', 'text/plain')
self.send_header('Content-Length', len(response))
self.end_headers()
self.wfile.write(response)