ZLMediaKit/src/Http/HttpDownloader.cpp

96 lines
2.6 KiB
C++
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2017-09-27 16:20:30 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2017-09-27 16:20:30 +08:00
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2017-05-05 18:02:54 +08:00
*/
#include "HttpDownloader.h"
#include "Util/MD5.h"
#include "Util/File.h"
2018-10-24 17:17:55 +08:00
using namespace toolkit;
2017-05-05 18:02:54 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit {
2017-05-05 18:02:54 +08:00
HttpDownloader::HttpDownloader() {
}
HttpDownloader::~HttpDownloader() {
2020-03-20 11:51:24 +08:00
closeFile();
2017-05-05 18:02:54 +08:00
}
2018-02-28 17:30:12 +08:00
void HttpDownloader::startDownload(const string& url, const string& filePath,bool bAppend,float timeOutSecond) {
2020-03-20 11:51:24 +08:00
_filePath = filePath;
if(_filePath.empty()){
_filePath = exeDir() + "HttpDownloader/" + MD5(url).hexdigest();
}
2020-04-24 12:39:22 +08:00
_saveFile = File::create_file(_filePath.data(), bAppend ? "ab" : "wb");
2020-03-20 11:51:24 +08:00
if(!_saveFile){
auto strErr = StrPrinter << "打开文件失败:" << filePath << endl;
throw std::runtime_error(strErr);
}
_bDownloadSuccess = false;
if(bAppend){
auto currentLen = ftell(_saveFile);
if(currentLen){
//最少续传一个字节怕遇到http 416的错误
currentLen -= 1;
fseek(_saveFile,-1,SEEK_CUR);
}
addHeader("Range", StrPrinter << "bytes=" << currentLen << "-" << endl);
}
setMethod("GET");
sendRequest(url,timeOutSecond);
2017-05-05 18:02:54 +08:00
}
2021-01-19 16:05:38 +08:00
ssize_t HttpDownloader::onResponseHeader(const string& status,const HttpHeader& headers) {
2018-02-23 15:36:51 +08:00
if(status != "200" && status != "206"){
2020-03-20 11:51:24 +08:00
//失败
shutdown(SockException(Err_shutdown,StrPrinter << "Http Status:" << status));
}
//后续全部是content
return -1;
2017-05-05 18:02:54 +08:00
}
void HttpDownloader::onResponseBody(const char* buf, size_t size, size_t recvedSize, size_t totalSize) {
2018-02-23 15:36:51 +08:00
if(_saveFile){
2020-03-20 11:51:24 +08:00
fwrite(buf,size,1,_saveFile);
}
2017-05-05 18:02:54 +08:00
}
void HttpDownloader::onResponseCompleted() {
2020-03-20 11:51:24 +08:00
closeFile();
//InfoL << "md5Sum:" << getMd5Sum(_filePath);
_bDownloadSuccess = true;
if(_onResult){
_onResult(Err_success,"success",_filePath);
_onResult = nullptr;
}
2017-05-05 18:02:54 +08:00
}
void HttpDownloader::onDisconnect(const SockException &ex) {
2020-03-20 11:51:24 +08:00
closeFile();
if(!_bDownloadSuccess){
File::delete_file(_filePath.data());
}
if(_onResult){
_onResult(ex.getErrCode(),ex.what(),_filePath);
_onResult = nullptr;
}
2017-05-05 18:02:54 +08:00
}
void HttpDownloader::closeFile() {
2020-03-20 11:51:24 +08:00
if(_saveFile){
fflush(_saveFile);
fclose(_saveFile);
_saveFile = nullptr;
}
2017-05-05 18:02:54 +08:00
}
2018-02-06 17:35:32 +08:00
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */