From 5c6f3246970fc4d4791f9137064ecfba55ba2a19 Mon Sep 17 00:00:00 2001 From: "P. Christeas" Date: Tue, 23 Nov 2010 20:52:44 +0200 Subject: [PATCH] http_well_known: new module, for RFC 5785 bzr revid: p_christ@hol.gr-20101123185244-6notdzskdw8jruau --- addons/http_well_known/__init__.py | 46 +++++++++++++++ addons/http_well_known/__openerp__.py | 44 +++++++++++++++ addons/http_well_known/doc/well-known.rst | 29 ++++++++++ addons/http_well_known/redirect.py | 69 +++++++++++++++++++++++ 4 files changed, 188 insertions(+) create mode 100644 addons/http_well_known/__init__.py create mode 100644 addons/http_well_known/__openerp__.py create mode 100644 addons/http_well_known/doc/well-known.rst create mode 100644 addons/http_well_known/redirect.py diff --git a/addons/http_well_known/__init__.py b/addons/http_well_known/__init__.py new file mode 100644 index 00000000000..45c4efee2e1 --- /dev/null +++ b/addons/http_well_known/__init__.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2010 OpenERP s.a. (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +import logging +import tools +from service.websrv_lib import HTTPDir +from service.http_server import reg_http_service +from redirect import RedirectHTTPHandler + +def init_well_known(): + reps = RedirectHTTPHandler.redirect_paths + + num_svcs = tools.config.get_misc('http-well-known', 'num_services', '0') + + for nsv in range(1, int(num_svcs)+1): + uri = tools.config.get_misc('http-well-known', 'service_%d' % nsv, False) + path = tools.config.get_misc('http-well-known', 'path_%d' % nsv, False) + if not (uri and path): + continue + reps['/'+uri] = path + + if reg_http_service(HTTPDir('/.well-known', RedirectHTTPHandler)): + logging.getLogger("web-services").info("Registered HTTP redirect handler at /.well-known" ) + + +init_well_known() + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/http_well_known/__openerp__.py b/addons/http_well_known/__openerp__.py new file mode 100644 index 00000000000..090b9a5ee01 --- /dev/null +++ b/addons/http_well_known/__openerp__.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2010 OpenERP s.a. (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + + +{ + "name" : "Publish well-known http URIs", + "version" : "0.1", + "depends" : [ "base", ], + 'description': """ + Implements IETF RFC 5785 for services discovery on a http server. + + May help some CalDAV clients bootstrap from the OpenERP server. + + Note that it needs explicit configuration in openerp-server.conf . +""", + "author" : "OpenERP SA", + 'category': 'Generic Modules/Others', + 'website': 'http://www.openerp.com', + "init_xml" : [ ], + "demo_xml" : [], + "update_xml" : [ ], + "installable" : True, + "active" : False, +} + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/http_well_known/doc/well-known.rst b/addons/http_well_known/doc/well-known.rst new file mode 100644 index 00000000000..ce7dbd7c9cf --- /dev/null +++ b/addons/http_well_known/doc/well-known.rst @@ -0,0 +1,29 @@ +================= +Well-known URIs +================= + +In accordance to IETF RFC 5785 [1], we shall publish a few locations +on the root of our http server, so that clients can discover our +services (CalDAV, eg.). + +This module merely installs a special http request handler, that will +redirect the URIs from "http://our-server:port/.well-known/xxx' to +the correct path for each xxx service. + +Note that well-known URIs cannot have a database-specific behaviour, +they are server-wide. So, we have to explicitly chose one of our databases +to serve at them. By default, the database of the configuration file +is chosen + +Example config: + +[http-well-known] +num_services = 2 +db_name = openerp-main ; must define that for path_1 below +service_1 = caldav +path_1 = /webdav/%(db_name)s/Calendars/ +service_2 = foo +path_2 = /other_db/static/Foo.html + + +[1] http://www.rfc-editor.org/rfc/rfc5785.txt \ No newline at end of file diff --git a/addons/http_well_known/redirect.py b/addons/http_well_known/redirect.py new file mode 100644 index 00000000000..85d01e7b05c --- /dev/null +++ b/addons/http_well_known/redirect.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2010 OpenERP s.a. (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + + +import logging +from service.websrv_lib import FixSendError, HTTPHandler, HttpOptions +from service.http_server import HttpLogHandler + +class RedirectHTTPHandler(HttpLogHandler, FixSendError, HttpOptions, HTTPHandler): + _logger = logging.getLogger('httpd.well-known') + _HTTP_OPTIONS = { 'Allow': ['OPTIONS', 'GET', 'HEAD', 'PROPFIND'] } + redirect_paths = {} + + def __init__(self,request, client_address, server): + HTTPHandler.__init__(self,request,client_address,server) + + def send_head(self): + """Common code for GET and HEAD commands. + + It will either send the correct redirect (Location) response + or a 404. + """ + + if self.path.endswith('/'): + self.path = self.path[:-1] + + if not self.path: + # Return an empty page + self.send_response(200) + self.send_header("Content-Length", 0) + self.end_headers() + return None + + if self.path not in self.redirect_paths: + self.send_error(404, "File not found") + return None + + self.send_response(301) + self.send_header("Location", self.redirect_paths[self.path]) + self.send_header("Content-Length", 0) + self.end_headers() + # Do we need a Cache-content: header here? + self._logger.debug("redirecting %s to %s", self.path, self.redirect_paths[self.path]) + return None + + def do_PROPFIND(self): + return self.do_HEAD() + + +#eof +