libevent和libcurl实现http和https服务器 cJSON使用

  前言

  libevent和libcurl都是功能强大的开源库;libevent主要实现服务器,包含了select、epoll等高并发的实现;libcurl实现了curl命令的API封装,主要做为客户端。这两个开源库的安装能够参考个人这篇博客:https://www.cnblogs.com/liudw-0215/p/9917422.html,而且个人代码都提交在了个人github上了,能够点左上角图标,跳转到github,仓库是libcurl。html

  1、curl的两种使用方法

  一、命令行模式

    所谓命令行模式,就是直接linux的命令行直接能够执行的curl命令,curl能够作不少事情,我主要介绍做为客户端发送xml和json数据,由于命令行模式很是要注意格式问题!linux

  (1)发送xml格式数据

  格式以下:git

  

echo '<?xml version="1.0" encoding="UTF-8"?>
 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:itsm="http://itsm.soa.csg.cn/">
   <soapenv:Header xmlns:auth="http://itsm.soa.csg.cn/">
     <auth:user>local_admin</auth:user>
     <auth:password>local_admin</auth:password>
   </soapenv:Header>
   <soapenv:Body>
     <itsm:accountOper>
       <operType>1</operType>
       <operItems>
         <operItem>
           <deviceName>测试虚拟机181106</deviceName>
           <deviceIP>11.11.22.23</deviceIP>
           <protocol>设备账户</protocol>
           <accountName>administrator</accountName>
         </operItem>
       </operItems>
     </itsm:accountOper>
   </soapenv:Body>
 </soapenv:Envelope>
 '|curl -X POST -H 'Content-type:text/xml' -d @- http://10.94.1.167:80/ITSMWebServer/itsm

  说明:github

    • echo后面跟的是xml格式数据,格式通常都是跟第三方平台约定好的,不能发这种格式,接收又是另外一种格式,那无法解析了,都要提早约定好的!
    • 中间是“|”管道符,将echo的输出做为curl的输入
    • POST 说明是post请求
    • -H 携带的消息头
    • 最后的url,是要发送的地址

  (2)发送json格式数据

  格式以下:web

  

curl -H "Content-Type:application/json" -H "appName:spvas" -H "password:123123" -H "pswdHashType:SHA1" -X POST  -k -g -d '{"param":[{"objectID":112,"type":1,"operate":1,"operatorID":100,"result":0,"time":1539941168,"policytype":0}]}' http://172.16.1.21:9999/rest/spvas/objChange.do

  说明:json

  •   -H 依然是消息头
  •        -d  后面是json格式的数据了

  二、libcurl库使用

  一、安装

  想要使用libcurl库,首先须要先安装,安装参考个人这篇博客写的很详细:https://www.cnblogs.com/liudw-0215/p/9917422.html安全

  二、使用libcurl的API

  主要就是调用libcurl库的API接口,下面介绍的http的POST请求,libcurl不少接口,不能一一介绍,须要时能够再去查找。服务器

  (1)初始化curl句柄并发

CURL* curl = NULL;
curl = curl_easy_init();

    (2)设置curl的urlapp

curl_easy_setopt(curl, CURLOPT_URL, "http://172.16.1.96:7777/login");

  (3)开启post请求开关

curl_easy_setopt(curl, CURLOPT_POST, true);

  (4)添加post数据

 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_str);

  (5)设定一个处理服务器响应的回调函数

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, deal_response);

  (6)给回调函数传递一个形参

curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseData);

  (7)向服务器发送请求,等待服务器的响应

res = curl_easy_perform(curl);

  三、整体代码

 客户端整体代码以下:

  

//
// Created by ldw on 2018/11/8.
//
#include "cJSON.h"
#include <curl/curl.h>
#include<string.h>

#define RESPONSE_DATA_LEN 4096

//用来接收服务器一个buffer
typedef struct login_response_data
{
    login_response_data() {
        memset(data, 0, RESPONSE_DATA_LEN);
        data_len = 0;
    }

    char data[RESPONSE_DATA_LEN];
    int data_len;

}response_data_t;


