1
# Copyright (C) 2007-2011 Canonical Ltd
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""HTTPS test server, available when ssl python module is available"""
22
from bzrlib.tests import (
29
class TestingHTTPSServerMixin:
31
def __init__(self, key_file, cert_file):
32
self.key_file = key_file
33
self.cert_file = cert_file
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)
43
def verify_request(self, request, client_address):
44
"""Verify the request.
46
Return True if we should proceed with this request, False if we should
47
not even touch a single byte in the socket !
49
serving = test_server.TestingTCPServerMixin.verify_request(
50
self, request, client_address)
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
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.
69
base = test_server.TestingTCPServerMixin
70
return base.ignored_exceptions_during_shutdown(self, e)
73
class TestingHTTPSServer(TestingHTTPSServerMixin,
74
http_server.TestingHTTPServer):
76
def __init__(self, server_address, request_handler_class,
77
test_case_server, key_file, cert_file):
78
TestingHTTPSServerMixin.__init__(self, key_file, cert_file)
79
http_server.TestingHTTPServer.__init__(
80
self, server_address, request_handler_class, test_case_server)
82
def get_request(self):
83
sock, addr = http_server.TestingHTTPServer.get_request(self)
84
return self._get_ssl_request(sock, addr)
87
class TestingThreadingHTTPSServer(TestingHTTPSServerMixin,
88
http_server.TestingThreadingHTTPServer):
90
def __init__(self, server_address, request_handler_class,
91
test_case_server, key_file, cert_file):
92
TestingHTTPSServerMixin.__init__(self, key_file, cert_file)
93
http_server.TestingThreadingHTTPServer.__init__(
94
self, server_address, request_handler_class, test_case_server)
96
def get_request(self):
97
sock, addr = http_server.TestingThreadingHTTPServer.get_request(self)
98
return self._get_ssl_request(sock, addr)
101
class HTTPSServer(http_server.HttpServer):
103
_url_protocol = 'https'
105
# The real servers depending on the protocol
106
http_server_class = {'HTTP/1.0': TestingHTTPSServer,
107
'HTTP/1.1': TestingThreadingHTTPSServer,
110
# Provides usable defaults since an https server requires both a
111
# private key and a certificate to work.
112
def __init__(self, request_handler=http_server.TestingHTTPRequestHandler,
113
protocol_version=None,
114
key_file=ssl_certs.build_path('server_without_pass.key'),
115
cert_file=ssl_certs.build_path('server.crt')):
116
http_server.HttpServer.__init__(self, request_handler=request_handler,
117
protocol_version=protocol_version)
118
self.key_file = key_file
119
self.cert_file = cert_file
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)
128
class HTTPSServer_urllib(HTTPSServer):
129
"""Subclass of HTTPSServer that gives https+urllib urls.
131
This is for use in testing: connections to this server will always go
132
through urllib where possible.
135
# urls returned by this server should require the urllib client impl
136
_url_protocol = 'https+urllib'
139
class HTTPSServer_PyCurl(HTTPSServer):
140
"""Subclass of HTTPSServer that gives http+pycurl urls.
142
This is for use in testing: connections to this server will always go
143
through pycurl where possible.
146
# We don't care about checking the pycurl availability as
147
# this server will be required only when pycurl is present
149
# urls returned by this server should require the pycurl client impl
150
_url_protocol = 'https+pycurl'