httpserver: Respond to HEAD requests. Closes GH-1203.

This commit is contained in:
Valentin Lorentz 2016-01-05 18:25:35 +01:00
parent 1d162dd048
commit 4576874128
2 changed files with 36 additions and 24 deletions

View File

@ -119,7 +119,7 @@ httpserver.set_default_templates(DEFAULT_TEMPLATES)
class FactoidsCallback(httpserver.SupyHTTPServerCallback):
name = 'Factoids web interface'
def doGet(self, handler, path):
def doGetOrHead(self, handler, path, write_content):
parts = path.split('/')[1:]
if path == '/':
self.send_response(200)
@ -132,18 +132,20 @@ class FactoidsCallback(httpserver.SupyHTTPServerCallback):
self.send_response(404)
self.send_header('Content-type', 'text/html; charset=utf-8')
self.end_headers()
self.write(httpserver.get_template('generic/error.html')%
{'title': 'Factoids - not a channel',
'error': 'This is not a channel'})
if write_content:
self.write(httpserver.get_template('generic/error.html')%
{'title': 'Factoids - not a channel',
'error': 'This is not a channel'})
return
if not self._plugin.registryValue('web.channel', channel):
self.send_response(403)
self.send_header('Content-type', 'text/html; charset=utf-8')
self.end_headers()
self.write(httpserver.get_template('generic/error.html')%
{'title': 'Factoids - unavailable',
'error': 'This channel does not exist or its factoids '
'are not available here.'})
if write_content:
self.write(httpserver.get_template('generic/error.html')%
{'title': 'Factoids - unavailable',
'error': 'This channel does not exist or its factoids '
'are not available here.'})
return
db = self._plugin.getDb(channel)
cursor = db.cursor()
@ -173,8 +175,9 @@ class FactoidsCallback(httpserver.SupyHTTPServerCallback):
self.send_response(200)
self.send_header('Content-type', 'text/html; charset=utf-8')
self.end_headers()
self.write(httpserver.get_template('factoids/channel.html')%
{'channel': channel, 'rows': content})
if write_content:
self.write(httpserver.get_template('factoids/channel.html')%
{'channel': channel, 'rows': content})
def doPost(self, handler, path, form):
if 'chan' in form:
self.send_response(303)

View File

@ -305,15 +305,21 @@ class SupyHTTPServerCallback(log.Firewalled):
def write(self, s):
self.wfile.write(s)
def doGet(self, handler, path, *args, **kwargs):
def doGetOrHead(self, handler, path, write_content):
response = self.defaultResponse.encode()
handler.send_response(405)
self.send_header('Content-Type', 'text/plain; charset=utf-8; charset=utf-8')
self.send_header('Content-Length', len(response))
self.end_headers()
self.wfile.write(response)
if write_content:
self.wfile.write(response)
doPost = doHead = doGet
def doGet(self, handler, path):
self.doGetOrHead(handler, path, write_content=True)
def doHead(self, handler, path):
self.doGetOrHead(handler, path, write_content=False)
doPost = doGet
def doHook(self, handler, subdir):
"""Method called when hooking this callback."""
@ -331,7 +337,7 @@ class Supy404(SupyHTTPServerCallback):
if I don't know what to serve.
What I'm saying is you just triggered a 404 Not Found, and I am not
trained to help you in such a case.""")
def doGet(self, handler, path, *args, **kwargs):
def doGetOrHead(self, handler, path, write_content):
response = self.response
if minisix.PY3:
response = response.encode()
@ -339,16 +345,15 @@ class Supy404(SupyHTTPServerCallback):
self.send_header('Content-Type', 'text/plain; charset=utf-8; charset=utf-8')
self.send_header('Content-Length', len(self.response))
self.end_headers()
self.wfile.write(response)
doPost = doHead = doGet
if write_content:
self.wfile.write(response)
class SupyIndex(SupyHTTPServerCallback):
"""Displays the index of available plugins."""
name = "index"
fullpath = True
defaultResponse = _("Request not handled.")
def doGet(self, handler, path):
def doGetOrHead(self, handler, path, write_content):
plugins = [x for x in handler.server.callbacks.items()]
if plugins == []:
plugins = _('No plugins available.')
@ -362,7 +367,8 @@ class SupyIndex(SupyHTTPServerCallback):
self.send_header('Content-Type', 'text/html; charset=utf-8')
self.send_header('Content-Length', len(response))
self.end_headers()
self.wfile.write(response)
if write_content:
self.wfile.write(response)
class Static(SupyHTTPServerCallback):
"""Serves static files."""
@ -372,7 +378,7 @@ class Static(SupyHTTPServerCallback):
def __init__(self, mimetype='text/plain; charset=utf-8'):
super(Static, self).__init__()
self._mimetype = mimetype
def doGet(self, handler, path):
def doGetOrHead(self, handler, path, write_content):
response = get_template(path)
if minisix.PY3:
response = response.encode()
@ -380,13 +386,14 @@ class Static(SupyHTTPServerCallback):
self.send_header('Content-type', self._mimetype)
self.send_header('Content-Length', len(response))
self.end_headers()
self.wfile.write(response)
if write_content:
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):
def doGetOrHead(self, handler, path, write_content):
response = None
file_path = conf.supybot.servers.http.favicon()
if file_path:
@ -407,7 +414,8 @@ class Favicon(SupyHTTPServerCallback):
# self.send_header('Content-Length', len(response))
# self.send_header('Content-type', 'image/' + ext)
# self.end_headers()
self.wfile.write(response)
if write_content:
self.wfile.write(response)
else:
response = _('No favicon set.')
if minisix.PY3:
@ -416,7 +424,8 @@ class Favicon(SupyHTTPServerCallback):
self.send_header('Content-type', 'text/plain; charset=utf-8')
self.send_header('Content-Length', len(response))
self.end_headers()
self.wfile.write(response)
if write_content:
self.wfile.write(response)
http_servers = []