设为首页 加入收藏

TOP

Linux 用libevent实现的简单http服务器(二)
2019-07-09 20:11:19 】 浏览:362
Tags:Linux libevent 实现 简单 http 服务器
, ".mp3") == 0) return "audio/mpeg"; if (strcmp(dot, ".ogg") == 0) return "application/ogg"; if (strcmp(dot, ".pac") == 0) return "application/x-ns-proxy-autoconfig"; return "text/plain; charset=utf-8"; } // 16进制数转化为10进制 int hexit(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return c - 'a' + 10; if (c >= 'A' && c <= 'F') return c - 'A' + 10; return 0; } /* * 这里的内容是处理%20之类的东西!是"解码"过程。 * %20 URL编码中的‘ ’(space) * %21 '!' %22 '"' %23 '#' %24 '$' * %25 '%' %26 '&' %27 ''' %28 '('...... * 相关知识html中的‘ ’(space)是&nbsp */ void encode_str(char* to, int tosize, const char* from) { int tolen; for (tolen = 0; *from != '\0' && tolen + 4 < tosize; ++from) { if (isalnum(*from) || strchr("/_.-~", *from) != (char*)0) { *to = *from; ++to; ++tolen; } else { sprintf(to, "%%%02x", (int) *from & 0xff); to += 3; tolen += 3; } } *to = '\0'; } void decode_str(char *to, char *from) { for ( ; *from != '\0'; ++to, ++from ) { if (from[0] == '%' && isxdigit(from[1]) && isxdigit(from[2])) { *to = hexit(from[1])*16 + hexit(from[2]); from += 2; } else { *to = *from; } } *to = '\0'; } void listencb(struct evconnlistener* listener, evutil_socket_t fd, struct sockaddr* cli, int len, void* ptr){ struct event_base* base = ptr; struct bufferevent* bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE); //set callback bufferevent_setcb(bev, readcb, writecb, eventcb, NULL); bufferevent_enable(bev, EV_READ | EV_WRITE); } // 断开连接的函数 void disconnect(struct bufferevent* bev){ bufferevent_disable(bev, EV_READ | EV_WRITE); } void send_respond_head(struct bufferevent* bev, int no, const char* desp, const char* type, long len){ char buf[1024] = {0}; // 状态行 sprintf(buf, "http/1.1 %d %s\r\n", no, desp); bufferevent_write(bev, buf, strlen(buf)); // 消息报头 memset(buf, 0, sizeof buf); sprintf(buf, "Content-Type:%s\r\n", type); sprintf(buf+strlen(buf), "Content-Length:%ld\r\n", len); bufferevent_write(bev, buf, strlen(buf)); // 空行 bufferevent_write(bev, "\r\n", 2); } void send_file(struct bufferevent* bev, const char* path){ int fd = open(path, O_RDONLY); if(fd == -1){ //todo 404 perror("open err"); exit(1); } char buf[4096]; int ret = 0; while((ret = read(fd, buf, sizeof buf)) > 0){ bufferevent_write(bev, buf, ret); } if(ret == -1){ perror("read err"); exit(1); } close(fd); } void send_dir(struct bufferevent* bev, const char* path){ char file[1024] = {0}; char buf[4096] = {0}; sprintf(buf, "<html><head><title>目录名: %s</title></head>", path); sprintf(buf+strlen(buf), "<body><h1>当前目录: %s</h1><table>", path); #if 1 DIR* dir = opendir(path); if(dir == NULL){ perror("opendir error"); exit(1); } // 读目录 char enstr[1024] = {0}; struct dirent* ptr = NULL; while((ptr = readdir(dir)) != NULL){ char* name = ptr->d_name; // 拼接文件的完整路径 sprintf(file, "%s/%s", path, name); encode_str(enstr, sizeof(enstr), name); struct stat st; int ret = stat(file, &st); if(ret == -1){ perror("stat err"); exit(1); } //
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇leadcode的Hot100系列--62. 不同.. 下一篇Linux 用tcp实现的简单http服务器

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目