http_server.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include <arpa/inet.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/socket.h>
  7. #include <sys/stat.h>
  8. #include <unistd.h>
  9. #define PORT 12580
  10. #define MAX_REQUEST_SIZE 1500
  11. #define MAX_RESPONSE_SIZE 1500
  12. // 网页根目录
  13. #define WEB_ROOT "/var/www/html/"
  14. #define EXIT_CODE 1
  15. #define min(a, b) ((a) < (b) ? (a) : (b))
  16. #define DEFAULT_PAGE "/index.html"
  17. int security_check(char *path) {
  18. // 检查路径是否包含 ..
  19. if (strstr(path, "..")) {
  20. return 0;
  21. }
  22. return 1;
  23. }
  24. ssize_t send_response(int sockfd, char *response) {
  25. return write(sockfd, response, strlen(response));
  26. }
  27. void send_header(int sockfd, int content_length, char *path) {
  28. char buffer[MAX_RESPONSE_SIZE];
  29. // 获取文件类型
  30. char *content_type;
  31. if (strstr(path, ".html")) {
  32. content_type = "text/html";
  33. } else if (strstr(path, ".css")) {
  34. content_type = "text/css";
  35. } else if (strstr(path, ".js")) {
  36. content_type = "application/javascript";
  37. } else if (strstr(path, ".png")) {
  38. content_type = "image/png";
  39. } else if (strstr(path, ".jpg")) {
  40. content_type = "image/jpeg";
  41. } else if (strstr(path, ".gif")) {
  42. content_type = "image/gif";
  43. } else {
  44. content_type = "text/plain;charset=utf-8";
  45. }
  46. sprintf(buffer, "HTTP/1.1 200 OK\nContent-Type: %s\nContent-Length: %d\n\n",
  47. content_type, content_length);
  48. send_response(sockfd, buffer);
  49. }
  50. void send_file(int sockfd, char *path) {
  51. printf("send_file: path: %s\n", path);
  52. int fd = open(path, 0);
  53. if (fd == -1) {
  54. send_response(sockfd,
  55. "HTTP/1.1 404 Not Found\nContent-Type: "
  56. "text/html\n\n<html><body><h1>404 Not Found</h1><p>DragonOS "
  57. "Http Server</p></body></html>");
  58. return;
  59. }
  60. int content_length = lseek(fd, 0, SEEK_END);
  61. int remaining = content_length;
  62. printf("send_file: content_length: %d\n", content_length);
  63. lseek(fd, 0, SEEK_SET);
  64. send_header(sockfd, content_length, path);
  65. char buffer[1048576];
  66. int readSize;
  67. while (remaining) {
  68. // 由于磁盘IO耗时较长,所以每次读取1MB,然后再分批发送
  69. int to_read = min(1048576, remaining);
  70. readSize = read(fd, &buffer, to_read);
  71. remaining -= readSize;
  72. void *p = buffer;
  73. while (readSize > 0) {
  74. int wsize = write(sockfd, p, min(readSize, MAX_RESPONSE_SIZE));
  75. if (wsize <= 0) {
  76. printf("send_file failed: wsize: %d\n", wsize);
  77. close(fd);
  78. return;
  79. }
  80. p += wsize;
  81. readSize -= wsize;
  82. }
  83. }
  84. close(fd);
  85. }
  86. void handle_request(int sockfd, char *request) {
  87. char *method, *url, *http_version;
  88. char path[MAX_REQUEST_SIZE];
  89. method = strtok(request, " ");
  90. url = strtok(NULL, " ");
  91. http_version = strtok(NULL, "\r\n");
  92. printf("handle_request: method: %s, url: %s, http_version: %s\n", method, url,
  93. http_version);
  94. // 检查空指针等异常情况
  95. if (method == NULL || url == NULL || http_version == NULL) {
  96. send_response(sockfd,
  97. "HTTP/1.1 400 Bad Request\nContent-Type: "
  98. "text/html\n\n<html><body><h1>400 Bad "
  99. "Request</h1><p>DragonOS Http Server</p></body></html>");
  100. return;
  101. }
  102. // 检查url是否为空
  103. if (strlen(url) == 0) {
  104. send_response(sockfd,
  105. "HTTP/1.1 400 Bad Request\nContent-Type: "
  106. "text/html\n\n<html><body><h1>400 Bad "
  107. "Request</h1><p>DragonOS Http Server</p></body></html>");
  108. return;
  109. }
  110. int default_page = 0;
  111. if (url[strlen(url) - 1] == '/') {
  112. default_page = 1;
  113. }
  114. if (strcmp(method, "GET") == 0) {
  115. if (default_page) {
  116. sprintf(path, "%s%s%s", WEB_ROOT, url, DEFAULT_PAGE);
  117. } else {
  118. sprintf(path, "%s%s", WEB_ROOT, url);
  119. }
  120. if (!security_check(path)) {
  121. send_response(sockfd,
  122. "HTTP/1.1 403 Forbidden\nContent-Type: "
  123. "text/html\n\n<html><body><h1>403 "
  124. "Forbidden</h1><p>DragonOS Http Server</p></body></html>");
  125. return;
  126. }
  127. send_file(sockfd, path);
  128. } else {
  129. send_response(sockfd,
  130. "HTTP/1.1 501 Not Implemented\nContent-Type: "
  131. "text/html\n\n<html><body><h1>501 Not "
  132. "Implemented</h1><p>DragonOS Http Server</p></body></html>");
  133. }
  134. }
  135. int main(int argc, char const *argv[]) {
  136. int server_fd, new_socket, valread;
  137. struct sockaddr_in address;
  138. int addrlen = sizeof(address);
  139. char buffer[MAX_REQUEST_SIZE] = {0};
  140. int opt = 1;
  141. // 创建socket
  142. if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
  143. perror("socket failed");
  144. exit(EXIT_CODE);
  145. }
  146. // 设置socket选项,允许地址重用
  147. // if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt,
  148. // sizeof(opt)))
  149. // {
  150. // perror("setsockopt failed");
  151. // exit(EXIT_CODE);
  152. // }
  153. // 设置地址和端口
  154. address.sin_family = AF_INET;
  155. address.sin_addr.s_addr = INADDR_ANY;
  156. address.sin_port = htons(PORT);
  157. // 把socket绑定到地址和端口上
  158. if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
  159. perror("bind failed");
  160. exit(EXIT_CODE);
  161. }
  162. // 监听socket
  163. if (listen(server_fd, 3) < 0) {
  164. perror("listen failed");
  165. exit(EXIT_CODE);
  166. }
  167. while (1) {
  168. printf("Waiting for a client...\n");
  169. // 等待并接受客户端连接
  170. if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
  171. (socklen_t *)&addrlen)) < 0) {
  172. perror("accept failed");
  173. exit(EXIT_CODE);
  174. }
  175. // 接收客户端消息
  176. valread = read(new_socket, buffer, MAX_REQUEST_SIZE);
  177. printf("%s\n", buffer);
  178. // 处理请求
  179. handle_request(new_socket, buffer);
  180. // 关闭客户端连接
  181. close(new_socket);
  182. }
  183. // 关闭tcp socket
  184. close(server_fd);
  185. return 0;
  186. }