/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/transport/http/wsgi.py

  • Committer: Jan Hudec
  • Date: 2006-11-25 20:38:39 UTC
  • mto: (2199.1.1 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 2200.
  • Revision ID: bulb@ucw.cz-20061125203839-9nuhv32z63hb8811
Option --pull for merge command.

Option --pull was added to merge command, that, if the merge would be
trivial, ie. if the common ancestor is equal to the local tip, causes pull of
the remote instead of creating the merger.

Simple test for this option is included.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""WSGI application for bzr HTTP smart server.
 
18
 
 
19
For more information about WSGI, see PEP 333:
 
20
    http://www.python.org/dev/peps/pep-0333/
 
21
"""
 
22
 
 
23
from cStringIO import StringIO
 
24
 
 
25
from bzrlib.transport import chroot, get_transport, smart
 
26
from bzrlib.urlutils import local_path_to_url
 
27
    
 
28
 
 
29
def make_app(root, prefix, path_var):
 
30
    """Convenience function to construct a WSGI bzr smart server.
 
31
    
 
32
    :param root: a local path that requests will be relative to.
 
33
    :param prefix: See RelpathSetter.
 
34
    :param path_var: See RelpathSetter.
 
35
    """
 
36
    base_transport = get_transport('readonly+' + local_path_to_url(root))
 
37
    app = SmartWSGIApp(base_transport)
 
38
    app = RelpathSetter(app, prefix, path_var)
 
39
    return app
 
40
 
 
41
 
 
42
class RelpathSetter(object):
 
43
    """WSGI middleware to set 'bzrlib.relpath' in the environ.
 
44
    
 
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
 
48
    situations.
 
49
 
 
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".
 
55
    """
 
56
    
 
57
    def __init__(self, app, prefix='', path_var='REQUEST_URI'):
 
58
        """Constructor.
 
59
 
 
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'.
 
65
        """
 
66
        self.app = app
 
67
        self.prefix = prefix
 
68
        self.path_var = path_var
 
69
 
 
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', {})
 
75
            return []
 
76
        environ['bzrlib.relpath'] = path[len(self.prefix):-len(suffix)]
 
77
        return self.app(environ, start_response)
 
78
 
 
79
 
 
80
class SmartWSGIApp(object):
 
81
    """A WSGI application for the bzr smart server."""
 
82
 
 
83
    def __init__(self, backing_transport):
 
84
        """Constructor.
 
85
 
 
86
        :param backing_transport: a transport.  Requests will be processed
 
87
            relative to this transport.
 
88
        """
 
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
 
92
        # something.
 
93
        self.backing_transport = chroot.ChrootTransportDecorator(
 
94
            'chroot+' + backing_transport.base, _decorated=backing_transport)
 
95
 
 
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')])
 
100
            return []
 
101
 
 
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
 
113
            # protocol.
 
114
            response_data = 'error\x01incomplete request\n'
 
115
        else:
 
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]
 
121
 
 
122
    def make_request(self, transport, write_func):
 
123
        return smart.SmartServerRequestProtocolOne(transport, write_func)