Keep pending content per remote address

This commit is contained in:
Simon Walton 2018-11-16 09:44:21 -08:00
parent 22c3f5239a
commit 5f51ed0210
2 changed files with 19 additions and 14 deletions

View file

@ -2521,33 +2521,37 @@ bool DomainServer::handleHTTPSRequest(HTTPSConnection* connection, const QUrl &u
bool DomainServer::processPendingContent(HTTPConnection* connection, QString itemName, QString filename, QByteArray dataChunk) { bool DomainServer::processPendingContent(HTTPConnection* connection, QString itemName, QString filename, QByteArray dataChunk) {
if (filename.endsWith(".zip", Qt::CaseInsensitive)) { if (filename.endsWith(".zip", Qt::CaseInsensitive)) {
static const QString TEMPORARY_CONTENT_FILEPATH { QDir::tempPath() + "/hifiUploadContent_XXXXXX.zip" }; static const QString TEMPORARY_CONTENT_FILEPATH { QDir::tempPath() + "/hifiUploadContent_XXXXXX.zip" };
const auto peerAddressHash = qHash(connection->socket()->peerAddress());
if (!_pendingFileContent) { if (_pendingContentFiles.find(peerAddressHash) == _pendingContentFiles.end()) {
_pendingFileContent = std::make_unique<QTemporaryFile>(TEMPORARY_CONTENT_FILEPATH); _pendingContentFiles.emplace(peerAddressHash, TEMPORARY_CONTENT_FILEPATH);
} }
if (!_pendingFileContent->open()) {
_pendingFileContent = nullptr; QTemporaryFile& _pendingFileContent = _pendingContentFiles[peerAddressHash];
if (!_pendingFileContent.open()) {
_pendingContentFiles.erase(peerAddressHash);
connection->respond(HTTPConnection::StatusCode400); connection->respond(HTTPConnection::StatusCode400);
return false; return false;
} }
_pendingFileContent->seek(_pendingFileContent->size()); _pendingFileContent.seek(_pendingFileContent.size());
_pendingFileContent->write(dataChunk); _pendingFileContent.write(dataChunk);
_pendingFileContent->close(); _pendingFileContent.close();
// Respond immediately - will timeout if we wait for restore. // Respond immediately - will timeout if we wait for restore.
connection->respond(HTTPConnection::StatusCode200); connection->respond(HTTPConnection::StatusCode200);
if (itemName == "restore-file-chunk-final" || itemName == "restore-file") { if (itemName == "restore-file-chunk-final" || itemName == "restore-file") {
auto deferred = makePromise("recoverFromUploadedBackup"); auto deferred = makePromise("recoverFromUploadedBackup");
deferred->then([this](QString error, QVariantMap result) { deferred->then([this, peerAddressHash](QString error, QVariantMap result) {
_pendingFileContent = nullptr; _pendingContentFiles.erase(peerAddressHash);
}); });
_contentManager->recoverFromUploadedFile(deferred, _pendingFileContent->fileName()); _contentManager->recoverFromUploadedFile(deferred, _pendingFileContent.fileName());
} else {
} }
} else if (filename.endsWith(".json", Qt::CaseInsensitive) } else if (filename.endsWith(".json", Qt::CaseInsensitive)
|| filename.endsWith(".json.gz", Qt::CaseInsensitive)) { || filename.endsWith(".json.gz", Qt::CaseInsensitive)) {
auto peerAddressHash = qHash(connection->socket()->peerAddress());
QByteArray& _pendingUploadedContent = _pendingUploadedContents[peerAddressHash];
_pendingUploadedContent += dataChunk; _pendingUploadedContent += dataChunk;
connection->respond(HTTPConnection::StatusCode200); connection->respond(HTTPConnection::StatusCode200);
@ -2555,7 +2559,7 @@ bool DomainServer::processPendingContent(HTTPConnection* connection, QString ite
// invoke our method to hand the new octree file off to the octree server // invoke our method to hand the new octree file off to the octree server
QMetaObject::invokeMethod(this, "handleOctreeFileReplacement", QMetaObject::invokeMethod(this, "handleOctreeFileReplacement",
Qt::QueuedConnection, Q_ARG(QByteArray, _pendingUploadedContent)); Qt::QueuedConnection, Q_ARG(QByteArray, _pendingUploadedContent));
_pendingUploadedContent.clear(); _pendingUploadedContents.erase(peerAddressHash);
} }
} else { } else {
connection->respond(HTTPConnection::StatusCode400); connection->respond(HTTPConnection::StatusCode400);

View file

@ -20,6 +20,7 @@
#include <QtCore/QStringList> #include <QtCore/QStringList>
#include <QtCore/QThread> #include <QtCore/QThread>
#include <QtCore/QUrl> #include <QtCore/QUrl>
#include <QHostAddress>
#include <QAbstractNativeEventFilter> #include <QAbstractNativeEventFilter>
#include <Assignment.h> #include <Assignment.h>
@ -283,8 +284,8 @@ private:
QHash<QUuid, QPointer<HTTPSConnection>> _pendingOAuthConnections; QHash<QUuid, QPointer<HTTPSConnection>> _pendingOAuthConnections;
QByteArray _pendingUploadedContent; std::unordered_map<uint, QByteArray> _pendingUploadedContents;
std::unique_ptr<QTemporaryFile> _pendingFileContent; std::unordered_map<uint, QTemporaryFile> _pendingContentFiles;
QThread _assetClientThread; QThread _assetClientThread;
}; };