• Linux网络编程11——简单的web服务器


    学习视频链接

    02-web大练习的概述_bilibili_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1iJ411S7UA/?p=132&spm_id_from=pageDriver&vd_source=0471cde1c644648fafd07b54e303c905

    目录

    一、项目展示

    二、HTTP 协议基础

    2.1 HTTP协议基础。

    2.2 请求消息(Request)

    2.3 响应消息 (Response)

    三、简单的代码

    3.1 代码

    3.2 后续需要增加的内容

    3.3 获取需要的数据

    3.4 错误处理函数

    3.5 正则表达式

    3.6 判断文件是否存在

    3.7 应答回复客户端

    3.8 文件类型区分

             3.9 细节处理

    3.10 文件夹处理

    3.11 汉字字符编码和解码 


    学习目标:实现一个简单的 web 服务器 myhttpd 能够给浏览器提供服务,供用户借助浏览器访问服务器主机中的文件

    一、项目展示

    启动服务器

    访问路径

    访问文件

    输入了错误的地址,访问不到需要的文件,就会展示错误页面

    二、HTTP 协议基础

    2.1 HTTP协议基础。

    HTTP,超文本传输协议 (HyperText Transfer Protocol)。互联网应用最为广泛的一种网络应用层协议。它可以减少网络传输,使浏览器更加高效。通常 HTTP 消息包括客户机向服务器的请求消息和服务器向客户机的响应消息

    2.2 请求消息(Request)

    浏览器 -> 发给 -> 服务器。主旨内容包含 4 部分:

    请求行:说明请求类型,要访问的资源,以及使用的 http 版本

    请求头:说明服务器要使用的附加信息

    空行:必须!即使没有请求数据

    请求数据:也叫主体,可以添加任意的其他数据

    2.3 响应消息 (Response)

    服务器 -> 发给 -> 浏览器。主旨内容包含 4 部分:

    状态行:包括 http 协议版本号,状态码,状态信息

    消息报头:说明客户端要使用的一些附加信息

    空行:必须!

    响应正文:服务器返回给客户端的文本信息

    三、简单的代码

    3.1 代码

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #define MAXSIZE 2048
    12. int init_listen_fd(int port, int epfd)
    13. {
    14. // 创建监听的套接字 lfd
    15. int lfd = socket(AF_INET, SOCK_STREAM, 0);
    16. if (lfd == -1) {
    17. perror("socket error");
    18. exit(1);
    19. }
    20. // 创建服务器地址结构 IP+port
    21. struct sockaddr_in srv_addr;
    22. bzero(&srv_addr, sizeof(srv_addr));
    23. srv_addr.sin_family = AF_INET;
    24. srv_addr.sin_port = htons(port);
    25. srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    26. // 端口复用
    27. int opt = 1;
    28. setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
    29. // 给 lfd 绑定地址结构
    30. int ret = bind(lfd, (struct sockaddr*)&srv_addr, sizeof(srv_addr));
    31. if (ret == -1) {
    32. perror("bind error");
    33. exit(1);
    34. }
    35. // 设置监听上限
    36. ret = listen(lfd, 128);
    37. if (ret == -1) {
    38. perror("listen error");
    39. exit(1);
    40. }
    41. // lfd 添加到 epoll 树上
    42. struct epoll_event ev;
    43. ev.events = EPOLLIN;
    44. ev.data.fd = lfd;
    45. ret = epoll_ctl(epfd, EPOLL_CTL_ADD, lfd, &ev);
    46. if (ret == -1) {
    47. perror("epoll_ctl add lfd error");
    48. exit(1);
    49. }
    50. return lfd;
    51. }
    52. void do_accept(int lfd, int epfd)
    53. {
    54. struct sockaddr_in clt_addr;
    55. socklen_t clt_addr_len = sizeof(clt_addr);
    56. int cfd = accept(lfd, (struct sockaddr*)&clt_addr, &clt_addr_len);
    57. if (cfd == -1) {
    58. perror("accept error");
    59. exit(1);
    60. }
    61. // 打印客户端IP+port
    62. char client_ip[64] = {0};
    63. printf("New Client IP: %s, Port: %d, cfd = %d\n",
    64. inet_ntop(AF_INET, &clt_addr.sin_addr.s_addr, client_ip, sizeof(client_ip)),
    65. ntohs(clt_addr.sin_port), cfd);
    66. // 设置 cfd 非阻塞
    67. int flag = fcntl(cfd, F_GETFL);
    68. flag |= O_NONBLOCK;
    69. fcntl(cfd, F_SETFL, flag);
    70. // 将新节点cfd 挂到 epoll 监听树上
    71. struct epoll_event ev;
    72. ev.data.fd = cfd;
    73. // 边沿非阻塞模式
    74. ev.events = EPOLLIN | EPOLLET;
    75. int ret = epoll_ctl(epfd, EPOLL_CTL_ADD, cfd, &ev);
    76. if (ret == -1) {
    77. perror("epoll_ctl add cfd error");
    78. exit(1);
    79. }
    80. }
    81. void do_read(int cfd, int epfd)
    82. {
    83. // read cfd 小 -- 大 write 回
    84. // 读取一行http协议, 拆分, 获取 get 文件名 协议号
    85. }
    86. void epoll_run(int port)
    87. {
    88. int i = 0;
    89. struct epoll_event all_events[MAXSIZE];
    90. // 创建一个epoll监听树根
    91. int epfd = epoll_create(MAXSIZE);
    92. if (epfd == -1) {
    93. perror("epoll_create error");
    94. exit(1);
    95. }
    96. // 创建lfd,并添加至监听树
    97. int lfd = init_listen_fd(port, epfd);
    98. while (1) {
    99. // 监听节点对应事件
    100. int ret = epoll_wait(epfd, all_events, MAXSIZE, -1);
    101. if (ret == -1) {
    102. perror("epoll_wait error");
    103. exit(1);
    104. }
    105. for (i=0; i
    106. // 只处理读事件, 其他事件默认不处理
    107. struct epoll_event *pev = &all_events[i];
    108. // 不是读事件
    109. if (!(pev->events & EPOLLIN)) {
    110. continue;
    111. }
    112. if (pev->data.fd == lfd) { // 接受连接请求
    113. do_accept(lfd, epfd);
    114. } else { // 读数据
    115. do_read(pev->data.fd, epfd);
    116. }
    117. }
    118. }
    119. }
    120. int main(int argc, char *argv[])
    121. {
    122. // 命令行参数获取 端口 和 server提供的目录
    123. if (argc < 3)
    124. {
    125. printf("./server port path\n");
    126. }
    127. // 获取用户输入的端口
    128. int port = atoi(argv[1]);
    129. // 改变进程工作目录
    130. int ret = chdir(argv[2]);
    131. if (ret != 0) {
    132. perror("chdir error");
    133. exit(1);
    134. }
    135. // 启动 epoll监听
    136. epoll_run(port);
    137. return 0;
    138. }

    3.2 后续需要增加的内容

    3.3 获取需要的数据

    1. nt get_line(int cfd, char *buf, int size)
    2. {
    3. int i = 0;
    4. char c = '\0';
    5. int n;
    6. //每次读一个字节,判断合理就放入缓冲区中
    7. while ((i < size - 1) && (c != '\n')) {
    8. n = recv(cfd, &c, 1, 0);
    9. if (n > 0) {
    10. if (c == '\r') {
    11. //MSG_PEEK 使得recv以拷贝的方式从缓冲区中读取数据(否则读取之后,缓冲区中的数据就没了)
    12. //试探性的获取缓冲区中的数据量
    13. n = recv(cfd, &c, 1, MSG_PEEK);
    14. //缓冲区中有数据并且结尾是 \n ,则读取数据
    15. if ((n > 0) && (c == '\n')) {
    16. recv(cfd, &c, 1, 0);
    17. }
    18. else {
    19. c = '\n';
    20. }
    21. }
    22. buf[i] = c;
    23. i++;
    24. }
    25. else {
    26. c = '\n';
    27. }
    28. }
    29. buf[i] = '\0';
    30. //recv失败
    31. if (n == -1) {
    32. i = -1;
    33. }
    34. return i;
    35. }

    3.4 错误处理函数

    1. // 断开连接
    2. void disconnect(int cfd, int epfd)
    3. {
    4. int ret = epoll_ctl(epfd, EPOLL_CTL_DEL, cfd, NULL);
    5. if(ret != 0) {
    6. perror("epoll_ctl error");
    7. exit(1);
    8. }
    9. close(cfd);
    10. }
    11. void do_read(int cfd, int epfd)
    12. {
    13. // 读取一行http协议, 拆分, 获取 get 文件名 协议号
    14. char line[1024] = { 0 };
    15. int len = get_line(cfd, line, sizeof(line)); // 确定读出 GET /hello.c HTTP/1.1
    16. if (len == 0) {
    17. printf("服务器检测到客户端关闭\n");
    18. disconnect(cfd, epfd);
    19. }
    20. else {
    21. // 现在要按照空格分割得到 /hello.c
    22. }
    23. }

    3.5 正则表达式

    1. void do_read(int cfd, int epfd)
    2. {
    3. // 读取一行http协议, 拆分, 获取 get 文件名 协议号
    4. char line[1024] = { 0 };
    5. int len = get_line(cfd, line, sizeof(line)); // 确定读出 GET /hello.c HTTP/1.1
    6. if (len == 0) {
    7. printf("服务器检测到客户端关闭\n");
    8. disconnect(cfd, epfd);
    9. }
    10. else {
    11. // 现在要按照空格分割得到 /hello.c
    12. char method[16], path[256], protocol[16];
    13. sscanf(line, "%[^ ] %[^ ] %[^ ]", method, path, protocol);
    14. printf("method=%s, path=%s, protocol=%s\n", method, path, protocol);
    15. }
    16. }

    现在测试代码

    正则表达式字符类

    正则表达式数量限定 

    3.6 判断文件是否存在

    1. // 处理http请求,判断文件是否存在,回发
    2. void http_request(const char *file)
    3. {
    4. struct stat sbuf;
    5. // 判断文件是否存在
    6. int ret = stat(file, &sbuf);
    7. if (ret != 0) {
    8. // 回发浏览器 404 错误页面
    9. perror("stat");
    10. exit(1);
    11. }
    12. if(S_ISREG(sbuf.st_mode)) { // 是一个普通文件
    13. printf("It's a file\n");
    14. }
    15. }
    16. void do_read(int cfd, int epfd)
    17. {
    18. // 读取一行http协议, 拆分, 获取 get 文件名 协议号
    19. char line[1024] = { 0 };
    20. int len = get_line(cfd, line, sizeof(line)); // 确定读出 GET /hello.c HTTP/1.1
    21. if (len == 0) {
    22. printf("服务器检测到客户端关闭\n");
    23. disconnect(cfd, epfd);
    24. }
    25. else {
    26. // 现在要按照空格分割得到 /hello.c
    27. char method[16], path[256], protocol[16];
    28. sscanf(line, "%[^ ] %[^ ] %[^ ]", method, path, protocol);
    29. printf("method=%s, path=%s, protocol=%s\n", method, path, protocol);
    30. // 丢弃缓冲区中后面的数据
    31. while (1) {
    32. char buf[1024] = { 0 };
    33. len = get_line(cfd, buf, sizeof(buf));
    34. printf("-- len = %d\n", len);
    35. if (len == '\n') {
    36. break;
    37. }
    38. if(len == -1) {
    39. break;
    40. }
    41. }
    42. if(strncasecmp(method, "GET", 3) == 0)
    43. {
    44. char *file = path + 1; // 取出客户端要访问的文件名
    45. http_request(file);
    46. }
    47. }
    48. }

    3.7 应答回复客户端

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #define MAXSIZE 2048
    13. int init_listen_fd(int port, int epfd)
    14. {
    15. // 创建监听的套接字 lfd
    16. int lfd = socket(AF_INET, SOCK_STREAM, 0);
    17. if (lfd == -1) {
    18. perror("socket error");
    19. exit(1);
    20. }
    21. // 创建服务器地址结构 IP+port
    22. struct sockaddr_in srv_addr;
    23. bzero(&srv_addr, sizeof(srv_addr));
    24. srv_addr.sin_family = AF_INET;
    25. srv_addr.sin_port = htons(port);
    26. srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    27. // 端口复用
    28. int opt = 1;
    29. setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
    30. // 给 lfd 绑定地址结构
    31. int ret = bind(lfd, (struct sockaddr*)&srv_addr, sizeof(srv_addr));
    32. if (ret == -1) {
    33. perror("bind error");
    34. exit(1);
    35. }
    36. // 设置监听上限
    37. ret = listen(lfd, 128);
    38. if (ret == -1) {
    39. perror("listen error");
    40. exit(1);
    41. }
    42. // lfd 添加到 epoll 树上
    43. struct epoll_event ev;
    44. ev.events = EPOLLIN;
    45. ev.data.fd = lfd;
    46. ret = epoll_ctl(epfd, EPOLL_CTL_ADD, lfd, &ev);
    47. if (ret == -1) {
    48. perror("epoll_ctl add lfd error");
    49. exit(1);
    50. }
    51. return lfd;
    52. }
    53. void do_accept(int lfd, int epfd)
    54. {
    55. struct sockaddr_in clt_addr;
    56. socklen_t clt_addr_len = sizeof(clt_addr);
    57. int cfd = accept(lfd, (struct sockaddr*)&clt_addr, &clt_addr_len);
    58. if (cfd == -1) {
    59. perror("accept error");
    60. exit(1);
    61. }
    62. // 打印客户端IP+port
    63. char client_ip[64] = {0};
    64. printf("New Client IP: %s, Port: %d, cfd = %d\n",
    65. inet_ntop(AF_INET, &clt_addr.sin_addr.s_addr, client_ip, sizeof(client_ip)),
    66. ntohs(clt_addr.sin_port), cfd);
    67. // 设置 cfd 非阻塞
    68. int flag = fcntl(cfd, F_GETFL);
    69. flag |= O_NONBLOCK;
    70. fcntl(cfd, F_SETFL, flag);
    71. // 将新节点cfd 挂到 epoll 监听树上
    72. struct epoll_event ev;
    73. ev.data.fd = cfd;
    74. // 边沿非阻塞模式
    75. ev.events = EPOLLIN | EPOLLET;
    76. int ret = epoll_ctl(epfd, EPOLL_CTL_ADD, cfd, &ev);
    77. if (ret == -1) {
    78. perror("epoll_ctl add cfd error");
    79. exit(1);
    80. }
    81. }
    82. // 通过文件名获取文件的类型
    83. const char *get_file_type(const char *name)
    84. {
    85. char *dot;
    86. // 自右向左查找‘.’字符, 如不存在返回NULL
    87. dot = strrchr(name, '.');
    88. if (dot == NULL)
    89. return "text/plain; charset=utf-8";
    90. if (strcmp(dot, ".html") == 0 || strcmp(dot, ".htm") == 0)
    91. return "text/html; charset=utf-8";
    92. if (strcmp(dot, ".jpg") == 0 || strcmp(dot, ".jpeg") == 0)
    93. return "image/jpeg";
    94. if (strcmp(dot, ".gif") == 0)
    95. return "image/gif";
    96. if (strcmp(dot, ".png") == 0)
    97. return "image/png";
    98. if (strcmp(dot, ".css") == 0)
    99. return "text/css";
    100. if (strcmp(dot, ".au") == 0)
    101. return "audio/basic";
    102. if (strcmp(dot, ".wav" ) == 0)
    103. return "audio/wav";
    104. if (strcmp(dot, ".avi") == 0)
    105. return "video/x-msvideo";
    106. if (strcmp(dot, ".mov") == 0 || strcmp(dot, ".qt") == 0)
    107. return "video/quicktime";
    108. if (strcmp(dot, ".mpeg") == 0 || strcmp(dot, ".mpe") == 0)
    109. return "video/mpeg";
    110. if (strcmp(dot, ".vrml") == 0 || strcmp(dot, ".wrl") == 0)
    111. return "model/vrml";
    112. if (strcmp(dot, ".midi") == 0 || strcmp(dot, ".mid") == 0)
    113. return "audio/midi";
    114. if (strcmp(dot, ".mp3") == 0)
    115. return "audio/mpeg";
    116. if (strcmp(dot, ".ogg") == 0)
    117. return "application/ogg";
    118. if (strcmp(dot, ".pac") == 0)
    119. return "application/x-ns-proxy-autoconfig";
    120. return "text/plain; charset=utf-8";
    121. }
    122. int get_line(int cfd, char *buf, int size)
    123. {
    124. int i = 0;
    125. char c = '\0';
    126. int n;
    127. //每次读一个字节,判断合理就放入缓冲区中
    128. while ((i < size - 1) && (c != '\n')) {
    129. n = recv(cfd, &c, 1, 0);
    130. if (n > 0) {
    131. if (c == '\r') {
    132. //MSG_PEEK 使得recv以拷贝的方式从缓冲区中读取数据(否则读取之后,缓冲区中的数据就没了)
    133. //试探性的获取缓冲区中的数据量
    134. n = recv(cfd, &c, 1, MSG_PEEK);
    135. //缓冲区中有数据并且结尾是 \n ,则读取数据
    136. if ((n > 0) && (c == '\n')) {
    137. recv(cfd, &c, 1, 0);
    138. }
    139. else {
    140. c = '\n';
    141. }
    142. }
    143. buf[i] = c;
    144. i++;
    145. }
    146. else {
    147. c = '\n';
    148. }
    149. }
    150. buf[i] = '\0';
    151. //recv失败
    152. if (n == -1) {
    153. i = -1;
    154. }
    155. return i;
    156. }
    157. // 断开连接
    158. void disconnect(int cfd, int epfd)
    159. {
    160. int ret = epoll_ctl(epfd, EPOLL_CTL_DEL, cfd, NULL);
    161. if(ret != 0) {
    162. perror("epoll_ctl error");
    163. exit(1);
    164. }
    165. close(cfd);
    166. }
    167. // 客户端的fd,错误号,错误描述,回发文件类型,文件长度
    168. void send_respond(int cfd, int no, char *disp, char *type, int len)
    169. {
    170. char buf[1024] = { 0 };
    171. sprintf (buf, "HTTP/1.1 %d %s\r\n", no, disp);
    172. sprintf(buf + strlen(buf), "%s\r\n", type) ;
    173. sprintf(buf + strlen(buf), "Content-Length:%d\r\n", len);
    174. send(cfd, buf, strlen(buf), 0);
    175. send(cfd, "\r\n", 2, 0);
    176. }
    177. // 发送服务器本地文件给浏览器
    178. void send_file(int cfd, const char *file)
    179. {
    180. int n = 0;
    181. char buf[1024];
    182. // 打开的服务器本地文件 —— cfd 能访问客户端的 socket
    183. int fd = open(file, O_RDONLY);
    184. if (fd == -1) {
    185. // 404 错误页面
    186. perror("open error");
    187. exit(1);
    188. }
    189. int ret;
    190. while ((n = read(fd, buf, sizeof(buf))) > 0) {
    191. ret = send(cfd, buf, n, 0);
    192. if (ret == -1) {
    193. if (ret == -1) {
    194. perror("send error");
    195. exit(1);
    196. }
    197. }
    198. }
    199. close(fd);
    200. }
    201. // 处理http请求,判断文件是否存在,回发
    202. void http_request(int cfd, const char *file)
    203. {
    204. struct stat sbuf;
    205. // 判断文件是否存在
    206. int ret = stat(file, &sbuf);
    207. if (ret != 0) {
    208. // 回发浏览器 404 错误页面
    209. perror("stat");
    210. //exit(1);
    211. }
    212. if(S_ISREG(sbuf.st_mode)) { // 是一个普通文件
    213. // 回发 http 协议应答
    214. // send_respond(cfd, 200, "OK", "Content-Type: text/plain; charset=iso-8859-1", sbuf.st_size);
    215. char *type = get_file_type(file);
    216. send_respond(cfd, 200, "OK", type, sbuf.st_size);
    217. // 回发 给客户端请求数据内容
    218. send_file(cfd, file);
    219. }
    220. }
    221. void do_read(int cfd, int epfd)
    222. {
    223. // 读取一行http协议, 拆分, 获取 get 文件名 协议号
    224. char line[1024] = { 0 };
    225. int len = get_line(cfd, line, sizeof(line)); // 确定读出 GET /hello.c HTTP/1.1
    226. if (len == 0) {
    227. printf("服务器检测到客户端关闭\n");
    228. disconnect(cfd, epfd);
    229. }
    230. else {
    231. // 现在要按照空格分割得到 /hello.c
    232. char method[16], path[256], protocol[16];
    233. sscanf(line, "%[^ ] %[^ ] %[^ ]", method, path, protocol);
    234. printf("method=%s, path=%s, protocol=%s\n", method, path, protocol);
    235. // 丢弃缓冲区中后面的数据
    236. while (1) {
    237. char buf[1024] = { 0 };
    238. len = get_line(cfd, buf, sizeof(buf));
    239. if (len == '\n') {
    240. break;
    241. }
    242. if(len == -1) {
    243. break;
    244. }
    245. }
    246. if(strncasecmp(method, "GET", 3) == 0)
    247. {
    248. char *file = path + 1; // 取出客户端要访问的文件名
    249. http_request(cfd, file);
    250. }
    251. }
    252. }
    253. void epoll_run(int port)
    254. {
    255. int i = 0;
    256. struct epoll_event all_events[MAXSIZE];
    257. // 创建一个epoll监听树根
    258. int epfd = epoll_create(MAXSIZE);
    259. if (epfd == -1) {
    260. perror("epoll_create error");
    261. exit(1);
    262. }
    263. // 创建lfd,并添加至监听树
    264. int lfd = init_listen_fd(port, epfd);
    265. while (1) {
    266. // 监听节点对应事件
    267. int ret = epoll_wait(epfd, all_events, MAXSIZE, -1);
    268. if (ret == -1) {
    269. perror("epoll_wait error");
    270. exit(1);
    271. }
    272. for (i=0; i
    273. // 只处理读事件, 其他事件默认不处理
    274. struct epoll_event *pev = &all_events[i];
    275. // 不是读事件
    276. if (!(pev->events & EPOLLIN)) {
    277. continue;
    278. }
    279. if (pev->data.fd == lfd) { // 接受连接请求
    280. do_accept(lfd, epfd);
    281. } else { // 读数据
    282. do_read(pev->data.fd, epfd);
    283. }
    284. }
    285. }
    286. }
    287. int main(int argc, char *argv[])
    288. {
    289. // 命令行参数获取 端口 和 server提供的目录
    290. if (argc < 3)
    291. {
    292. printf("./server port path\n");
    293. }
    294. // 获取用户输入的端口
    295. int port = atoi(argv[1]);
    296. // 改变进程工作目录
    297. int ret = chdir(argv[2]);
    298. if (ret != 0) {
    299. perror("chdir error");
    300. exit(1);
    301. }
    302. // 启动 epoll监听
    303. epoll_run(port);
    304. return 0;
    305. }

    单个文件请求成功

    mp3 格式,他的头文件是这样的

    3.8 文件类型区分

    1. // 通过文件名获取文件的类型
    2. const char *get_file_type(const char *name)
    3. {
    4. char *dot;
    5. // 自右向左查找‘.’字符, 如不存在返回NULL
    6. dot = strrchr(name, '.');
    7. if (dot == NULL)
    8. return "text/plain; charset=utf-8";
    9. if (strcmp(dot, ".html") == 0 || strcmp(dot, ".htm") == 0)
    10. return "text/html; charset=utf-8";
    11. if (strcmp(dot, ".jpg") == 0 || strcmp(dot, ".jpeg") == 0)
    12. return "image/jpeg";
    13. if (strcmp(dot, ".gif") == 0)
    14. return "image/gif";
    15. if (strcmp(dot, ".png") == 0)
    16. return "image/png";
    17. if (strcmp(dot, ".css") == 0)
    18. return "text/css";
    19. if (strcmp(dot, ".au") == 0)
    20. return "audio/basic";
    21. if (strcmp(dot, ".wav" ) == 0)
    22. return "audio/wav";
    23. if (strcmp(dot, ".avi") == 0)
    24. return "video/x-msvideo";
    25. if (strcmp(dot, ".mov") == 0 || strcmp(dot, ".qt") == 0)
    26. return "video/quicktime";
    27. if (strcmp(dot, ".mpeg") == 0 || strcmp(dot, ".mpe") == 0)
    28. return "video/mpeg";
    29. if (strcmp(dot, ".vrml") == 0 || strcmp(dot, ".wrl") == 0)
    30. return "model/vrml";
    31. if (strcmp(dot, ".midi") == 0 || strcmp(dot, ".mid") == 0)
    32. return "audio/midi";
    33. if (strcmp(dot, ".mp3") == 0)
    34. return "audio/mpeg";
    35. if (strcmp(dot, ".ogg") == 0)
    36. return "application/ogg";
    37. if (strcmp(dot, ".pac") == 0)
    38. return "application/x-ns-proxy-autoconfig";
    39. return "text/plain; charset=utf-8";
    40. }
    41. // 处理http请求,判断文件是否存在,回发
    42. void http_request(int cfd, const char *file)
    43. {
    44. struct stat sbuf;
    45. // 判断文件是否存在
    46. int ret = stat(file, &sbuf);
    47. if (ret != 0) {
    48. // 回发浏览器 404 错误页面
    49. perror("stat");
    50. //exit(1);
    51. }
    52. if(S_ISREG(sbuf.st_mode)) { // 是一个普通文件
    53. // 回发 http 协议应答
    54. // send_respond(cfd, 200, "OK", "Content-Type: text/plain; charset=iso-8859-1", sbuf.st_size);
    55. char *type = get_file_type(file);
    56. send_respond(cfd, 200, "OK", type, sbuf.st_size);
    57. // 回发 给客户端请求数据内容
    58. send_file(cfd, file);
    59. }
    60. }

    3.9 细节处理

    1、没有找到文件的页面

    也可以自己在文件夹中写一个错误页面,然后没找到的发发送写过的错误页面

    2、和客户端第二次请求 ico 文件处理

    如上图,只需要在文件夹中放一个需要的 ico 文件,浏览器就能获取需要的网页图标

    3、get 处理完成后删除服务器和客户端的连接

    3.10 文件夹处理

    拼接一个 html 页面

    3.11 汉字字符编码和解码 

     

  • 相关阅读:
    spring boot破解xjar.go加密后的jar包
    Godot 4.0 加载为占位符(InstancePlaceholder)的用法和特点
    【FISCO-BCOS】十七、角色的权限控制
    docker 启动 centos/golang 容器 及参数说明
    中国信息通信设备行业发展趋势及投资风险研究报告
    【Obsidian样式】修改文件夹名称和文件名称前的图标
    Eclipse项目导入笔记大全&踩坑大全
    AI智能文案写作工具,迅速生成高质量的文案
    【Vue.js生命周期】什么是生命周期?声明周期详解
    分享一个java+springboot+vue校园电动车租赁系统(源码、调试、开题、lw)
  • 原文地址:https://blog.csdn.net/HuanBianCheng27/article/details/127814345