Skip to content

Commit 298eb28

Browse files
authored
Merge pull request #296 from kollivier/download_fixes
Fix issues with relative CSS url() detection…
2 parents 5bdd4ab + d58b038 commit 298eb28

3 files changed

Lines changed: 30 additions & 4 deletions

File tree

ricecooker/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
SUSHI_BAR_CLIENT = None
2222
STAGE = False
2323

24+
# When this is set to true, any failure will raise an error and stop the chef.
25+
# This will likely be set to true in a future version of ricecooker, once
26+
# we can ensure all ricecooker internal functions handle non-fatal errors
27+
# properly.
28+
STRICT = False
29+
2430
# Sometimes chef runs will get stuck indefinitely waiting on data from SSL conn,
2531
# so we add a timeout value as suggested in https://stackoverflow.com/a/30771995
2632
socket.setdefaulttimeout(20)

ricecooker/utils/downloader.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from selenium import webdriver
1212
import selenium.webdriver.support.ui as selenium_ui
1313
from requests_file import FileAdapter
14-
from ricecooker.config import LOGGER, PHANTOMJS_PATH
14+
from ricecooker.config import LOGGER, PHANTOMJS_PATH, STRICT
1515
from ricecooker.utils.html import download_file
1616
from ricecooker.utils.caching import CacheForeverHeuristic, FileCache, CacheControlAdapter, InvalidatingCacheControlAdapter
1717

@@ -146,6 +146,8 @@ def make_request(url, clear_cookies=False, headers=None, timeout=60, *args, **kw
146146

147147
if response.status_code != 200:
148148
print("NOT FOUND:", url)
149+
if STRICT:
150+
response.raise_for_status()
149151

150152
return response
151153

@@ -235,17 +237,30 @@ def css_content_middleware(content, url, **kwargs):
235237
if css_middleware:
236238
content = css_middleware(content, url, **kwargs)
237239

238-
file_dir = os.path.dirname(urlparse(url).path)
240+
root_parts = urlparse(url)
239241

240242
# Download linked fonts and images
241243
def repl(match):
242244
src = match.group(1)
245+
243246
if src.startswith('//localhost'):
244247
return 'url()'
245248
# Don't download data: files
246249
if src.startswith('data:'):
247250
return match.group(0)
248-
src_url = urljoin(base_url, os.path.join(file_dir, src))
251+
parts = urlparse(src)
252+
root_url = None
253+
if url:
254+
root_url = url[:url.rfind('/') + 1]
255+
256+
if parts.scheme and parts.netloc:
257+
src_url = src
258+
elif parts.path.startswith('/') and url:
259+
src_url = '{}://{}{}'.format(root_parts.scheme, root_parts.netloc, root_parts.path)
260+
elif url and root_url:
261+
src_url = urljoin(root_url, src)
262+
else:
263+
src_url = urljoin(base_url, src)
249264

250265
if _is_blacklisted(src_url, url_blacklist):
251266
print(' Skipping downloading blacklisted url', src_url)

ricecooker/utils/html.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from urllib.parse import urlparse, unquote
1010

1111
from .caching import FileCache, CacheControlAdapter
12-
from ricecooker.config import PHANTOMJS_PATH
12+
from ricecooker.config import LOGGER, PHANTOMJS_PATH, STRICT
1313

1414

1515

@@ -99,6 +99,11 @@ def download_file(url, destpath, filename=None, baseurl=None, subpath=None, midd
9999
response = request_fn(url)
100100
content = response.content
101101

102+
if STRICT:
103+
response.raise_for_status()
104+
elif response.status_code >= 400:
105+
LOGGER.warning("URL {} returned status {}".format(url, response.status_code))
106+
102107
# if there are any middleware callbacks, apply them to the content
103108
if middleware_callbacks:
104109
content = content.decode()

0 commit comments

Comments
 (0)