/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/tests/http_server.py

  • Committer: Vincent Ladeuil
  • Date: 2008-01-05 22:09:47 UTC
  • mto: (3928.1.1 bzr.integration)
  • mto: This revision was merged to the branch mainline in revision 3929.
  • Revision ID: v.ladeuil+lp@free.fr-20080105220947-t2kymulzeqf1g5n5
Fix the server name in script and ssl files.

* bzrlib/tests/ssl_certs/create_ssls.py:
(ssl_params): We use localhost, not 127.0.0.1.

* bzrlib/tests/ssl_certs/server_without_pass.key,
bzrlib/tests/ssl_certs/server_with_pass.key,
bzrlib/tests/ssl_certs/server.csr,
bzrlib/tests/ssl_certs/server.crt:
Re-generated since the server name was wrong.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
2
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
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
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
import errno
18
18
import httplib
20
20
import posixpath
21
21
import random
22
22
import re
23
 
import select
24
23
import SimpleHTTPServer
25
24
import socket
26
25
import SocketServer
31
30
import urlparse
32
31
 
33
32
from bzrlib import transport
34
 
from bzrlib.tests import test_server
35
33
from bzrlib.transport import local
36
34
 
37
35
 
56
54
 
57
55
    def setup(self):
58
56
        SimpleHTTPServer.SimpleHTTPRequestHandler.setup(self)
59
 
        self._cwd = self.server._home_dir
60
57
        tcs = self.server.test_case_server
61
58
        if tcs.protocol_version is not None:
62
59
            # If the test server forced a protocol version, use it
138
135
            # common)
139
136
            self.send_response(301)
140
137
            self.send_header("Location", self.path + "/")
141
 
            # Indicates that the body is empty for HTTP/1.1 clients
 
138
            # Indicates that the body is empty for HTTP/1.1 clients 
142
139
            self.send_header('Content-Length', '0')
143
140
            self.end_headers()
144
141
            return None
178
175
            content_length += self._header_line_length(
179
176
                'Content-Range', 'bytes %d-%d/%d' % (start, end, file_size))
180
177
            content_length += len('\r\n') # end headers
181
 
            content_length += end - start + 1
 
178
            content_length += end - start # + 1
182
179
        content_length += len(boundary_line)
183
180
        self.send_header('Content-length', content_length)
184
181
        self.end_headers()
283
280
        return self._translate_path(path)
284
281
 
285
282
    def _translate_path(self, path):
286
 
        """Translate a /-separated PATH to the local filename syntax.
287
 
 
288
 
        Note that we're translating http URLs here, not file URLs.
289
 
        The URL root location is the server's startup directory.
290
 
        Components that mean special things to the local file system
291
 
        (e.g. drive or directory names) are ignored.  (XXX They should
292
 
        probably be diagnosed.)
293
 
 
294
 
        Override from python standard library to stop it calling os.getcwd()
295
 
        """
296
 
        # abandon query parameters
297
 
        path = urlparse.urlparse(path)[2]
298
 
        path = posixpath.normpath(urllib.unquote(path))
299
 
        path = path.decode('utf-8')
300
 
        words = path.split('/')
301
 
        words = filter(None, words)
302
 
        path = self._cwd
303
 
        for num, word in enumerate(words):
304
 
            if num == 0:
 
283
        return SimpleHTTPServer.SimpleHTTPRequestHandler.translate_path(
 
284
            self, path)
 
285
 
 
286
    if sys.platform == 'win32':
 
287
        # On win32 you cannot access non-ascii filenames without
 
288
        # decoding them into unicode first.
 
289
        # However, under Linux, you can access bytestream paths
 
290
        # without any problems. If this function was always active
 
291
        # it would probably break tests when LANG=C was set
 
292
        def _translate_path(self, path):
 
293
            """Translate a /-separated PATH to the local filename syntax.
 
294
 
 
295
            For bzr, all url paths are considered to be utf8 paths.
 
296
            On Linux, you can access these paths directly over the bytestream
 
297
            request, but on win32, you must decode them, and access them
 
298
            as Unicode files.
 
299
            """
 
300
            # abandon query parameters
 
301
            path = urlparse.urlparse(path)[2]
 
302
            path = posixpath.normpath(urllib.unquote(path))
 
303
            path = path.decode('utf-8')
 
304
            words = path.split('/')
 
305
            words = filter(None, words)
 
306
            path = os.getcwdu()
 
307
            for word in words:
305
308
                drive, word = os.path.splitdrive(word)
306
 
            head, word = os.path.split(word)
307
 
            if word in (os.curdir, os.pardir): continue
308
 
            path = os.path.join(path, word)
309
 
        return path
 
309
                head, word = os.path.split(word)
 
310
                if word in (os.curdir, os.pardir): continue
 
311
                path = os.path.join(path, word)
 
312
            return path
310
313
 
311
314
 
312
315
class TestingHTTPServerMixin:
317
320
        # server), allowing dynamic behaviors to be defined from
318
321
        # the tests cases.
319
322
        self.test_case_server = test_case_server
320
 
        self._home_dir = test_case_server._home_dir
321
323
 
322
 
    def stop_server(self):
 
324
    def tearDown(self):
323
325
         """Called to clean-up the server.
324
 
 
 
326
 
325
327
         Since the server may be (surely is, even) in a blocking listen, we
326
328
         shutdown its socket before closing it.
327
329
         """
344
346
             # WSAENOTCONN (10057) 'Socket is not connected' is harmless on
345
347
             # windows (occurs before the first connection attempt
346
348
             # vila--20071230)
347
 
 
348
 
             # 'Socket is not connected' can also occur on OSX, with a
349
 
             # "regular" ENOTCONN (when something went wrong during test case
350
 
             # setup leading to self.setUp() *not* being called but
351
 
             # self.stop_server() still being called -- vila20081106
352
 
             if not len(e.args) or e.args[0] not in (errno.ENOTCONN, 10057):
 
349
             if not len(e.args) or e.args[0] != 10057:
353
350
                 raise
354
351
         # Let the server properly close the socket
355
352
         self.server_close()
356
353
 
357
 
 
358
354
class TestingHTTPServer(SocketServer.TCPServer, TestingHTTPServerMixin):
359
355
 
360
356
    def __init__(self, server_address, request_handler_class,
383
379
        # lying around.
384
380
        self.daemon_threads = True
385
381
 
386
 
    def process_request_thread(self, request, client_address):
387
 
        SocketServer.ThreadingTCPServer.process_request_thread(
388
 
            self, request, client_address)
389
 
        # Under some circumstances (as in bug #383920), we need to force the
390
 
        # shutdown as python delays it until gc occur otherwise and the client
391
 
        # may hang.
392
 
        try:
393
 
            # The request process has been completed, the thread is about to
394
 
            # die, let's shutdown the socket if we can.
395
 
            request.shutdown(socket.SHUT_RDWR)
396
 
        except (socket.error, select.error), e:
397
 
            if e[0] in (errno.EBADF, errno.ENOTCONN):
398
 
                # Right, the socket is already down
399
 
                pass
400
 
            else:
401
 
                raise
402
 
 
403
382
 
404
383
class HttpServer(transport.Server):
405
384
    """A test server for http transports.
438
417
        # Allows tests to verify number of GET requests issued
439
418
        self.GET_request_nb = 0
440
419
 
441
 
    def create_httpd(self, serv_cls, rhandler_cls):
442
 
        return serv_cls((self.host, self.port), self.request_handler, self)
443
 
 
444
 
    def __repr__(self):
445
 
        return "%s(%s:%s)" % \
446
 
            (self.__class__.__name__, self.host, self.port)
 
420
    def create_httpd(self):
 
421
        return TestingHTTPServer((self.host, self.port), self.request_handler,
 
422
                                 self)
447
423
 
448
424
    def _get_httpd(self):
449
425
        if self._httpd is None:
462
438
            if serv_cls is None:
463
439
                raise httplib.UnknownProtocol(proto_vers)
464
440
            else:
465
 
                self._httpd = self.create_httpd(serv_cls, rhandler)
466
 
            self.host, self.port = self._httpd.socket.getsockname()
 
441
                self._httpd = serv_cls((self.host, self.port), rhandler, self)
 
442
            host, self.port = self._httpd.socket.getsockname()
467
443
        return self._httpd
468
444
 
469
445
    def _http_start(self):
494
470
                httpd.handle_request()
495
471
            except socket.timeout:
496
472
                pass
497
 
            except (socket.error, select.error), e:
498
 
                if (e[0] == errno.EBADF
499
 
                    or (sys.platform == 'win32' and e[0] == 10038)):
500
 
                    # Starting with python-2.6, handle_request may raise socket
501
 
                    # or select exceptions when the server is shut down (as we
502
 
                    # do).
503
 
                    # 10038 = WSAENOTSOCK
504
 
                    # http://msdn.microsoft.com/en-us/library/ms740668%28VS.85%29.aspx
505
 
                    pass
506
 
                else:
507
 
                    raise
508
473
 
509
474
    def _get_remote_url(self, path):
510
475
        path_parts = path.split(os.path.sep)
522
487
        """Capture Server log output."""
523
488
        self.logs.append(format % args)
524
489
 
525
 
    def start_server(self, backing_transport_server=None):
526
 
        """See bzrlib.transport.Server.start_server.
527
 
 
 
490
    def setUp(self, backing_transport_server=None):
 
491
        """See bzrlib.transport.Server.setUp.
 
492
        
528
493
        :param backing_transport_server: The transport that requests over this
529
494
            protocol should be forwarded to. Note that this is currently not
530
495
            supported for HTTP.
531
496
        """
532
497
        # XXX: TODO: make the server back onto vfs_server rather than local
533
498
        # disk.
534
 
        if not (backing_transport_server is None
535
 
                or isinstance(backing_transport_server,
536
 
                              test_server.LocalURLServer)):
537
 
            raise AssertionError(
538
 
                "HTTPServer currently assumes local transport, got %s" % \
539
 
                backing_transport_server)
 
499
        assert backing_transport_server is None or \
 
500
            isinstance(backing_transport_server, local.LocalURLServer), \
 
501
            "HTTPServer currently assumes local transport, got %s" % \
 
502
            backing_transport_server
540
503
        self._home_dir = os.getcwdu()
541
504
        self._local_path_parts = self._home_dir.split(os.path.sep)
542
505
        self._http_base_url = None
559
522
        self._http_starting.release()
560
523
        self.logs = []
561
524
 
562
 
    def stop_server(self):
563
 
        self._httpd.stop_server()
 
525
    def tearDown(self):
 
526
        """See bzrlib.transport.Server.tearDown."""
 
527
        self._httpd.tearDown()
564
528
        self._http_running = False
565
529
        # We don't need to 'self._http_thread.join()' here since the thread is
566
530
        # a daemonic one and will be garbage collected anyway. Joining just