Code Repositories xandikos / e57792c
initial work on support for server-info. Jelmer Vernooń≥ 2 years ago
2 changed file(s) with 72 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
2121 """CalDAV/CardDAV server."""
2222
2323 __version__ = (0, 0, 6)
24 version_string = '.'.join(map(str, __version__))
2425
2526 import defusedxml.ElementTree # noqa: This does some monkey-patching on-load
0 # Xandikos
1 # Copyright (C) 2017 Jelmer Vernooń≥ <jelmer@jelmer.uk>, et al.
2 #
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; version 3
6 # of the License or (at your option) any later version of
7 # the License.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 # MA 02110-1301, USA.
18
19 """Server info.
20
21 See https://www.ietf.org/archive/id/draft-douglass-server-info-03.txt
22 """
23
24 import hashlib
25
26 from xandikos import version_string
27 from xandikos import webdav
28
29 ET = webdav.ET
30
31 # Feature to advertise server-info support.
32 FEATURE = 'server-info'
33 SERVER_INFO_MIME_TYPE = 'application/server-info+xml'
34
35
36 class ServerInfo(object):
37 """Server info."""
38
39 def __init__(self):
40 self._token = None
41 self._features = []
42 self._applications = []
43
44 def add_feature(self, feature):
45 self._features.append(feature)
46 self._token = None
47
48 @property
49 def token(self):
50 if self._token is None:
51 h = hashlib.sha1sum()
52 h.update(version_string.encode('utf-8'))
53 for z in (self._features + self._applications):
54 h.update(z.encode('utf-8'))
55 self._token = h.hexdigest()
56 return self._token
57
58 def get_body(self):
59 el = ET.Element('{DAV:}server-info')
60 el.set('token', self.token)
61 server_el = ET.SubElement(el, 'server-instance-info')
62 ET.SubElement(server_el, 'name').text = 'Xandikos'
63 ET.SubElement(server_el, 'version').text = version_string
64 features_el = ET.SubElement(el, 'features')
65 for feature in self._features:
66 features_el.append(feature)
67 applications_el = ET.SubElement(el, 'applications')
68 for application in self.applications:
69 applications_el.append(application)
70 return el