/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: 2018-07-08 14:45:27 UTC
  • mto: This revision was merged to the branch mainline in revision 7036.
  • Revision ID: jelmer@jelmer.uk-20180708144527-codhlvdcdg9y0nji
Fix a bunch of merge tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007-2011 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
20
21
 
21
 
from bzrlib.tests import (
 
22
from . import (
22
23
    http_server,
23
24
    ssl_certs,
 
25
    test_server,
24
26
    )
25
27
 
26
28
 
30
32
        self.key_file = key_file
31
33
        self.cert_file = cert_file
32
34
 
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.
 
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 !
38
48
        """
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
 
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 as 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
        base = test_server.TestingTCPServerMixin
 
63
        return base.ignored_exceptions_during_shutdown(self, e)
44
64
 
45
65
 
46
66
class TestingHTTPSServer(TestingHTTPSServerMixin,
52
72
        http_server.TestingHTTPServer.__init__(
53
73
            self, server_address, request_handler_class, test_case_server)
54
74
 
 
75
    def get_request(self):
 
76
        sock, addr = http_server.TestingHTTPServer.get_request(self)
 
77
        return self._get_ssl_request(sock, addr)
 
78
 
55
79
 
56
80
class TestingThreadingHTTPSServer(TestingHTTPSServerMixin,
57
81
                                  http_server.TestingThreadingHTTPServer):
62
86
        http_server.TestingThreadingHTTPServer.__init__(
63
87
            self, server_address, request_handler_class, test_case_server)
64
88
 
 
89
    def get_request(self):
 
90
        sock, addr = http_server.TestingThreadingHTTPServer.get_request(self)
 
91
        return self._get_ssl_request(sock, addr)
 
92
 
65
93
 
66
94
class HTTPSServer(http_server.HttpServer):
67
95
 
73
101
                         }
74
102
 
75
103
    # Provides usable defaults since an https server requires both a
76
 
    # private key and certificate to work.
 
104
    # private key and a certificate to work.
77
105
    def __init__(self, request_handler=http_server.TestingHTTPRequestHandler,
78
106
                 protocol_version=None,
79
107
                 key_file=ssl_certs.build_path('server_without_pass.key'),
84
112
        self.cert_file = cert_file
85
113
        self.temp_files = []
86
114
 
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)
 
115
    def create_server(self):
 
116
        return self.server_class(
 
117
            (self.host, self.port), self.request_handler_class, self,
 
118
            self.key_file, self.cert_file)
90
119
 
91
120
 
92
121
class HTTPSServer_urllib(HTTPSServer):
98
127
 
99
128
    # urls returned by this server should require the urllib client impl
100
129
    _url_protocol = 'https+urllib'
101
 
 
102
 
 
103
 
class HTTPSServer_PyCurl(HTTPSServer):
104
 
    """Subclass of HTTPSServer that gives http+pycurl urls.
105
 
 
106
 
    This is for use in testing: connections to this server will always go
107
 
    through pycurl where possible.
108
 
    """
109
 
 
110
 
    # We don't care about checking the pycurl availability as
111
 
    # this server will be required only when pycurl is present
112
 
 
113
 
    # urls returned by this server should require the pycurl client impl
114
 
    _url_protocol = 'https+pycurl'