~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/https_server.py

Merge previous attempt into current trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007-2010 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
30
30
        self.key_file = key_file
31
31
        self.cert_file = cert_file
32
32
 
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.
 
33
    def _get_ssl_request (self, sock, addr):
 
34
        """Wrap the socket with SSL"""
 
35
        ssl_sock = ssl.wrap_socket(sock, server_side=True,
 
36
                                   keyfile=self.key_file,
 
37
                                   certfile=self.cert_file,
 
38
                                   do_handshake_on_connect=False)
 
39
        return ssl_sock, addr
 
40
 
 
41
    def verify_request(self, request, client_address):
 
42
        """Verify the request.
 
43
 
 
44
        Return True if we should proceed with this request, False if we should
 
45
        not even touch a single byte in the socket !
38
46
        """
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
44
 
 
 
47
        serving = self.serving is not None and self.serving.isSet()
 
48
        if serving:
 
49
            request.do_handshake()
 
50
        return serving
45
51
 
46
52
class TestingHTTPSServer(TestingHTTPSServerMixin,
47
53
                         http_server.TestingHTTPServer):
52
58
        http_server.TestingHTTPServer.__init__(
53
59
            self, server_address, request_handler_class, test_case_server)
54
60
 
 
61
    def _get_request (self):
 
62
        sock, addr = http_server.TestingHTTPServer._get_request(self)
 
63
        return self._get_ssl_request(sock, addr)
 
64
 
55
65
 
56
66
class TestingThreadingHTTPSServer(TestingHTTPSServerMixin,
57
67
                                  http_server.TestingThreadingHTTPServer):
62
72
        http_server.TestingThreadingHTTPServer.__init__(
63
73
            self, server_address, request_handler_class, test_case_server)
64
74
 
 
75
    def _get_request (self):
 
76
        sock, addr = http_server.TestingThreadingHTTPServer._get_request(self)
 
77
        return self._get_ssl_request(sock, addr)
 
78
 
65
79
 
66
80
class HTTPSServer(http_server.HttpServer):
67
81
 
73
87
                         }
74
88
 
75
89
    # Provides usable defaults since an https server requires both a
76
 
    # private key and certificate to work.
 
90
    # private key and a certificate to work.
77
91
    def __init__(self, request_handler=http_server.TestingHTTPRequestHandler,
78
92
                 protocol_version=None,
79
93
                 key_file=ssl_certs.build_path('server_without_pass.key'),