Skip to content

Commit

Permalink
Process submitted_url using PreparedRequest.prepare_url instead of ju…
Browse files Browse the repository at this point in the history
…st requote_uri.
  • Loading branch information
rebeccacremona committed Dec 5, 2023
1 parent 00fd3b4 commit 065aa8d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion perma_web/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def post(self, request, format=None):
role='primary',
status='pending',
record_type='response',
url=link.submitted_url,
url=link.ascii_safe_url,
).save()

# create screenshot placeholder
Expand Down
12 changes: 10 additions & 2 deletions perma_web/perma/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1599,8 +1599,16 @@ def ia_identifier(self):

@cached_property
def ascii_safe_url(self):
""" Encoded URL as string rather than unicode. """
return requests.utils.requote_uri(self.submitted_url)
"""URL as encoded internally by python requests"""
try:
# Attempt to quote the URL as well as possible:
# - percent encoding
# - unicode domains to punycode
# - etc.
return requests.Request('GET', self.submitted_url).prepare().url
except requests.exceptions.RequestException:
# If that fails, just percent encode everything for safety
return requests.utils.requote_uri(self.submitted_url)

@cached_property
def url_details(self):
Expand Down

0 comments on commit 065aa8d

Please sign in to comment.