使用libcurl实现的上传器

头文件
ExpandedBlockStart.gif /**********************************************************************
* Copyright (C) 2014 -  - All Rights Reserved
*
* 文件名称:        Uploader_LibCurl.h
* 摘    要:        上传器 - LibCurl实现
*     
* 作    者:        yanglinbo,
* 修    改:        查看文件最下方.

**********************************************************************
*/


#ifndef __Uploader_LibCurl_H__
#define __Uploader_LibCurl_H__


#include 
#include 
#include < string>


class CUploader
ExpandedBlockStart.gif {
public:
    CUploader(void);
    virtual ~CUploader(void);

public:
ExpandedSubBlockStart.gif    /// 线程入口函数
    virtual bool run();

ExpandedSubBlockStart.gif    /// 启动上传
    virtual bool start(const std::string& strUrl, const std::string& strLocalFile);

ExpandedSubBlockStart.gif    /// 停止上传
    virtual bool stop();

ExpandedSubBlockStart.gif    /// 是否运行状态
    bool isRunning() const;

protected:
ExpandedSubBlockStart.gif    /// 读取回调
    static size_t handleRead(void *buffer, size_t size, size_t nmemb, void *userp);

ExpandedSubBlockStart.gif    /// 进度回调
    static size_t handleProgress(void *buffer, double dltotal, double dlnow, double ultotal, double ulnow);

protected:
ExpandedSubBlockStart.gif    /// 读取回调
    size_t onUpload(void *buffer, size_t size, size_t nmemb);

ExpandedSubBlockStart.gif    /// 进度回调
    size_t onProgress(const double& ultotal, const double& ulnow);

ExpandedSubBlockStart.gif    /// 上传回调
    void onUpload();

protected:
ExpandedSubBlockStart.gif    /// 设置libcurl选项
    bool setOption();

ExpandedSubBlockStart.gif    /// 清除数据
    void clear();

protected:
ExpandedSubBlockStart.gif    CURL*            m_pCurl;                ///< libcurl句柄
    FILE*            m_pFile;                ///< 文件指针

ExpandedSubBlockStart.gif    bool            m_bRunning;                ///< 运行标志

ExpandedSubBlockStart.gif    std::string        m_strRemoteUrl;            ///< 远程链接
    std::string        m_strLocalFilePath;        ///< 本地文件路径

ExpandedSubBlockStart.gif    size_t            m_nLocalFileSize;        ///< 本地文件大小
};



#endif

实现文件
ExpandedBlockStart.gif /**********************************************************************
* Copyright (C) 2014 -  - All Rights Reserved
*
* 文件名称:        Uploader_LibCurl.cpp
* 摘    要:        上传器 - LibCurl实现
*     
* 作    者:        yanglinbo,
* 修    改:        查看文件最下方.

**********************************************************************
*/


#include "StdAfx.h"
#include "Uploader.h"
#include 
#include 


CUploader::CUploader( void)
: m_pCurl(NULL)
, m_pFile(NULL)
, m_bRunning( false)
, m_nLocalFileSize(0)
ExpandedBlockStart.gif {
}


CUploader::~CUploader( void)
ExpandedBlockStart.gif {
    stop();
}


bool CUploader::run()
ExpandedBlockStart.gif {
    onUpload();
    return true;
}


bool CUploader::isRunning()  const
ExpandedBlockStart.gif {
    return m_bRunning;
}


void CUploader::clear()
ExpandedBlockStart.gif {
    if (m_pFile)
ExpandedSubBlockStart.gif    {
        fclose(m_pFile);
        m_pFile = NULL;
    }


    if (m_pCurl)
ExpandedSubBlockStart.gif    {
        curl_easy_cleanup(m_pCurl);
        m_pCurl = NULL;
        curl_global_cleanup();
    }


    m_strRemoteUrl.clear();
    m_strLocalFilePath.clear();

    m_nLocalFileSize = 0;
}


