From d131f6f42d3b80bde6205ed3b8d2d0cde1baf4e7 Mon Sep 17 00:00:00 2001 From: "P. Christeas" Date: Mon, 26 Jul 2010 12:33:36 +0300 Subject: [PATCH] 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 --- bin/service/http_server.py | 2 +- bin/service/websrv_lib.py | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/bin/service/http_server.py b/bin/service/http_server.py index 498d6cf271b..cefca98a897 100644 --- a/bin/service/http_server.py +++ b/bin/service/http_server.py @@ -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) diff --git a/bin/service/websrv_lib.py b/bin/service/websrv_lib.py index 3f27c7f1d02..ba8c17a2e70 100644 --- a/bin/service/websrv_lib.py +++ b/bin/service/websrv_lib.py @@ -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