基于libevent库实现的http server示例

最近在工作当中接触到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
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值