~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/https_server.py

  • Committer: Ian Clatworthy
  • Date: 2009-01-19 02:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 3944.
  • Revision ID: ian.clatworthy@canonical.com-20090119022415-mo0mcfeiexfktgwt
apply jam's log --short fix (Ian Clatworthy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
 
17
"""HTTPS test server, available when ssl python module is available"""
 
18
 
 
19
import ssl
 
20
 
 
21
from bzrlib.tests import (
 
22
    http_server,
 
23
    ssl_certs,
 
24
    )
 
25
 
 
26
 
 
27
class TestingHTTPSServerMixin:
 
28
 
 
29
    def __init__(self, key_file, cert_file):
 
30
        self.key_file = key_file
 
31
        self.cert_file = cert_file
 
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.
 
38
        """
 
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
 
 
45
 
 
46
class TestingHTTPSServer(TestingHTTPSServerMixin,
 
47
                         http_server.TestingHTTPServer):
 
48
 
 
49
    def __init__(self, server_address, request_handler_class,
 
50
                 test_case_server, key_file, cert_file):
 
51
        TestingHTTPSServerMixin.__init__(self, key_file, cert_file)
 
52
        http_server.TestingHTTPServer.__init__(
 
53
            self, server_address, request_handler_class, test_case_server)
 
54
 
 
55
 
 
56
class TestingThreadingHTTPSServer(TestingHTTPSServerMixin,
 
57
                                  http_server.TestingThreadingHTTPServer):
 
58
 
 
59
    def __init__(self, server_address, request_handler_class,
 
60
                 test_case_server, key_file, cert_file):
 
61
        TestingHTTPSServerMixin.__init__(self, key_file, cert_file)
 
62
        http_server.TestingThreadingHTTPServer.__init__(
 
63
            self, server_address, request_handler_class, test_case_server)
 
64
 
 
65
 
 
66
class HTTPSServer(http_server.HttpServer):
 
67
 
 
68
    _url_protocol = 'https'
 
69
 
 
70
    # The real servers depending on the protocol
 
71
    http_server_class = {'HTTP/1.0': TestingHTTPSServer,
 
72
                         'HTTP/1.1': TestingThreadingHTTPSServer,
 
73
                         }
 
74
 
 
75
    # Provides usable defaults since an https server requires both a
 
76
    # private key and certificate to work.
 
77
    def __init__(self, request_handler=http_server.TestingHTTPRequestHandler,
 
78
                 key_file=ssl_certs.build_path('server_without_pass.key'),
 
79
                 cert_file=ssl_certs.build_path('server.crt')):
 
80
        http_server.HttpServer.__init__(self, request_handler)
 
81
        self.key_file = key_file
 
82
        self.cert_file = cert_file
 
83
        self.temp_files = []
 
84
 
 
85
    def create_httpd(self, serv_cls, rhandler_cls):
 
86
        return serv_cls((self.host, self.port), self.request_handler,
 
87
                        self, self.key_file, self.cert_file)
 
88
 
 
89
 
 
90
class HTTPSServer_urllib(HTTPSServer):
 
91
    """Subclass of HTTPSServer that gives https+urllib urls.
 
92
 
 
93
    This is for use in testing: connections to this server will always go
 
94
    through urllib where possible.
 
95
    """
 
96
 
 
97
    # urls returned by this server should require the urllib client impl
 
98
    _url_protocol = 'https+urllib'
 
99
 
 
100
 
 
101
class HTTPSServer_PyCurl(HTTPSServer):
 
102
    """Subclass of HTTPSServer that gives http+pycurl urls.
 
103
 
 
104
    This is for use in testing: connections to this server will always go
 
105
    through pycurl where possible.
 
106
    """
 
107
 
 
108
    # We don't care about checking the pycurl availability as
 
109
    # this server will be required only when pycurl is present
 
110
 
 
111
    # urls returned by this server should require the pycurl client impl
 
112
    _url_protocol = 'https+pycurl'