/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.1.18 by Robert Collins
Lalo Martins remotebranch patch
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.1.18 by Robert Collins
Lalo Martins remotebranch patch
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.1.18 by Robert Collins
Lalo Martins remotebranch patch
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
2004.1.28 by v.ladeuil+lp at free
Merge bzr.dev. Including http modifications by "smart" related code
17
from cStringIO import StringIO
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
18
import errno
2004.1.29 by v.ladeuil+lp at free
New tests for http range requests handling.
19
from SimpleHTTPServer import SimpleHTTPRequestHandler
2164.2.29 by Vincent Ladeuil
Test the http redirection at the request level even if it's not
20
import re
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
21
import socket
2213.1.1 by v.ladeuil+lp at free
Workaround SimpleHTTPRequestHandler.translate_path limitation in
22
import urlparse
1530.1.14 by Robert Collins
Remove duplicate web server from HTTPTestUtil.
23
2018.5.39 by Robert Collins
Merge integration and knit-index utf8 decoding fixes.
24
from bzrlib import smart
25
import bzrlib.smart.request
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
26
from bzrlib.tests import TestCaseWithTransport
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
27
from bzrlib.tests.HttpServer import (
28
    HttpServer,
29
    TestingHTTPRequestHandler,
30
    )
2004.1.28 by v.ladeuil+lp at free
Merge bzr.dev. Including http modifications by "smart" related code
31
from bzrlib.transport import (
32
    get_transport,
33
    )
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
34
35
36
class WallRequestHandler(TestingHTTPRequestHandler):
37
    """Whatever request comes in, close the connection"""
38
39
    def handle_one_request(self):
40
        """Handle a single HTTP request, by abruptly closing the connection"""
41
        self.close_connection = 1
42
43
44
class BadStatusRequestHandler(TestingHTTPRequestHandler):
45
    """Whatever request comes in, returns a bad status"""
46
47
    def parse_request(self):
48
        """Fakes handling a single HTTP request, returns a bad status"""
49
        ignored = TestingHTTPRequestHandler.parse_request(self)
50
        try:
51
            self.send_response(0, "Bad status")
52
            self.end_headers()
53
        except socket.error, e:
2158.2.1 by v.ladeuil+lp at free
Windows tests cleanup.
54
            # We don't want to pollute the test results with
55
            # spurious server errors while test succeed. In our
2188.1.1 by Aaron Bentley
Windows tests cleanup. (Vincent Ladeuil)
56
            # case, it may occur that the test has already read
2158.2.1 by v.ladeuil+lp at free
Windows tests cleanup.
57
            # the 'Bad Status' and closed the socket while we are
58
            # still trying to send some headers... So the test is
2188.1.1 by Aaron Bentley
Windows tests cleanup. (Vincent Ladeuil)
59
            # ok, but if we raise the exception, the output is
2158.2.1 by v.ladeuil+lp at free
Windows tests cleanup.
60
            # dirty. So we don't raise, but we close the
61
            # connection, just to be safe :)
62
            spurious = [errno.EPIPE,
63
                        errno.ECONNRESET,
64
                        errno.ECONNABORTED,
65
                        ]
66
            if (len(e.args) > 0) and (e.args[0] in spurious):
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
67
                self.close_connection = 1
68
                pass
69
            else:
70
                raise
71
        return False
72
73
74
class InvalidStatusRequestHandler(TestingHTTPRequestHandler):
75
    """Whatever request comes in, returns am invalid status"""
76
77
    def parse_request(self):
78
        """Fakes handling a single HTTP request, returns a bad status"""
79
        ignored = TestingHTTPRequestHandler.parse_request(self)
80
        self.wfile.write("Invalid status line\r\n")
81
        return False
82
83
84
class BadProtocolRequestHandler(TestingHTTPRequestHandler):
85
    """Whatever request comes in, returns a bad protocol version"""
86
87
    def parse_request(self):
