/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 breezy/transport/http/wsgi.py

  • Committer: Jelmer Vernooij
  • Date: 2018-11-24 15:56:05 UTC
  • mto: This revision was merged to the branch mainline in revision 7214.
  • Revision ID: jelmer@jelmer.uk-20181124155605-g8yh28kpruubzgjk
Remove references to bzrtools.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
    http://www.python.org/dev/peps/pep-0333/
21
21
"""
22
22
 
23
 
from cStringIO import StringIO
24
 
 
25
 
from bzrlib.smart import protocol, medium
26
 
from bzrlib.transport import chroot, get_transport
27
 
from bzrlib.urlutils import local_path_to_url
 
23
from __future__ import absolute_import
 
24
 
 
25
from io import BytesIO
 
26
 
 
27
from ...bzr.smart import medium
 
28
from ...transport import chroot, get_transport
 
29
from ...urlutils import local_path_to_url
28
30
 
29
31
 
30
32
def make_app(root, prefix, path_var='REQUEST_URI', readonly=True,
31
 
    load_plugins=True, enable_logging=True):
 
33
             load_plugins=True, enable_logging=True):
32
34
    """Convenience function to construct a WSGI bzr smart server.
33
35
 
34
36
    :param root: a local path that requests will be relative to.
41
43
    else:
42
44
        base_transport = get_transport(local_url)
43
45
    if load_plugins:
44
 
        from bzrlib.plugin import load_plugins
 
46
        from ...plugin import load_plugins
45
47
        load_plugins()
46
48
    if enable_logging:
47
 
        import bzrlib.trace
48
 
        bzrlib.trace.enable_default_logging()
 
49
        import breezy.trace
 
50
        breezy.trace.enable_default_logging()
49
51
    app = SmartWSGIApp(base_transport, prefix)
50
52
    app = RelpathSetter(app, '', path_var)
51
53
    return app
52
54
 
53
55
 
54
56
class RelpathSetter(object):
55
 
    """WSGI middleware to set 'bzrlib.relpath' in the environ.
 
57
    """WSGI middleware to set 'breezy.relpath' in the environ.
56
58
 
57
59
    Different servers can invoke a SmartWSGIApp in different ways.  This
58
60
    middleware allows an adminstrator to configure how to the SmartWSGIApp will
63
65
    a typical Apache and mod_fastcgi configuration will set `REQUEST_URI` to
64
66
    "/some/prefix/repo/branch/.bzr/smart".  A RelpathSetter with
65
67
    prefix="/some/prefix/" and path_var="REQUEST_URI" will set that request's
66
 
    'bzrlib.relpath' variable to "repo/branch".
 
68
    'breezy.relpath' variable to "repo/branch".
67
69
    """
68
70
 
69
71
    def __init__(self, app, prefix='', path_var='REQUEST_URI'):
71
73
 
72
74
        :param app: WSGI app to wrap, e.g. a SmartWSGIApp instance.
73
75
        :param path_var: the variable in the WSGI environ to calculate the
74
 
            'bzrlib.relpath' variable from.
 
76
            'breezy.relpath' variable from.
75
77
        :param prefix: a prefix to strip from the variable specified in
76
 
            path_var before setting 'bzrlib.relpath'.
 
78
            path_var before setting 'breezy.relpath'.
77
79
        """
78
80
        self.app = app
79
81
        self.prefix = prefix
85
87
        if not (path.startswith(self.prefix) and path.endswith(suffix)):
86
88
            start_response('404 Not Found', [])
87
89
            return []
88
 
        environ['bzrlib.relpath'] = path[len(self.prefix):-len(suffix)]
 
90
        environ['breezy.relpath'] = path[len(self.prefix):-len(suffix)]
89
91
        return self.app(environ, start_response)
90
92
 
91
93
 
101
103
            backing_transport.  This is used to interpret relpaths received from
102
104
            the client.
103
105
        """
104
 
        # Use a ChrootTransportDecorator so that this web application won't
 
106
        # Use a ChrootServer so that this web application won't
105
107
        # accidentally let people access locations they shouldn't.
106
108
        # e.g. consider a smart server request for "get /etc/passwd" or
107
109
        # something.
112
114
        # While the chroot server can technically be torn down at this point,
113
115
        # as all it does is remove the scheme registration from transport's
114
116
        # protocol dictionary, we don't *just in case* there are parts of
115
 
        # bzrlib that will invoke 'get_transport' on urls rather than cloning
 
117
        # breezy that will invoke 'get_transport' on urls rather than cloning
116
118
        # around the existing transport.
117
 
        #self.chroot_server.stop_server()
 
119
        # self.chroot_server.stop_server()
118
120
 
119
121
    def __call__(self, environ, start_response):
120
122
        """WSGI application callable."""
122
124
            start_response('405 Method not allowed', [('Allow', 'POST')])
123
125
            return []
124
126
 
125
 
        relpath = environ['bzrlib.relpath']
 
127
        relpath = environ['breezy.relpath']
126
128
 
127
129
        if not relpath.startswith('/'):
128
130
            relpath = '/' + relpath
137
139
            # Remove the root_client_path from the relpath, and set
138
140
            # adjusted_tcp to None to tell the request handler that no further
139
141
            # path translation is required.
140
 
            adjusted_rcp = None
 
142
            adjusted_rcp = '.'
141
143
            adjusted_relpath = relpath[len(self.root_client_path):]
142
144
        elif self.root_client_path.startswith(relpath):
143
145
            # The relpath traverses some of the mandatory root client path.
155
157
            raise AssertionError(adjusted_relpath)
156
158
 
157
159
        transport = self.backing_transport.clone(adjusted_relpath)
158
 
        out_buffer = StringIO()
 
160
        out_buffer = BytesIO()
159
161
        request_data_length = int(environ['CONTENT_LENGTH'])
160
162
        request_data_bytes = environ['wsgi.input'].read(request_data_length)
161
163
        smart_protocol_request = self.make_request(
162
 
            transport, out_buffer.write, request_data_bytes, adjusted_rcp)
 
164
            transport, out_buffer.write, request_data_bytes,
 
165
            adjusted_rcp)
163
166
        if smart_protocol_request.next_read_size() != 0:
164
167
            # The request appears to be incomplete, or perhaps it's just a
165
168
            # newer version we don't understand.  Regardless, all we can do
166
169
            # is return an error response in the format of our version of the
167
170
            # protocol.
168
 
            response_data = 'error\x01incomplete request\n'
 
171
            response_data = b'error\x01incomplete request\n'
169
172
        else:
170
173
            response_data = out_buffer.getvalue()
171
174
        headers = [('Content-type', 'application/octet-stream')]