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:
- HTTP file uploads to remote servers
- HTTP file downloads from remote URLs
- HTTPS secure transfers
- Integration with imtcom file transfer interface
- Qt Network-based implementation
Architecture
Design Patterns
Adapter Pattern:
- Adapts Qt Network classes to IFileTransfer interface
- Provides common API for file transfers
- Enables interchangeable transfer mechanisms
Facade Pattern:
- Simplifies Qt Network API complexity
- Single interface for upload/download operations
- Hides HTTP protocol details
Core Classes
HTTP File Transfer:**
├─ 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>
imthttp::CHttpFileTransfer httpTransfer;
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:**
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:**
class CSecureHttpTransferComp : public CHttpFileTransfer
{
public:
bool UploadFileWithAuth(const QString& filePath,
const QUrl& url,
const QString& authToken)
{
QNetworkAccessManager manager;
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
return false;
}
QNetworkRequest request(url);
request.setRawHeader("Authorization",
QString("Bearer %1").arg(authToken).toUtf8());
request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/octet-stream");
QNetworkReply* reply = manager.post(request, &file);
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
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);
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)
{
public:
void InitializeComponent() override
{
auto httpTransfer = std::make_unique<imthttp::CHttpFileTransfer>();
m_fileTransfer = httpTransfer.get();
}
bool UploadToServer(const QString& localFile, const QString& remoteUrl)
{
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)
{
QFileInfo fileInfo(localFile);
QString fileName = fileInfo.fileName();
QString timestamp = QDateTime::currentDateTime()
.toString("yyyyMMdd_hhmmss");
QString remoteFileName = QString("%1_%2").arg(timestamp).arg(fileName);
QUrl uploadUrl = QUrl(QString("%1/backup/%2")
.arg(m_backupServerUrl)
.arg(remoteFileName));
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
- Check return values from UploadFile/DownloadFile
- Implement retry logic for network failures
- Handle HTTP error codes appropriately
- Provide user feedback for long transfers
- Log transfer errors for debugging
Security Considerations
- Always use HTTPS for sensitive data transfers
- Validate SSL certificates in production
- Implement authentication (OAuth, API tokens)
- Never embed credentials in URLs
- Sanitize file paths to prevent path traversal
- Implement file size limits
Performance Optimization
- Use asynchronous transfers for large files
- Implement progress tracking for user feedback
- Consider chunked uploads for very large files
- Reuse QNetworkAccessManager instances
- Implement transfer timeout handling
- Use compression when appropriate
Integration with Other Modules
With imtcom (Server Communication):
- Implements IFileTransfer interface
- Integrates with server connection management
- Common file transfer abstraction
With imtfile (File Operations):
- Local file I/O for transfers
- File compression before upload
- File validation after download
With imtcrypt (Encryption):
- Encrypt files before upload
- Decrypt files after download
- Secure sensitive data transfers
References
Related Modules:
- imtcom - Server communication and file transfer interface
- imtfile - File I/O operations
- imtcrypt - File encryption
Qt Network Module:
HTTP Specifications:
- HTTP/1.1: RFC 2616
- HTTP/2: RFC 7540
- HTTPS: RFC 2818