Example #1
0
HttpReply *CachedHttp::request(const HttpRequest &req) {
    bool cacheable = req.operation == QNetworkAccessManager::GetOperation ||
                     (cachePostRequests && req.operation == QNetworkAccessManager::PostOperation);
    if (!cacheable) {
        qDebug() << "Not cacheable" << req.url;
        return http.request(req);
    }
    const QByteArray key = requestHash(req);
    const QByteArray value = cache->value(key);
    if (!value.isNull()) {
        qDebug() << "CachedHttp HIT" << req.url;
        return new CachedHttpReply(value, req);
    }
    qDebug() << "CachedHttp MISS" << req.url.toString();
    return new WrappedHttpReply(cache, key, http.request(req));
}
void AssetResourceRequest::requestMappingForPath(const AssetPath& path) {
    auto assetClient = DependencyManager::get<AssetClient>();
    _assetMappingRequest = assetClient->createGetMappingRequest(path);

    // make sure we'll hear about the result of the get mapping request
    connect(_assetMappingRequest, &GetMappingRequest::finished, this, [this, path](GetMappingRequest* request){
        Q_ASSERT(_state == InProgress);
        Q_ASSERT(request == _assetMappingRequest);

        switch (request->getError()) {
            case MappingRequest::NoError:
                // we have no error, we should have a resulting hash - use that to send of a request for that asset
                qDebug() << "Got mapping for:" << path << "=>" << request->getHash();

                requestHash(request->getHash());

                break;
            default: {
                switch (request->getError()) {
                    case MappingRequest::NotFound:
                        // no result for the mapping request, set error to not found
                        _result = NotFound;
                        break;
                    case MappingRequest::NetworkError:
                        // didn't hear back from the server, mark it unavailable
                        _result = ServerUnavailable;
                        break;
                    default:
                        _result = Error;
                        break;
                }

                // since we've failed we know we are finished
                _state = Finished;
                emit finished();

                break;
            }
        }

        _assetMappingRequest->deleteLater();
        _assetMappingRequest = nullptr;
    });

    _assetMappingRequest->start();
}
void AssetResourceRequest::doSend() {
    auto parts = _url.path().split(".", QString::SkipEmptyParts);
    auto hash = parts.length() > 0 ? parts[0] : "";
    auto extension = parts.length() > 1 ? parts[1] : "";

    // We'll either have a hash or an ATP path to a file (that maps to a hash)

    if (urlIsAssetHash()) {
        // We've detected that this is a hash - simply use AssetClient to request that asset
        auto parts = _url.path().split(".", QString::SkipEmptyParts);
        auto hash = parts.length() > 0 ? parts[0] : "";

        requestHash(hash);
    } else {
        // This is an ATP path, we'll need to figure out what the mapping is.
        // This may incur a roundtrip to the asset-server, or it may return immediately from the cache in AssetClient.

        auto path = _url.path();
        requestMappingForPath(path);
    }
}