~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/https_server.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-10-13 06:08:53 UTC
  • mfrom: (4737.1.1 merge-2.0-into-devel)
  • Revision ID: pqm@pqm.ubuntu.com-20091013060853-erk2aaj80fnkrv25
(andrew) Merge lp:bzr/2.0 into lp:bzr, including fixes for #322807,
        #389413, #402623 and documentation improvements.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2011 Canonical Ltd
 
1
# Copyright (C) 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
17
17
"""HTTPS test server, available when ssl python module is available"""
18
18
 
19
19
import ssl
20
 
import sys
21
20
 
22
21
from bzrlib.tests import (
23
22
    http_server,
24
23
    ssl_certs,
25
 
    test_server,
26
24
    )
27
25
 
28
26
 
32
30
        self.key_file = key_file
33
31
        self.cert_file = cert_file
34
32
 
35
 
    def _get_ssl_request (self, sock, addr):
36
 
        """Wrap the socket with SSL"""
37
 
        ssl_sock = ssl.wrap_socket(sock, server_side=True,
38
 
                                   keyfile=self.key_file,
39
 
                                   certfile=self.cert_file,
40
 
                                   do_handshake_on_connect=False)
41
 
        return ssl_sock, addr
42
 
 
43
 
    def verify_request(self, request, client_address):
44
 
        """Verify the request.
45
 
 
46
 
        Return True if we should proceed with this request, False if we should
47
 
        not even touch a single byte in the socket !
 
33
    def get_request (self):
 
34
        """Get the request and client address from the socket.
 
35
 
 
36
        This is called in response to a connection issued to the server, we
 
37
        wrap the socket with SSL.
48
38
        """
49
 
        serving = test_server.TestingTCPServerMixin.verify_request(
50
 
            self, request, client_address)
51
 
        if serving:
52
 
            try:
53
 
                request.do_handshake()
54
 
            except ssl.SSLError, e:
55
 
                # FIXME: We proabaly want more tests to capture which ssl
56
 
                # errors are worth reporting but mostly our tests want an https
57
 
                # server that works -- vila 2012-01-19
58
 
                return False
59
 
        return serving
60
 
 
61
 
    def ignored_exceptions_during_shutdown(self, e):
62
 
        if (sys.version < (2, 7) and isinstance(e, TypeError)
63
 
            and e.args[0] == "'member_descriptor' object is not callable"):
64
 
            # Fixed in python-2.7 (and some Ubuntu 2.6) there is a bug where
65
 
            # the ssl socket fail to raise a socket.error when trying to read
66
 
            # from a closed socket. This is rarely observed in practice but
67
 
            # still make valid selftest runs fail if not caught.
68
 
            return True
69
 
        base = test_server.TestingTCPServerMixin
70
 
        return base.ignored_exceptions_during_shutdown(self, e)
 
39
        sock, addr = self.socket.accept()
 
40
        sslconn = ssl.wrap_socket(sock, server_side=True,
 
41
                                  keyfile=self.key_file,
 
42
                                  certfile=self.cert_file)
 
43
        return sslconn, addr
71
44
 
72
45
 
73
46
class TestingHTTPSServer(TestingHTTPSServerMixin,
79
52
        http_server.TestingHTTPServer.__init__(
80
53
            self, server_address, request_handler_class, test_case_server)
81
54
 
82
 
    def get_request(self):
83
 
        sock, addr = http_server.TestingHTTPServer.get_request(self)
84
 
        return self._get_ssl_request(sock, addr)
85
 
 
86
55
 
87
56
class TestingThreadingHTTPSServer(TestingHTTPSServerMixin,
88
57
                                  http_server.TestingThreadingHTTPServer):
93
62
        http_server.TestingThreadingHTTPServer.__init__(
94
63
            self, server_address, request_handler_class, test_case_server)
95
64
 
96
 
    def get_request(self):
97
 
        sock, addr = http_server.TestingThreadingHTTPServer.get_request(self)
98
 
        return self._get_ssl_request(sock, addr)
99
 
 
100
65
 
101
66
class HTTPSServer(http_server.HttpServer):
102
67
 
108
73
                         }
109
74
 
110
75
    # Provides usable defaults since an https server requires both a
111
 
    # private key and a certificate to work.
 
76
    # private key and certificate to work.
112
77
    def __init__(self, request_handler=http_server.TestingHTTPRequestHandler,
113
78
                 protocol_version=None,
114
79
                 key_file=ssl_certs.build_path('server_without_pass.key'),
119
84
        self.cert_file = cert_file
120
85
        self.temp_files = []
121
86
 
122
 
    def create_server(self):
123
 
        return self.server_class(
124
 
            (self.host, self.port), self.request_handler_class, self,
125
 
            self.key_file, self.cert_file)
 
87
    def create_httpd(self, serv_cls, rhandler_cls):
 
88
        return serv_cls((self.host, self.port), self.request_handler,
 
89
                        self, self.key_file, self.cert_file)
126
90
 
127
91
 
128
92
class HTTPSServer_urllib(HTTPSServer):