ImagingTools Core SDK
imthttp Namespace Reference

HTTP Client and File Transfer Module. More...

Detailed Description

HTTP Client and File Transfer Module.

The imthttp module provides HTTP-based file transfer capabilities, enabling file uploads and downloads via HTTP/HTTPS protocols with simple, straightforward API.

Overview

This lightweight module provides HTTP file operations:

Architecture

Design Patterns

Adapter Pattern:

Facade Pattern:

Core Classes

HTTP File Transfer:**

CHttpFileTransfer (imtcom::IFileTransfer)
├─ UploadFile(filePath, url) - Upload file via HTTP POST
├─ DownloadFile(filePath, url) - Download file via HTTP GET
└─ Qt Network-based implementation
Uses:
├─ QNetworkAccessManager - HTTP request management
├─ QNetworkRequest - HTTP request configuration
├─ QNetworkReply - HTTP response handling
└─ QFile - Local file I/O
Interface for file upload and download operations via URLs.

Usage Examples

File Upload

Upload File to Server:**

#include <imthttp/CHttpFileTransfer.h>
// Create HTTP file transfer
imthttp::CHttpFileTransfer httpTransfer;
// Upload file
QString localFile = "/path/to/local/document.pdf";
QUrl uploadUrl = QUrl("https://api.example.com/upload");
bool success = httpTransfer.UploadFile(localFile, uploadUrl);
if (success) {
qDebug() << "File uploaded successfully";
} else {
qCritical() << "File upload failed";
}

File Download

Download File from Server:**

// Download file
QString saveToFile = "/path/to/save/downloaded.pdf";
QUrl downloadUrl = QUrl("https://example.com/files/document.pdf");
bool success = httpTransfer.DownloadFile(saveToFile, downloadUrl);
if (success) {
qDebug() << "File downloaded successfully to:" << saveToFile;
} else {
qCritical() << "File download failed";
}

Secure HTTPS Transfer

HTTPS Upload with Authentication:**

// For advanced scenarios, extend CHttpFileTransfer
class CSecureHttpTransferComp : public CHttpFileTransfer
{
public:
bool UploadFileWithAuth(const QString& filePath,
const QUrl& url,
const QString& authToken)
{
// Create network manager
QNetworkAccessManager manager;
// Open file
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
return false;
}
// Create request with authentication
QNetworkRequest request(url);
request.setRawHeader("Authorization",
QString("Bearer %1").arg(authToken).toUtf8());
request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/octet-stream");
// Upload file
QNetworkReply* reply = manager.post(request, &file);
// Wait for completion
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
// Check result
bool success = (reply->error() == QNetworkReply::NoError);
reply->deleteLater();
return success;
}
};

Progress Tracking

Track Upload/Download Progress:**

class CProgressTrackingTransfer : public imthttp::CHttpFileTransfer
{
Q_OBJECT
public:
bool UploadFileWithProgress(const QString& filePath,
const QUrl& url,
IProgressManager* progress = nullptr)
{
QNetworkAccessManager manager;
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
return false;
}
QNetworkRequest request(url);
QNetworkReply* reply = manager.post(request, &file);
// Connect progress signal
if (progress) {
connect(reply, &QNetworkReply::uploadProgress,
[progress](qint64 bytesSent, qint64 bytesTotal) {
if (bytesTotal > 0) {
int percent = (bytesSent * 100) / bytesTotal;
progress->SetProgressValue(percent);
}
});
}
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
bool success = (reply->error() == QNetworkReply::NoError);
reply->deleteLater();
return success;
}
};

Integration Patterns

Integration with imtcom

Pattern: Pluggable File Transfer:**

class CFileManagerComp : public ACF_COMPONENT(IFileManager)
{
I_REFERENCE(imtcom::IFileTransfer, m_fileTransfer)
public:
void InitializeComponent() override
{
// Use HTTP file transfer
auto httpTransfer = std::make_unique<imthttp::CHttpFileTransfer>();
m_fileTransfer = httpTransfer.get();
}
bool UploadToServer(const QString& localFile, const QString& remoteUrl)
{
// Use IFileTransfer interface (could be HTTP, FTP, etc.)
return m_fileTransfer->UploadFile(localFile, QUrl(remoteUrl));
}
bool DownloadFromServer(const QString& remoteUrl,
const QString& localFile)
{
return m_fileTransfer->DownloadFile(localFile, QUrl(remoteUrl));
}
};

Backup and Sync

Pattern: Cloud Backup via HTTP:**

class CCloudBackupComp : public ACF_COMPONENT(ICloudBackup)
{
imthttp::CHttpFileTransfer m_httpTransfer;
QString m_backupServerUrl;
QString m_authToken;
public:
bool BackupFile(const QString& localFile)
{
// Generate remote file name
QFileInfo fileInfo(localFile);
QString fileName = fileInfo.fileName();
QString timestamp = QDateTime::currentDateTime()
.toString("yyyyMMdd_hhmmss");
QString remoteFileName = QString("%1_%2").arg(timestamp).arg(fileName);
// Build upload URL
QUrl uploadUrl = QUrl(QString("%1/backup/%2")
.arg(m_backupServerUrl)
.arg(remoteFileName));
// Upload to cloud
bool success = m_httpTransfer.UploadFile(localFile, uploadUrl);
if (success) {
qDebug() << "Backed up to cloud:" << remoteFileName;
}
return success;
}
bool RestoreFile(const QString& remoteFileName,
const QString& localFile)
{
QUrl downloadUrl = QUrl(QString("%1/backup/%2")
.arg(m_backupServerUrl)
.arg(remoteFileName));
return m_httpTransfer.DownloadFile(localFile, downloadUrl);
}
};

Best Practices

Error Handling

Security Considerations

Performance Optimization

Integration with Other Modules

With imtcom (Server Communication):

With imtfile (File Operations):

With imtcrypt (Encryption):

References

Related Modules:

Qt Network Module:

HTTP Specifications: