Compare commits

...

3 commits

Author SHA1 Message Date
Simon Willison
7b205af0b5 Strip querystring from path_from_header, refs #712 2020-03-25 21:09:00 -07:00
Simon Willison
0f217b5e8d Apply path_from_header to entire scope, refs #712 2020-03-25 21:03:17 -07:00
Simon Willison
fb0460dd0b Experimental path_from_header feature, refs #712 2020-03-25 19:52:42 -07:00
3 changed files with 16 additions and 0 deletions

View file

@ -136,6 +136,7 @@ CONFIG_OPTIONS = (
"Allow display of template debug information with ?_context=1",
),
ConfigOption("base_url", "/", "Datasette URLs should use this base"),
ConfigOption("path_from_header", "", "HTTP header to override incoming path"),
)
DEFAULT_CONFIG = {option.name: option.default for option in CONFIG_OPTIONS}
@ -739,6 +740,13 @@ class DatasetteRouter(AsgiRouter):
self.ds = datasette
super().__init__(routes)
async def __call__(self, scope, receive, send):
path_from_header = self.ds.config("path_from_header")
if path_from_header:
raw_path = dict(scope["headers"])[path_from_header.encode("utf8")].split(b"?")[0]
scope = dict(scope, raw_path=raw_path)
return await super().__call__(scope, receive, send)
async def route_path(self, scope, receive, send, path):
# Strip off base_url if present before routing
base_url = self.ds.config("base_url")

View file

@ -241,3 +241,10 @@ For example, if you are sending traffic from ``https://www.example.com/tools/dat
You can do that like so::
datasette mydatabase.db --config base_url:/tools/datasette/
.. _path_from_header:
path_from_header
----------------
See `issue #712 <https://github.com/simonw/datasette/issues/712>`__.

View file

@ -1308,6 +1308,7 @@ def test_config_json(app_client):
"hash_urls": False,
"template_debug": False,
"base_url": "/",
"path_from_header": "",
} == response.json