| 10 | #include <QUrl> |
| 11 | |
| 12 | HttpThread::HttpThread(std::string const & url, downloader::IHttpThreadCallback & cb, int64_t beg, int64_t end, |
| 13 | int64_t size, std::string const & pb) |
| 14 | : m_callback(cb) |
| 15 | , m_begRange(beg) |
| 16 | , m_endRange(end) |
| 17 | , m_downloadedBytes(0) |
| 18 | , m_expectedSize(size) |
| 19 | { |
| 20 | QUrl const qUrl(url.c_str()); |
| 21 | QNetworkRequest request(qUrl); |
| 22 | |
| 23 | // use Range header only if we don't download whole file from start |
| 24 | if (!(beg == 0 && end < 0)) |
| 25 | { |
| 26 | if (end > 0) |
| 27 | { |
| 28 | LOG(LDEBUG, (url, "downloading range [", beg, ",", end, "]")); |
| 29 | QString const range = QString("bytes=") + QString::number(beg) + '-' + QString::number(end); |
| 30 | request.setRawHeader("Range", range.toUtf8()); |
| 31 | } |
| 32 | else |
| 33 | { |
| 34 | LOG(LDEBUG, (url, "resuming download from position", beg)); |
| 35 | QString const range = QString("bytes=") + QString::number(beg) + '-'; |
| 36 | request.setRawHeader("Range", range.toUtf8()); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /// Use single instance for whole app |
| 41 | static QNetworkAccessManager netManager; |
| 42 | |
| 43 | if (pb.empty()) |
| 44 | m_reply = netManager.get(request); |
| 45 | else |
| 46 | { |
| 47 | request.setRawHeader("Content-Type", "application/json"); |
| 48 | request.setRawHeader("Content-Length", QString::number(pb.size()).toLocal8Bit()); |
| 49 | m_reply = netManager.post(request, pb.c_str()); |
| 50 | } |
| 51 | |
| 52 | connect(m_reply, SIGNAL(metaDataChanged()), this, SLOT(OnHeadersReceived())); |
| 53 | connect(m_reply, SIGNAL(readyRead()), this, SLOT(OnChunkDownloaded())); |
| 54 | connect(m_reply, SIGNAL(finished()), this, SLOT(OnDownloadFinished())); |
| 55 | LOG(LDEBUG, ("Connecting to", url, "[", beg, ",", end, "]", "size=", size)); |
| 56 | } |
| 57 | |
| 58 | HttpThread::~HttpThread() |
| 59 | { |