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 chroot, 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
# Use a ChrootTransportDecorator so that this web application won't
90
# accidentally let people access locations they shouldn't.
91
# e.g. consider a smart server request for "get /etc/passwd" or
93
self.backing_transport = chroot.ChrootTransportDecorator(
94
'chroot+' + backing_transport.base, _decorated=backing_transport)
96
def __call__(self, environ, start_response):
97
"""WSGI application callable."""
98
if environ['REQUEST_METHOD'] != 'POST':
99
start_response('405 Method not allowed', [('Allow', 'POST')])
102
relpath = environ['bzrlib.relpath']
103
transport = self.backing_transport.clone(relpath)
104
out_buffer = StringIO()
105
smart_protocol_request = self.make_request(transport, out_buffer.write)
106
request_data_length = int(environ['CONTENT_LENGTH'])
107
request_data_bytes = environ['wsgi.input'].read(request_data_length)
108
smart_protocol_request.accept_bytes(request_data_bytes)
109
if smart_protocol_request.next_read_size() != 0:
110
# The request appears to be incomplete, or perhaps it's just a
111
# newer version we don't understand. Regardless, all we can do
112
# is return an error response in the format of our version of the
114
response_data = 'error\x01incomplete request\n'
116
response_data = out_buffer.getvalue()
117
headers = [('Content-type', 'application/octet-stream')]
118
headers.append(("Content-Length", str(len(response_data))))
119
start_response('200 OK', headers)
120
return [response_data]
122
def make_request(self, transport, write_func):
123
return smart.SmartServerRequestProtocolOne(transport, write_func)