Skip to content
Snippets Groups Projects
Commit c426b950 authored by Saleh Ahmed Khan's avatar Saleh Ahmed Khan
Browse files

Finalised code style and comment style, conforms to standards.

parent ef0f156c
No related branches found
No related tags found
No related merge requests found
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
"stdio.h": "c", "stdio.h": "c",
"stat.h": "c", "stat.h": "c",
"server.h": "c", "server.h": "c",
"types.h": "c" "types.h": "c",
"xlocale": "c"
} }
} }
\ No newline at end of file
...@@ -2,7 +2,11 @@ ...@@ -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_start = strstr(buffer, "GET") + 4;
const char* path_end = strstr(buffer, "HTTP")-1; const char* path_end = strstr(buffer, "HTTP")-1;
char path[path_end - path_start]; char path[path_end - path_start];
...@@ -12,10 +16,14 @@ void parse_request_and_send_response(char* root, char* buffer, int newsockfd) { ...@@ -12,10 +16,14 @@ void parse_request_and_send_response(char* root, char* buffer, int newsockfd) {
strcpy(full_path, root); strcpy(full_path, root);
strcat(full_path, path); strcat(full_path, path);
/* If the file is found, repond with the 200 header and the file */ /* If the file is found, repond with
if (file_found(full_path)) respond_OK_and_send_file(full_path, newsockfd); the 200 header and the file */
/* otherwise, respond with a 404 NOT FOUND response */ if (file_found(full_path))
else respond_NOTFOUND(newsockfd); 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) { int file_found(char* path_to_file) {
...@@ -48,12 +56,14 @@ void respond_NOTFOUND(int newsockfd) { ...@@ -48,12 +56,14 @@ void respond_NOTFOUND(int newsockfd) {
} }
size_t get_size(int fd) { size_t get_size(int fd) {
/* Using fstat() to get filesize */
struct stat st; struct stat st;
fstat(fd, &st); fstat(fd, &st);
return st.st_size; return st.st_size;
} }
void get_mime_type(char* path, char* mime) { void get_mime_type(char* path, char* mime) {
/* Get mime type from extension */
if (strstr(path, ".jpg") != NULL) { if (strstr(path, ".jpg") != NULL) {
strcpy(mime, "image/jpeg"); strcpy(mime, "image/jpeg");
} else if (strstr(path, ".html") != NULL) { } else if (strstr(path, ".html") != NULL) {
...@@ -68,7 +78,11 @@ void get_mime_type(char* path, char* mime) { ...@@ -68,7 +78,11 @@ void get_mime_type(char* path, char* mime) {
} }
void make_response_header(char* response, char* path, size_t size) { 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]; char mime[64];
get_mime_type(path, mime); 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
...@@ -2,14 +2,6 @@ ...@@ -2,14 +2,6 @@
#ifndef GET_H_INCLUDED #ifndef GET_H_INCLUDED
#define 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 */ /* Libraries used */
#include "server.h" #include "server.h"
......
server: server.c get.c 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 debug: server.c get.c
gcc -o server server.c get.c -lpthread -g gcc -o server server.c get.c -g
client: client.c
gcc -o client client.c
clean: clean:
rm server rm server
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "server.h" #include "server.h"
#include "get.h"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
/* Sample code starts here, not changed /* Sample code starts here, not changed
......
/* 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 */ /* Libraries used */
#include <stdio.h> #include <stdio.h>
...@@ -11,3 +22,5 @@ ...@@ -11,3 +22,5 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <unistd.h> #include <unistd.h>
#include <sys/sendfile.h> #include <sys/sendfile.h>
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment