1
# Copyright (C) 2006 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""WSGI application for bzr HTTP smart server.
19
For more information about WSGI, see PEP 333:
20
http://www.python.org/dev/peps/pep-0333/
23
from cStringIO import StringIO
25
from bzrlib.transport import get_transport, smart
26
from bzrlib.urlutils import local_path_to_url
29
def make_app(root, prefix, path_var):
30
"""Convenience function to construct a WSGI bzr smart server.
32
:param root: a local path that requests will be relative to.
33
:param prefix: See RelpathSetter.
34
:param path_var: See RelpathSetter.
36
base_transport = get_transport('readonly+' + local_path_to_url(root))
37
app = SmartWSGIApp(base_transport)
38
app = RelpathSetter(app, prefix, path_var)
42
class RelpathSetter(object):
43
"""WSGI middleware to set 'bzrlib.relpath' in the environ.
45
Different servers can invoke a SmartWSGIApp in different ways. This
46
middleware allows an adminstrator to configure how to the SmartWSGIApp will
47
determine what path it should be serving for a given request for many common
50
For example, a request for "/some/prefix/repo/branch/.bzr/smart" received by
51
a typical Apache and mod_fastcgi configuration will set `REQUEST_URI` to
52
"/some/prefix/repo/branch/.bzr/smart". A RelpathSetter with
53
prefix="/some/prefix/" and path_var="REQUEST_URI" will set that request's
54
'bzrlib.relpath' variable to "repo/branch".
57
def __init__(self, app, prefix='', path_var='REQUEST_URI'):
60
:param app: WSGI app to wrap, e.g. a SmartWSGIApp instance.
61
:param path_var: the variable in the WSGI environ to calculate the
62
'bzrlib.relpath' variable from.
63
:param prefix: a prefix to strip from the variable specified in
64
path_var before setting 'bzrlib.relpath'.
68
self.path_var = path_var
70
def __call__(self, environ, start_response):
71
path = environ[self.path_var]
72
suffix = '/.bzr/smart'
73
if not (path.startswith(self.prefix) and path.endswith(suffix)):
74
start_response('404 Not Found', {})
76
environ['bzrlib.relpath'] = path[len(self.prefix):-len(suffix)]
77
return self.app(environ, start_response)
80
class SmartWSGIApp(object):
81
"""A WSGI application for the bzr smart server."""
83
def __init__(self, backing_transport):
86
:param backing_transport: a transport. Requests will be processed
87
relative to this transport.
89
self.backing_transport = backing_transport
91
def __call__(self, environ, start_response):
92
"""WSGI application callable."""
93
if environ['REQUEST_METHOD'] != 'POST':
94
start_response('405 Method not allowed', [('Allow', 'POST')])
97
relpath = environ['bzrlib.relpath']
98
transport = self.backing_transport.clone(relpath)
99
#assert transport.base.startswith(self.backing_transport.base)
100
out_buffer = StringIO()
101
smart_protocol_request = self.make_request(transport, out_buffer.write)
102
request_data_length = int(environ['CONTENT_LENGTH'])
103
request_data_bytes = environ['wsgi.input'].read(request_data_length)
104
smart_protocol_request.accept_bytes(request_data_bytes)
105
assert smart_protocol_request.next_read_size() == 0, (
106
"not finished reading, but all data sent to protocol.")
107
response_data = out_buffer.getvalue()
108
headers = [('Content-type', 'application/octet-stream')]
109
headers.append(("Content-Length", str(len(response_data))))
110
start_response('200 OK', headers)
111
return [response_data]
113
def make_request(self, transport, write_func):
114
return smart.SmartServerRequestProtocolOne(transport, write_func)