Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ lake exe generate-manual --depth 2

Then run a local web server on its output:
```
python3 -m http.server 8880 --directory _out/html-multi &
python3 ./server.py 8880 &
```

Then open <http://localhost:8880> in your browser.
Expand Down
63 changes: 63 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python3

# This wrapper turns off the Python HTTP server's overly aggressive
# cache headers, which can get in the way of Verso hovers.

from http import server # Python 3
from http.server import ThreadingHTTPServer, test
import os


class NonCachingHTTPRequestHandler(server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_my_headers()
server.SimpleHTTPRequestHandler.end_headers(self)

def send_my_headers(self):
self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
self.send_header("Pragma", "no-cache")
self.send_header("Expires", "0")

if __name__ == '__main__':
import argparse
import contextlib

parser = argparse.ArgumentParser()
parser.add_argument('-b', '--bind', metavar='ADDRESS',
help='bind to this address '
'(default: all interfaces)')
parser.add_argument('-d', '--directory', default="_out/html-multi",
help='serve this directory '
'(default: _out/html-multi)')
parser.add_argument('-p', '--protocol', metavar='VERSION',
default='HTTP/1.0',
help='conform to this HTTP version '
'(default: %(default)s)')
parser.add_argument('port', default=8000, type=int, nargs='?',
help='bind to this port '
'(default: %(default)s)')
args = parser.parse_args()

# ensure dual-stack is not disabled; ref #38907
class DualStackServer(ThreadingHTTPServer):

def server_bind(self):
# suppress exception when protocol is IPv4
with contextlib.suppress(Exception):
self.socket.setsockopt(
socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
return super().server_bind()

def finish_request(self, request, client_address):
self.RequestHandlerClass(request, client_address, self,
directory=args.directory)

print(args)

test(
HandlerClass=NonCachingHTTPRequestHandler,
ServerClass=DualStackServer,
port=args.port,
bind=args.bind,
protocol=args.protocol,
)