设为首页 加入收藏

TOP

基于 libevent 开源框架实现的 web 服务器(三)
2019-05-23 14:37:26 】 浏览:252
Tags:基于 libevent 开源 框架 实现 web 服务器
g)
10 { 11 printf("******************send_dir name is %s\n",dirname); 12 13 char* buf_dir_html = (char *)malloc(MAXFORGTML_); 14 struct dirent **ptr; 15 int dir_total_num = scandir(dirname,&ptr,NULL,alphasort); 16 17 //html head 18 sprintf(buf_dir_html,"<!doctype HTML>\ 19 <html>\ 20 <head>\ 21 <title>Curent dir:%s</title>\ 22 </head>\ 23 <body>\ 24 <h1>Curretn Dir Content:%s </h1>\ 25 <table>\ 26 <tr>\ 27 <td>Name</td><td>Size</td><td>Type</td>\ 28 </tr>",dirname,dirname); 29 bufferevent_write(bufev,buf_dir_html,strlen(buf_dir_html)); 30 31 for(int i=0;i<dir_total_num;++i) 32 { 33 char buf_current_name[1024] = {0}; 34 if( 0 == strcmp(dirname,"./")) 35 { 36 sprintf(buf_current_name,"%s%s",dirname,ptr[i]->d_name); 37 } 38 else 39 { 40 sprintf(buf_current_name,"%s/%s",dirname,ptr[i]->d_name); 41 } 42 printf("++++++++++++++++++++send cur dir <name>>%s\n",buf_current_name); 43 struct stat st; 44 memset(&st,0,sizeof(st)); 45 stat(buf_current_name,&st); 46 47 sprintf(buf_dir_html, 48 "<tr>\ 49 <td><a href=\"%s\">%s</a></td>\ 50 <td>%ld</td>\ 51 <td>%s</td>\ 52 </tr>", 53 buf_current_name, 54 ptr[i]->d_name,st.st_size, 55 judge_type_dir_or_nondir(buf_current_name)!= 0?"dir":"plain file"); 56 bufferevent_write(bufev,buf_dir_html,strlen(buf_dir_html)); 57 memset((char*)buf_dir_html,0,sizeof(buf_dir_html)); 58 } 59 60 //html end 61 sprintf(buf_dir_html, 62 "</table>\ 63 </body>\ 64 </html>"); 65 bufferevent_write(bufev,buf_dir_html,strlen(buf_dir_html)); 66 bufferevent_write(bufev,"\r\n",2); 67 68 free(buf_dir_html); 69 return 0; 70 } View Code

 

最后就是一些小函数了: 判断文件类型 和是否为目录:

  判断文件类型(这里如果出问题 打开图片等时会出现问题):

 1 // File Name: _get_file_type.c
 2 // Author: jiujue
 3 // Created Time: 2019年04月06日 星期六 19时14分07秒
 4 
 5 
 6 #include "head.h"
 7 
 8 const char *get_file_type(const char *name)
 9 {
10     char* dot;
11 
12     
13     dot = strrchr(name, '.');   
14     if (dot == NULL)
15         return "text/plain; charset=utf-8";
16     if (strcmp(dot, ".html") == 0 || strcmp(dot, ".htm") == 0)
17         return "text/html; charset=utf-8";
18     if (strcmp(dot, ".jpg") == 0 || strcmp(dot, ".jpeg") == 0)
19         return "image/jpeg";
20     if (strcmp(dot, ".gif") == 0)
21         return "image/gif";
22     if (strcmp(dot, ".png") == 0)
23         return "image/png";
24     if (strcmp(dot, ".css") == 0)
25         return "text/css";
26     if (strcmp(dot, ".au") == 0)
27         return "audio/basic";
28     if (strcmp( dot, ".wav" ) == 0)
29         return "audio/wav";
30     if (strcmp(dot, ".avi") == 0)
31         return "video/x-msvideo";
32     if (strcmp(dot, ".mov") == 0 || strcmp(dot, ".qt") == 0)
33         return "video/quicktime";
34     if (strcmp(dot
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Devc++编程过程中的一些报错总结 下一篇小饭店预约登记管理系统

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目