-
-
Notifications
You must be signed in to change notification settings - Fork 697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base_url is omitted in JSON and CSV views #1519
Comments
|
Having a live demo running on Cloud Run that proxies through Apache and uses |
I now have a |
The relevant test is this one: Lines 1608 to 1649 in 3025505
I modified that test to add
BUT... in the debugger:
Those all have the correct prefix! But that's not what I'm seeing in my Something very weird is going on here. |
I added
And then hit {
"edit_sql_url": "/foo/bar/fixtures?sql=select+%2A+from+compound_three_primary_keys+limit+1",
"settings": {
"force_https_urls": false,
"template_debug": true,
"trace_debug": false,
"base_url": "/foo/bar/"
},
"show_hide_link": "/fixtures?sql=select+%2A+from+compound_three_primary_keys+limit+1&_context=1&_hide_sql=1",
"show_hide_text": "hide",
"show_hide_hidden": "",
"renderers": {
"json": "/fixtures.json?sql=select+*+from+compound_three_primary_keys+limit+1&_context=1"
},
"url_csv": "/fixtures.csv?sql=select+*+from+compound_three_primary_keys+limit+1&_context=1&_size=max",
"url_csv_path": "/fixtures.csv",
"base_url": "/foo/bar/"
} This is so strange. And it's really strange that the bug doesn't show up in the tests. |
Here's the code that generates datasette/datasette/views/database.py Lines 416 to 420 in 8584993
And here's the code for datasette/datasette/views/database.py Lines 432 to 433 in 8584993
And for datasette/datasette/views/base.py Lines 600 to 602 in 8584993
|
The implementations of datasette/datasette/utils/__init__.py Lines 228 to 254 in 8584993
datasette/datasette/utils/__init__.py Lines 710 to 729 in 8584993
|
In the
I'm going to add a |
Modified my
And now the
That explains the bug - that request doesn't maintain the original path prefix of |
Still not clear why the tests pass but the live example fails. |
Figured it out! The test is not an accurate recreation of what is happening, because it doesn't simulate a request with a path of |
https://docs.datasette.io/en/stable/deploying.html#apache-proxy-configuration says I should use |
I think what's happening here is Apache is actually making a request to This is pretty confusing! I think Datasette should ONLY reply to But shipping that change could break existing deployments. Maybe that should be a breaking change for 1.0. |
In the meantime I can catch these errors by changing the test to run each path twice, once with and once without the prefix. This should accurately simulate how Apache is working here. |
Thanks to #1522 I have a live demo that exhibits this bug now: https://apache-proxy-demo.datasette.io/prefix/fixtures/attraction_characteristic |
This worked, I managed to get the tests to fail! Here's the change I made: diff --git a/tests/test_html.py b/tests/test_html.py
index f24165b..dbdfe59 100644
--- a/tests/test_html.py
+++ b/tests/test_html.py
@@ -1614,12 +1614,19 @@ def test_metadata_sort_desc(app_client):
"/fixtures/compound_three_primary_keys/a,a,a",
"/fixtures/paginated_view",
"/fixtures/facetable",
+ "/fixtures?sql=select+1",
],
)
-def test_base_url_config(app_client_base_url_prefix, path):
+@pytest.mark.parametrize("use_prefix", (True, False))
+def test_base_url_config(app_client_base_url_prefix, path, use_prefix):
client = app_client_base_url_prefix
- response = client.get("/prefix/" + path.lstrip("/"))
+ path_to_get = path
+ if use_prefix:
+ path_to_get = "/prefix/" + path.lstrip("/")
+ response = client.get(path_to_get)
soup = Soup(response.body, "html.parser")
+ if path == "/fixtures?sql=select+1":
+ assert False
for el in soup.findAll(["a", "link", "script"]):
if "href" in el.attrs:
href = el["href"]
@@ -1642,11 +1649,12 @@ def test_base_url_config(app_client_base_url_prefix, path):
# If this has been made absolute it may start http://localhost/
if href.startswith("http://localhost/"):
href = href[len("http://localost/") :]
- assert href.startswith("/prefix/"), {
+ assert href.startswith("/prefix/"), json.dumps({
"path": path,
+ "path_to_get": path_to_get,
"href_or_src": href,
"element_parent": str(el.parent),
- }
+ }, indent=4, default=repr)
def test_base_url_affects_metadata_extra_css_urls(app_client_base_url_prefix): |
Adding that test found (I hope!) all of the remaining |
Ouch a nasty bug crept through there - https://datasette-apache-proxy-demo-j7hipcg4aq-uc.a.run.app/prefix/fixtures/compound_three_primary_keys says
|
OK, i think I got all of them this time! The latest demo is now live at https://datasette-apache-proxy-demo.fly.dev/prefix/fixtures/sortable?_facet=pk2 I'm closing this issue, but feel free to re-open it if you spot any that I missed. |
On further thought I'm not going to do this. Having Datasette work behind a proxy the way it does right now is clearly easy for people to deploy (now that I've fixed the bugs) and I trust my improved tests to catch problems in the future. |
thanks so very much for the prompt attention and fix! Plus, the animated GIF showing the bug is just extra and I love it. Interactions like this are why I love open source. |
I have a datasette deployment, using Apache2 to reverse proxy:
In settings.json I have
and datasette works correctly. However, if you view a query and then click on the 'This data as json, CSV' both links omit the base_url prefix and are therefore 404.
The text was updated successfully, but these errors were encountered: