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

Code cleaned up, better separation of functions

parent 9d282279
No related branches found
No related tags found
No related merge requests found
...@@ -3,3 +3,4 @@ test/* ...@@ -3,3 +3,4 @@ test/*
.vscode/c_cpp_properties.json .vscode/c_cpp_properties.json
.vscode/settings.json .vscode/settings.json
home/file.txt home/file.txt
server
...@@ -5,6 +5,11 @@ ...@@ -5,6 +5,11 @@
"iostream": "c", "iostream": "c",
"get.h": "c", "get.h": "c",
"system_error": "c", "system_error": "c",
"ostream": "c" "ostream": "c",
"initializer_list": "c",
"type_traits": "c",
"xstring": "c",
"xutility": "c",
"stdio.h": "c"
} }
} }
\ No newline at end of file
#define FOUND_RESPONSE "HTTP/1.0 200 OK\r\n" /* add \n*/
#define NOT_FOUND_RESPONSE "HTTP/1.0 404\r\n\r\n"
#include "get.h" #include "get.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/sendfile.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define FOUND 0 #define FOUND 1
#define NOT_FOUND -1 #define NOT_FOUND 0
void get(char* req, char* root, FILE** content) { void parse_request_and_send_response(char* root, char* buffer, int newsockfd) {
// Extract just the file path from the request message into the char array 'path'. const char* path_start = strstr(buffer, "GET") + 4;
const char* path_start = strstr(req, "GET") + 4; const char* path_end = strstr(buffer, "HTTP")-1;
const char* path_end = strstr(req, "HTTP")-1;
char path[path_end - path_start]; char path[path_end - path_start];
// int l = (path_end-path_start);
// printf("%c", l);
strncpy(path, path_start, path_end - path_start); strncpy(path, path_start, path_end - path_start);
path[sizeof(path)] = '\0'; // null terminator path[sizeof(path)] = '\0'; // null terminator
char full_path[sizeof(path) + sizeof(root)]; char full_path[sizeof(path) + sizeof(root)];
strcpy(full_path, root); strcpy(full_path, root);
strcat(full_path, path); strcat(full_path, path);
printf("%s\n _____ \n", full_path);
if (find_file(full_path) == FOUND) open_file(content, full_path);
else *content = NULL;
/* 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 find_file(char* path_to_file) { int file_found(char* path_to_file) {
FILE *fp; /* Try to open the file at given path, check
fp = fopen(path_to_file, "r"); if a non-NULL FILE pointer is returned */
FILE* fp = fopen(path_to_file, "r");
if (fp != NULL) { if (fp != NULL) {
fclose(fp); fclose(fp);
printf("FOUND\n");
return FOUND; return FOUND;
} else { } else {
printf("NOT FOUND\n");
return NOT_FOUND; return NOT_FOUND;
} }
} }
void open_file(FILE** file, char* path_to_file) { void respond_OK_and_send_file(char* full_path, int newsockfd) {
*file = fopen(path_to_file, "r"); /* First open the file, get the information for
the header (size, mime type) and send the file */
int fd = open(full_path, O_RDONLY);
size_t size = get_size(fd);
char response[128];
make_response_header(response, full_path, size);
write(newsockfd, response, strlen(response));
sendfile(newsockfd, fd, NULL, size);
close(fd);
}
void respond_NOTFOUND(int newsockfd) {
/* Send a 404 NOT FOUND response to the client */
write(newsockfd, NOT_FOUND_RESPONSE, strlen(NOT_FOUND_RESPONSE));
}
size_t get_size(int fd) {
struct stat st;
fstat(fd, &st);
return st.st_size;
}
void get_mime_type(char* path, char* mime) {
if (strstr(path, ".jpg") != NULL) {
strcpy(mime, "image/jpeg");
} else if (strstr(path, ".html") != NULL) {
strcpy(mime, "text/html");
} else if (strstr(path, ".css") != NULL) {
strcpy(mime, "text/css");
} else if (strstr(path, ".js") != NULL) {
strcpy(mime, "text/javascript");
} else {
strcpy(mime, "unknown");
}
}
void make_response_header(char* response, char* path, size_t 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);
} }
\ No newline at end of file
...@@ -5,8 +5,12 @@ ...@@ -5,8 +5,12 @@
#include <stdio.h> #include <stdio.h>
/* Prototypes for the functions */ /* Prototypes for the functions */
void get(char* req, char* root, FILE** content); // means everything incl header void parse_request_and_send_response(char* root, char* buffer, int newsockfd);
int find_file(char* path_to_file); int file_found(char* path_to_file);
void open_file(FILE** file, char* path_to_file); void respond_OK_and_send_file(char* full_path, int newsockfd);
void respond_NOTFOUND(int newsockfd);
void get_mime_type(char* path, char* mime);
void make_response_header(char* response, char* full_path, size_t size);
size_t get_size(int fd);
#endif #endif
\ No newline at end of file
...@@ -4,11 +4,13 @@ ...@@ -4,11 +4,13 @@
#define FOUND_RESPONSE "HTTP/1.0 200 OK\r\n" /* add \n*/ #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 NOT_FOUND_RESPONSE "HTTP/1.0 404\r\n\r\n"
#define FOUND 0
#define BUF 100000 #define BUF 100000
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <pthread.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
...@@ -27,7 +29,7 @@ int main(int argc, char *argv[]) ...@@ -27,7 +29,7 @@ int main(int argc, char *argv[])
socklen_t clilen; socklen_t clilen;
int n; int n;
if (argc < 2) { if (argc < 2) { // change this
fprintf(stderr,"ERROR, no port provided\n"); fprintf(stderr,"ERROR, no port provided\n");
exit(1); exit(1);
} }
...@@ -41,9 +43,7 @@ int main(int argc, char *argv[]) ...@@ -41,9 +43,7 @@ int main(int argc, char *argv[])
bzero((char *) &serv_addr, sizeof(serv_addr)); bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]); portno = atoi(argv[1]);
/* Create address we're going to listen on (given port number) /* Create address we're going to listen on (given port number)
- converted to network byte order & any IP address for - converted to network byte order & any IP address for
this machine */ this machine */
...@@ -66,7 +66,6 @@ int main(int argc, char *argv[]) ...@@ -66,7 +66,6 @@ int main(int argc, char *argv[])
be accepted. Get back a new file descriptor to communicate on. */ be accepted. Get back a new file descriptor to communicate on. */
newsockfd = accept( sockfd, (struct sockaddr *) &cli_addr, newsockfd = accept( sockfd, (struct sockaddr *) &cli_addr,
&clilen); &clilen);
if (newsockfd < 0) { if (newsockfd < 0) {
perror("ERROR on accept"); perror("ERROR on accept");
exit(1); exit(1);
...@@ -77,38 +76,16 @@ int main(int argc, char *argv[]) ...@@ -77,38 +76,16 @@ int main(int argc, char *argv[])
then process */ then process */
n = read(newsockfd,buffer,BUF-1); n = read(newsockfd,buffer,BUF-1);
/* *************** using the get.c functionality here: */ /* ******************************************************************************** */
/* ******************************************************************************** */
/* ******************************************************************************** */
char* root = argv[2]; char* root = argv[2];
// Extract just the file path from the request message into the char array 'path'. // Extract just the file path from the request message into the char array 'path'.
const char* path_start = strstr(buffer, "GET") + 4; parse_request_and_send_response(root, buffer, newsockfd);
const char* path_end = strstr(buffer, "HTTP")-1;
char path[path_end - path_start];
strncpy(path, path_start, path_end - path_start);
path[sizeof(path)] = '\0'; // null terminator
char full_path[sizeof(path) + sizeof(root)];
strcpy(full_path, root);
strcat(full_path, path);
strcpy(full_path, strchr(strchr(full_path, '/')+1, '/')+1);
printf("%s\n (_(_(_)_)_) \n", full_path);
if (find_file(full_path) == 0) { // 0 =found
int fd = open(full_path, O_RDONLY);
struct stat st;
fstat(fd, &st);
size_t size = st.st_size;
char response[256];
sprintf(response, "%sContent-Length: %zu\r\n\r\n", FOUND_RESPONSE, size);//
printf("(%zu) Hellooo\n%s", size, response);
write(newsockfd, response, strlen(response));
sendfile(newsockfd, fd, NULL, size);
} else{
n = write(newsockfd, NOT_FOUND_RESPONSE, strlen(NOT_FOUND_RESPONSE));
}
printf("Here is the message: %s\n",buffer); // printf("Here is the message: %s\n",buffer);
printf("Here is the path calculated: %s + %s = %s\n", root, path, full_path); // printf("Here is the path calculated: %s + %s = %s\n", root, path, full_path);
if (n < 0) { if (n < 0) {
perror("ERROR writing to socket"); perror("ERROR writing to socket");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment