remove some hardcoded string lengths
This commit is contained in:
parent
1c51408dd9
commit
630057e3ab
11
src/file.c
11
src/file.c
|
@ -9,16 +9,13 @@ void get_file_path(char *uri, char *file_path) {
|
||||||
*question = '\0';
|
*question = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
char index_file[] = "index.html";
|
char index_file[] = "index.html"; // 11 chars + \0
|
||||||
if(uri[strlen(uri) - 1] == '/') {
|
if(uri[strlen(uri) - 1] == '/') {
|
||||||
strncat(uri, index_file, 20);
|
strncat(uri, index_file, strlen(index_file) + 1);
|
||||||
}
|
}
|
||||||
printf("%s\n", uri);
|
|
||||||
|
|
||||||
strncpy(file_path, file_dir, 20);
|
strncpy(file_path, file_dir, strlen(file_dir) + 1);
|
||||||
printf("%s\n", file_path);
|
strncat(file_path, uri, strlen(uri) + 1);
|
||||||
strncat(file_path, uri, 30);
|
|
||||||
printf("%s\n", file_path);
|
|
||||||
|
|
||||||
const char *dot = strrchr(file_path, '.');
|
const char *dot = strrchr(file_path, '.');
|
||||||
if(!dot || dot == file_path) {
|
if(!dot || dot == file_path) {
|
||||||
|
|
|
@ -113,11 +113,8 @@ int main() {
|
||||||
} else {
|
} else {
|
||||||
char file_path[4096];
|
char file_path[4096];
|
||||||
get_file_path(uri, file_path);
|
get_file_path(uri, file_path);
|
||||||
printf("Getting file\n");
|
|
||||||
|
|
||||||
FILE *file = fopen(file_path, "r");
|
FILE *file = fopen(file_path, "r");
|
||||||
if(!file) {
|
if(!file) {
|
||||||
printf("File does not exist\n");
|
|
||||||
char not_found[] = "404 page not found";
|
char not_found[] = "404 page not found";
|
||||||
send_response(client_sock_fd, NOT_FOUND_HEAD, "text/plain", not_found, (int)sizeof(not_found));
|
send_response(client_sock_fd, NOT_FOUND_HEAD, "text/plain", not_found, (int)sizeof(not_found));
|
||||||
} else {
|
} else {
|
||||||
|
@ -125,8 +122,6 @@ int main() {
|
||||||
unsigned long file_size = ftell(file);
|
unsigned long file_size = ftell(file);
|
||||||
fseek(file, 0, SEEK_SET);
|
fseek(file, 0, SEEK_SET);
|
||||||
|
|
||||||
printf("Size of File: %ld\n", file_size);
|
|
||||||
|
|
||||||
char mime_type[15];
|
char mime_type[15];
|
||||||
get_mime_type(uri, mime_type);
|
get_mime_type(uri, mime_type);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue