1
# Copyright (C) 2008-2011 Canonical Ltd.
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.
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.
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
17
"""Serve branches at urls that mimic a transport's file system layout."""
21
from breezy import branch, errors, lru_cache, urlutils
22
from breezy.config import LocationConfig
23
from breezy.bzr.smart import request
25
from breezy.transport import get_transport
26
from breezy.transport.http import wsgi
28
from paste.request import path_info_pop
29
from paste import httpexceptions
30
from paste import urlparser
33
from ..apps.branch import BranchWSGIApp
34
from ..apps import favicon_app, robots_app, static_app
35
from ..controllers.directory_ui import DirectoryUI
38
class BranchesFromTransportServer(object):
40
def __init__(self, transport, root, name=None):
41
self.transport = transport
44
self._config = root._config
46
def app_for_branch(self, branch):
48
name = branch._get_nick(local=True)
53
branch_app = BranchWSGIApp(
56
{'cachepath': self._config.SQL_DIR},
57
self.root.graph_cache,
59
use_cdn=self._config.get_option('use_cdn'),
63
def app_for_non_branch(self, environ):
64
segment = path_info_pop(environ)
66
raise httpexceptions.HTTPMovedPermanently.relative_redirect(
67
environ['SCRIPT_NAME'] + '/', environ)
74
environ['loggerhead.static.url'], self.transport, name)
76
new_transport = self.transport.clone(segment)
78
new_name = urlutils.join(self.name, segment)
80
new_name = '/' + segment
81
return BranchesFromTransportServer(new_transport, self.root, new_name)
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')
89
# TODO: Use something here that uses the transport API
90
# rather than relying on the local filesystem API.
91
base = self.transport.base
93
path = util.local_path_from_url(base)
94
except errors.InvalidURL:
95
raise httpexceptions.HTTPNotFound()
97
return urlparser.make_static(None, path)
99
def check_serveable(self, config):
100
if not config.get_user_option_as_bool('http_serve', default=True):
101
raise httpexceptions.HTTPNotFound()
103
def __call__(self, environ, start_response):
104
path = environ['PATH_INFO']
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)
115
self.check_serveable(b.get_config())
116
if path.startswith('/.bzr'):
117
return self.app_for_bazaar_data(path)(environ, start_response)
119
return self.app_for_branch(b)(environ, start_response)
122
_transport_store = threading.local()
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
135
class BranchesFromTransportRoot(object):
137
def __init__(self, base, config):
138
self.graph_cache = lru_cache.LRUCache(10)
140
self._config = config
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)
154
transport = get_transport_for_thread(self.base)
155
return BranchesFromTransportServer(
156
transport, self)(environ, start_response)
159
class UserBranchesFromTransportRoot(object):
161
def __init__(self, base, config):
162
self.graph_cache = lru_cache.LRUCache(10)
164
self._config = config
165
self.trunk_dir = config.get_option('trunk_dir')
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)
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)
188
return BranchesFromTransportServer(
189
transport.clone(self.trunk_dir), self)(
190
environ, start_response)