-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
EmbedAPI: support "pretty URLs" #11811
Conversation
Tru for the URL ending with `index.html` if the original URL doesn't exist in the storage. This makes Sphinx HTMLDir build, Docusaurus and other documentation tools with pretty URLs to work properly.
Failing test |
readthedocs/embed/v3/views.py
Outdated
file_path | ||
) as fd: # pylint: disable=invalid-name | ||
return fd.read() | ||
tryfiles = [file_path, os.path.join(file_path, "index.html")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use build_media_storage.join, otherwise this is likely vulnerable to path traversal. Also, I think we already have some logic for handling index files somewhere, we just check if the file ends with / and append index.html, otherwise just treat it as a regular file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found this code:
readthedocs.org/readthedocs/proxito/views/mixins.py
Lines 66 to 76 in 7195740
# Handle our backend storage not supporting directory indexes, | |
# so we need to append index.html when appropriate. | |
if not filename or filename.endswith("/"): | |
filename += "index.html" | |
# If the filename starts with `/`, the join will fail, | |
# so we strip it before joining it. | |
try: | |
storage_path = build_media_storage.join( | |
base_storage_path, filename.lstrip("/") | |
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated this PR following that code 👍🏼
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we just check if the file ends with / and append index.html
We can't do that in this case. The filename
I'm receiving when using Docusaurus doesn't have an ending /
:
(Pdb++) filename
'/docs/tutorial-basics/create-a-page'
I'm not finding where in the code we manage this case from El Proxito. These type of URLs are definitely being served correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found why it works on El Proxito, we are performing a redirect when the URL doesn't ends with /
▶ curl -sIL http://test-builds.devthedocs.org/en/docusaurus/docs/tutorial-basics/create-a-page | head
HTTP/1.1 302 Found
Server: nginx/1.22.0
Date: Tue, 03 Dec 2024 07:41:12 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: keep-alive
Location: /en/docusaurus/docs/tutorial-basics/create-a-page/
Access-Control-Allow-Origin: *
Content-Language: en
Vary: Accept-Language, Cookie
...
This redirect is done at
log.info("Redirecting to index file.", tryfile=tryfile) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, we always redirect to the correct path instead of trying to serve the file from two different URLs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't do that in this case. The filename I'm receiving when using Docusaurus doesn't have an ending /:
Do you mean that Docusaurus creates links without /?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean that Docusaurus creates links without /?
Yes.
There are a few documentation tools that don't use ending /
, in particular those that are SPA for some reason.
If you go to https://test-builds.readthedocs.io/en/docusaurus/docs/intro we will redirect you to https://test-builds.readthedocs.io/en/docusaurus/docs/intro/. However, if you click any link, the resulting URL in your browser won't have that ending /
-- that's the URL we are sending to the backend, and since we don't find the file in the storage we are returning 404.
Checking for the index.html
solves this issue.
Tru for the URL ending with
index.html
if the original URL doesn't exist in the storage.This makes Sphinx HTMLDir build, Docusaurus and other documentation tools with pretty URLs to work properly.