better file reading and cleanup

This commit is contained in:
Michael Thomson 2024-02-10 10:13:37 -05:00
parent 11a649fbb3
commit bdafb30b0a
No known key found for this signature in database
3 changed files with 33 additions and 35 deletions

BIN
server

Binary file not shown.

View File

@ -90,24 +90,40 @@ static void start(const char *PORT) {
} }
} }
size_t read_file(char *buf, char *filename) { char *read_file(char *filename, size_t *file_size) {
FILE *fptr = fopen(filename, "r"); // 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 // Calculate the content length
fseek(fptr, 0, SEEK_END); fseek(fp, 0, SEEK_END);
size_t body_length = ftell(fptr); *file_size = ftell(fp);
rewind(fptr); rewind(fp);
// Read in file // allocate memory for file contents
if (fread(buf, 1, body_length, fptr) == 0) { char *buffer = malloc(*file_size + 1);
fprintf(stderr, "fread: unable to read file contents"); if (buffer == NULL) {
exit(1); fprintf(stderr, "read_file: memory allocation failed.\n");
fclose(fp);
return NULL;
} }
buf[body_length] = '\0'; // terminate string
// Read the file contents into buffer
fclose(fptr); size_t bytes_read = fread(buffer, 1, *file_size, fp);
if (bytes_read != *file_size) {
return body_length; 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) { void respond(int *client_fd) {
@ -127,10 +143,9 @@ void respond(int *client_fd) {
} else { } else {
filename = "404.html"; filename = "404.html";
} }
// read in file and get length size_t body_length;
char *body = malloc(MAX_BUFFER_SIZE); char *body = read_file(filename, &body_length);
size_t body_length = read_file(body, filename);
// Create the Content-Length header // Create the Content-Length header
struct Header headers[MAX_HEADER_NUM]; struct Header headers[MAX_HEADER_NUM];
@ -146,8 +161,6 @@ void respond(int *client_fd) {
char response_string[MAX_BUFFER_SIZE]; char response_string[MAX_BUFFER_SIZE];
response_to_string(response_string, &response); response_to_string(response_string, &response);
printf("%s", response_string);
if (send(*client_fd, response_string, strlen(response_string), 0) == -1) { if (send(*client_fd, response_string, strlen(response_string), 0) == -1) {
perror("send content"); perror("send content");
exit(1); exit(1);
@ -170,13 +183,6 @@ void launch(struct Server *server) {
continue; 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()) { if (!fork()) {
close(sockfd); close(sockfd);
respond(&client_fd); respond(&client_fd);

View File

@ -5,14 +5,6 @@
#define BACKLOG 128 #define BACKLOG 128
#define GET "GET";
#define POST "POST";
struct Route {
char *path;
char *method;
};
struct Server { struct Server {
const char *PORT; const char *PORT;
}; };