bool CUploader::setOption()
ExpandedBlockStart.gif {
    // 远程URL,支持 http, https, ftp
    curl_easy_setopt(m_pCurl, CURLOPT_URL, m_strRemoteUrl.c_str());

    curl_easy_setopt(m_pCurl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)m_nLocalFileSize);

    // 设置User-Agent
    std::string useragent = _T("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1");
    curl_easy_setopt(m_pCurl, CURLOPT_USERAGENT, useragent.c_str());

    // 设置重定向的最大次数
    curl_easy_setopt(m_pCurl, CURLOPT_MAXREDIRS, 5);

    // 设置301、302跳转跟随location
    curl_easy_setopt(m_pCurl, CURLOPT_FOLLOWLOCATION, 1);

    curl_easy_setopt(m_pCurl, CURLOPT_UPLOAD, TRUE);

    curl_easy_setopt(m_pCurl, CURLOPT_NOSIGNAL, 1L);
    curl_easy_setopt(m_pCurl, CURLOPT_POST, TRUE);
    curl_easy_setopt(m_pCurl, CURLOPT_FORBID_REUSE, TRUE);

    // 上传内容回调函数
    curl_easy_setopt(m_pCurl, CURLOPT_READFUNCTION, handleRead);
    curl_easy_setopt(m_pCurl, CURLOPT_READDATA, this);

    // 进度回调函数
    curl_easy_setopt(m_pCurl, CURLOPT_NOPROGRESS, 0);
    curl_easy_setopt(m_pCurl, CURLOPT_PROGRESSDATA, this);
    curl_easy_setopt(m_pCurl, CURLOPT_PROGRESSFUNCTION, handleProgress);

    // 跳过服务器SSL验证,不使用CA证书
    curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYPEER, 0L);

    // 验证服务器端发送的证书,默认是 2(高),1(中),0(禁用)
    curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYHOST, 0L);

    return true;
}


bool CUploader::start( const std:: string& strUrl,  const std:: string& strLocalFile)
ExpandedBlockStart.gif {
    if (strUrl.empty()) return false;

    if (m_bRunning == truereturn true;

    clear();

    m_strRemoteUrl    = strUrl;
    m_strLocalFilePath    = strLocalFile;

    // 打开文件
    m_pFile = fopen(m_strLocalFilePath.c_str(), "rb");
    if (m_pFile == NULL)
ExpandedSubBlockStart.gif    {
        return false;
    }


    // 获取文件大小
    struct stat file_info;
    if (fstat(fileno(m_pFile), &file_info) != 0) 
ExpandedSubBlockStart.gif    {
        return false;
    }

    m_nLocalFileSize = file_info.st_size;

    // 初始化libcurl
    m_pCurl = curl_easy_init();
    if (m_pCurl == NULL)
ExpandedSubBlockStart.gif    {
        return false;
    }


    // 设置libcurl的选项
    setOption();

    m_bRunning = true;

    return true;
}


bool CUploader::stop()
ExpandedBlockStart.gif {
    clear();

    m_bRunning = false;

    return true;
}


size_t CUploader::handleRead(  void *buffer, size_t size, size_t nmemb,  void *userp )
ExpandedBlockStart.gif {
    CUploader* pDownloader = (CUploader*) userp;
    if (pDownloader)
ExpandedSubBlockStart.gif    {
        return pDownloader->onUpload(buffer, size, nmemb);
    }

    return 0;
}


size_t CUploader::handleProgress(  void *buffer,  double dltotal,  double dlnow,  double ultotal,  double ulnow )
ExpandedBlockStart.gif {
    CUploader* pDownloader = (CUploader*) buffer;
    if (pDownloader)
ExpandedSubBlockStart.gif    {
        pDownloader->onProgress(ultotal, ulnow);
    }

    return 0;
}


size_t CUploader::onProgress(  const  double& ultotal,  const  double& ulnow )
ExpandedBlockStart.gif {
    TRACE("%.2f / %.2f (%.2g %%)\n", ulnow, ultotal, ulnow*100.0/ultotal);
    return 0;
}


size_t CUploader::onUpload(  void *buffer, size_t size, size_t nmemb )
ExpandedBlockStart.gif {
    size_t return_size = fread(buffer, size, nmemb, m_pFile);
    return return_size;
}


void CUploader::onUpload()
ExpandedBlockStart.gif {
    // 执行上传
    CURLcode return_code = CURLE_OK;
    return_code = curl_easy_perform(m_pCurl);

    // 关闭文件
    if (m_pFile)
ExpandedSubBlockStart.gif    {
        fclose(m_pFile);
        m_pFile = NULL;
    }


    // 上传失败
    if (return_code != CURLE_OK)
ExpandedSubBlockStart.gif    {
        return;
    }


    // 获取状态码
    int response_code = 0;
    curl_easy_getinfo(m_pCurl, CURLINFO_RESPONSE_CODE, &response_code);
}


示例代码:
CUploader uploader
uploader.start("ftp://upload:[email protected]/hello.exe", "C:\\fly.exe");
uploader.run();

你可能感兴趣的:(操作系统)