88
        """Fakes handling a single HTTP request, returns a bad status"""
89
        ignored = TestingHTTPRequestHandler.parse_request(self)
90
        # Returns an invalid protocol version, but curl just
91
        # ignores it and those cannot be tested.
92
        self.wfile.write("%s %d %s\r\n" % ('HTTP/0.0',
93
                                           404,
94
                                           'Look at my protocol version'))
95
        return False
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
96
97
2004.1.27 by v.ladeuil+lp at free
Fix bug #57644 by issuing an explicit error message.
98
class ForbiddenRequestHandler(TestingHTTPRequestHandler):
99
    """Whatever request comes in, returns a 403 code"""
100
101
    def parse_request(self):
102
        """Handle a single HTTP request, by replying we cannot handle it"""
103
        ignored = TestingHTTPRequestHandler.parse_request(self)
104
        self.send_error(403)
105
        return False
106
107
2004.1.28 by v.ladeuil+lp at free
Merge bzr.dev. Including http modifications by "smart" related code
108
class HTTPServerWithSmarts(HttpServer):
109
    """HTTPServerWithSmarts extends the HttpServer with POST methods that will
110
    trigger a smart server to execute with a transport rooted at the rootdir of
111
    the HTTP server.
112
    """
113
114
    def __init__(self):
115
        HttpServer.__init__(self, SmartRequestHandler)
116
117
118
class SmartRequestHandler(TestingHTTPRequestHandler):
119
    """Extend TestingHTTPRequestHandler to support smart client POSTs."""
120
121
    def do_POST(self):
122
        """Hand the request off to a smart server instance."""
123
        self.send_response(200)
124
        self.send_header("Content-type", "application/octet-stream")
2164.2.28 by Vincent Ladeuil
TestingHTTPServer.test_case_server renamed from test_case to avoid confusions.
125
        transport = get_transport(self.server.test_case_server._home_dir)
2004.1.28 by v.ladeuil+lp at free
Merge bzr.dev. Including http modifications by "smart" related code
126
        # TODO: We might like to support streaming responses.  1.0 allows no
127
        # Content-length in this case, so for integrity we should perform our
128
        # own chunking within the stream.
129
        # 1.1 allows chunked responses, and in this case we could chunk using
130
        # the HTTP chunking as this will allow HTTP persistence safely, even if
131
        # we have to stop early due to error, but we would also have to use the
132
        # HTTP trailer facility which may not be widely available.
133
        out_buffer = StringIO()
2018.5.39 by Robert Collins
Merge integration and knit-index utf8 decoding fixes.
134
        smart_protocol_request = smart.protocol.SmartServerRequestProtocolOne(
2004.1.28 by v.ladeuil+lp at free
Merge bzr.dev. Including http modifications by "smart" related code
135
                transport, out_buffer.write)
136
        # if this fails, we should return 400 bad request, but failure is
137
        # failure for now - RBC 20060919
138
        data_length = int(self.headers['Content-Length'])
139
        # Perhaps there should be a SmartServerHTTPMedium that takes care of
140
        # feeding the bytes in the http request to the smart_protocol_request,
141
        # but for now it's simpler to just feed the bytes directly.
142
        smart_protocol_request.accept_bytes(self.rfile.read(data_length))
143
        assert smart_protocol_request.next_read_size() == 0, (
144
            "not finished reading, but all data sent to protocol.")
145
        self.send_header("Content-Length", str(len(out_buffer.getvalue())))
146
        self.end_headers()
147
        self.wfile.write(out_buffer.getvalue())
148
149
2004.1.29 by v.ladeuil+lp at free
New tests for http range requests handling.
150
class SingleRangeRequestHandler(TestingHTTPRequestHandler):
151
    """Always reply to range request as if they were single.
152
153
    Don't be explicit about it, just to annoy the clients.
154
    """
155
156
    def get_multiple_ranges(self, file, file_size, ranges):
