HttpDownloader.cpp 2.59 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
xiongziliang committed
9 10 11 12 13
 */

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

xiongziliang committed
16
namespace mediakit {
xiongziliang committed
17 18 19 20 21 22

HttpDownloader::HttpDownloader() {

}

HttpDownloader::~HttpDownloader() {
23
    closeFile();
xiongziliang committed
24 25
}

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

50
ssize_t HttpDownloader::onResponseHeader(const string& status,const HttpHeader& headers) {
51
    if(status != "200" && status != "206"){
52 53 54 55 56
        //失败
        shutdown(SockException(Err_shutdown,StrPrinter << "Http Status:" << status));
    }
    //后续全部是content
    return -1;
xiongziliang committed
57 58
}

59
void HttpDownloader::onResponseBody(const char* buf, size_t size, size_t recvedSize, size_t totalSize) {
60
    if(_saveFile){
61 62
        fwrite(buf,size,1,_saveFile);
    }
xiongziliang committed
63
}
64

65
void HttpDownloader::onResponseCompleted() {
66 67 68 69 70 71 72
    closeFile();
    //InfoL << "md5Sum:" << getMd5Sum(_filePath);
    _bDownloadSuccess = true;
    if(_onResult){
        _onResult(Err_success,"success",_filePath);
        _onResult = nullptr;
    }
xiongziliang committed
73 74 75
}

void HttpDownloader::onDisconnect(const SockException &ex) {
76 77 78 79 80 81 82 83
    closeFile();
    if(!_bDownloadSuccess){
        File::delete_file(_filePath.data());
    }
    if(_onResult){
        _onResult(ex.getErrCode(),ex.what(),_filePath);
        _onResult = nullptr;
    }
xiongziliang committed
84 85 86
}

void HttpDownloader::closeFile() {
87 88 89 90 91
    if(_saveFile){
        fflush(_saveFile);
        fclose(_saveFile);
        _saveFile = nullptr;
    }
xiongziliang committed
92 93
}

94

xiongziliang committed
95
} /* namespace mediakit */