资源简介
http服务端, 解析客户端上传的资源类型,并在本地保存相应类型及名称的文件,
代码片段和文件信息
#include “main.h“
int init_win_socket();
void generic_handler(struct evhttp_request *req void *arg);
void http_handler(struct evhttp_request *req void *arg);
std::ofstream outfile;
int main(int argc char **argv)
{
#ifdef WIN32
init_win_socket();
#endif
char *http_addr = “127.0.0.1“;
const int PORT = 9995;
//create a event_base
struct event_base *base = event_base_new();
if (!base)
{
fprintf(stderr “Could not initialize libevent!\n“);
return 1;
}
struct evhttp *http_server = evhttp_new(base);
if (!http_server)
{
return -1;
}
int ret = evhttp_bind_socket(http_server http_addr PORT);
if (ret != 0)
{
return -1;
}
evhttp_set_gencb(http_server http_handler NULL);
printf(“http server start OK! \n“);
event_base_dispatch(base);
evhttp_free(http_server);
event_base_free(base);
WSACleanup();
system(“pause“);
return 0;
}
int init_win_socket()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2 2) &wsaData) != 0)
{
return -1;
}
return 0;
}
void http_handler(struct evhttp_request *req void *arg)
{
struct evbuffer *buf;
buf = evbuffer_new();
// 分析请求
char *decode_uri = _strdup((char*)evhttp_request_get_uri(req));
struct evkeyvalq http_query;
evhttp_parse_query(decode_uri &http_query);
free(decode_uri);
char *uri = _strdup((char*)evhttp_request_uri(req));
char GetWayURL[100] = { 0 };
const int MSG_DATA_SIZE = 15000;
char body_data[MSG_DATA_SIZE] = {0};
int body_datalen = 0;
// ?
if (memcmp(uri GetWayURL strlen(GetWayURL) != 0))
{
free(uri);
buf = evbuffer_new();
evbuffer_add_printf(buf “%s“ “HTTP_BADREQUEST“);
evhttp_send_reply(req HTTP_BADREQUEST “OK“ buf);
evbuffer_free(buf);
return;
}
free(uri);
//get data from post
memset(body_data 0 sizeof(body_data));
size_t len_of_file_to_recv = 0;
bool is_first = true;
size_t loc = -1;
std::string body_str;
while (len_of_file_to_recv < req->body_size)
{
body_datalen = evbuffer_remove(req->input_buffer body_data sizeof(body_data));
if (body_datalen > MSG_DATA_SIZE)
{
buf = evbuffer_new();
evbuffer_add_printf(buf “%s“ “HTTP_DATALENTOLANG“);
// output
evhttp_send_reply(req HTTP_BADREQUEST “OK“ buf);
// mem release
evbuffer_free(buf);
return;
}
else if (is_first)
{
std::string buf_in = body_data;
size_t loc = buf_in.find(“\r\n\r\n“);
size_t loc_filename = -1;
//get file description and extract the file name
//and cat precise time to the extracted file name to result in a unique file name
char *filename = NULL;
filename = new char[loc+1];
memcpy(filename body_data loc);
filename[loc] = 0;
buf_in = filename;
memset(filename 0 loc+1);
loc_filename = buf_in.find(“filename“);
std::string file_name_front;
if (-1 != loc_filename)
{
loc_filename += strlen(“filename“);
while (l
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2016-12-26 15:37 Debug\
文件 1020928 2016-12-26 15:37 Debug\libeventHttpServer.exe
文件 4746240 2016-12-26 15:37 Debug\libeventHttpServer.pdb
目录 0 2016-12-15 14:09 include\
目录 0 2016-12-23 09:22 include\event2\
文件 30747 2016-12-15 14:24 include\event2\buffer.h
文件 28644 2016-12-17 13:40 include\event2\bufferevent.h
文件 4448 2012-02-11 06:17 include\event2\bufferevent_compat.h
文件 3832 2012-02-11 06:17 include\event2\bufferevent_ssl.h
文件 4138 2016-12-15 14:51 include\event2\bufferevent_struct.h
文件 4586 2012-02-11 06:17 include\event2\buffer_compat.h
文件 24648 2015-01-05 21:42 include\event2\dns.h
文件 12131 2012-02-11 06:17 include\event2\dns_compat.h
文件 2572 2012-02-11 06:17 include\event2\dns_struct.h
文件 45203 2016-12-15 14:24 include\event2\event.h
文件 7405 2016-12-19 10:10 include\event2\event_compat.h
文件 4050 2016-12-15 14:52 include\event2\event_struct.h
文件 32393 2016-12-19 11:39 include\event2\http.h
文件 3139 2016-12-19 10:09 include\event2\http_compat.h
文件 4176 2016-12-19 14:38 include\event2\http_struct.h
文件 2614 2016-12-23 09:22 include\event2\keyvalq_struct.h
文件 5961 2016-12-17 13:55 include\event2\listener.h
文件 20984 2012-02-11 06:17 include\event2\rpc.h
文件 2327 2012-02-11 06:17 include\event2\rpc_compat.h
文件 3211 2012-02-11 06:17 include\event2\rpc_struct.h
文件 4438 2012-02-11 06:17 include\event2\tag.h
文件 2117 2012-02-11 06:17 include\event2\tag_compat.h
文件 9475 2016-12-15 14:53 include\event2\thread.h
文件 22598 2016-12-15 14:28 include\event2\util.h
文件 1201 2012-02-11 06:24 include\Makefile.am
文件 18917 2015-01-05 22:31 include\Makefile.in
............此处省略34个文件信息
- 上一篇:Monster.unitypackage
- 下一篇:统计自然语言处理 宗成庆 第2版
相关资源
- 《图解HTTP》完整彩色文字编排版本超
- VS2015 NuGet 使用libcurl工程
- G-140W-C光猫联通固件软件版本:3FE46
- HTTP-2基础教程[美]StephenLudin.zip
- QT5中使用QHttpQFtp.rar
- HTTP/HTTPS协议权威指南
- OkHttpClientManager
- HttpWatch Pro 10.0.62.0 带序列号全网最新
- 美萍美发管理系统2018V3最新版
- 计算机网络教程:自顶向下方法 pdf
- 海康平台Http接口帮助文档+
- 微赞最新内核2.0.8纯净版安装包https
- httpwatch汉化破解版
- 《图解HTTP》+《图解TCP/IP》pdf版带书签
- 《HTTP权威指南》高清中文版.pdf
- HttpWatch Pro v8.5.27 With License.lic
- M6315技术手册
- WAS9 集群 & IBM HTTP Server 9 部署文档
- slam十四讲第13章 单目稠密重建数据集
- Https权威指南(高清带书签) + 图解
- 并发编程笔记(httpswww.bilibili.comvide
- HTTP权威指南(中文高清原版).pdf版
- httpwatch pro 10.X 专业版含正版许可证
- libSkyChaserHttp_src.zip
- HTTP权威指南(高清+非扫描+带目录)
- 《图解HTTP》中文版PDF
- OKHttp网络请求封装
- adams文件学习资料
-
ijkpla
yer全格式支持HTTPS支持so包 - qt 线程池实现多线程
评论
共有 条评论