Skip to content

Commit

Permalink
[http] Reject broken range before request (yt-dlp#3079)
Browse files Browse the repository at this point in the history
* And fix filesize estimate for byterange downloads

Closes yt-dlp#2001
Authored by: Lesmiscore, Jules-A, pukkandan
  • Loading branch information
Lesmiscore authored Mar 19, 2022
1 parent e655220 commit be4685a
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions yt_dlp/downloader/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
parse_http_range,
sanitized_Request,
ThrottledDownload,
try_get,
write_xattr,
XAttrMetadataError,
XAttrUnavailableError,
Expand Down Expand Up @@ -55,7 +56,6 @@ class DownloadContext(dict):

ctx.open_mode = 'wb'
ctx.resume_len = 0
ctx.data_len = None
ctx.block_size = self.params.get('buffersize', 1024)
ctx.start_time = time.time()
ctx.chunk_size = None
Expand Down Expand Up @@ -102,6 +102,8 @@ def establish_connection():
if ctx.is_resume:
self.report_resuming_byte(ctx.resume_len)
ctx.open_mode = 'ab'
elif req_start is not None:
range_start = req_start
elif ctx.chunk_size > 0:
range_start = 0
else:
Expand All @@ -118,11 +120,16 @@ def establish_connection():
else:
range_end = None

if range_end and ctx.data_len is not None and range_end >= ctx.data_len:
range_end = ctx.data_len - 1
has_range = range_start is not None
ctx.has_range = has_range
if try_get(None, lambda _: range_start > range_end):
ctx.resume_len = 0
ctx.open_mode = 'wb'
raise RetryDownload(Exception(f'Conflicting range. (start={range_start} > end={range_end})'))

if try_get(None, lambda _: range_end >= ctx.content_len):
range_end = ctx.content_len - 1

request = sanitized_Request(url, request_data, headers)
has_range = range_start is not None
if has_range:
set_range(request, range_start, range_end)
# Establish connection
Expand All @@ -146,16 +153,16 @@ def establish_connection():
or content_range_end == range_end
or content_len < range_end)
if accept_content_len:
ctx.data_len = content_len
ctx.content_len = content_len
ctx.data_len = min(content_len, req_end or content_len) - (req_start or 0)
return
# Content-Range is either not present or invalid. Assuming remote webserver is
# trying to send the whole file, resume is not possible, so wiping the local file
# and performing entire redownload
self.report_unable_to_resume()
ctx.resume_len = 0
ctx.open_mode = 'wb'
ctx.data_len = int_or_none(ctx.data.info().get('Content-length', None))
return
ctx.data_len = ctx.content_len = int_or_none(ctx.data.info().get('Content-length', None))
except (compat_urllib_error.HTTPError, ) as err:
if err.code == 416:
# Unable to resume (requested range not satisfiable)
Expand Down Expand Up @@ -331,7 +338,7 @@ def retry(e):
elif speed:
throttle_start = None

if not is_test and ctx.chunk_size and ctx.data_len is not None and byte_counter < ctx.data_len:
if not is_test and ctx.chunk_size and ctx.content_len is not None and byte_counter < ctx.content_len:
ctx.resume_len = byte_counter
# ctx.block_size = block_size
raise NextFragment()
Expand Down

0 comments on commit be4685a

Please sign in to comment.