#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "evhttp.h"
#include "event.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"
int main()
{
struct evhttp *http_server = NULL;
short http_port = ;
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 -;
} //设置请求超时时间(s)
evhttp_set_timeout(http_server, );
//设置事件处理函数,evhttp_set_cb针对每一个事件(请求)注册一个处理函数,
//区别于evhttp_set_gencb函数,是对所有请求设置一个统一的处理函数
evhttp_set_cb(http_server, "/post", http_handler_testpost_msg, NULL);
evhttp_set_cb(http_server, "/get", http_handler_testget_msg, NULL); //循环监听
event_dispatch();
//实际上不会释放,代码不会运行到这一步
evhttp_free(http_server); return ;
}
//解析post请求数据
void get_post_message(char *buf, struct evhttp_request *req)
{
size_t post_size = ; post_size = evbuffer_get_length(req->input_buffer);//获取数据长度
printf("====line:%d,post len:%d\n", __LINE__, post_size);
if (post_size <= )
{
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, -), copy_len);
buf[post_size] = '\0';
printf("====line:%d,post msg:%s\n", __LINE__, buf);
}
} //处理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.\n");
return;
} char buf[BUF_MAX] = { };
get_post_message(buf, req);//获取请求数据,一般是json格式的数据
if (buf == NULL)
{
printf("====line:%d,%s\n", __LINE__, "get_post_message return null.\n");
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.\n");
return;
}
evbuffer_add_printf(retbuff, "Receive post request,Thamks for the request!\n");
evhttp_send_reply(req, HTTP_OK, "Client", retbuff);
evbuffer_free(retbuff);
}
//解析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.\n");
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.\n");
return;
} char *sign = NULL;
char *data = NULL;
struct evkeyvalq sign_params = { };
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.\n");
}
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.\n");
}
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);
}

最新文章

  1. 移动端Web开发如何处理横竖屏
  2. ubuntu快捷键设置,查看系统
  3. sql proc触发异常处理回滚
  4. io外挂
  5. opencart添加任意页面到导航栏(修改一级菜单)
  6. Django学习-23-ModelForm
  7. [JLOI2011]飞行路线
  8. 猴子 JDFZ模拟赛
  9. HTML HTML5 新特性
  10. 【原创】驱动枚举之QueryServiceStatus
  11. UGUI自定义组件之Image根据Text大小自动调整
  12. 「SCOI2016」美味 解题报告
  13. 说说GIL
  14. Hibernate小解惑.
  15. PAT 甲级 1135 Is It A Red-Black Tree
  16. 帝国cms建站总结-(分页)
  17. 树莓派 CSI摄像头 No data received from sensor. Check all connections, including the Sunny one on the camera board
  18. 四、API使用参考
  19. jquery选择器玩得不6啊,只能慢慢写判断了,唉..........................
  20. GitHub+Hexo 搭建个人网站

热门文章

  1. mybatis-geneator
  2. VS Code中无法识别npm命令
  3. Mysql - 关于relay_log_recovery参数的测试
  4. 转:查看oracle数据库允许的最大连接数和当前连接数
  5. Unity-遇到的问题小总结
  6. 09.Django基础七之Ajax
  7. 关于react-router最新版本的使用
  8. linux mint 17编译android 2.3.1错误记录
  9. 04-numpy读取本地数据和索引
  10. SpringBootSecurity学习(16)前后端分离版之 OAuth2.0 加密配置