Fix OSError when saving CSS files whose URL has a query string - #58
Open
agu2347 wants to merge 1 commit into
Open
Fix OSError when saving CSS files whose URL has a query string#58agu2347 wants to merge 1 commit into
agu2347 wants to merge 1 commit into
Conversation
link.href.split('/')[-1] was used directly as the local output
filename, which includes the URL's query string verbatim (e.g.
'?v=1516052760.0'). Characters like '?' and '=' are invalid in
Windows filenames, causing OSError: [Errno 22] Invalid argument
when io.open() tried to create the file.
Add filename_from_href() which parses the URL and uses only the
path's basename, dropping the query string and fragment.
Fixes peterbe#53
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
run()derives the local output filename for each downloaded/minified CSS file directly from the URL:If the URL includes a query string (e.g. a cache-busting param like
?v=1516052760.0, common on production sites), that query string ends up as part of the filename. On Windows,?and=are not valid filename characters, soio.open()raises:Fixes #53.
Fix
Added a
filename_from_href()helper that parses the URL properly withurllib.parse.urlparse()and uses onlyos.path.basename(parsed.path), dropping the query string and fragment. Both theorig_nameandbefore_namefilename computations inrun()now go through this helper.Testing
Added
tests/test_main.py(plainunittest, since the existingtests/test_mincss.pydepends onnose, which is unmaintained/unavailable on current Python) with 4 cases covering: a URL with a query string (reproducing the exact reported bug), a query string + fragment, a plain URL (no regression), and a relative path with a query string.Confirmed the new test fails against the pre-fix logic (reproducing the exact query-string-retained-in-filename defect) and passes after the fix.