diff --git a/.vscode/settings.json b/.vscode/settings.json index 4f9b610c76728c5e56eb95b4aa04d568b7657f62..d4eeda89348d886f1f2f3de616228ae5a05b6dcd 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,6 +13,7 @@ "stdio.h": "c", "stat.h": "c", "server.h": "c", - "types.h": "c" + "types.h": "c", + "xlocale": "c" } } \ No newline at end of file diff --git a/get.c b/get.c index 863588f81f2a1ed69964bac3d835fc959fa687dd..6b54aec97ad3dfa9efb4c49e602e3ffd99dd7079 100644 --- a/get.c +++ b/get.c @@ -2,7 +2,11 @@ -void parse_request_and_send_response(char* root, char* buffer, int newsockfd) { +void parse_request_and_send_response(char* root, + char* buffer,int newsockfd) { + /* Get the [directory/filename.extension] from + the GET request recieved in the buffer, then + combine with root (from argv[2]) to get path. */ const char* path_start = strstr(buffer, "GET") + 4; const char* path_end = strstr(buffer, "HTTP")-1; char path[path_end - path_start]; @@ -12,10 +16,14 @@ void parse_request_and_send_response(char* root, char* buffer, int newsockfd) { strcpy(full_path, root); strcat(full_path, path); - /* If the file is found, repond with the 200 header and the file */ - if (file_found(full_path)) respond_OK_and_send_file(full_path, newsockfd); - /* otherwise, respond with a 404 NOT FOUND response */ - else respond_NOTFOUND(newsockfd); + /* If the file is found, repond with + the 200 header and the file */ + if (file_found(full_path)) + respond_OK_and_send_file(full_path, newsockfd); + /* otherwise, respond with a + 404 NOT FOUND response */ + else + respond_NOTFOUND(newsockfd); } int file_found(char* path_to_file) { @@ -48,12 +56,14 @@ void respond_NOTFOUND(int newsockfd) { } size_t get_size(int fd) { + /* Using fstat() to get filesize */ struct stat st; fstat(fd, &st); return st.st_size; } void get_mime_type(char* path, char* mime) { + /* Get mime type from extension */ if (strstr(path, ".jpg") != NULL) { strcpy(mime, "image/jpeg"); } else if (strstr(path, ".html") != NULL) { @@ -68,7 +78,11 @@ void get_mime_type(char* path, char* mime) { } void make_response_header(char* response, char* path, size_t size) { + /* Our server reponds with a header containing + a mime type and file size */ char mime[64]; get_mime_type(path, mime); - sprintf(response, "%sContent-Length: %zu\r\nContent-Type: %s\r\n\r\n", FOUND_RESPONSE, size, mime); + sprintf(response, + "%sContent-Length: %zu\r\nContent-Type: %s\r\n\r\n", + FOUND_RESPONSE, size, mime); } \ No newline at end of file diff --git a/get.h b/get.h index d37a54cecd56ac73cdb6dbdf3a182e6fac992a7e..15b1b2e45d8282c312e1fea5392048f0d63b0ed6 100644 --- a/get.h +++ b/get.h @@ -2,14 +2,6 @@ #ifndef GET_H_INCLUDED #define GET_H_INCLUDED -/* Hash defines */ -#define FOUND_RESPONSE "HTTP/1.0 200 OK\r\n" /* add \n*/ -#define NOT_FOUND_RESPONSE "HTTP/1.0 404\r\n\r\n" -#define FOUND 1 -#define NOT_FOUND 0 -#define BUF 100000 -#define INFINITE_LOOP 1 - /* Libraries used */ #include "server.h" diff --git a/makefile b/makefile index b8b253ac6dc7b5e319294b7639a2362d150530a3..9e2ac9e40d4fb8cd7db8dadd8ea104bdc563e8a6 100644 --- a/makefile +++ b/makefile @@ -1,9 +1,7 @@ server: server.c get.c - gcc -o server server.c get.c -lpthread + gcc -o server server.c get.c debug: server.c get.c - gcc -o server server.c get.c -lpthread -g -client: client.c - gcc -o client client.c + gcc -o server server.c get.c -g clean: rm server diff --git a/server.c b/server.c index 89595a103a46e67365b3e7607708ff564891d2b9..5b1b21a2279c47d9961035af9e3d359fa20a5701 100644 --- a/server.c +++ b/server.c @@ -11,6 +11,7 @@ #include "server.h" +#include "get.h" int main(int argc, char *argv[]) { /* Sample code starts here, not changed diff --git a/server.h b/server.h index d184f5c767fe412f213101bb469c4f4d5d616040..4b35f8df340649b4d508a70fe79bc3ebcc8c923d 100644 --- a/server.h +++ b/server.h @@ -1,3 +1,14 @@ +/* Include guards */ +#ifndef SERVER_H_INCLUDED +#define SERVER_H_INCLUDED + +/* Hash defines */ +#define FOUND_RESPONSE "HTTP/1.0 200 OK\r\n" +#define NOT_FOUND_RESPONSE "HTTP/1.0 404\r\n\r\n" +#define FOUND 1 +#define NOT_FOUND 0 +#define BUF 100000 +#define INFINITE_LOOP 1 /* Libraries used */ #include <stdio.h> @@ -10,4 +21,6 @@ #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> -#include <sys/sendfile.h> \ No newline at end of file +#include <sys/sendfile.h> + +#endif \ No newline at end of file