基于libcurl封装的HTTP客户端库

@[toc]

libcurl安装

libcurl的编译安装请参考博客https://blog.csdn.net/gg_simida/article/details/80536860

HttpClient

我们的目标是封装一个HttpClient类,支持GET、POST或者自定义方法,支持发送和接收文本、json、xml、form-data、x-www-form-urlencoded数据,支持自定义头部Headers等

// HttpRequest.h
#ifndef HTTP_REQUEST_H_
#define HTTP_REQUEST_H_

#include <string>
#include <vector>
#include <map>

using std::string;
using std::vector;
using std::map;

typedef std::map<std::string, std::string> KeyValue;

// F(id, str)
#define FOREACH_CONTENT_TYPE(F) \
    F(TEXT_PLAIN,               "text/plain")   \
    F(TEXT_HTML,                "text/html")    \
    F(TEXT_XML,                 "text/xml")     \
    F(APPLICATION_JSON,         "application/json") \
    F(APPLICATION_XML,          "application/xml")  \
    F(APPLICATION_JAVASCRIPT,   "application/javascript")   \
    \
    F(FORM_DATA,                "multipart/form-data")  \
    \
    F(X_WWW_FORM_URLENCODED,    "application/x-www-form-urlencoded")    \
    F(QUERY_STRING,             "text/plain")

#define ENUM_CONTENT_TYPE(id, _)    id,
enum ContentType {
    FOREACH_CONTENT_TYPE(ENUM_CONTENT_TYPE)
};

struct FormData {
    enum FormDataType {
        CONTENT,
        FILENAME
    } type;
    string       data;
    FormData() {
        type = CONTENT;
    }
    FormData(const char* data, FormDataType type = CONTENT) {
        this->type = type;
        this->data = data;
    }
    FormData(const string& str, FormDataType type = CONTENT) {
        this->type = type;
        this->data = str;
    }
    FormData(int n) {
        this->type = CONTENT;
        this->data = std::to_string(n);
    }
    FormData(long long n) {
        this->type = CONTENT;
        this->data = std::to_string(n);
    }
    FormData(float f) {
        this->type = CONTENT;
        this->data = std::to_string(f);
    }
    FormData(double lf) {
        this->type = CONTENT;
        this->data = std::to_string(lf);
    }
};

typedef std::multimap<std::string, FormData>     Form;

struct HttpRequest {
    // request line
    string              method;
    string              url;
    string              version;

    // headers
    KeyValue            headers;

    // body
    ContentType     content_type;
    string          text;
    KeyValue        kvs; // QUERY_STRING,X_WWW_FORM_URLENCODED
    Form            form; // FORM_DATA
};

struct HttpResponse {
    // status line
    string version;
    int    status_code;
    string status_message;

    // headers
    KeyValue headers;

    // body
    string body;
};

#endif // HTTP_REQUEST_H_

HttpRequest.h头文件中定义了ContentType,FormData,HttpRequest,HttpResponse等数据结构;

// HttpClient.h
#ifndef HTTP_CLIENT_H_
#define HTTP_CLIENT_H_

/***************************************************************
HttpClient based libcurl
***************************************************************/

#include <curl/curl.h>

#include "HttpRequest.h"

class HttpClient {
public:
    HttpClient();
    ~HttpClient();

    int Send(const HttpRequest& req, HttpResponse* res);
    static const char* strerror(int errcode);

    void SetTimeout(int sec) {m_timeout = sec;}
    void AddHeader(string key, string value) {
        m_headers[key] = value;
    }
    void DelHeader(string key) {
        auto iter = m_headers.find(key);
        if (iter != m_headers.end()) {
            m_headers.erase(iter);
        }
    }
    void ClearHeader() {
        m_headers.clear();
    }

protected:
    int curl(const HttpRequest& req, HttpResponse* res);

private:
    int m_timeout; // unit:s default:10s
    KeyValue m_headers;
};

#endif  // HTTP_CLIENT_H_

HttpClient.h头文件声明了基于libcurl的HTTP客户端类;

// HttpClient.cpp
#include "HttpClient.h"

#include <string.h>
#include <string>
using std::string;

#define SPACE_CHARS     " \t\r\n"
static string trim(const string& str) {
    string::size_type pos1 = str.find_first_not_of(SPACE_CHARS);
    if (pos1 == string::npos)   return "";

    string::size_type pos2 = str.find_last_not_of(SPACE_CHARS);
    return str.substr(pos1, pos2-pos1+1);
}

