最近在工作当中接触到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