读书人

UtilBox(ub)基础组件 - EasyCurl 发送

发布时间: 2012-08-31 12:55:03 作者: rapoo

UtilBox(ub)基础组件 -- EasyCurl 发送http请求(2)

EasyCurl简单封装了一下libcurl调用,对外的提供抽象化的接口,下面就把源码分享出来,此源码和接口还带进一步完善优化,尤其是一些curl_set_opt()的上层封装。大家可以自己写哈,最后写一个满足自己需求的Curl。

ubplus_curl.h : EasyCurl的类定义和接口,还有一些宏

/* * ===================================================================================== * *       Filename:  ubplus_curl.cc * *    Description:  A curl wrapper of libcurl * *        Created:  08/16/2012 04:55:34 PM *       Compiler:  g++ 4.6.1 * *         Author:  Michael LiuXin * * ===================================================================================== */#include "ubplus_curl.h"#define CURL_OK 0namespace ubplus{#define CURL_GLOBAL_INIT_ONCE __global_init();const char* ERROR_MSG[] = {"EasyCurl OK","Global Init Failed","Easy Init Failed","Easy Perform Failed","Easy SetOption Failed","Curl Handle Not Vaild","Curl URL Not Vaild","Curl PostData Not Vaild","Curl User Buffer Not Vaild","Curl TimeOut Not Vaild","Unkown Error"};// *********************************************************** size_t EasyCurl::__data_callback(char* content,size_t size,size_t n,void* null){UB_LOG_DEBUG("[ok=curl] Default callback invoke");// copy to buffer(this is from user) and ensure space is enoughif (NULL != EasyCurl::user_buffer.mem) {size_t full = n*size;// we have no spaceif (EasyCurl::user_buffer.offset >= EasyCurl::user_buffer.max) {UB_LOG_TRACE("[ok=curl] User buffer has no space , ignore ");return full;}// as write enough as we cansize_t to_write = (EasyCurl::user_buffer.offset+n*size<EasyCurl::user_buffer.max? n*size : (EasyCurl::user_buffer.max-EasyCurl::user_buffer.offset));memcpy(EasyCurl::user_buffer.mem+EasyCurl::user_buffer.offset,content,to_write);UB_LOG_DEBUG("[ok=memory] Copy memory to user buffer");EasyCurl::user_buffer.offset += to_write;EasyCurl::total_size_ += to_write;} return n*size;}// ***********************************************************bool EasyCurl::__global_init(){if (!EasyCurl::global_init_) {// call global init only one timeCURLcode code = curl_global_init(CURL_GLOBAL_ALL);if (CURL_OK != code) {this->is_good_ = false;this->error_ = UBPLUS_CURL_GLOBAL_INIT_FAILD;UB_LOG_FATAL("[err=curl] Curl global init error[%s]",curl_easy_strerror(code));return false;} else {  UB_LOG_TRACE("[ok=curl] Curl global init");EasyCurl::global_init_ = true;return true;}} elsereturn true;}// ***********************************************************EasyCurl::EasyCurl(){CURL_GLOBAL_INIT_ONCE// curl initialthis->curl_ = curl_easy_init();if (!this->curl_) {this->error_ = UBPLUS_CURL_EASY_INIT_FAILD;this->is_good_ = false;UB_LOG_WARNNING("[warn=curl] Curl easy init failed");return;} else {// set our default write functionif (CURL_OK != curl_easy_setopt(this->curl_,CURLOPT_WRITEFUNCTION,EasyCurl::__data_callback)) {this->error_ = UBPLUS_CURL_SET_OPTION_FAILD;this->is_good_ = false;} else {this->error_ = UBPLUS_CURL_OK;this->is_good_ = true;}}}// ***********************************************************EasyCurl::~EasyCurl(){if (this->curl_) {UB_LOG_DEBUG("[ok=curl] Curl cleanup ok");curl_easy_cleanup(this->curl_);}}// ***********************************************************int EasyCurl::set_curl_callback(CURL_CALLBACK func){if (!this->curl_ || !this->is_good_) return this->error_ = UBPLUS_CURL_HANDLE_NOT_VAILD;if (CURL_OK == curl_easy_setopt(this->curl_,CURLOPT_WRITEFUNCTION,func))return UBPLUS_CURL_OK;else {UB_LOG_WARNNING("[warn=curl] set option[call_back] failed");this->error_ = UBPLUS_CURL_SET_OPTION_FAILD;return UBPLUS_CURL_SET_OPTION_FAILD;}}// ***********************************************************int EasyCurl::set_curl_callback_context(void* user_context){if (!this->curl_ || !this->is_good_) return this->error_ = UBPLUS_CURL_HANDLE_NOT_VAILD;if (CURL_OK == curl_easy_setopt(this->curl_,CURLOPT_WRITEDATA,user_context))return UBPLUS_CURL_OK;else {UB_LOG_WARNNING("[warn=curl] set option[call_back_context] failed");this->error_ = UBPLUS_CURL_SET_OPTION_FAILD;return UBPLUS_CURL_SET_OPTION_FAILD;}}// ***********************************************************int EasyCurl::set_user_buffer(char** user,size_t len) {if (NULL == user || NULL == *user || 0 == len) {this->is_good_ = false;return this->error_ = UBPLUS_CURL_USER_BUFFER_NOT_VAILD;}EasyCurl::user_buffer.mem = *user;memset(EasyCurl::user_buffer.mem,0,len);EasyCurl::user_buffer.offset = 0;EasyCurl::user_buffer.max = len;UB_LOG_DEBUG("[ok=curl] User Buffer set");return UBPLUS_CURL_OK;}// ***********************************************************int EasyCurl::fetch() {if (!this->curl_ || !this->is_good_) return this->error_ = UBPLUS_CURL_HANDLE_NOT_VAILD;// actual perform curlCURLcode code = curl_easy_perform(this->curl_);// don't forget to clear user bufferEasyCurl::user_buffer.offset = 0;if (CURL_OK == code) {return UBPLUS_CURL_OK;} else {UB_LOG_WARNNING("[warn=curl] curl perform error[%s]",curl_easy_strerror(code));this->error_ = UBPLUS_CURL_PERFORM_FAILD;return UBPLUS_CURL_PERFORM_FAILD;}}// ***********************************************************int EasyCurl::set_timeout(unsigned long time) {if (!this->curl_ || !this->is_good_) return this->error_ = UBPLUS_CURL_HANDLE_NOT_VAILD;if (0 == time)return this->error_ = UBPLUS_CURL_TIMEOUT_NOT_VAILD;if (CURL_OK == curl_easy_setopt(this->curl_,CURLOPT_TIMEOUT,time))return UBPLUS_CURL_OK;else {UB_LOG_WARNNING("[warn=curl] set option[timeout] failed");this->error_ = UBPLUS_CURL_SET_OPTION_FAILD;return UBPLUS_CURL_SET_OPTION_FAILD;}}// ***********************************************************int EasyCurl::set_url(std::string url) {if (!this->curl_ || !this->is_good_) return this->error_ = UBPLUS_CURL_HANDLE_NOT_VAILD;if (url.length()<5)return this->error_ = UBPLUS_CURL_URL_NOT_VAILD;if (CURL_OK == curl_easy_setopt(this->curl_,CURLOPT_URL,url.c_str()))return UBPLUS_CURL_OK;else {UB_LOG_WARNNING("[warn=curl] set option[url] failed");this->error_ = UBPLUS_CURL_URL_NOT_VAILD;return UBPLUS_CURL_URL_NOT_VAILD;}}// ***********************************************************int EasyCurl::set_post_data(void* post_data) {if (!this->curl_ || !this->is_good_) return this->error_ = UBPLUS_CURL_HANDLE_NOT_VAILD;if (NULL == post_data)return this->error_ = UBPLUS_CURL_POST_NOT_VAILD;if (CURL_OK == curl_easy_setopt(this->curl_,CURLOPT_POSTFIELDS,post_data))return UBPLUS_CURL_OK;else {UB_LOG_WARNNING("[warn=curl] set option[post] failed");this->error_ = UBPLUS_CURL_POST_NOT_VAILD;return UBPLUS_CURL_POST_NOT_VAILD;}}size_t EasyCurl::total_size_ = 0;bool EasyCurl::global_init_ = false;EasyCurl::curl_buffer EasyCurl::user_buffer;} // end of namespace


读书人网 >软件架构设计

热点推荐