add byte range handling to FileResourceRequest

This commit is contained in:
Stephen Birarda 2017-04-17 19:17:45 -07:00 committed by Atlante45
parent 8145e416f9
commit 229a481232
2 changed files with 32 additions and 1 deletions

View file

@ -11,6 +11,8 @@
#include "FileResourceRequest.h"
#include <cstdlib>
#include <QFile>
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;

View file

@ -37,6 +37,7 @@ public:
Timeout,
ServerUnavailable,
AccessDenied,
InvalidByteRange,
InvalidURL,
NotFound
};