HTTPd, websrv_lib: implement extendable OPTIONS handler.

For CalDAV, we need to allow the handler to fine-tune its OPTIONS response.
So, better cleanup the API and allow request handlers to define their OPTIONS.

bzr revid: p_christ@hol.gr-20100726093336-afzp15rys9oua4a0
This commit is contained in:
P. Christeas 2010-07-26 12:33:36 +03:00
parent e8b1da52ab
commit d131f6f42d
2 changed files with 23 additions and 3 deletions

View File

@ -297,7 +297,7 @@ def init_xmlrpc():
class StaticHTTPHandler(HttpLogHandler, FixSendError, HttpOptions, HTTPHandler):
_logger = logging.getLogger('httpd')
_HTTP_OPTIONS = 'OPTIONS GET HEAD'
_HTTP_OPTIONS = { 'Allow': ['OPTIONS', 'GET', 'HEAD'] }
def __init__(self,request, client_address, server):
HTTPHandler.__init__(self,request,client_address,server)

View File

@ -179,17 +179,37 @@ class FixSendError:
self.wfile.write(content)
class HttpOptions:
_HTTP_OPTIONS = 'OPTIONS'
_HTTP_OPTIONS = {'Allow': ['OPTIONS' ] }
def do_OPTIONS(self):
"""return the list of capabilities """
opts = self._HTTP_OPTIONS
nopts = self._prep_OPTIONS(opts)
if nopts:
opts = nopts
self.send_response(200)
self.send_header("Content-Length", 0)
self.send_header('Allow', self._HTTP_OPTIONS)
for key, value in opts.items():
if isinstance(value, basestring):
self.send_header(key, value)
elif isinstance(value, (tuple, list)):
self.send_header(key, ', '.join(value))
self.end_headers()
def _prep_OPTIONS(self, opts):
"""Prepare the OPTIONS response, if needed
Sometimes, like in special DAV folders, the OPTIONS may contain
extra keywords, perhaps also dependant on the request url.
@param the options already. MUST be copied before being altered
@return the updated options.
"""
return opts
class MultiHTTPHandler(FixSendError, HttpOptions, BaseHTTPRequestHandler):
""" this is a multiple handler, that will dispatch each request
to a nested handler, iff it matches