必威体育Betway必威体育官网
当前位置:首页 > IT技术

基于libevent库实现的http server示例

时间:2019-10-23 23:45:51来源:IT技术作者:seo实验室小编阅读:66次「手机版」
 

libevent

最近在工作当中接触到libevent库,用于http server端功能还是比较强大,特在此记录一笔,以备后面查漏补缺。首先是下载安装,直接去官网下载对应版本的安装包,解压安装即可,这里就不啰嗦了。

   #tar

   #./configure

  #make

  #make install

完成安装之后,就可以开始编写自己的示例程序了,先上代码

#include <stdio.h>
#include <stdlib.h>
#include <evhttp.h>
#include <event.h>
#include <string.h>
#include "event2/http.h"
#include "event2/event.h"
#include "event2/buffer.h"
#include "event2/bufferevent.h"
#include "event2/bufferevent_compat.h"
#include "event2/http_struct.h"
#include "event2/http_compat.h"
#include "event2/util.h"
#include "event2/listener.h"

#define BUF_MAX 1024*16

//解析post请求数据
void get_post_message(char *buf, struct evhttp_request *req)
{
    size_t post_size = 0;

    post_size = evbuffer_get_length(req->input_buffer);//获取数据长度
    printf("====line:%d,post len:%d\n",__LINE__,post_size);
    if (post_size <= 0)
    {
        printf("====line:%d,post msg is empty!\n",__LINE__);
        return;
    }
    else
    {
        size_t copy_len = post_size > BUF_MAX ? BUF_MAX : post_size;
        printf("====line:%d,post len:%d, copy_len:%d\n",__LINE__,post_size,copy_len);
        memcpy(buf, evbuffer_pullup(req->input_buffer,-1), copy_len);
        buf[post_size] = '\0';
        printf("====line:%d,post msg:%s\n",__LINE__,buf);
    }
}

//解析http头,主要用于get请求时解析uri和请求参数
char *find_http_header(struct evhttp_request *req,struct evkeyvalq *params,const char *query_char)
{
    if(req == NULL || params == NULL || query_char == NULL)
    {
        printf("====line:%d,%s\n",__LINE__,"input params is null.");
        return NULL;
    }

    struct evhttp_uri *decoded = NULL;
    char *query = NULL; 
    char *query_result = NULL;
    const char *path;
    const char *uri = evhttp_request_get_uri(req);//获取请求uri

    if(uri == NULL)
    {
        printf("====line:%d,evhttp_request_get_uri return null\n",__LINE__);
        return NULL;
    }
    else
    {
        printf("====line:%d,Got a GET request for <%s>\n",__LINE__,uri);
    }

    //解码uri
    decoded = evhttp_uri_parse(uri);
    if (!decoded) 
    {
        printf("====line:%d,It's not a good URI. Sending badrequest\n",__LINE__);
        evhttp_send_ERROR(req, HTTP_BADREQUEST, 0);
        return;
    }

    //获取uri中的path部分
    path = evhttp_uri_get_path(decoded);
    if (path == NULL) 
    {
        path = "/";
    }
    else
    {
        printf("====line:%d,path is:%s\n",__LINE__,path);
    }

    //获取uri中的参数部分
    query = (char*)evhttp_uri_get_query(decoded);
    if(query == NULL)
    {
        printf("====line:%d,evhttp_uri_get_query return null\n",__LINE__);
        return NULL;
    }

    //查询指定参数的值
    evhttp_parse_query_str(query, params);          
    query_result = (char*)evhttp_find_header(params, query_char);

    return query_result;
}

//处理get请求
void http_handler_testget_msg(struct evhttp_request *req,void *arg)
{
    if(req == NULL)
    {
        printf("====line:%d,%s\n",__LINE__,"input param req is null.");
        return;
    }

    char *sign = NULL;
    char *data = NULL;
    struct evkeyvalq sign_params = {0};
    sign = find_http_header(req,&sign_params,"sign");//获取get请求uri中的sign参数
    if(sign == NULL)
    {
        printf("====line:%d,%s\n",__LINE__,"request uri no param sign.");
    }
    else
    {
        printf("====line:%d,get request param: sign=[%s]\n",__LINE__,sign);
    }

    data = find_http_header(req,&sign_params,"data");//获取get请求uri中的data参数
    if(data == NULL)
    {
        printf("====line:%d,%s\n",__LINE__,"request uri no param data.");
    }
    else
    {
        printf("====line:%d,get request param: data=[%s]\n",__LINE__,data);
    }
    printf("\n");

    //回响应
    struct evbuffer *retbuff = NULL;
    retbuff = evbuffer_new();
    if(retbuff == NULL)
    {
        printf("====line:%d,%s\n",__LINE__,"retbuff is null.");
        return;
    }
    evbuffer_add_printf(retbuff,"Receive get request,Thamks for the request!");
    evhttp_send_reply(req,HTTP_OK,"Client",retbuff);
    evbuffer_free(retbuff);
}

