HttpDownloader.cpp 2.5 KB
Newer Older
xiongziliang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * HttpDownloader.cpp
 *
 *  Created on: 2017年5月5日
 *      Author: xzl
 */

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

using namespace ZL::Util;

namespace ZL {
namespace Http {

HttpDownloader::HttpDownloader() {

}

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

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

void HttpDownloader::onResponseHeader(const string& status,const HttpHeader& headers) {
	if(status != "200" && status != "206"){
		//失败
		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) {
	if(_saveFile){
		fwrite(buf,size,1,_saveFile);
	}
}
68 69 70 71 72 73 74 75 76 77 78
//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
79 80
void HttpDownloader::onResponseCompleted() {
	closeFile();
81
	//InfoL << "md5Sum:" << getMd5Sum(_filePath);
82
	_bDownloadSuccess = true;
xiongziliang committed
83 84 85 86 87 88 89 90
	if(_onResult){
		_onResult(Err_success,"success",_filePath.data());
		_onResult = nullptr;
	}
}

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

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

} /* namespace Http */
} /* namespace ZL */