server.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python
  2. # Based on http/server.py from Python
  3. from argparse import ArgumentParser
  4. import contextlib
  5. from http.server import SimpleHTTPRequestHandler
  6. from http.server import ThreadingHTTPServer
  7. import os
  8. import socket
  9. class MyHTTPRequestHandler(SimpleHTTPRequestHandler):
  10. extensions_map = {
  11. ".manifest": "text/cache-manifest",
  12. ".html": "text/html",
  13. ".png": "image/png",
  14. ".jpg": "image/jpg",
  15. ".svg": "image/svg+xml",
  16. ".css": "text/css",
  17. ".js": "application/x-javascript",
  18. ".wasm": "application/wasm",
  19. "": "application/octet-stream",
  20. }
  21. def __init__(self, *args, maps=None, **kwargs):
  22. self.maps = maps or []
  23. SimpleHTTPRequestHandler.__init__(self, *args, **kwargs)
  24. def end_headers(self):
  25. self.send_my_headers()
  26. SimpleHTTPRequestHandler.end_headers(self)
  27. def send_my_headers(self):
  28. self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
  29. self.send_header("Pragma", "no-cache")
  30. self.send_header("Expires", "0")
  31. def translate_path(self, path):
  32. for map_path, map_prefix in self.maps:
  33. if path.startswith(map_prefix):
  34. res = os.path.join(map_path, path.removeprefix(map_prefix).lstrip("/"))
  35. break
  36. else:
  37. res = super().translate_path(path)
  38. return res
  39. def serve_forever(port: int, ServerClass):
  40. handler = MyHTTPRequestHandler
  41. addr = ("0.0.0.0", port)
  42. with ServerClass(addr, handler) as httpd:
  43. host, port = httpd.socket.getsockname()[:2]
  44. url_host = f"[{host}]" if ":" in host else host
  45. print(f"Serving HTTP on {host} port {port} (http://{url_host}:{port}/) ...")
  46. try:
  47. httpd.serve_forever()
  48. except KeyboardInterrupt:
  49. print("\nKeyboard interrupt received, exiting.")
  50. return 0
  51. def main():
  52. parser = ArgumentParser(allow_abbrev=False)
  53. parser.add_argument("port", nargs="?", type=int, default=8080)
  54. parser.add_argument("-d", dest="directory", type=str, default=None)
  55. parser.add_argument("--map", dest="maps", nargs="+", type=str, help="Mappings, used as e.g. \"$HOME/projects/SDL:/sdl\"")
  56. args = parser.parse_args()
  57. maps = []
  58. for m in args.maps:
  59. try:
  60. path, uri = m.split(":", 1)
  61. except ValueError:
  62. parser.error(f"Invalid mapping: \"{m}\"")
  63. maps.append((path, uri))
  64. class DualStackServer(ThreadingHTTPServer):
  65. def server_bind(self):
  66. # suppress exception when protocol is IPv4
  67. with contextlib.suppress(Exception):
  68. self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
  69. return super().server_bind()
  70. def finish_request(self, request, client_address):
  71. self.RequestHandlerClass(
  72. request,
  73. client_address,
  74. self,
  75. directory=args.directory,
  76. maps=maps,
  77. )
  78. return serve_forever(
  79. port=args.port,
  80. ServerClass=DualStackServer,
  81. )
  82. if __name__ == "__main__":
  83. raise SystemExit(main())