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

RFC-2396-encode URLs #37

Merged
merged 6 commits into from
Nov 29, 2022
Merged

RFC-2396-encode URLs #37

merged 6 commits into from
Nov 29, 2022

Conversation

Gallaecio
Copy link
Contributor

No description provided.

@Gallaecio Gallaecio requested review from kmike, wRAR and BurnzZ November 1, 2022 10:28
@Gallaecio
Copy link
Contributor Author

Gallaecio commented Nov 3, 2022

@kmike brought up that it is not RFC-3986, but java.net.URI’s flavor of RFC-2396, that we need to support.

After looking at the standards, and Java’s deviations, I think for our purposes updating the lists of characters to escape in different URL parts to those of the older standard is enough, and Java’s deviations do not seem to affect those, other than they make it clear that (escaped) UTF-8 characters are supported.

@Gallaecio Gallaecio changed the title RFC-3986-encode URLs RFC-2396-encode URLs Nov 3, 2022
@codecov-commenter
Copy link

codecov-commenter commented Nov 3, 2022

Codecov Report

Merging #37 (a3703b4) into main (b2edeac) will increase coverage by 3.17%.
The diff coverage is 62.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #37      +/-   ##
==========================================
+ Coverage   37.15%   40.32%   +3.17%     
==========================================
  Files           9        9              
  Lines         323      372      +49     
  Branches       45       54       +9     
==========================================
+ Hits          120      150      +30     
- Misses        203      217      +14     
- Partials        0        5       +5     
Impacted Files Coverage Δ
zyte_api/utils.py 68.75% <61.22%> (-24.59%) ⬇️
zyte_api/aio/client.py 27.39% <100.00%> (ø)

path_encoding: str = "utf8",
quote_path: bool = True,
) -> str:
"""Fork of ``w3lib.url.safe_url_string`` that enforces `RFC-3986`_.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @Gallaecio! Do you know if there is any way to prevent double-encoding in this function?

In [1]: from w3lib.url import safe_download_url, safe_url_string, canonicalize_url

In [2]: safe_url_string("https://www.example.com/product/hometales-pack-of-2-sky/626857878294#bcrumbSearch:pillow%20cover")
Out[2]: 'https://www.example.com/product/hometales-pack-of-2-sky/626857878294#bcrumbSearch:pillow%20cover'

vs

In [1]: from zyte_api.utils import _safe_url_string

In [2]: _safe_url_string("https://www.example.com/product/hometales-pack-of-2-sky/626857878294#bcrumbSearch:pillow%20cover")
Out[2]: 'https://www.example.com/product/hometales-pack-of-2-sky/626857878294#bcrumbSearch:pillow%2520cover'

The issue is that sometimes input URLs are encoded, and sometimes they are not. w3lib's safe_url_string tries support both. It seems that after this change we might break the client library for users who send the percent-escaped URLs (e.g. the website itself may already provide encoded versions).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch, let me fix that and add a test for it.

@Gallaecio
Copy link
Contributor Author

Rather than fixing my implementation here, I am thinking I should clean up and extend the corresponding tests on w3lib, then update w3lib to support the URL limitations of as many servers as possible (including Zyte API’s), and then use upstream safe_url_string here instead of a fork.

@kmike kmike merged commit 1dc76fa into zytedata:main Nov 29, 2022
@kmike
Copy link
Collaborator

kmike commented Nov 29, 2022

Thanks @Gallaecio!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants