zoukankan      html  css  js  c++  java
  • 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。

      一、curl的两种使用方法

      1、命令行模式

        所谓命令行模式,就是直接linux的命令行直接可以执行的curl命令,curl可以做很多事情,我主要介绍作为客户端发送xml和json数据,因为命令行模式非常要注意格式问题!

      (1)发送xml格式数据

      格式如下:

      

    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

      说明:

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

      (2)发送json格式数据

      格式如下:

      

    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

      说明:

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

      2、libcurl库使用

      1、安装

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

      2、使用libcurl的API

      主要就是调用libcurl库的API接口,下面介绍的http的POST请求,libcurl很多接口,不能一一介绍,需要时可以再去查找。

      (1)初始化curl句柄

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

        (2)设置curl的url

    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);

      3、总体代码

     客户端总体代码如下:

      

    //
    // 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
    ",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

      二、libevent库

     1、安装

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

      2、搭建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;
    }

       3、测试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服务器呢?跟随我找到谜底吧!

      4、搭建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
    ");
                return -1;
            }
            if (lp <= 0) {
                fprintf(stderr, "Port must be positive
    ");
                return -1;
            }
            if (lp >= USHRT_MAX) {
                fprintf(stderr, "Port must fit 16-bit range
    ");
                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”;再加上跳过证书认证,就可以了

      

      

      

      

  • 相关阅读:
    OI无关 透彻随笔
    置顶帖(传送门)/to 学弟 一个菜鸡的故事
    洛谷P1494 小Z的袜子 莫队
    关于 对拍 的一些认识
    关于 带权二分/wqs二分 的一些认识
    关于 KDtree 的一些认识
    关于 网络流 的一些认识
    省选 考前模板
    关于 多项式 的一些认识/多项式入门
    小球与盒子 的奇妙关系
  • 原文地址:https://www.cnblogs.com/liudw-0215/p/9927470.html
Copyright © 2011-2022 走看看