/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk

« back to all changes in this revision

Viewing changes to loggerhead/apps/transport.py

  • Committer: Jelmer Vernooij
  • Date: 2018-10-20 17:34:46 UTC
  • mto: (491.6.1 breezy)
  • mto: This revision was merged to the branch mainline in revision 494.
  • Revision ID: jelmer@jelmer.uk-20181020173446-eywejowpq25sg8pw
Rename serve-branches to loggerhead-serve.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008-2011 Canonical Ltd.
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335  USA
 
16
#
 
17
"""Serve branches at urls that mimic a transport's file system layout."""
 
18
 
 
19
import threading
 
20
 
 
21
from breezy import branch, errors, lru_cache, urlutils
 
22
from breezy.config import LocationConfig
 
23
from breezy.bzr.smart import request
 
24
import breezy.ui
 
25
from breezy.transport import get_transport
 
26
from breezy.transport.http import wsgi
 
27
 
 
28
from paste.request import path_info_pop
 
29
from paste import httpexceptions
 
30
from paste import urlparser
 
31
 
 
32
from .. import util
 
33
from ..apps.branch import BranchWSGIApp
 
34
from ..apps import favicon_app, robots_app, static_app
 
35
from ..controllers.directory_ui import DirectoryUI
 
36
 
 
37
 
 
38
class BranchesFromTransportServer(object):
 
39
 
 
40
    def __init__(self, transport, root, name=None):
 
41
        self.transport = transport
 
42
        self.root = root
 
43
        self.name = name
 
44
        self._config = root._config
 
45
 
 
46
    def app_for_branch(self, branch):
 
47
        if not self.name:
 
48
            name = branch._get_nick(local=True)
 
49
            is_root = True
 
50
        else:
 
51
            name = self.name
 
52
            is_root = False
 
53
        branch_app = BranchWSGIApp(
 
54
            branch,
 
55
            name,
 
56
            {'cachepath': self._config.SQL_DIR},
 
57
            self.root.graph_cache,
 
58
            is_root=is_root,
 
59
            use_cdn=self._config.get_option('use_cdn'),
 
60
            )
 
61
        return branch_app.app
 
62
 
 
63
    def app_for_non_branch(self, environ):
 
64
        segment = path_info_pop(environ)
 
65
        if segment is None:
 
66
            raise httpexceptions.HTTPMovedPermanently.relative_redirect(
 
67
                environ['SCRIPT_NAME'] + '/', environ)
 
68
        elif segment == '':
 
69
            if self.name:
 
70
                name = self.name
 
71
            else:
 
72
                name = '/'
 
73
            return DirectoryUI(
 
74
                environ['loggerhead.static.url'], self.transport, name)
 
75
        else:
 
76
            new_transport = self.transport.clone(segment)
 
77
            if self.name:
 
78
                new_name = urlutils.join(self.name, segment)
 
79
            else:
 
80
                new_name = '/' + segment
 
81
            return BranchesFromTransportServer(new_transport, self.root, new_name)
 
82
 
 
83
    def app_for_bazaar_data(self, relpath):
 
84
        if relpath == '/.bzr/smart':
 
85
            root_transport = get_transport_for_thread(self.root.base)
 
86
            wsgi_app = wsgi.SmartWSGIApp(root_transport)
 
87
            return wsgi.RelpathSetter(wsgi_app, '', 'loggerhead.path_info')
 
88
        else:
 
89
            # TODO: Use something here that uses the transport API
 
90
            # rather than relying on the local filesystem API.
 
91
            base = self.transport.base
 
92
            try:
 
93
                path = util.local_path_from_url(base)
 
94
            except errors.InvalidURL:
 
95
                raise httpexceptions.HTTPNotFound()
 
96
            else:
 
97
                return urlparser.make_static(None, path)
 
98
 
 
99
    def check_serveable(self, config):
 
100
        if not config.get_user_option_as_bool('http_serve', default=True):
 
101
            raise httpexceptions.HTTPNotFound()
 
