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

void HttpDownloader::onResponseHeader(const string& status,const HttpHeader& headers) {
67
    if(status != "200" && status != "206"){
xiongziliang committed
68 69 70 71 72 73 74 75 76 77 78 79 80
		//失败
		shutdown();
		closeFile();
		File::delete_file(_filePath.data());
		if(_onResult){
			auto errMsg = StrPrinter << "Http Status:" << status << endl;
			_onResult(Err_other,errMsg.data(),_filePath.data());
			_onResult = nullptr;
		}
	}
}

void HttpDownloader::onResponseBody(const char* buf, size_t size, size_t recvedSize, size_t totalSize) {
81
    if(_saveFile){
xiongziliang committed
82 83 84
		fwrite(buf,size,1,_saveFile);
	}
}
85

xiongziliang committed
86 87
void HttpDownloader::onResponseCompleted() {
	closeFile();
88
	//InfoL << "md5Sum:" << getMd5Sum(_filePath);
89
	_bDownloadSuccess = true;
xiongziliang committed
90 91 92 93 94 95 96 97
	if(_onResult){
		_onResult(Err_success,"success",_filePath.data());
		_onResult = nullptr;
	}
}

void HttpDownloader::onDisconnect(const SockException &ex) {
	closeFile();
98 99 100
	if(!_bDownloadSuccess){
		File::delete_file(_filePath.data());
	}
xiongziliang committed
101 102 103 104 105 106 107 108
	if(_onResult){
		_onResult(ex.getErrCode(),ex.what(),_filePath.data());
		_onResult = nullptr;
	}
}

void HttpDownloader::closeFile() {
	if(_saveFile){
109
		fflush(_saveFile);
xiongziliang committed
110 111 112 113 114
		fclose(_saveFile);
		_saveFile = nullptr;
	}
}

115

xiongziliang committed
116
} /* namespace mediakit */