~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/HTTPTestUtil.py

  • Committer: Martin Pool
  • Date: 2005-09-30 05:56:05 UTC
  • mto: (1185.14.2)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: mbp@sourcefrog.net-20050930055605-a2c534529b392a7d
- fix upgrade for transport changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
2
 
#
 
1
# Copyright (C) 2005 by Canonical Ltd
 
2
 
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
#
 
12
 
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
from cStringIO import StringIO
18
 
import errno
19
 
from SimpleHTTPServer import SimpleHTTPRequestHandler
20
 
import socket
21
 
import urlparse
22
 
 
23
 
from bzrlib.tests import TestCaseWithTransport
24
 
from bzrlib.tests.HttpServer import (
25
 
    HttpServer,
26
 
    TestingHTTPRequestHandler,
27
 
    )
28
 
from bzrlib.transport import (
29
 
    get_transport,
30
 
    smart,
31
 
    )
32
 
 
33
 
 
34
 
class WallRequestHandler(TestingHTTPRequestHandler):
35
 
    """Whatever request comes in, close the connection"""
 
17
import BaseHTTPServer, SimpleHTTPServer, socket, errno, time
 
18
from bzrlib.selftest import TestCaseInTempDir
 
19
 
 
20
 
 
21
class WebserverNotAvailable(Exception):
 
22
    pass
 
23
 
 
24
class BadWebserverPath(ValueError):
 
25
    def __str__(self):
 
26
        return 'path %s is not in %s' % self.args
 
27
 
 
28
class TestingHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
 
29
    def log_message(self, format, *args):
 
30
        self.server.test_case.log("webserver - %s - - [%s] %s" %
 
31
                                  (self.address_string(),
 
32
                                   self.log_date_time_string(),
 
33
                                   format%args))
36
34
 
37
35
    def handle_one_request(self):
38
 
        """Handle a single HTTP request, by abruptly closing the connection"""
39
 
        self.close_connection = 1
40
 
 
41
 
 
42
 
class BadStatusRequestHandler(TestingHTTPRequestHandler):
43
 
    """Whatever request comes in, returns a bad status"""
44
 
 
45
 
    def parse_request(self):
46
 
        """Fakes handling a single HTTP request, returns a bad status"""
47
 
        ignored = TestingHTTPRequestHandler.parse_request(self)
48
 
        try:
49
 
            self.send_response(0, "Bad status")
50
 
            self.end_headers()
51
 
        except socket.error, e:
52
 
            # We don't want to pollute the test results with
53
 
            # spurious server errors while test succeed. In our
54
 
            # case, it may occur that the test has already read
55
 
            # the 'Bad Status' and closed the socket while we are
56
 
            # still trying to send some headers... So the test is
57
 
            # ok, but if we raise the exception, the output is
58
 
            # dirty. So we don't raise, but we close the
59
 
            # connection, just to be safe :)
60
 
            spurious = [errno.EPIPE,
61
 
                        errno.ECONNRESET,
62
 
                        errno.ECONNABORTED,
63
 
                        ]
64
 
            if (len(e.args) > 0) and (e.args[0] in spurious):
65
 
                self.close_connection = 1
 
36
        """Handle a single HTTP request.
 
37
 
 
38
        You normally don't need to override this method; see the class
 
39
        __doc__ string for information on how to handle specific HTTP
 
40
        commands such as GET and POST.
 
41
 
 
42
        """
 
43
        for i in xrange(1,11): # Don't try more than 10 times
 
44
            try:
 
45
                self.raw_requestline = self.rfile.readline()
 
46
            except socket.error, e:
 
47
                if e.args[0] == errno.EAGAIN:
 
48
                    self.log_message('EAGAIN (%d) while reading from raw_requestline' % i)
 
49
                    time.sleep(0.01)
 
50
                    continue
 
51
                raise
 
52
            else:
 
53
                break
 
54
        if not self.raw_requestline:
 
55
            self.close_connection = 1
 
56
            return
 
57
        if not self.parse_request(): # An error code has been sent, just exit
 
58
            return
 
59
        mname = 'do_' + self.command
 
60
        if not hasattr(self, mname):
 
61
            self.send_error(501, "Unsupported method (%r)" % self.command)
 
62
            return
 
63
        method = getattr(self, mname)
 
64
        method()
 
65
 
 
66
class TestingHTTPServer(BaseHTTPServer.HTTPServer):
 
67
    def __init__(self, server_address, RequestHandlerClass, test_case):
 
68
        BaseHTTPServer.HTTPServer.__init__(self, server_address,
 
69
                                                RequestHandlerClass)
 
70
        self.test_case = test_case
 
71
 
 
72
 
 
73
class TestCaseWithWebserver(TestCaseInTempDir):
 
74
    """Derived class that starts a localhost-only webserver
 
75
    (in addition to what TestCaseInTempDir does).
 
76
 
 
77
    This is useful for testing RemoteBranch.
 
78
    """
 
79
 
 
80
    _HTTP_PORTS = range(13000, 0x8000)
 
81
 
 
82
    def _http_start(self):
 
