Add support for POST and HEAD requests to the HTTP server.

This commit is contained in:
Valentin Lorentz 2011-06-25 11:37:10 +02:00
parent fa3fc7d20a
commit fc41fc6153

View File

@ -31,6 +31,7 @@
An embedded and centralized HTTP server for Supybot's plugins. An embedded and centralized HTTP server for Supybot's plugins.
""" """
import cgi
from threading import Event, Thread from threading import Event, Thread
from cStringIO import StringIO from cStringIO import StringIO
from SocketServer import ThreadingMixIn from SocketServer import ThreadingMixIn
@ -80,7 +81,7 @@ class SupyHTTPServer(HTTPServer):
return callback return callback
class SupyHTTPRequestHandler(BaseHTTPRequestHandler): class SupyHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self): def do_X(self, callbackMethod, *args, **kwargs):
if self.path == '/': if self.path == '/':
callback = SupyIndex() callback = SupyIndex()
else: else:
@ -92,10 +93,29 @@ class SupyHTTPRequestHandler(BaseHTTPRequestHandler):
# Some shortcuts # Some shortcuts
for name in ('send_response', 'send_header', 'end_headers', 'rfile', for name in ('send_response', 'send_header', 'end_headers', 'rfile',
'wfile'): 'wfile', 'headers'):
setattr(callback, name, getattr(self, name)) setattr(callback, name, getattr(self, name))
# We call doGet, because this is more supybotic than do_GET. # We call doX, because this is more supybotic than do_X.
callback.doGet(self, '/' + '/'.join(self.path.split('/')[2:])) getattr(callback, callbackMethod)(self,
'/' + '/'.join(self.path.split('/')[2:]),
*args, **kwargs)
def do_GET(self):
print 'GET !'
self.do_X('doGet')
def do_POST(self):
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
self.do_X('doPost', form=form)
def do_HEAD(self):
self.do_X('doHead')
def log_message(self, format, *args): def log_message(self, format, *args):
log.info('HTTP request: %s - %s' % log.info('HTTP request: %s - %s' %
@ -118,6 +138,8 @@ class SupyHTTPServerCallback:
self.end_headers() self.end_headers()
self.wfile.write(self.defaultResponse) self.wfile.write(self.defaultResponse)
doPost = doHead = doGet
def doUnhook(self, handler): def doUnhook(self, handler):
"""Method called when unhooking this callback.""" """Method called when unhooking this callback."""
pass pass
@ -137,6 +159,8 @@ class Supy404(SupyHTTPServerCallback):
self.end_headers() self.end_headers()
self.wfile.write(self.response) self.wfile.write(self.response)
doPost = doHead = doGet
class SupyIndex(SupyHTTPServerCallback): class SupyIndex(SupyHTTPServerCallback):
"""Displays the index of available plugins.""" """Displays the index of available plugins."""
name = "index" name = "index"