diff --git a/server b/server index 3fe46c0..1d595ce 100755 Binary files a/server and b/server differ diff --git a/server.c b/server.c index e478da1..805c02e 100644 --- a/server.c +++ b/server.c @@ -90,24 +90,40 @@ static void start(const char *PORT) { } } -size_t read_file(char *buf, char *filename) { - FILE *fptr = fopen(filename, "r"); +char *read_file(char *filename, size_t *file_size) { + // read in file + FILE *fp = fopen(filename, "r"); + if (fp == NULL) { + fprintf(stderr, "read_file: error opening file: %s\n", filename); + return NULL; + } // Calculate the content length - fseek(fptr, 0, SEEK_END); - size_t body_length = ftell(fptr); - rewind(fptr); + fseek(fp, 0, SEEK_END); + *file_size = ftell(fp); + rewind(fp); - // Read in file - if (fread(buf, 1, body_length, fptr) == 0) { - fprintf(stderr, "fread: unable to read file contents"); - exit(1); + // allocate memory for file contents + char *buffer = malloc(*file_size + 1); + if (buffer == NULL) { + fprintf(stderr, "read_file: memory allocation failed.\n"); + fclose(fp); + return NULL; } - buf[body_length] = '\0'; // terminate string - - fclose(fptr); - - return body_length; + + // Read the file contents into buffer + size_t bytes_read = fread(buffer, 1, *file_size, fp); + if (bytes_read != *file_size) { + fprintf(stderr, "read_file: error reading file.\n"); + free(buffer); + fclose(fp); + return NULL; + } + + fclose(fp); + buffer[*file_size] = '\0'; + + return buffer; } void respond(int *client_fd) { @@ -127,10 +143,9 @@ void respond(int *client_fd) { } else { filename = "404.html"; } - - // read in file and get length - char *body = malloc(MAX_BUFFER_SIZE); - size_t body_length = read_file(body, filename); + + size_t body_length; + char *body = read_file(filename, &body_length); // Create the Content-Length header struct Header headers[MAX_HEADER_NUM]; @@ -146,8 +161,6 @@ void respond(int *client_fd) { char response_string[MAX_BUFFER_SIZE]; response_to_string(response_string, &response); - printf("%s", response_string); - if (send(*client_fd, response_string, strlen(response_string), 0) == -1) { perror("send content"); exit(1); @@ -170,13 +183,6 @@ void launch(struct Server *server) { continue; } - char name[INET6_ADDRSTRLEN]; - char port[10]; - getnameinfo((struct sockaddr *)&client_addr, client_addr_len, name, - sizeof(name), port, sizeof(port), - NI_NUMERICHOST | NI_NUMERICSERV); - printf("New connection from: %s:%s\n", name, port); - if (!fork()) { close(sockfd); respond(&client_fd); diff --git a/server.h b/server.h index bf3f77f..c7ff4af 100644 --- a/server.h +++ b/server.h @@ -5,14 +5,6 @@ #define BACKLOG 128 -#define GET "GET"; -#define POST "POST"; - -struct Route { - char *path; - char *method; -}; - struct Server { const char *PORT; };