83
        import SimpleHTTPServer, BaseHTTPServer, socket, errno
 
84
        httpd = None
 
85
        for port in self._HTTP_PORTS:
 
86
            try:
 
87
                httpd = TestingHTTPServer(('localhost', port),
 
88
                                          TestingHTTPRequestHandler,
 
89
                                          self)
 
90
            except socket.error, e:
 
91
                if e.args[0] == errno.EADDRINUSE:
 
92
                    continue
 
93
                print >>sys.stderr, "Cannot run webserver :-("
 
94
                raise
 
95
            else:
 
96
                break
 
97
 
 
98
        if httpd is None:
 
99
            raise WebserverNotAvailable("Cannot run webserver :-( "
 
100
                                        "no free ports in range %s..%s" %
 
101
                                        (_HTTP_PORTS[0], _HTTP_PORTS[-1]))
 
102
 
 
103
        self._http_base_url = 'http://localhost:%s/' % port
 
104
        self._http_starting.release()
 
105
        httpd.socket.settimeout(0.1)
 
106
 
 
107
        while self._http_running:
 
108
            try:
 
109
                httpd.handle_request()
 
110
            except socket.timeout:
66
111
                pass
67
 
            else:
68
 
                raise
69
 
        return False
70
 
 
71
 
 
72
 
class InvalidStatusRequestHandler(TestingHTTPRequestHandler):
73
 
    """Whatever request comes in, returns am invalid status"""
74
 
 
75
 
    def parse_request(self):
76
 
        """Fakes handling a single HTTP request, returns a bad status"""
77
 
        ignored = TestingHTTPRequestHandler.parse_request(self)
78
 
        self.wfile.write("Invalid status line\r\n")
79
 
        return False
80
 
 
81
 
 
82
 
class BadProtocolRequestHandler(TestingHTTPRequestHandler):
83
 
    """Whatever request comes in, returns a bad protocol version"""
84
 
 
85
 
    def parse_request(self):
86
 
        """Fakes handling a single HTTP request, returns a bad status"""
87
 
        ignored = TestingHTTPRequestHandler.parse_request(self)
88
 
        # Returns an invalid protocol version, but curl just
89
 
        # ignores it and those cannot be tested.
90
 
        self.wfile.write("%s %d %s\r\n" % ('HTTP/0.0',
91
 
                                           404,
92
 
                                           'Look at my protocol version'))
93
 
        return False
94
 
 
95
 
 
96
 
class ForbiddenRequestHandler(TestingHTTPRequestHandler):
97
 
    """Whatever request comes in, returns a 403 code"""
98
 
 
99
 
    def parse_request(self):
100
 
        """Handle a single HTTP request, by replying we cannot handle it"""
101
 
        ignored = TestingHTTPRequestHandler.parse_request(self)
102
 
        self.send_error(403)
103
 
        return False
104
 
 
105
 
 
106
 
class HTTPServerWithSmarts(HttpServer):
107
 
    """HTTPServerWithSmarts extends the HttpServer with POST methods that will
108
 
    trigger a smart server to execute with a transport rooted at the rootdir of
109
 
    the HTTP server.
110
 
    """
111
 
 
112
 
    def __init__(self):
113
 
        HttpServer.__init__(self, SmartRequestHandler)
114
 
 
115
 
 
116
 
class SmartRequestHandler(TestingHTTPRequestHandler):
117
 
    """Extend TestingHTTPRequestHandler to support smart client POSTs."""
118
 
 
119
 
    def do_POST(self):
120
 
        """Hand the request off to a smart server instance."""
121
 
        self.send_response(200)
122
 
        self.send_header("Content-type", "application/octet-stream")
123
 
        transport = get_transport(self.server.test_case._home_dir)
124
 
        # TODO: We might like to support streaming responses.  1.0 allows no
125
 
        # Content-length in this case, so for integrity we should perform our
126
 
        # own chunking within the stream.
127
 
        # 1.1 allows chunked responses, and in this case we could chunk using
128
 
        # the HTTP chunking as this will allow HTTP persistence safely, even if
129
 
        # we have to stop early due to error, but we would also have to use the
130
 
        # HTTP trailer facility which may not be widely available.
131
 
        out_buffer = StringIO()
132
 
        smart_protocol_request = smart.SmartServerRequestProtocolOne(
133
 
                transport, out_buffer.write)
134
 
        # if this fails, we should return 400 bad request, but failure is
135
 
        # failure for now - RBC 20060919
136
 
        data_length = int(self.headers['Content-Length'])
137
 
        # Perhaps there should be a SmartServerHTTPMedium that takes care of
138
 
        # feeding the bytes in the http request to the smart_protocol_request,
139
 
        # but for now it's simpler to just feed the bytes directly.
140
 
        smart_protocol_request.accept_bytes(self.rfile.read(data_length))
141
 
        assert smart_protocol_request.next_read_size() == 0, (
142
 
            "not finished reading, but all data sent to protocol.")
143
 
        self.send_header("Content-Length", str(len(out_buffer.getvalue())))
144
 
        self.end_headers()