102
 
 
103
    def __call__(self, environ, start_response):
 
104
        path = environ['PATH_INFO']
 
105
        try:
 
106
            b = branch.Branch.open_from_transport(self.transport)
 
107
        except errors.NotBranchError:
 
108
            if path.startswith('/.bzr'):
 
109
                self.check_serveable(LocationConfig(self.transport.base))
 
110
                return self.app_for_bazaar_data(path)(environ, start_response)
 
111
            if not self.transport.listable() or not self.transport.has('.'):
 
112
                raise httpexceptions.HTTPNotFound()
 
113
            return self.app_for_non_branch(environ)(environ, start_response)
 
114
        else:
 
115
            self.check_serveable(b.get_config())
 
116
            if path.startswith('/.bzr'):
 
117
                return self.app_for_bazaar_data(path)(environ, start_response)
 
118
            else:
 
119
                return self.app_for_branch(b)(environ, start_response)
 
120
 
 
121
 
 
122
_transport_store = threading.local()
 
123
 
 
124
def get_transport_for_thread(base):
 
125
    thread_transports = getattr(_transport_store, 'transports', None)
 
126
    if thread_transports is None:
 
127
        thread_transports = _transport_store.transports = {}
 
128
    if base in thread_transports:
 
129
        return thread_transports[base]
 
130
    transport = get_transport(base)
 
131
    thread_transports[base] = transport
 
132
    return transport
 
133
 
 
134
 
 
135
class BranchesFromTransportRoot(object):
 
136
 
 
137
    def __init__(self, base, config):
 
138
        self.graph_cache = lru_cache.LRUCache(10)
 
139
        self.base = base
 
140
        self._config = config
 
141
 
 
142
    def __call__(self, environ, start_response):
 
143
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
 
144
        environ['loggerhead.path_info'] = environ['PATH_INFO']
 
145
        if environ['PATH_INFO'].startswith('/static/'):
 
146
            segment = path_info_pop(environ)
 
147
            assert segment == 'static'
 
148
            return static_app(environ, start_response)
 
149
        elif environ['PATH_INFO'] == '/favicon.ico':
 
150
            return favicon_app(environ, start_response)
 
151
        elif environ['PATH_INFO'] == '/robots.txt':
 
152
            return robots_app(environ, start_response)
 
153
        else:
 
154
            transport = get_transport_for_thread(self.base)
 
155
            return BranchesFromTransportServer(
 
156
                transport, self)(environ, start_response)
 
157
 
 
158
 
 
159
class UserBranchesFromTransportRoot(object):
 
160
 
 
161
    def __init__(self, base, config):
 
162
        self.graph_cache = lru_cache.LRUCache(10)
 
163
        self.base = base
 
164
        self._config = config
 
165
        self.trunk_dir = config.get_option('trunk_dir')
 
166
 
 
167
    def __call__(self, environ, start_response):
 
168
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
 
169
        environ['loggerhead.path_info'] = environ['PATH_INFO']
 
170
        path_info = environ['PATH_INFO']
 
171
        if path_info.startswith('/static/'):
 
172
            segment = path_info_pop(environ)
 
173
            assert segment == 'static'
 
174
            return static_app(environ, start_response)
 
175
        elif path_info == '/favicon.ico':
 
176
            return favicon_app(environ, start_response)
 
177
        elif environ['PATH_INFO'] == '/robots.txt':
 
178
            return robots_app(environ, start_response)
 
179
        else:
 
180
            transport = get_transport_for_thread(self.base)
 
181
            # segments starting with ~ are user branches
 
182
            if path_info.startswith('/~'):
 
183
                segment = path_info_pop(environ)
 
184
                return BranchesFromTransportServer(
 
185
                    transport.clone(segment[1:]), self, segment)(
 
186
                    environ, start_response)
 
187
            else:
 
188
                return BranchesFromTransportServer(
 
189
                    transport.clone(self.trunk_dir), self)(
 
190
                    environ, start_response)