]> git.proxmox.com Git - ceph.git/blame - ceph/src/seastar/tests/unit/https-server.py
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / seastar / tests / unit / https-server.py
CommitLineData
1e59de90
TL
1#!/usr/bin/env python3
2#
3# This file is open source software, licensed to you under the terms
4# of the Apache License, Version 2.0 (the "License"). See the NOTICE file
5# distributed with this work for additional information regarding copyright
6# ownership. You may not use this file except in compliance with the License.
7#
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
19
20#
21# Copyright (C) 2023 Kefu Chai ( tchaikov@gmail.com )
22#
23
24
25import argparse
26import socket
27import ssl
28from http.server import HTTPServer as _HTTPServer
29from http.server import SimpleHTTPRequestHandler
30
31
32class HTTPSServer(_HTTPServer):
33 def __init__(self, addr, port, context):
34 super().__init__((addr, port), SimpleHTTPRequestHandler)
35 self.context = context
36
37 def get_request(self):
38 sock, addr = self.socket.accept()
39 ssl_conn = self.context.wrap_socket(sock, server_side=True)
40 return ssl_conn, addr
41
42 def get_listen_port(self):
43 if self.socket.family == socket.AF_INET:
44 addr, port = self.socket.getsockname()
45 return port
46 elif self.socket.family == socket.AF_INET6:
47 address, port, flowinfo, scope_id = self.socket.getsockname()
48 return port
49 else:
50 raise Exception(f"unknown family: {self.socket.family}")
51
52
53if __name__ == "__main__":
54 parser = argparse.ArgumentParser(description="httpd for testing TLS")
55 parser.add_argument('--server', action='store',
56 help='server address in <host>:<port> format',
57 default='localhost:11311')
58 parser.add_argument('--cert', action='store',
59 help='path to the certificate')
60 parser.add_argument('--key', action='store',
61 help='path to the private key')
62 args = parser.parse_args()
63 host, port = args.server.split(':')
64
65 context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
66 context.load_cert_chain(certfile=args.cert, keyfile=args.key)
67 with HTTPSServer(host, int(port), context) as server:
68 # print out the listening port when ready to serve
69 print(server.get_listen_port(), flush=True)
70 server.serve_forever()