main.c 6.2 KB

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