//处理从服务器返回的数据,将数据拷贝到arg中
size_t deal_response(void *ptr, size_t n, size_t m, void *arg)
{
    int count = m*n;

    response_data_t *response_data = (response_data_t*)arg;

    memcpy(response_data->data, ptr, count);

    response_data->data_len = count;

    return response_data->data_len;
}

#define POSTDATA "{\"username\":\"gailun\",\"password\":\"123123\",\"driver\":\"yes\"}"

int main()
{
    

    char *post_str = NULL;
   
    CURL* curl = NULL;
    CURLcode res;
    response_data_t responseData;//专门用来存放从服务器返回的数据
    //初始化curl句柄
    curl = curl_easy_init();
    if(curl == NULL) {
        return 1;
    }

    //封装一个数据协议
    /*

       ====给服务端的协议====
     http://ip:port/login [json_data]
    {
        username: "gailun",
        password: "123123",
        driver:   "yes"
    }
     *
     *
     * */
    //(1)封装一个json字符串
    cJSON *root = cJSON_CreateObject();

    cJSON_AddStringToObject(root, "username", "ldw");
    cJSON_AddStringToObject(root, "password", "123123");
    cJSON_AddStringToObject(root, "driver", "yes");

    post_str = cJSON_Print(root);
    cJSON_Delete(root);
    root = NULL;


    //(2) 向web服务器 发送http请求 其中post数据 json字符串
    //1 设置curl url
    curl_easy_setopt(curl, CURLOPT_URL, "http://172.16.1.96:7777/login");

    //客户端忽略CA证书认证 用于https跳过证书认证
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);

    //2 开启post请求开关
    curl_easy_setopt(curl, CURLOPT_POST, true);
    //3 添加post数据
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_str);

    //4 设定一个处理服务器响应的回调函数
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, deal_response);

    //5 给回调函数传递一个形参
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseData);

    //6 向服务器发送请求,等待服务器的响应
    res = curl_easy_perform(curl);
    if (res != CURLE_OK) {
        return 1;
    }
    curl_easy_cleanup(curl);
    
    //(3)  处理服务器响应的数据 此刻的responseData就是从服务器获取的数据
    /*

      //成功
    {
        result: "ok",
    }
    //失败
    {
        result: "error",
        reason: "why...."
    }

     *
     * */
    //(4) 解析服务器返回的json字符串
    //cJSON *root;
    root = cJSON_Parse(responseData.data);

    cJSON *result = cJSON_GetObjectItem(root, "result");
    if(result && strcmp(result->valuestring, "ok") == 0) {
        printf("data:%s\n",responseData.data);
        //登录成功
        return 0;

    }
    else {
        //登录失败
        cJSON* reason = cJSON_GetObjectItem(root, "reason");
        if (reason) {
            //已知错误
           return 1;

        }
        else {
            //未知的错误
          return 1;
        }

        return 1;
    }

    return 0;
}
View Code

  这是客户端的整体代码,可是还没法测试,由于没有服务端,下面会介绍用libevent库来搭建http的服务端;由于数据格式是json,因此用到了cJSON,能够到个人github上进行下载,编译命令:g++ login.cpp cJSON.cpp -o login -lcurl

  2、libevent库

 一、安装

    libevent依然是开源库,使用以前依然须要安装,安装参考个人这篇博客写的很详细:https://www.cnblogs.com/liudw-0215/p/9917422.html

  二、搭建http服务器

    安装以后,就可使用了,主要都是调用libcurl库的API函数,main函数以下:

