Skip to content

Commit

Permalink
Follow up on create_redirects.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjordan committed Oct 21, 2024
1 parent 4358d41 commit 5d3164a
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions workbench_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3098,7 +3098,7 @@ def check_input(config, args):
for count, row in enumerate(check_for_redirects_csv_data, start=1):
if row["redirect_source"].lower().startswith("http"):
message = (
'Redirect source values cannot contain a hostname, they must be a path only. Please correct "'
'Redirect source values cannot contain a hostname, they must be a path only, without a hostname. Please correct "'
+ row["redirect_source"]
+ " (row "
+ str(count)
Expand All @@ -3108,7 +3108,32 @@ def check_input(config, args):
warnings_about_redirect_input_csv = True
continue

# Log if source path doesn't exist. We don't use issue_request() since we
# Check to see if the redirect source value is already a redirect. We don't use issue_request()
# since we don't want to override config["allow_redirects"] for this one request.
is_redirect_url = config["host"].rstrip("/") + "/" + row["redirect_source"]
is_redirect_response = requests.head(
is_redirect_url,
allow_redirects=False,
verify=config["secure_ssl_only"],
auth=(config["username"], config["password"]),
)
if str(is_redirect_response.status_code).startswith("30"):
message = (
'Redirect source path "'
+ row["redirect_source"].strip()
+ '" (row '
+ str(count)
+ ') is already a redirect to "'
+ is_redirect_response.headers["Location"]
+ '" (HTTP response code is '
+ str(is_redirect_response.status_code)
+ ")."
)
logging.warning(message)
warnings_about_redirect_input_csv = True
continue

# Log whether the source path exists. We don't use issue_request() since we
# don't want to override config["allow_redirects"] for this one request.
path_exists_url = config["host"].rstrip("/") + "/" + row["redirect_source"]
path_exists_response = requests.head(
Expand All @@ -3125,33 +3150,23 @@ def check_input(config, args):
+ str(count)
+ ") does not exist (HTTP response code is "
+ str(path_exists_response.status_code)
+ "). This may be intentional."
+ ")."
)
logging.warning(message)
warnings_about_redirect_input_csv = True
continue

# Check to see if the redirect source value is already a redirect. We don't use issue_request()
# since we don't want to override config["allow_redirects"] for this one request.
is_redirect_url = config["host"].rstrip("/") + "/" + row["redirect_source"]
is_redirect_response = requests.head(
is_redirect_url,
allow_redirects=False,
verify=config["secure_ssl_only"],
auth=(config["username"], config["password"]),
)
if str(is_redirect_response.status_code).startswith("30"):
else:
# We've already tested for 3xx responses, so assume that the path exists.
message = (
'Redirect from "'
'Redirect source path "'
+ row["redirect_source"].strip()
+ '" (row '
+ str(count)
+ ") is already a redirect (HTTP response code is "
+ str(is_redirect_response.status_code)
+ ")."
+ ") already exists."
)
logging.warning(message)
warnings_about_redirect_input_csv = True
continue

if warnings_about_redirect_input_csv is True:
message = (
Expand Down

0 comments on commit 5d3164a

Please sign in to comment.