/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: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

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