//处理post请求
void http_handler_testpost_msg(struct evhttp_request *req,void *arg)
{
    if(req == NULL)
    {
        printf("====line:%d,%s\n",__LINE__,"input param req is null.");
        return;
    }

    char buf[BUF_MAX] = {0};
    get_post_message(buf, req);//获取请求数据,一般是json格式的数据
    if(buf == NULL)
    {
        printf("====line:%d,%s\n",__LINE__,"get_post_message return null.");
        return;
    }
    else
    {
        //可以使用json库解析需要的数据
        printf("====line:%d,request data:%s",__LINE__,buf);
    }

    //回响应
    struct evbuffer *retbuff = NULL;
    retbuff = evbuffer_new();
    if(retbuff == NULL)
    {
        printf("====line:%d,%s\n",__LINE__,"retbuff is null.");
        return;
    }
    evbuffer_add_printf(retbuff,"Receive post request,Thamks for the request!");
    evhttp_send_reply(req,HTTP_OK,"Client",retbuff);
    evbuffer_free(retbuff);
}

int main()
{
    struct evhttp *http_server = NULL;
    short http_port = 8081;
    char *http_addr = "0.0.0.0";

    //初始化
    event_init();
    //启动http服务端
    http_server = evhttp_start(http_addr,http_port);
    if(http_server == NULL)
    {
        printf("====line:%d,%s\n",__LINE__,"http server start failed.");
        return -1;
    }

    //设置请求超时时间(s)
    evhttp_set_timeout(http_server,5);
    //设置事件处理函数,evhttp_set_cb针对每一个事件(请求)注册一个处理函数,
    //区别于evhttp_set_gencb函数,是对所有请求设置一个统一的处理函数
    evhttp_set_cb(http_server,"/me/testpost",http_handler_testpost_msg,NULL);
    evhttp_set_cb(http_server,"/me/testget",http_handler_testget_msg,NULL);

    //循环监听
    event_dispatch();
    //实际上不会释放,代码不会运行到这一步
    evhttp_free(http_server);

    return 0;
}

关于代码,相应的注释已经很清楚了,这里主要说明一下编译有关的问题,编译的时候需要引入libevent库相关的头文件目录,动态库路径和名称。

编译命令:gcc -o http_server http_server.c -I/usr/local/include/ -L/usr/local/lib/ -levent

-I/usr/local/include/ :头文件路劲

-L/usr/local/lib/ :动态库路劲

-levent :动态库名称(libevent.so)

附get请求和post请求及处理结果,使用postman工具模拟发送http命令,

下载地址:postman下载地址下载后一键安装即可用。

客户端get请求及响应:

客户端get请求及响应

客户端post请求及响应:

客户端post请求及响应

服务端处理get请求:

服务端处理get请求

服务端处理post请求:

服务端处理post请求

文章最后发布于: 2018-08-31 14:23:26

相关阅读

Http perkperkperk

关于HTTP协议,一篇就够了 HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(W

HTTP协议/RTP/RTSP协议/RTMP协议的区别

RTSP、 RTMP、HTTP的共同点、区别共同点:1:RTSP RTMP HTTP都是在应用应用层。2: 理论上RTSP RTMPHTTP都可以做直播和点播,但一般做直

Java爬虫系列:使用HttpClient抓取页面HTML

今天就来介绍下抓取html内容的工具:HttpClient。 围绕下面几个点展开: 1.什么是HttpClient 2.HttpClient入门实例 3.复杂应用 4.结

http请求报400错误的原因分析

HTTP 400 错误 - 请求无效 (Bad request) 在ajax请求后台数据时有时会报 HTTP 400 错误 - 请求无效 (Bad request);出现这个请求

WinHttp的简单使用用例

// WinHttp.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <string>#include <iostream>#include <windows.

分享到:

栏目导航

推荐阅读

热门阅读