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