main.c 6.1 KB

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