设为首页 加入收藏

TOP

Linux 用libevent实现的简单http服务器(三)
2019-07-09 20:11:19 】 浏览:370
Tags:Linux libevent 实现 简单 http 服务器
如果是文件 if(S_ISREG(st.st_mode)){ sprintf(buf+strlen(buf), "<tr><td><a href=\"%s\">%s</a></td><td>%ld</td></tr>", enstr, name, (long)st.st_size); } // 如果是目录 else if(S_ISDIR(st.st_mode)){ sprintf(buf+strlen(buf), "<tr><td><a href=\"%s/\">%s/</a></td><td>%ld</td></tr>", enstr, name, (long)st.st_size); } bufferevent_write(bev, buf, strlen(buf)); memset(buf, 0, sizeof(buf)); } sprintf(buf+strlen(buf), "</table></body></html>"); bufferevent_write(bev, buf, strlen(buf)); closedir(dir); #endif } void http_get_request(const char* line, struct bufferevent* bev){ char method[12] = {0}; char res[1024] = {0}; sscanf(line, "%[^ ] %[^ ]", method, res); // 转码 将不能识别的中文乱码 - > 中文 // 解码 %23 %34 %5f char destr[1024]; decode_str(destr, res); // 处理path /xx // 去掉path中的/ char* file = destr + 1; char fbuf[1024]; if(strcmp(res, "/") == 0){ file = "./"; //getcwd(fbuf, sizeof(fbuf)); //printf("root:[%s]\n", fbuf); // file = fbuf; } struct stat st; printf("file:[%s]\n", file); int ret = stat(file, &st); if(ret == -1){ //todo 404 perror("stat error"); exit(1); } if(S_ISDIR(st.st_mode)){ //dir // 发送头信息 send_respond_head(bev, 200, "OK", get_file_type(".html"), 100000); send_dir(bev, file); } else if(S_ISREG(st.st_mode)){ //reg file send_respond_head(bev, 200, "OK", get_file_type(file), st.st_size); send_file(bev, file); } } void readcb (struct bufferevent* bev,void* ctx){ struct evbuffer* evbuf = bufferevent_get_input(bev); char* line; size_t len; line = evbuffer_readln(evbuf, &len, EVBUFFER_EOL_CRLF); /* char* tmp = line; while(1){ tmp = evbuffer_readln(evbuf, &len, EVBUFFER_EOL_CRLF); if(tmp) break; printf("%s\n", tmp); } printf("line:[%s]", line); */ if(strncasecmp("get", line, 3) == 0){ http_get_request(line, bev); //disconnect(bev); } } void writecb (struct bufferevent* bev,void* ctx){ } void eventcb (struct bufferevent* bev,short what,void* ctx){ }

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇leadcode的Hot100系列--62. 不同.. 下一篇Linux 用tcp实现的简单http服务器

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目