int main(int argc, char *argv[]) {
    //自定义信号处理函数
    signal(SIGHUP, signal_handler);
    signal(SIGTERM, signal_handler);
    signal(SIGINT, signal_handler);
    signal(SIGQUIT, signal_handler);

    //默认参数
    char *httpd_option_listen = "0.0.0.0";
    int httpd_option_port = 7777;
    int httpd_option_daemon = 0;
    int httpd_option_timeout = 120; //in seconds

    //获取参数
    int c;
    while ((c = getopt(argc, argv, "l:p:dt:h")) != -1) {
        switch (c) {
            case 'l' :
                httpd_option_listen = optarg;
                break;
            case 'p' :
                httpd_option_port = atoi(optarg);
                break;
            case 'd' :
                httpd_option_daemon = 1;
                break;
            case 't' :
                httpd_option_timeout = atoi(optarg);
                break;
            case 'h' :
            default :
                show_help();
                exit(EXIT_SUCCESS);
        }
    }

    //判断是否设置了-d,以daemon运行
    if (httpd_option_daemon) {
        pid_t pid;
        pid = fork();
        if (pid < 0) {
            perror("fork failed");
            exit(EXIT_FAILURE);
        }
        if (pid > 0) {
            //生成子进程成功,退出父进程
            exit(EXIT_SUCCESS);
        }
    }


    /* 使用libevent建立HTTP Server */

    //初始化event API
    event_init();

    //建立一个http server
    struct evhttp *httpd;

    httpd = evhttp_start(httpd_option_listen, httpd_option_port);

    evhttp_set_timeout(httpd, httpd_option_timeout);

    //也能够为特定的URI指定callback
    evhttp_set_cb(httpd, "/", httpd_handler, NULL);
    evhttp_set_cb(httpd, "/login", login_handler, NULL);

    //循环处理events
    event_dispatch();

    evhttp_free(httpd);

    return 0;
}

   三、测试http服务

  •   启动服务端  

  从个人github上下载以后,http服务在libcurl/http_server/这个目录,写Makefile,而后直接make就能够了,以下:

  

  make以后生成了server,执行:./server,启动服务

  •   启动客户端

  在libcurl/login/这个目录,执行:g++ login.cpp cJSON.cpp -o login -lcurl,进行编译,生成login,启动客户端:./login,客户端运行结果,以下:

  

  服务端响应结果,以下:

  

  至此,完成了演示,用libcurl和libevent搭建的http服务器与客户端,没有问题。是否是以为到此就结束了,才没有呢?下面,将要介绍https服务器,那为何要用https服务器呢?跟随我找到谜底吧!

  四、搭建https服务器

  (1)https介绍

  http传输过程都是明文传输,很不安全;就产生https,进行加密传输,但加密过程并无那么简单,以下图所示:

  

  说明:

  主要经历了两个阶段:

  •   非对称加密过程

  经过公钥、私钥和CA证书,进行验证,最终得到会话密钥

  •   对称加密过程

  可能会想?直接都用非对称加密得了,为啥用对称加密?由于非对称效率很低,因此要用对称加密!

  用非对称过程获得的密钥,对数据进行加密而后传输。

  (2)https服务器实现

  libevent库应该从2.1版本以后才支持https的,因此在2.1以前的版本还要单独安装openssl!

  mian函数以下:

  

int main (int argc, char **argv)
{ 
    /*OpenSSL 初始化 */
    common_setup ();              

    if (argc > 1) {
        char *end_ptr;
        long lp = strtol(argv[1], &end_ptr, 0);
        if (*end_ptr) {
            fprintf(stderr, "Invalid integer\n");
            return -1;
        }
        if (lp <= 0) {
            fprintf(stderr, "Port must be positive\n");
            return -1;
        }
        if (lp >= USHRT_MAX) {
            fprintf(stderr, "Port must fit 16-bit range\n");
            return -1;
        }

        serverPort = (unsigned short)lp;
    }

    /* now run http server (never returns) */
    return serve_some_http ();
}

  (3)测试https服务器

  •   启动服务端  

  从个人github上下载以后,http服务在libcurl/https_server/这个目录,写Makefile,而后直接make就能够了;

  •   启动客户端

  修改http的客户端就能够了,以下:

  

curl_easy_setopt(curl, CURLOPT_URL, "https://172.16.1.96:8080/login");
 
//客户端忽略CA证书认证 用于https跳过证书认证
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);

  说明:

  在http后面加上“s”;再加上跳过证书认证,就能够了

相关文章
相关标签/搜索