main.c (821B)
1 #include "file.h" 2 #include "http.h" 3 #include "tcp.h" 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <unistd.h> 8 9 int 10 main() { 11 int sock, asock; 12 sock = create_socket(); 13 14 char *buf; 15 16 for (;;) { 17 asock = accept_socket(sock); 18 19 char *request = read_socket(asock); 20 printf("%s", request); 21 22 char *file_name = requested_file(request); 23 /* Get rid of the leading / in the file name e.g. /index.html */ 24 file_name++; 25 26 /* If file is not found */ 27 if (valid_path(file_name)) { 28 if (is_directory(file_name)) { 29 strncat(file_name, "index.html", 11); 30 buf = read_file(file_name); 31 } else { 32 buf = read_file(file_name); 33 } 34 } else { 35 buf = read_file("index.html"); 36 } 37 38 char *text = respond(buf); 39 write(asock, text, strlen(text)); 40 41 free(request); 42 free(buf); 43 free(text); 44 } 45 }