Skip to content
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

Proxito: browndate for redirecting / to README.html #11348

Merged
merged 7 commits into from
May 29, 2024
4 changes: 3 additions & 1 deletion readthedocs/projects/tasks/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ def _create_imported_files_and_search_index(

# Create the imported file only if it's a top-level 404 file,
# or if it's an index file. We don't need to keep track of all files.
if relpath == "404.html" or filename in ["index.html", "README.html"]:
# TODO: delete README.html from this list after deprecation.
tryfiles = ["index.html", "README.html"]
if relpath == "404.html" or filename in tryfiles:
html_files_to_save.append(html_file)

# We first index the files in ES, and then save the objects in the DB.
Expand Down
19 changes: 12 additions & 7 deletions readthedocs/proxito/views/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
ServeRedirectMixin,
StorageFileNotFound,
)
from readthedocs.proxito.views.utils import allow_readme_html_as_index
from readthedocs.redirects.exceptions import InfiniteRedirectException
from readthedocs.storage import build_media_storage

Expand Down Expand Up @@ -641,12 +642,16 @@ def _get_index_file_redirect(self, request, project, version, filename, full_pat
- /en/latest/foo -> /en/latest/foo/README.html
- /en/latest/foo/ -> /en/latest/foo/README.html
"""
tryfiles = ["index.html", "README.html"]
# If the path ends with `/`, we already tried to serve
# the `/index.html` file, so we only need to test for
# the `/README.html` file.
if full_path.endswith("/"):
tryfiles = ["README.html"]

if allow_readme_html_as_index():
tryfiles = ["index.html", "README.html"]
# If the path ends with `/`, we already tried to serve
# the `/index.html` file, so we only need to test for
# the `/README.html` file.
if full_path.endswith("/"):
tryfiles = ["README.html"]
else:
tryfiles = ["index.html"]

tryfiles = [
(filename.rstrip("/") + f"/{tryfile}").lstrip("/") for tryfile in tryfiles
Expand All @@ -664,7 +669,7 @@ def _get_index_file_redirect(self, request, project, version, filename, full_pat
log.info("Redirecting to index file.", tryfile=tryfile)
# Use urlparse so that we maintain GET args in our redirect
parts = urlparse(full_path)
if tryfile.endswith("README.html"):
if allow_readme_html_as_index() and tryfile.endswith("README.html"):
new_path = parts.path.rstrip("/") + "/README.html"
else:
new_path = parts.path.rstrip("/") + "/"
Expand Down
23 changes: 23 additions & 0 deletions readthedocs/proxito/views/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import datetime

import pytz
import structlog
from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import render

Expand Down Expand Up @@ -56,3 +60,22 @@ def proxito_404_page_handler(
)
r.status_code = http_status
return r


def allow_readme_html_as_index():
tzinfo = pytz.timezone("America/Los_Angeles")
now = datetime.datetime.now(tz=tzinfo)

# Brownout dates as published in https://about.readthedocs.com/blog/2024/05/readme-html-deprecated/
# fmt: off
return not any([
# 12 hours browndate
datetime.datetime(2024, 6, 10, 0, 0, 0, tzinfo=tzinfo) < now < datetime.datetime(2024, 6, 10, 12, 0, 0, tzinfo=tzinfo),
# 24 hours browndate
datetime.datetime(2024, 6, 17, 0, 0, 0, tzinfo=tzinfo) < now < datetime.datetime(2024, 6, 18, 0, 0, 0, tzinfo=tzinfo),
# 48 hours browndate
datetime.datetime(2024, 6, 24, 0, 0, 0, tzinfo=tzinfo) < now < datetime.datetime(2024, 6, 26, 0, 0, 0, tzinfo=tzinfo),
# Deprecated after July 1st
datetime.datetime(2024, 7, 1, 0, 0, 0, tzinfo=tzinfo) < now,
]) and settings.RTD_ENFORCE_BROWNOUTS_FOR_DEPRECATIONS
# fmt: on