http_well_known: new module, for RFC 5785

bzr revid: p_christ@hol.gr-20101123185244-6notdzskdw8jruau
This commit is contained in:
P. Christeas 2010-11-23 20:52:44 +02:00
parent 0d051581b4
commit 5c6f324697
4 changed files with 188 additions and 0 deletions

View File

@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2010 OpenERP s.a. (<http://openerp.com>).
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
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:

View File

@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2010 OpenERP s.a. (<http://openerp.com>).
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
{
"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:

View File

@ -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

View File

@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2010 OpenERP s.a. (<http://openerp.com>).
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
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