145
 
        self.wfile.write(out_buffer.getvalue())
146
 
 
147
 
 
148
 
class SingleRangeRequestHandler(TestingHTTPRequestHandler):
149
 
    """Always reply to range request as if they were single.
150
 
 
151
 
    Don't be explicit about it, just to annoy the clients.
152
 
    """
153
 
 
154
 
    def get_multiple_ranges(self, file, file_size, ranges):
155
 
        """Answer as if it was a single range request and ignores the rest"""
156
 
        (start, end) = ranges[0]
157
 
        return self.get_single_range(file, file_size, start, end)
158
 
 
159
 
 
160
 
class NoRangeRequestHandler(TestingHTTPRequestHandler):
161
 
    """Ignore range requests without notice"""
162
 
 
163
 
    # Just bypass the range handling done by TestingHTTPRequestHandler
164
 
    do_GET = SimpleHTTPRequestHandler.do_GET
165
 
 
166
 
 
167
 
class TestCaseWithWebserver(TestCaseWithTransport):
168
 
    """A support class that provides readonly urls that are http://.
169
 
 
170
 
    This is done by forcing the readonly server to be an http
171
 
    one. This will currently fail if the primary transport is not
172
 
    backed by regular disk files.
173
 
    """
174
 
    def setUp(self):
175
 
        super(TestCaseWithWebserver, self).setUp()
176
 
        self.transport_readonly_server = HttpServer
177
 
 
178
 
 
179
 
class TestCaseWithTwoWebservers(TestCaseWithWebserver):
180
 
    """A support class providinf readonly urls (on two servers) that are http://.
181
 
 
182
 
    We setup two webservers to allows various tests involving
183
 
    proxies or redirections from one server to the other.
184
 
    """
185
 
    def setUp(self):
186
 
        super(TestCaseWithTwoWebservers, self).setUp()
187
 
        self.transport_secondary_server = HttpServer
188
 
        self.__secondary_server = None
189
 
 
190
 
    def create_transport_secondary_server(self):
191
 
        """Create a transport server from class defined at init.
192
 
 
193
 
        This is mostly a hook for daughter classes.
194
 
        """
195
 
        return self.transport_secondary_server()
196
 
 
197
 
    def get_secondary_server(self):
198
 
        """Get the server instance for the secondary transport."""
199
 
        if self.__secondary_server is None:
200
 
            self.__secondary_server = self.create_transport_secondary_server()
201
 
            self.__secondary_server.setUp()
202
 
            self.addCleanup(self.__secondary_server.tearDown)
203
 
        return self.__secondary_server
204
 
 
205
 
 
206
 
class FakeProxyRequestHandler(TestingHTTPRequestHandler):
207
 
    """Append a '-proxied' suffix to file served"""
208
 
 
209
 
    def translate_path(self, path):
210
 
        # We need to act as a proxy and accept absolute urls,
211
 
        # which SimpleHTTPRequestHandler (grand parent) is not
212
 
        # ready for. So we just drop the protocol://host:port
213
 
        # part in front of the request-url (because we know we
214
 
        # would not forward the request to *another* proxy).
215
 
 
216
 
        # So we do what SimpleHTTPRequestHandler.translate_path
217
 
        # do beginning with python 2.4.3: abandon query
218
 
        # parameters, scheme, host port, etc (which ensure we
219
 
        # provide the right behaviour on all python versions).
220
 
        path = urlparse.urlparse(path)[2]
221
 
        # And now, we can apply *our* trick to proxy files
222
 
        self.path += '-proxied'
223
 
        # An finally we leave our mother class do whatever it
224
 
        # wants with the path
225
 
        return TestingHTTPRequestHandler.translate_path(self, path)
226
 
 
227
 
 
 
112
 
 
113
    def get_remote_url(self, path):
 
114
        import os
 
115
 
 
116
        path_parts = path.split(os.path.sep)
 
117
        if os.path.isabs(path):
 
118
            if path_parts[:len(self._local_path_parts)] != \
 
119
                   self._local_path_parts:
 
120
                raise BadWebserverPath(path, self.test_dir)
 
121
            remote_path = '/'.join(path_parts[len(self._local_path_parts):])
 
122
        else:
 
123
            remote_path = '/'.join(path_parts)
 
124
 
 
125
        self._http_starting.acquire()
 
126
        self._http_starting.release()
 
127
        return self._http_base_url + remote_path
 
128
 
 
129
    def setUp(self):
 
130
        TestCaseInTempDir.setUp(self)
 
131
        import threading, os
 
132
        self._local_path_parts = self.test_dir.split(os.path.sep)
 
133
        self._http_starting = threading.Lock()
 
134
        self._http_starting.acquire()
 
135
        self._http_running = True
 
136
        self._http_base_url = None
 
137
        self._http_thread = threading.Thread(target=self._http_start)
 
138
        self._http_thread.setDaemon(True)
 
139
        self._http_thread.start()
 
140
 
 
141
    def tearDown(self):
 
142
        self._http_running = False
 
143
        self._http_thread.join()
 
144
        TestCaseInTempDir.tearDown(self)
228
145