/* 原创文章 转载请附上原链接: https://www.cnblogs.com/jiujue/p/10707153.html */
自己实现的如有缺漏欢迎提出
直接代码 一切皆在代码中
首先是 主函数文件 和 头文件
头文件:
1 #ifndef _HEAD_H_ 2 #define _HEAD_H_ 3 4 #include<stdio.h> 5 #include<string.h> 6 #include<stdlib.h> 7 #include<time.h> 8 #include<math.h> 9 #include <fcntl.h> 10 #include <ctype.h> 11 #include <dirent.h> 12 #include <unistd.h> 13 #include <event2/event.h> 14 #include <event2/listener.h> 15 #include <event2/bufferevent.h> 16 #include <sys/socket.h> 17 #include <sys/types.h> 18 #include <sys/stat.h> 19 #include <arpa/inet.h> 20 21 int judge_type_dir_or_nondir(const char* name); 22 int send_dir_asheml(struct bufferevent *bufev, char *dirname, void *arg); 23 struct evconnlistener* libev_start(struct event_base*base, const char* Ip,int Port); 24 int send_html_head(struct bufferevent* bufev, int stat_no, const char* stat_desc, char* type); 25 const char *get_file_type(const char *name); 26 int send_file(struct bufferevent *bufev,char* file); 27 28 #endif
主函数文件:
1 // File Name: main.c 2 // Author: jiujue 3 // Created Time: 2019年04月05日 星期五 19时54分48秒 4 5 #include "head.h" 6 7 int main(int argc, const char* argv[]) 8 { 9 if(argc < 4) 10 { 11 printf("argument not enough\neg:./app Ip Port sourceDir\n"); 12 exit(1); 13 } 14 15 16 int ret = chdir(argv[3]); 17 if(-1 == ret) 18 { 19 perror("chdir error"); 20 exit(1); 21 } 22 else 23 { 24 printf("chdir successful,to -> %s\n",argv[3]); 25 } 26 27 struct event_base* base = event_base_new(); 28 29 struct evconnlistener* listen = libev_start(base,argv[1],atoi(argv[2])); 30 31 event_base_dispatch(base); 32 33 evconnlistener_free(listen); 34 event_base_free(base); 35 36 return 0 ; 37 }
接下来是 调用libevent框架了 (重头戏来了 注意回调的设置哦):
1 // File Name: _libev.c 2 // Author: jiujue 3 // Created Time: 2019年04月05日 星期五 19时54分08秒 4 #include "head.h" 5 #define PATH_OF_404_ "404.html" 6 7 void read_cb(struct bufferevent* bufev, void* arg) 8 { 9 printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<,,Happen read_cb\n"); 10 char buf[1024] = {0}, method[12] = {0}, path[1024] = {0}, protocol[12] = {0}; 11 bufferevent_read(bufev,buf,sizeof(buf)); 12 //printf("-----------------------recv http request :%s\n",buf); 13 sscanf(buf,"%s %s %s",method,path,protocol); 14 char *file = path+1; 15 if(0 == (strcmp(path,"/") ) ) 16 { 17 file = (char*)"./"; 18 } 19 20 int isFile = judge_type_dir_or_nondir(file); 21 printf("fffffffff is %d \n",isFile); 22 if(0 == isFile) 23 {//is palin file 24 printf("send file <name>>%s\n",file); 25 send_file(bufev,file); 26 } 27 if(isFile == 1){//is dir 28 printf("send dir <name>>%s\n",file); 29 send_html_head(bufev,200,"OK",(char*)"text/html"); 30 send_dir_asheml(bufev,file,NULL); 31 } 32 else if(-1 == isFile) 33 {//is not found file or directory 34 printf("send 404 <