http_server.c 5.8 KB

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