From 229a481232d85877fd9f8b91e6d30be5b7586893 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 17 Apr 2017 19:17:45 -0700 Subject: [PATCH] add byte range handling to FileResourceRequest --- .../networking/src/FileResourceRequest.cpp | 32 ++++++++++++++++++- libraries/networking/src/ResourceRequest.h | 1 + 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/libraries/networking/src/FileResourceRequest.cpp b/libraries/networking/src/FileResourceRequest.cpp index 58a2074103..3895fbc34d 100644 --- a/libraries/networking/src/FileResourceRequest.cpp +++ b/libraries/networking/src/FileResourceRequest.cpp @@ -11,6 +11,8 @@ #include "FileResourceRequest.h" +#include + #include void FileResourceRequest::doSend() { @@ -25,7 +27,35 @@ void FileResourceRequest::doSend() { QFile file(filename); if (file.exists()) { if (file.open(QFile::ReadOnly)) { - _data = file.readAll(); + + if (!_byteRange.isSet()) { + // no byte range, read the whole file + _data = file.readAll(); + } else { + if (file.size() < std::abs(_byteRange.fromInclusive) || file.size() < _byteRange.toExclusive) { + _result = ResourceRequest::InvalidByteRange; + } else { + // we have a byte range to handle + 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.fromInclusive); + + if (_byteRange.toExclusive > 0) { + // there is additional data to read from the front of the file + // handle that now + file.seek(0); + _data.append(file.read(_byteRange.toExclusive)); + } + } + } + + } + _result = ResourceRequest::Success; } else { _result = ResourceRequest::AccessDenied; diff --git a/libraries/networking/src/ResourceRequest.h b/libraries/networking/src/ResourceRequest.h index 77fc8d2591..ef40cb3455 100644 --- a/libraries/networking/src/ResourceRequest.h +++ b/libraries/networking/src/ResourceRequest.h @@ -37,6 +37,7 @@ public: Timeout, ServerUnavailable, AccessDenied, + InvalidByteRange, InvalidURL, NotFound };