static inline bool is_unambiguous(char c) {
    return (c >= '0' && c <= '9') ||
           (c >= 'A' && c <= 'Z') ||
           (c >= 'a' && c <= 'z') ||
           c == '-' ||
           c == '_' ||
           c == '.' ||
           c == '~';
}

static string escape(const string& istr) {
    string ostr;
    const char* p = istr.c_str();
    int len = istr.size();
    char szHex[4] = {0};
    for (int i = 0; i < len; ++i) {
        if (is_unambiguous(p[i])) {
            ostr += p[i];
        }
        else {
            sprintf(szHex, "%%%02X", p[i]);
            ostr += szHex;
        }
    }
    return ostr;
}

#define DEFAULT_TIMEOUT     10
HttpClient::HttpClient() {
    m_timeout = DEFAULT_TIMEOUT;
}

HttpClient::~HttpClient() {

}

int HttpClient::Send(const HttpRequest& req, HttpResponse* res) {
    return curl(req, res);
}

static size_t s_formget_cb(void *arg, const char *buf, size_t len) {
    return len;
}

static size_t s_header_cb(char* buf, size_t size, size_t cnt, void* userdata) {
    if (buf == NULL || userdata == NULL)    return 0;

    HttpResponse* res = (HttpResponse*)userdata;

    string str(buf);
    string::size_type pos = str.find_first_of(':');
    if (pos == string::npos) {
        if (res->version.empty() && strncmp(buf, "HTTP", 4) == 0) {
            // status line
            // HTTP/1.1 200 OK\r\n
            char* space1 = strchr(buf, ' ');
            char* space2 = strchr(space1+1, ' ');
            if (space1 && space2) {
                *space1 = '\0';
                *space2 = '\0';
                res->version = buf;
                res->status_code = atoi(space1+1);
                res->status_message = trim(space2+1);
            }
        }
    }
    else {
        // headers
        string key = trim(str.substr(0, pos));
        string value = trim(str.substr(pos+1));
        res->headers[key] = value;
    }
    return size*cnt;
}

static size_t s_body_cb(char *buf, size_t size, size_t cnt, void *userdata) {
    if (buf == NULL || userdata == NULL)    return 0;

    HttpResponse* res = (HttpResponse*)userdata;
    res->body.append(buf, size*cnt);
    return size*cnt;
}

