/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-07-08 10:56:06 UTC
  • mto: This revision was merged to the branch mainline in revision 7030.
  • Revision ID: jelmer@jelmer.uk-20180708105606-d53hkks89qq88twu
Use separate .as_bytes method rather than __bytes__.

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