From c426b950199c21bd40cef0bcda82a28fdcc40d0d Mon Sep 17 00:00:00 2001 From: Saleh Ahmed Khan <s.khan20@student.unimelb.edu.au> Date: Thu, 19 Apr 2018 19:50:32 +1000 Subject: [PATCH] Finalised code style and comment style, conforms to standards. --- .vscode/settings.json | 3 ++- get.c | 26 ++++++++++++++++++++------ get.h | 8 -------- makefile | 6 ++---- server.c | 1 + server.h | 15 ++++++++++++++- 6 files changed, 39 insertions(+), 20 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 4f9b610..d4eeda8 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 863588f..6b54aec 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 d37a54c..15b1b2e 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 b8b253a..9e2ac9e 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 89595a1..5b1b21a 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 d184f5c..4b35f8d 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 -- GitLab