157
        """Answer as if it was a single range request and ignores the rest"""
158
        (start, end) = ranges[0]
159
        return self.get_single_range(file, file_size, start, end)
160
161
162
class NoRangeRequestHandler(TestingHTTPRequestHandler):
163
    """Ignore range requests without notice"""
164
165
    # Just bypass the range handling done by TestingHTTPRequestHandler
166
    do_GET = SimpleHTTPRequestHandler.do_GET
167
168
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
169
class TestCaseWithWebserver(TestCaseWithTransport):
170
    """A support class that provides readonly urls that are http://.
171
2004.3.3 by vila
Better (but still incomplete) design for bogus servers.
172
    This is done by forcing the readonly server to be an http
173
    one. This will currently fail if the primary transport is not
174
    backed by regular disk files.
1185.1.18 by Robert Collins
Lalo Martins remotebranch patch
175
    """
176
    def setUp(self):
1530.1.14 by Robert Collins
Remove duplicate web server from HTTPTestUtil.
177
        super(TestCaseWithWebserver, self).setUp()
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
178
        self.transport_readonly_server = HttpServer
2167.3.5 by v.ladeuil+lp at free
Tests for proxies, covering #74759.
179
180
181
class TestCaseWithTwoWebservers(TestCaseWithWebserver):
2164.2.13 by v.ladeuil+lp at free
Add tests for redirection. Preserve transport decorations.
182
    """A support class providing readonly urls on two servers that are http://.
2167.3.5 by v.ladeuil+lp at free
Tests for proxies, covering #74759.
183
2164.2.25 by Vincent Ladeuil
Fix typos noticed by Aaron.
184
    We set up two webservers to allows various tests involving
2167.3.5 by v.ladeuil+lp at free
Tests for proxies, covering #74759.
185
    proxies or redirections from one server to the other.
186
    """
187
    def setUp(self):
188
        super(TestCaseWithTwoWebservers, self).setUp()
189
        self.transport_secondary_server = HttpServer
190
        self.__secondary_server = None
191
192
    def create_transport_secondary_server(self):
193
        """Create a transport server from class defined at init.
194
195
        This is mostly a hook for daughter classes.
196
        """
197
        return self.transport_secondary_server()
198
199
    def get_secondary_server(self):
200
        """Get the server instance for the secondary transport."""
201
        if self.__secondary_server is None:
202
            self.__secondary_server = self.create_transport_secondary_server()
203
            self.__secondary_server.setUp()
204
            self.addCleanup(self.__secondary_server.tearDown)
205
        return self.__secondary_server
206
207
208
class FakeProxyRequestHandler(TestingHTTPRequestHandler):
209
    """Append a '-proxied' suffix to file served"""
210
211
    def translate_path(self, path):
2213.1.1 by v.ladeuil+lp at free
Workaround SimpleHTTPRequestHandler.translate_path limitation in
212
        # We need to act as a proxy and accept absolute urls,
213
        # which SimpleHTTPRequestHandler (grand parent) is not
214
        # ready for. So we just drop the protocol://host:port
215
        # part in front of the request-url (because we know we
216
        # would not forward the request to *another* proxy).
217
218
        # So we do what SimpleHTTPRequestHandler.translate_path
219
        # do beginning with python 2.4.3: abandon query
220
        # parameters, scheme, host port, etc (which ensure we
221
        # provide the right behaviour on all python versions).
222
        path = urlparse.urlparse(path)[2]
223
        # And now, we can apply *our* trick to proxy files
224
        self.path += '-proxied'
225
        # An finally we leave our mother class do whatever it
226
        # wants with the path
227
        return TestingHTTPRequestHandler.translate_path(self, path)
228
2167.3.5 by v.ladeuil+lp at free
Tests for proxies, covering #74759.
229
2164.2.13 by v.ladeuil+lp at free
Add tests for redirection. Preserve transport decorations.
230
class RedirectRequestHandler(TestingHTTPRequestHandler):
231
    """Redirect all request to the specified server"""
