HttpDownloader.cpp 4 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 */

#include "HttpDownloader.h"
#include "Util/MD5.h"
#include "Util/File.h"

using namespace ZL::Util;

namespace ZL {
namespace Http {

HttpDownloader::HttpDownloader() {

}

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

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

void HttpDownloader::onResponseHeader(const string& status,const HttpHeader& headers) {
71 72
    _downloadTicker.resetTime();
    if(status != "200" && status != "206"){
xiongziliang committed
73 74 75 76 77 78 79 80 81 82 83 84 85
		//失败
		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) {
86 87
    _downloadTicker.resetTime();
    if(_saveFile){
xiongziliang committed
88 89 90
		fwrite(buf,size,1,_saveFile);
	}
}
91 92 93 94 95 96 97 98 99 100 101
//string getMd5Sum(const string &filePath){
//	auto fp = File::createfile_file(filePath.data(),"rb");
//	fseek(fp,0,SEEK_END);
//	auto sz = ftell(fp);
//	char tmp[sz];
//	fseek(fp,0,SEEK_SET);
//	auto rd = fread(tmp,1,sz,fp);
//	InfoL << sz << " " << rd;
//	fclose(fp);
//	return MD5(string(tmp,sz)).hexdigest();
//}
xiongziliang committed
102 103
void HttpDownloader::onResponseCompleted() {
	closeFile();
104
	//InfoL << "md5Sum:" << getMd5Sum(_filePath);
105
	_bDownloadSuccess = true;
xiongziliang committed
106 107 108 109 110 111 112 113
	if(_onResult){
		_onResult(Err_success,"success",_filePath.data());
		_onResult = nullptr;
	}
}

void HttpDownloader::onDisconnect(const SockException &ex) {
	closeFile();
114 115 116
	if(!_bDownloadSuccess){
		File::delete_file(_filePath.data());
	}
xiongziliang committed
117 118 119 120 121 122 123 124
	if(_onResult){
		_onResult(ex.getErrCode(),ex.what(),_filePath.data());
		_onResult = nullptr;
	}
}

void HttpDownloader::closeFile() {
	if(_saveFile){
125
		fflush(_saveFile);
xiongziliang committed
126 127 128 129 130
		fclose(_saveFile);
		_saveFile = nullptr;
	}
}

131
void HttpDownloader::onManager(){
132
    if(_downloadTicker.elapsedTime() > _timeOutSecond * 1000){
133 134 135 136 137 138 139
        //超时
        onDisconnect(SockException(Err_timeout,"download timeout"));
        shutdown();
    }
}


xiongziliang committed
140 141
} /* namespace Http */
} /* namespace ZL */