From fd081b11ca49029bbcf7bdd180e71d014284fb63 Mon Sep 17 00:00:00 2001
From: Saleh Ahmed Khan <s.khan20@student.unimelb.edu.au>
Date: Sun, 15 Apr 2018 15:59:21 +1000
Subject: [PATCH] partially working server

---
 .gitignore    |  4 ++++
 get.c         | 45 +++++++++++++++++++++++++++++++++++++++++++++
 get.h         | 10 ++++++++++
 home/file.css |  0
 makefile      |  2 +-
 server.c      | 34 +++++++++++-----------------------
 6 files changed, 71 insertions(+), 24 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 get.c
 create mode 100644 get.h
 create mode 100644 home/file.css

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3333489
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+/.vscode
+client
+server
+debug
\ No newline at end of file
diff --git a/get.c b/get.c
new file mode 100644
index 0000000..7d4b488
--- /dev/null
+++ b/get.c
@@ -0,0 +1,45 @@
+#include "get.h"
+#include <stdio.h>
+#include <string.h>
+
+
+void valid_get(char* buffer) { // checks if the request is valid
+    // const char* path_start = strchr(buffer, ' ');
+    // const char* path_end = strrchr(req, ' ');
+
+    // printf("gucci\n");
+}
+
+void parse(char* req, char* root) {
+    // Extract just the file path from the request
+    // message into the char array 'path'.
+    // printf("%s\n", req);
+    const char* path_start = strchr(req, ' ') + 1;
+    // printf("%c start\n", *path_start);
+    const char* path_end = strrchr(req, ' ');
+    // printf("%c final\n", *path_end);
+    // int path_len = path_start - path_end;
+    char path[path_end - path_start];
+    strncpy(path, path_start, path_end - path_start); 
+    path[sizeof(path)] = '\0'; // null terminator
+    // printf("root %s\n", root);
+    // printf("path %s\n", path);
+    char full_path[sizeof(path) + sizeof(root)];
+    strncpy(full_path, root, sizeof(root));
+    strcat(full_path, path);
+    // full_path[sizeof(full_path)-1] = 9;
+    printf("waiittt %s end\n", full_path);
+    find_file(full_path);
+}
+
+void find_file(char* path_to_file) {
+    FILE *fp;
+
+    fp = fopen(path_to_file, "r");
+    if (fp != NULL) {
+        printf("success...\n");fclose(fp);
+    } else {
+        printf("fail\n");
+    }
+    
+}
\ No newline at end of file
diff --git a/get.h b/get.h
new file mode 100644
index 0000000..4fe2996
--- /dev/null
+++ b/get.h
@@ -0,0 +1,10 @@
+/* the include guards */
+#ifndef GET_H_INCLUDED
+#define GET_H_INCLUDED
+
+/* Prototypes for the functions */
+void valid_get(char* buffer);
+void parse(char* req, char* root);
+void find_file(char* path_to_file);
+
+#endif
\ No newline at end of file
diff --git a/home/file.css b/home/file.css
new file mode 100644
index 0000000..e69de29
diff --git a/makefile b/makefile
index 922856f..30c7d65 100644
--- a/makefile
+++ b/makefile
@@ -1,5 +1,5 @@
 server: server.c
-	gcc -o server server.c
+	gcc -o server server.c get.c
 
 client: client.c
 	gcc -o client client.c
diff --git a/server.c b/server.c
index 35a9984..74c0557 100644
--- a/server.c
+++ b/server.c
@@ -10,6 +10,7 @@
 #include <netinet/in.h>
 #include <unistd.h>
 
+#include "get.h"
 
 int main(int argc, char *argv[])
 {
@@ -19,18 +20,14 @@ int main(int argc, char *argv[])
 	socklen_t clilen;
 	int n;
 
-	if (argc < 2) 
-	{
+	if (argc < 2) {
 		fprintf(stderr,"ERROR, no port provided\n");
 		exit(1);
 	}
 
 	 /* Create TCP socket */
-	
 	sockfd = socket(AF_INET, SOCK_STREAM, 0);
-
-	if (sockfd < 0) 
-	{
+	if (sockfd < 0) {
 		perror("ERROR opening socket");
 		exit(1);
 	}
@@ -43,64 +40,55 @@ int main(int argc, char *argv[])
 	/* Create address we're going to listen on (given port number)
 	 - converted to network byte order & any IP address for 
 	 this machine */
-	
 	serv_addr.sin_family = AF_INET;
 	serv_addr.sin_addr.s_addr = INADDR_ANY;
 	serv_addr.sin_port = htons(portno);  // store in machine-neutral format
 
 	 /* Bind address to the socket */
-	
-	if (bind(sockfd, (struct sockaddr *) &serv_addr,
-			sizeof(serv_addr)) < 0) 
-	{
+	if (bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
 		perror("ERROR on binding");
 		exit(1);
 	}
 	
 	/* Listen on socket - means we're ready to accept connections - 
 	 incoming connection requests will be queued */
-	
 	listen(sockfd,5);
-	
 	clilen = sizeof(cli_addr);
 
 	/* Accept a connection - block until a connection is ready to
 	 be accepted. Get back a new file descriptor to communicate on. */
-
 	newsockfd = accept(	sockfd, (struct sockaddr *) &cli_addr, 
 						&clilen);
 
-	if (newsockfd < 0) 
-	{
+	if (newsockfd < 0) {
 		perror("ERROR on accept");
 		exit(1);
 	}
 	
 	bzero(buffer,256);
-
 	/* Read characters from the connection,
 		then process */
-	
 	n = read(newsockfd,buffer,255);
 
-	if (n < 0) 
-	{
+	if (n < 0) {
 		perror("ERROR reading from socket");
 		exit(1);
 	}
 	
+    /* using the get.c functionality here: */
+	char* root = argv[2];
+    parse(buffer, root);
+
 	printf("Here is the message: %s\n",buffer);
 
 	n = write(newsockfd,"I got your message",18);
 	
-	if (n < 0) 
-	{
+	if (n < 0) {
 		perror("ERROR writing to socket");
 		exit(1);
 	}
 	
 	/* close socket */
-	
 	close(sockfd);
 	
 	return 0; 
-- 
GitLab