基于libevent使用c语言实现http服务端的基础框架

http服务端实现

libevent的开源库自行在github下载,然后根据交叉编译工具进行编译

编译参数仅供参考,如果需要开启ssl,自行查阅资料如何加上openssl一起编译编译,本文只针对简单的http server框架的实现提供代码
./configure --prefix=$(pwd)/…/output --host=aarch64-linux-gnu --disable-openssl CFLAGS=-fPIC
make clean && make && make install


#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>

#include <event.h>
#include <event2/event.h>
#include <event2/http.h>
#include <event2/http_struct.h>
#include <event2/http_compat.h>
#include <event2/buffer_compat.h>
#include <event2/util.h>
#include <event2/event_compat.h>
#include <event2/buffer.h>
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <event2/http.h>
#include <event2/http_struct.h>
//#include "libevent_http.h"

#define COMMON_SUCCESS_MSG 	"{\"msg\":\"success\", \"code\":200}"
#define COMMON_TEST_MSG 	"{\"msg\":\"This is a test!\", \"code\":200}"
#define COMMON_FAIL_MSG    	"{\"msg\":\"bad requst\", \"code\":500}"

void httpServer_CommonDel(struct evhttp_request *req, void *arg) 
{
    int RecvDataLen = 0;
    char lengthBuffer[16]={0};
	char szData[100] = {0};
    char *pcRecvData = NULL;

    /* 只支持POST GET两种请求 */
    if ((req->type != EVHTTP_REQ_POST) && (req->type != EVHTTP_REQ_GET))
    {
        printf("not support method[%d]\n", req->type);
        return;
    }

    /*获取POST方法的数据*/
    RecvDataLen = EVBUFFER_LENGTH(req->input_buffer);
    pcRecvData = (char *) EVBUFFER_DATA(req->input_buffer);
    
    printf("%s get post_data, len =%d   uri:%s\n",__FUNCTION__, RecvDataLen, req->uri);
    printf("RCV:\n%s\n",pcRecvData);
    
    /* 自行添加对应uri的处理 */
    if (NULL != strstr(req->uri, "/test"))
    {
        strncpy(szData, COMMON_TEST_MSG, sizeof(szData) - 1);
    }
    else
    {
        strncpy(szData, COMMON_SUCCESS_MSG, sizeof(szData) - 1);
    }
    
    //HTTP header
    evutil_snprintf(lengthBuffer, sizeof(lengthBuffer) - 1, "%lu", strlen(szData));
    evhttp_add_header(req->output_headers, "Content-Type", "application/json; charset=UTF-8");
    evhttp_add_header(req->output_headers, "Content-Length",lengthBuffer);
    evhttp_add_header(req->output_headers, "Connection", "close");

    struct evbuffer *buf = evhttp_request_get_output_buffer(req);
    evbuffer_add(buf, szData, strlen(szData));
    
    //将封装好的evbuffer 发送给客户端
    evhttp_send_reply(req, 200, "OK", buf);
}

int main(int argc, char **argv)
{
    struct evhttp *pstHttpEV = NULL;
    //初始化event_init
    event_init();

    pstHttpEV = evhttp_start("0.0.0.0", 7689);

    evhttp_set_timeout(pstHttpEV, 30);

    /*通用处理回调函数指定*/
    evhttp_set_gencb(pstHttpEV, httpServer_CommonDel, NULL);

    //循环处理events
    event_dispatch();

    evhttp_free(pstHttpEV);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值