HttpDownloader.cpp 3.21 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xiongziliang committed
3
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
xiongziliang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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.
xiongziliang committed
25 26 27 28 29
 */

#include "HttpDownloader.h"
#include "Util/MD5.h"
#include "Util/File.h"
xiongziliang committed
30
using namespace toolkit;
xiongziliang committed
31

xiongziliang committed
32
namespace mediakit {
xiongziliang committed
33 34 35 36 37 38 39 40 41

HttpDownloader::HttpDownloader() {

}

HttpDownloader::~HttpDownloader() {
	closeFile();
}

xiongziliang committed
42
void HttpDownloader::startDownload(const string& url, const string& filePath,bool bAppend,float timeOutSecond) {
xiongziliang committed
43 44
	_filePath = filePath;
	if(_filePath.empty()){
xiongziliang committed
45
		_filePath = exeDir() + "HttpDownloader/" + MD5(url).hexdigest();
xiongziliang committed
46 47 48 49 50 51
	}
	_saveFile = File::createfile_file(_filePath.data(),bAppend ? "ab" : "wb");
	if(!_saveFile){
		auto strErr = StrPrinter << "打开文件失败:" << filePath << endl;
		throw std::runtime_error(strErr);
	}
52
	_bDownloadSuccess = false;
xiongziliang committed
53 54
	if(bAppend){
		auto currentLen = ftell(_saveFile);
55 56 57 58 59
		if(currentLen){
			//最少续传一个字节,怕遇到http 416的错误
			currentLen -= 1;
			fseek(_saveFile,-1,SEEK_CUR);
		}
xiongziliang committed
60 61 62
		addHeader("Range", StrPrinter << "bytes=" << currentLen << "-" << endl);
	}
	setMethod("GET");
xiongziliang committed
63
	sendRequest(url,timeOutSecond);
xiongziliang committed
64 65
}

66
int64_t HttpDownloader::onResponseHeader(const string& status,const HttpHeader& headers) {
67
    if(status != "200" && status != "206"){
xiongziliang committed
68
		//失败
xiongziliang committed
69
		shutdown(SockException(Err_shutdown,StrPrinter << "Http Status:" << status));
xiongziliang committed
70
	}
71 72
	//后续全部是content
	return -1;
xiongziliang committed
73 74
}

75
void HttpDownloader::onResponseBody(const char* buf, int64_t size, int64_t recvedSize, int64_t totalSize) {
76
    if(_saveFile){
xiongziliang committed
77 78 79
		fwrite(buf,size,1,_saveFile);
	}
}
80

81
void HttpDownloader::onResponseCompleted() {
xiongziliang committed
82
	closeFile();
83
	//InfoL << "md5Sum:" << getMd5Sum(_filePath);
84
	_bDownloadSuccess = true;
xiongziliang committed
85
	if(_onResult){
xiongziliang committed
86
		_onResult(Err_success,"success",_filePath);
xiongziliang committed
87 88 89 90 91 92
		_onResult = nullptr;
	}
}

void HttpDownloader::onDisconnect(const SockException &ex) {
	closeFile();
93 94 95
	if(!_bDownloadSuccess){
		File::delete_file(_filePath.data());
	}
xiongziliang committed
96
	if(_onResult){
xiongziliang committed
97
		_onResult(ex.getErrCode(),ex.what(),_filePath);
xiongziliang committed
98 99 100 101 102 103
		_onResult = nullptr;
	}
}

void HttpDownloader::closeFile() {
	if(_saveFile){
104
		fflush(_saveFile);
xiongziliang committed
105 106 107 108 109
		fclose(_saveFile);
		_saveFile = nullptr;
	}
}

110

xiongziliang committed
111
} /* namespace mediakit */