summaryrefslogtreecommitdiff
path: root/server/src/leap/soledad/server/_resource.py
blob: 046e20d1814d7d5026691657c8d5b693b0613f0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
# resource.py
# Copyright (C) 2016 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
A twisted resource that serves the Soledad Server.
"""
from twisted.web.error import Error
from twisted.web.resource import Resource

from ._blobs import blobs_resource
from ._config import get_config
from ._server_info import ServerInfo
from ._wsgi import get_sync_resource


__all__ = ['SoledadResource']


class SoledadResource(Resource):
    """
    This is a dummy twisted resource, used only to allow different entry points
    for the Soledad Server.
    """

    def __init__(self, sync_pool=None):
        conf = get_config()
        self._blobs_enabled = conf['soledad-server']['blobs']
        server_info = ServerInfo(self._blobs_enabled)
        sync_resource = get_sync_resource(sync_pool)
        self.children = {
            '': server_info,
            'sync': sync_resource,
            'blobs': blobs_resource,
            'sync': sync_resource,
        }

    def getChild(self, path, request):
        """
        Decide which child resource to serve based on the given path.
        """
        # requests to / return server information
        if path == '':
            return self.children['']

        # requests to /blobs will serve blobs if enabled
        if path == 'blobs':
            if not self._blobs_enabled:
                msg = 'Blobs feature is disabled in this server.'
                raise Error(403, message=msg)
            return self.children['blobs']

        # other requesta are routed to legacy sync resource
        request.postpath.insert(0, request.prepath.pop())
        return self.children['sync']