Ejemplo n.º 1
0
bool FileServer::handleRequest(QHttpRequest *request, QHttpResponse *response) {
    if (request->method() != QHttpRequest::HTTP_GET) {
        response->setHeader("Content-Length", "0");
        response->writeHead(QHttpResponse::STATUS_METHOD_NOT_ALLOWED);
        response->end();
        return true;
    }
    
    QString filePath = request->path();
    const QString dir = filePath.left(filePath.lastIndexOf("/") + 1);

    if (!dir.startsWith(CACHE_PATH)) {
        filePath = filePath.mid(filePath.indexOf("/") + 1);
        filePath.prepend(WEB_INTERFACE_PATH);
    }
    
    if (!QFile::exists(filePath)) {
        if (dir.startsWith(CACHE_PATH)) {
            const QByteArray url = QByteArray::fromBase64(filePath.mid(filePath.lastIndexOf("/") + 1).toUtf8());
            getCachedFile(dir, QUrl::fromEncoded(url), response);
            return true;
        }
        
        return false;
    }
    
    QFile file(filePath);
    
    if (file.open(QFile::ReadOnly)) {
        response->setHeader("Content-Length", QByteArray::number(file.size()));
        response->writeHead(QHttpResponse::STATUS_OK);
        response->end(file.readAll());
        file.close();
        return true;
    }
    
    response->setHeader("Content-Length", "0");
    response->writeHead(QHttpResponse::STATUS_INTERNAL_SERVER_ERROR);
    response->end();
    return true;
}
Ejemplo n.º 2
0
bool CacheManager::getCachedFile(const File& file, const std::string& information,
                                 std::string &cachedFileName, bool isPersistent)
{
    return getCachedFile(file.filename(), information, cachedFileName, isPersistent);
}