int HttpClient::curl(const HttpRequest& req, HttpResponse* res) {
    CURL* handle = curl_easy_init();

    // SSL
    curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
    curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0);

    // method
    curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, req.method.c_str());

    // url
    curl_easy_setopt(handle, CURLOPT_URL, req.url.c_str());

    // header
    struct curl_slist *headers = NULL;
    if (m_headers.size() != 0) {
        for (auto& pair : m_headers) {
            string header = pair.first;
            header += ": ";
            header += pair.second;
            headers = curl_slist_append(headers, header.c_str());
        }
    }
    const char* psz = "text/plain";
    switch (req.content_type) {
#define CASE_CONTENT_TYPE(id, str)  \
    case id: psz = str;    break;

        FOREACH_CONTENT_TYPE(CASE_CONTENT_TYPE)
#undef  CASE_CONTENT_TYPE
    }
    string strContentType("Content-type: ");
    strContentType += psz;
    headers = curl_slist_append(headers, strContentType.c_str());
    curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
    //hlogd("%s %s", req.method.c_str(), req.url.c_str());
    //hlogd("%s", strContentType.c_str());

    // body or params
    struct curl_httppost* httppost = NULL;
    struct curl_httppost* lastpost = NULL;
    switch (req.content_type) {
    case FORM_DATA:
    {
        for (auto& pair : req.form) {
            CURLformoption opt = pair.second.type == FormData::FILENAME ? CURLFORM_FILE : CURLFORM_COPYCONTENTS;
            curl_formadd(&httppost, &lastpost,
                    CURLFORM_COPYNAME, pair.first.c_str(),
                    opt, pair.second.data.c_str(),
                    CURLFORM_END);
        }
        if (httppost) {
            curl_easy_setopt(handle, CURLOPT_HTTPPOST, httppost);
            curl_formget(httppost, NULL, s_formget_cb);
        }
    }
    break;
    case QUERY_STRING:
    case X_WWW_FORM_URLENCODED:
    {
        string params;
        auto iter = req.kvs.begin();
        while (iter != req.kvs.end()) {
            if (iter != req.kvs.begin()) {
                params += '&';
            }
            params += escape(iter->first);
            params += '=';
            params += escape(iter->second);
            iter++;
        }
        if (req.content_type == QUERY_STRING) {
            string url_with_params(req.url);
            url_with_params += '?';
            url_with_params += params;
            curl_easy_setopt(handle, CURLOPT_URL, url_with_params.c_str());
            //hlogd("%s", url_with_params.c_str());
        }
        else {
            curl_easy_setopt(handle, CURLOPT_POSTFIELDS, params.c_str());
            //hlogd("%s", params.c_str());
        }
    }
    break;
    default:
    {
        if (req.text.size() != 0) {
         curl_easy_setopt(handle, CURLOPT_POSTFIELDS, req.text.c_str());
         //hlogd("%s", req.text.c_str());
        }
    }
    break;
    }

    if (m_timeout != 0) {
        curl_easy_setopt(handle, CURLOPT_TIMEOUT, m_timeout);
    }

    curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, s_body_cb);
    curl_easy_setopt(handle, CURLOPT_WRITEDATA, res);

    curl_easy_setopt(handle, CURLOPT_HEADER, 0);
    curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, s_header_cb);
    curl_easy_setopt(handle, CURLOPT_HEADERDATA, res);

    int ret = curl_easy_perform(handle);
    if (ret != 0) {
        //hloge("%d: %s", ret, curl_easy_strerror((CURLcode)ret));
    }

    //if (res->body.length() != 0) {
        //hlogd("Response:%s", res->body.c_str());
    //}
    //double total_time, name_time, conn_time, pre_time;
    //curl_easy_getinfo(handle, CURLINFO_TOTAL_TIME, &total_time);
    //curl_easy_getinfo(handle, CURLINFO_NAMELOOKUP_TIME, &name_time);
    //curl_easy_getinfo(handle, CURLINFO_CONNECT_TIME, &conn_time);
    //curl_easy_getinfo(handle, CURLINFO_PRETRANSFER_TIME, &pre_time);
    //hlogd("TIME_INFO: %lf,%lf,%lf,%lf", total_time, name_time, conn_time, pre_time);

    if (headers) {
        curl_slist_free_all(headers);
    }
    if (httppost) {
        curl_formfree(httppost);
    }

    curl_easy_cleanup(handle);

    return ret;
}

const char* HttpClient::strerror(int errcode) {
    return curl_easy_strerror((CURLcode)errcode);
}

HttpClient.cpp源文件根据HttpRequest利用libcurl完成请求,在回调函数中解析填充HttpResponse

// test.cpp
#include "HttpClient.h"

#include <stdio.h>

int main(int argc, char* argv[]) {
    HttpClient session;
    HttpRequest req;
    req.method = "GET";
    req.url = "www.baidu.com";
    HttpResponse res;
    int ret = session.Send(req, &res);
    if (ret != 0) {
        printf("%s %s failed => %d:%s\n", req.method.c_str(), req.url.c_str(), ret, HttpClient::strerror(ret));
    }
    else {
        printf("%s %d %s\r\n", res.version.c_str(), res.status_code, res.status_message.c_str());
        for (auto& header : res.headers) {
            printf("%s: %s\r\n", header.first.c_str(), header.second.c_str());
        }
        printf("\r\n");
        printf("%s", res.body.c_str());
        printf("\n");
    }
    return ret;
}
g++ -std=c++11 HttpClient.cpp test.cpp -o test -lcurl

封装过后使用起来相当方便吧,一点不输pythonrequests

完整http客户端

完整的http客户端接口见 https://github.com/ithewei/hw/blob/master/http/client/http_client.h

git clone https://github.com/ithewei/hw.git

# 使用libcurl
make curl DEFINES="WITH_CURL CURL_STATICLIB"
bin/curl -h
bin/curl -v www.baidu.com

# 不使用libcurl,自实现的http客户端
make curl

# 使用openssl支持https
make curl DEFINES="WITH_OPENSSL"
bin/curl -v https:///www.baidu.com

# 使用nghhtp2支持http2
make curl DEFINES="WITH_NGHTTP2"
bin/curl -v www.nghttp2.org --http2
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,188评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,464评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,562评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,893评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,917评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,708评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,430评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,342评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,801评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,976评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,115评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,804评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,458评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,008评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,135评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,365评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,055评论 2 355

推荐阅读更多精彩内容