Skip to content

Commit fd8e286

Browse files
committed
feat: Learned --cert and --key fields to support HTTPS for Safari. See docs.
Create a local certificate using mkcert, and then simwrapper serve --key [keyfilename.pem] --cert [certfile.pem] will start the local server using HTTPS. Your browser will not recognize the key unless you import the certificate files from mkcert first. This is useful for Safari, which does not support http://localhost Using mkcert: three commands get you going. ------------- brew install mkcert nss # install mkcert and nss from homebrew mkcert localhost # create PEM certificate for "localhost" mkcert -install # installs certificate in browser cert database
1 parent 337f052 commit fd8e286

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

simwrapper/MiniFileServer.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import re
55
import sys
66
import socket
7+
import ssl
78

89
try:
910
# Python3
1011
import http.server as SimpleHTTPServer
11-
from http.server import SimpleHTTPRequestHandler, test
12+
from http.server import HTTPServer, SimpleHTTPRequestHandler, test
1213

1314
except ImportError:
1415
# Python 2
@@ -135,16 +136,26 @@ def find_free_port(port):
135136
finally:
136137
s.close()
137138

138-
def run_mini_file_server(port):
139+
def run_mini_file_server(port, cert, key):
139140
print("\n-----------------------------------------------------------------")
140141
print("SimWrapper file server: port", port)
142+
if cert and key: print("Using HTTPS with PEM cert/key")
141143
print(current_dir)
142144

143145
free_port = find_free_port(port)
144146

145147
print("-----------------------------------------------------------------\n")
146-
test(HandlerClass=RangeRequestHandler, port=free_port)
148+
# test(HandlerClass=RangeRequestHandler, port=free_port)
149+
httpd = HTTPServer(('localhost', free_port), RangeRequestHandler)
147150

151+
if cert and key: httpd.socket = ssl.wrap_socket(
152+
httpd.socket,
153+
certfile=cert,
154+
keyfile=key,
155+
server_side=True
156+
)
157+
158+
httpd.serve_forever()
148159

149160
if __name__ == '__main__':
150161
run_mini_file_server(8000)

simwrapper/cli.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@ def cli():
1616

1717
@cli.command(help="Start a local file server in this folder")
1818
@click.argument('port', default=default_port)
19-
def serve(port):
20-
MiniFileServer.run_mini_file_server(port)
19+
@click.option('--cert', default=None, help="PEM Certificate filename. Provide both a certificate and key to serve HTTPS")
20+
@click.option('--key', default=None, help="PEM Key filename. Provide both a certificate and key to serve HTTPS")
21+
def serve(port, cert, key):
22+
if (cert and not key) or (not cert and key):
23+
raise click.BadParameter("need both a cert and a key to enable HTTPS")
24+
25+
MiniFileServer.run_mini_file_server(port, cert, key)
2126

2227
@cli.command(help="Open your browser and view this folder using SimWrapper")
2328
@click.argument('site', default='vsp')
24-
def open(site):
29+
@click.option('--cert', default=None, help="PEM Certificate filename. Provide both a certificate and key to serve HTTPS")
30+
@click.option('--key', default=None, help="PEM Key filename. Provide both a certificate and key to serve HTTPS")
31+
def open(site, cert, key):
32+
if (cert and not key) or (not cert and key):
33+
raise click.BadParameter("need both a cert and a key to enable HTTPS")
34+
2535
port = MiniFileServer.find_free_port(default_port)
2636
# Build the full URL for this site, including the free port number
2737
url = ''
@@ -35,4 +45,4 @@ def open(site):
3545
webbrowser.open(url, new=2, autoraise=True) # in a new tab
3646

3747
# Then start local fileserver
38-
MiniFileServer.run_mini_file_server(port)
48+
MiniFileServer.run_mini_file_server(port, cert, key)

0 commit comments

Comments
 (0)