ZLMediaKit/src/Http/HttpDownloader.cpp

119 lines
3.3 KiB
C++
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2017-09-27 16:20:30 +08:00
* MIT License
2017-05-05 18:02:54 +08:00
*
2019-05-08 15:40:07 +08:00
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
2017-09-27 16:20:30 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
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() {
closeFile();
}
2018-02-28 17:30:12 +08:00
void HttpDownloader::startDownload(const string& url, const string& filePath,bool bAppend,float timeOutSecond) {
2017-05-05 18:02:54 +08:00
_filePath = filePath;
if(_filePath.empty()){
2017-05-23 16:48:33 +08:00
_filePath = exeDir() + "HttpDownloader/" + MD5(url).hexdigest();
2017-05-05 18:02:54 +08:00
}
_saveFile = File::createfile_file(_filePath.data(),bAppend ? "ab" : "wb");
if(!_saveFile){
auto strErr = StrPrinter << "打开文件失败:" << filePath << endl;
throw std::runtime_error(strErr);
}
_bDownloadSuccess = false;
2017-05-05 18:02:54 +08:00
if(bAppend){
auto currentLen = ftell(_saveFile);
2017-05-05 23:54:30 +08:00
if(currentLen){
//最少续传一个字节怕遇到http 416的错误
currentLen -= 1;
fseek(_saveFile,-1,SEEK_CUR);
}
2017-05-05 18:02:54 +08:00
addHeader("Range", StrPrinter << "bytes=" << currentLen << "-" << endl);
}
setMethod("GET");
2018-02-28 17:30:12 +08:00
sendRequest(url,timeOutSecond);
2017-05-05 18:02:54 +08:00
}
int64_t HttpDownloader::onResponseHeader(const string& status,const HttpHeader& headers) {
2018-02-23 15:36:51 +08:00
if(status != "200" && status != "206"){
2017-05-05 18:02:54 +08:00
//失败
shutdown();
closeFile();
File::delete_file(_filePath.data());
if(_onResult){
auto errMsg = StrPrinter << "Http Status:" << status << endl;
2019-03-27 18:41:52 +08:00
_onResult(Err_other,errMsg,_filePath);
2017-05-05 18:02:54 +08:00
_onResult = nullptr;
}
}
//后续全部是content
return -1;
2017-05-05 18:02:54 +08:00
}
2019-01-16 14:26:06 +08:00
void HttpDownloader::onResponseBody(const char* buf, int64_t size, int64_t recvedSize, int64_t totalSize) {
2018-02-23 15:36:51 +08:00
if(_saveFile){
2017-05-05 18:02:54 +08:00
fwrite(buf,size,1,_saveFile);
}
}
void HttpDownloader::onResponseCompleted() {
2017-05-05 18:02:54 +08:00
closeFile();
//InfoL << "md5Sum:" << getMd5Sum(_filePath);
_bDownloadSuccess = true;
2017-05-05 18:02:54 +08:00
if(_onResult){
2019-03-27 18:41:52 +08:00
_onResult(Err_success,"success",_filePath);
2017-05-05 18:02:54 +08:00
_onResult = nullptr;
}
}
void HttpDownloader::onDisconnect(const SockException &ex) {
closeFile();
if(!_bDownloadSuccess){
File::delete_file(_filePath.data());
}
2017-05-05 18:02:54 +08:00
if(_onResult){
2019-03-27 18:41:52 +08:00
_onResult(ex.getErrCode(),ex.what(),_filePath);
2017-05-05 18:02:54 +08:00
_onResult = nullptr;
}
}
void HttpDownloader::closeFile() {
if(_saveFile){
fflush(_saveFile);
2017-05-05 18:02:54 +08:00
fclose(_saveFile);
_saveFile = nullptr;
}
}
2018-02-06 17:35:32 +08:00
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */