From 043c587395cf0672d7a66fc97984c64cd6d049a1 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 21 Apr 2017 19:16:56 -0700 Subject: [PATCH] Fix byte range for file resource requests --- .../src/assets/SendAssetTask.cpp | 12 +++------ libraries/networking/src/ByteRange.h | 6 +++++ .../networking/src/FileResourceRequest.cpp | 27 +++++++------------ 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/assignment-client/src/assets/SendAssetTask.cpp b/assignment-client/src/assets/SendAssetTask.cpp index 5a394eaa07..eab88e0d46 100644 --- a/assignment-client/src/assets/SendAssetTask.cpp +++ b/assignment-client/src/assets/SendAssetTask.cpp @@ -68,11 +68,9 @@ void SendAssetTask::run() { QFile file { filePath }; if (file.open(QIODevice::ReadOnly)) { - if (!byteRange.isSet()) { - // if the byte range is not set, force it to be from 0 to the end of the file - byteRange.fromInclusive = 0; - byteRange.toExclusive = file.size(); - } + + // first fixup the range based on the now known file size + byteRange.fixupRange(file.size()); // check if we're being asked to read data that we just don't have // because of the file size @@ -82,10 +80,6 @@ void SendAssetTask::run() { << byteRange.fromInclusive << ":" << byteRange.toExclusive; } else { // we have a valid byte range, handle it and send the asset - - // first fixup the range based on the now known file size - byteRange.fixupRange(file.size()); - auto size = byteRange.size(); if (byteRange.fromInclusive >= 0) { diff --git a/libraries/networking/src/ByteRange.h b/libraries/networking/src/ByteRange.h index b5e214605d..77932a8c28 100644 --- a/libraries/networking/src/ByteRange.h +++ b/libraries/networking/src/ByteRange.h @@ -30,6 +30,12 @@ struct ByteRange { } void fixupRange(int64_t fileSize) { + if (!isSet()) { + // if the byte range is not set, force it to be from 0 to the end of the file + fromInclusive = 0; + toExclusive = fileSize; + } + if (fromInclusive > 0 && toExclusive == 0) { // we have a left side of the range that is non-zero // if the RHS of the range is zero, set it to the end of the file now diff --git a/libraries/networking/src/FileResourceRequest.cpp b/libraries/networking/src/FileResourceRequest.cpp index 9e2b8f4ab1..1e549e5fa3 100644 --- a/libraries/networking/src/FileResourceRequest.cpp +++ b/libraries/networking/src/FileResourceRequest.cpp @@ -34,24 +34,17 @@ void FileResourceRequest::doSend() { if (file.size() < _byteRange.fromInclusive || file.size() < _byteRange.toExclusive) { _result = ResourceRequest::InvalidByteRange; } else { - if (!_byteRange.isSet()) { - // no byte range, read the whole file - _data = file.readAll(); + // fix it up based on the known size of the file + _byteRange.fixupRange(file.size()); + + if (_byteRange.fromInclusive >= 0) { + // this is a positive byte range, simply skip to that part of the file and read from there + file.seek(_byteRange.fromInclusive); + _data = file.read(_byteRange.size()); } else { - // we have a byte range to handle - - // fix it up based on the known size of the file - _byteRange.fixupRange(file.size()); - - if (_byteRange.fromInclusive > 0) { - // this is a positive byte range, simply skip to that part of the file and read from there - file.seek(_byteRange.fromInclusive); - _data = file.read(_byteRange.size()); - } else { - // this is a negative byte range, we'll need to grab data from the end of the file first - file.seek(file.size() + _byteRange.fromInclusive); - _data = file.read(_byteRange.size()); - } + // this is a negative byte range, we'll need to grab data from the end of the file first + file.seek(file.size() + _byteRange.fromInclusive); + _data = file.read(_byteRange.size()); } _result = ResourceRequest::Success;