#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);
}

Libevent::evhttp服务器的更多相关文章

  1. Libevent::evhttp服务器下载

    void http_handler_Get_Download(struct evhttp_request *req, void *arg) { if (req == NULL) { return; } ...

  2. Libevent库基础(2)

    带缓冲区的事件 bufferevent #include <event2/bufferevent.h> read/write 两个缓冲. 借助 队列. 创建.销毁bufferevent: ...

  3. Ubuntu14.04+Beanstalkd1.9最佳实践

    目录 [TOC] 1.基本概念 1.1.什么是Beanstalkd?   Beanstalkd 是一个轻量级消息中间件,它最大特点是将自己定位为基于管道 (tube) 和任务 (job) 的工作队列. ...

  4. Python 之路:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy

    一.Memcached Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负债.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速 ...

  5. Thrift 基础(C++ rpc )

    一.thrift简介 thrift是Facebook开源的一套rpc框架,目前被许多公司使用 我理解的特点 使用IDL语言生成多语言的实现代码,程序员只需要实现自己的业务逻辑 支持序列化和反序列化操作 ...

  6. Facebook如何管理150亿张照片

    Facebook 的照片分享很受欢迎,迄今,Facebook 用户已经上传了150亿张照片,加上缩略图,总容量超过1.5PB,而每周新增的照片为2亿2000万张,约25TB,高峰期,Facebook ...

  7. libevent和libcurl实现http和https服务器 cJSON使用

    前言 libevent和libcurl都是功能强大的开源库:libevent主要实现服务器,包含了select.epoll等高并发的实现:libcurl实现了curl命令的API封装,主要作为客户端. ...

  8. Windows 上静态编译 Libevent 2.0.10 并实现一个简单 HTTP 服务器(无数截图)

    [文章作者:张宴 本文版本:v1.0 最后修改:2011.03.30 转载请注明原文链接:http://blog.s135.com/libevent_windows/] 本文介绍了如何在 Window ...

  9. Windows 上静态编译 Libevent 2.0.10 并实现一个简单 HTTP 服务器(图文并茂,还有实例下载)

    [文章作者:张宴 本文版本:v1.0 最后修改:2011.03.30 转载请注明原文链接:http://blog.s135.com/libevent_windows/] 本文介绍了如何在 Window ...

随机推荐

  1. 小斌之SpringCloud 开发某某交易所

    最近一个月都在弄某某交易所,让之前学的东西能够用上,在这里分享一下收货的东西吧 #### 简介 系统是进行了二次开发,用的是ZTuo开源框架第一个版本,节省了很多时间(坑也挺多,哈哈哈),文章结尾贴源 ...

  2. Lucene 全文检索入门

    博客地址:http://www.moonxy.com 一.前言 Lucene 是 apache 软件基金会的一个子项目,由 Doug Cutting 开发,是一个开放源代码的全文检索引擎工具包,但它不 ...

  3. [VB.NET Tips]字符串分隔

    在实际应用中,很多场景下都需要分隔字符串,如解析CSV文件等. 一般我们使用split方法来按照指定的分隔符来进行分隔字符串获得一个数组. Split方法的签名是: Split(ParamArray ...

  4. JavaScript之深入对象(一)

    在之前的<JavaScript对象基础>中,我们大概了解了对象的创建和使用,知道对象可以使用构造函数和字面量方式创建.那么今天,我们就一起来深入了解一下JavaScript中的构造函数以及 ...

  5. 【linux】【jenkins】自动化运维二 安装插件

    gitlab安装教程参考:https://www.cnblogs.com/jxd283465/p/11525629.html 1.Maven Integration Plugins Jenkins安装 ...

  6. Ubuntu18.04 显卡驱动+Cuda安装踩坑记录 以及Ubuntu虚拟内存的添加

    前几天买了张亮机卡,终于把主显卡成功直连到Unraid OS的虚拟机上了.然后就开始安装ubuntu系统开始配置环境,遇到了不少坑,特此记录. gcc版本问题 在安装显卡驱动的时候,不要修改gcc版本 ...

  7. golang1.13中重要的新特新

    本文索引 语言变化 数字字面量 越界索引报错的完善 工具链改进 GOPROXY GOSUMDB GOPRIVATE 标准库的新功能 判断变量是否为0值 错误处理的革新 Unwrap Is As gol ...

  8. Android Studio [真机测试/开发者模式]

    一.手机设置 首先根据自己的手机型号百度打开开发者模式, 我的是vivo,设置--->更多设置-->关于手机-->软件版本号连续点击会提示开启开发者模式. 并在开发者选项里打开USB ...

  9. 基于Spark的电影推荐系统(实战简介)

    写在前面 一直不知道这个专栏该如何开始写,思来想去,还是暂时把自己对这个项目的一些想法 和大家分享 的形式来展现.有什么问题,欢迎大家一起留言讨论. 这个项目的源代码是在https://github. ...

  10. Kurskal算法

    Kruskal算法是以边为主要关注对象的最小生成树算法,是最小生成树最佳的算法实现. 其时间复杂度为O(ElogE)(E为边的数量),而Prime算法采用邻接矩阵的方法是O(V^2)(V为顶点数量). ...