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

  • Committer: Jelmer Vernooij
  • Date: 2020-05-24 00:39:50 UTC
  • mto: This revision was merged to the branch mainline in revision 7504.
  • Revision ID: jelmer@jelmer.uk-20200524003950-bbc545r76vc5yajg
Add github action.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""HTTPS test server, available when ssl python module is available"""
 
18
 
 
19
import ssl
 
20
 
 
21
from . import (
 
22
    http_server,
 
23
    ssl_certs,
 
24
    test_server,
 
25
    )
 
26
 
 
27
 
 
28
class TestingHTTPSServerMixin:
 
29
 
 
30
    def __init__(self, key_file, cert_file):
 
31
        self.key_file = key_file
 
32
        self.cert_file = cert_file
 
33
 
 
34
    def _get_ssl_request(self, sock, addr):
 
35
        """Wrap the socket with SSL"""
 
36
        ssl_sock = ssl.wrap_socket(sock, server_side=True,
 
37
                                   keyfile=self.key_file,
 
38
                                   certfile=self.cert_file,
 
39
                                   do_handshake_on_connect=False)
 
40
        return ssl_sock, addr
 
41
 
 
42
    def verify_request(self, request, client_address):
 
43
        """Verify the request.
 
44
 
 
45
        Return True if we should proceed with this request, False if we should
 
46
        not even touch a single byte in the socket !
 
47
        """
 
48
        serving = test_server.TestingTCPServerMixin.verify_request(
 
49
            self, request, client_address)
 
50
        if serving:
 
51
            try:
 
52
                request.do_handshake()
 
53
            except ssl.SSLError:
 
54
                # FIXME: We proabaly want more tests to capture which ssl
 
55
                # errors are worth reporting but mostly our tests want an https
 
56
                # server that works -- vila 2012-01-19
 
57
                return False
 
58
        return serving
 
59
 
 
60
    def ignored_exceptions_during_shutdown(self, e):
 
61
        base = test_server.TestingTCPServerMixin
 
62
        return base.ignored_exceptions_during_shutdown(self, e)
 
63
 
 
64
 
 
65
class TestingHTTPSServer(TestingHTTPSServerMixin,
 
66
                         http_server.TestingHTTPServer):
 
67
 
 
68
    def __init__(self, server_address, request_handler_class,
 
69
                 test_case_server, key_file, cert_file):
 
70
        TestingHTTPSServerMixin.__init__(self, key_file, cert_file)
 
71
        http_server.TestingHTTPServer.__init__(
 
72
            self, server_address, request_handler_class, test_case_server)
 
73
 
 
74
    def get_request(self):
 
75
        sock, addr = http_server.TestingHTTPServer.get_request(self)
 
76
        return self._get_ssl_request(sock, addr)
 
77
 
 
78
 
 
79
class TestingThreadingHTTPSServer(TestingHTTPSServerMixin,
 
80
                                  http_server.TestingThreadingHTTPServer):
 
81
 
 
82
    def __init__(self, server_address, request_handler_class,
 
83
                 test_case_server, key_file, cert_file):
 
84
        TestingHTTPSServerMixin.__init__(self, key_file, cert_file)
 
85
        http_server.TestingThreadingHTTPServer.__init__(
 
86
            self, server_address, request_handler_class, test_case_server)
 
87
 
 
88
    def get_request(self):
 
89
        sock, addr = http_server.TestingThreadingHTTPServer.get_request(self)
 
90
        return self._get_ssl_request(sock, addr)
 
91
 
 
92
 
 
93
class HTTPSServer(http_server.HttpServer):
 
94
 
 
95
    _url_protocol = 'https'
 
96
 
 
97
    # The real servers depending on the protocol
 
98
    http_server_class = {'HTTP/1.0': TestingHTTPSServer,
 
99
                         'HTTP/1.1': TestingThreadingHTTPSServer,
 
100
                         }
 
101
 
 
102
    # Provides usable defaults since an https server requires both a
 
103
    # private key and a certificate to work.
 
104
    def __init__(self, request_handler=http_server.TestingHTTPRequestHandler,
 
105
                 protocol_version=None,
 
106
                 key_file=ssl_certs.build_path('server_without_pass.key'),
 
107
                 cert_file=ssl_certs.build_path('server.crt')):
 
108
        http_server.HttpServer.__init__(self, request_handler=request_handler,
 
109
                                        protocol_version=protocol_version)
 
110
        self.key_file = key_file
 
111
        self.cert_file = cert_file
 
112
        self.temp_files = []
 
113
 
 
114
    def create_server(self):
 
115
        return self.server_class(
 
116
            (self.host, self.port), self.request_handler_class, self,
 
117
            self.key_file, self.cert_file)
 
118
 
 
119
 
 
120
class HTTPSServer_urllib(HTTPSServer):
 
121
    """Subclass of HTTPSServer that gives https+urllib urls.
 
122
 
 
123
    This is for use in testing: connections to this server will always go
 
124
    through urllib where possible.
 
125
    """
 
126
 
 
127
    # urls returned by this server should require the urllib client impl
 
128
    _url_protocol = 'https+urllib'