232
233
    def parse_request(self):
234
        """Redirect a single HTTP request to another host"""
235
        valid = TestingHTTPRequestHandler.parse_request(self)
236
        if valid:
2164.2.29 by Vincent Ladeuil
Test the http redirection at the request level even if it's not
237
            tcs = self.server.test_case_server
238
            code, target = tcs.is_redirected(self.path)
239
            if code is not None and target is not None:
240
                # Redirect as instructed
241
                self.send_response(code)
2164.2.16 by Vincent Ladeuil
Add tests.
242
                self.send_header('Location', target)
243
                self.end_headers()
244
                return False # The job is done
2164.2.29 by Vincent Ladeuil
Test the http redirection at the request level even if it's not
245
            else:
246
                # We leave the parent class serve the request
247
                pass
2164.2.13 by v.ladeuil+lp at free
Add tests for redirection. Preserve transport decorations.
248
        return valid
249
250
251
class HTTPServerRedirecting(HttpServer):
252
    """An HttpServer redirecting to another server """
253
2164.2.16 by Vincent Ladeuil
Add tests.
254
    def __init__(self, request_handler=RedirectRequestHandler):
2164.2.13 by v.ladeuil+lp at free
Add tests for redirection. Preserve transport decorations.
255
        HttpServer.__init__(self, request_handler)
2164.2.29 by Vincent Ladeuil
Test the http redirection at the request level even if it's not
256
        # redirections is a list of tuples (source, target, code)
257
        # - source is a regexp for the paths requested
258
        # - target is a replacement for re.sub describing where
259
        #   the request will be redirected
260
        # - code is the http error code associated to the
261
        #   redirection (301 permanent, 302 temporarry, etc
262
        self.redirections = []
263
264
    def redirect_to(self, host, port):
265
        """Redirect all requests to a specific host:port"""
266
        self.redirections = [('(.*)',
267
                              r'http://%s:%s\1' % (host, port) ,
268
                              301)]
269
270
    def is_redirected(self, path):
271
        """Is the path redirected by this server.
272
273
        :param path: the requested relative path
274
275
        :returns: a tuple (code, target) if a matching
276
             redirection is found, (None, None) otherwise.
277
        """
278
        code = None
279
        target = None
280
        for (rsource, rtarget, rcode) in self.redirections:
281
            target, match = re.subn(rsource, rtarget, path)
282
            if match:
283
                code = rcode
284
                break # The first match wins
285
            else:
286
                target = None
287
        return code, target
2164.2.13 by v.ladeuil+lp at free
Add tests for redirection. Preserve transport decorations.
288
2167.3.5 by v.ladeuil+lp at free
Tests for proxies, covering #74759.
289
2164.2.22 by Vincent Ladeuil
Take Aaron's review comments into account.
290
class TestCaseWithRedirectedWebserver(TestCaseWithTwoWebservers):
291
   """A support class providing redirections from one server to another.
292
2164.2.25 by Vincent Ladeuil
Fix typos noticed by Aaron.
293
   We set up two webservers to allows various tests involving
2164.2.22 by Vincent Ladeuil
Take Aaron's review comments into account.
294
   redirections.
295
   The 'old' server is redirected to the 'new' server.
296
   """
297
298
   def create_transport_secondary_server(self):
299
       """Create the secondary server redirecting to the primary server"""
300
       new = self.get_readonly_server()
301
       redirecting = HTTPServerRedirecting()
302
       redirecting.redirect_to(new.host, new.port)
303
       return redirecting
304
305
   def setUp(self):
306
       super(TestCaseWithRedirectedWebserver, self).setUp()
307
       # The redirections will point to the new server
308
       self.new_server = self.get_readonly_server()
309
       # The requests to the old server will be redirected
310
       self.old_server = self.get_secondary_server()
311
312