From 914da89e5e6bae45a3403a85f2b7b2d1039b7f2b Mon Sep 17 00:00:00 2001
From: Alexander Cock <acock@student.unimelb.edu.au>
Date: Wed, 1 May 2019 07:36:11 +0000
Subject: [PATCH] Fixed postRequest function, made page names into static
 strings

---
 image_tagger.c | 68 ++++++++++++++++++++++++++++++++------------------
 1 file changed, 44 insertions(+), 24 deletions(-)

diff --git a/image_tagger.c b/image_tagger.c
index 65edecf..0c4ae13 100644
--- a/image_tagger.c
+++ b/image_tagger.c
@@ -30,16 +30,23 @@ static int const HTTP_400_LENGTH = 47;
 static char const * const HTTP_404 = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
 static int const HTTP_404_LENGTH = 45;
 
-//static char * PLAYER_1_NAME;
-//static char * PLAYER_2_NAME;
-//static int PLAYER_1_GAME_STATE = 0;
-//static int PLAYER_2_GAME_STATE = 0;
-
+static char* P1_NAME;
+static char* P2_NAME;
+static int P1_GAME_STATE = 0;
+static int P2_GAME_STATE = 0;
 static char* KEYWORD_LIST_P1[100];
 static char* KEYWORD_LIST_P2[100];
 static int NUM_KEYWORD_P1 = 0;
 static int NUM_KEYWORD_P2 = 0;
 
+static char* PAGE_1 = "1_intro.html";
+static char* PAGE_2 = "2_start.html";
+static char* PAGE_3 = "3_first_turn.html";
+static char* PAGE_4 = "4_accepted.html";
+static char* PAGE_5 = "5_discarded.html";
+static char* PAGE_6 = "6_endgame.html";
+static char* PAGE_7 = "7_gameover.html";
+
 // represents the types of method
 typedef enum
 {
@@ -52,25 +59,25 @@ static char* getURL(char* curr) {
 	char* url;
 	if (strncmp(curr, "1_intro.html ", 13) == 0 || strncmp(curr, " HTTP/1.1", 9) == 0)
     {
-        url = "1_intro.html";
+        url = PAGE_1;
     } else if(strncmp(curr, "2_start.html ", 13) == 0)
     {
-        url = "2_start.html";
+        url = PAGE_2;
     } else if(strncmp(curr, "3_first_turn.html ", 18) == 0 || strncmp(curr, "?start=Start ", 13) == 0)
     {
-        url = "3_first_turn.html";
+        url = PAGE_3;
     } else if(strncmp(curr, "4_accepted.html ", 16) == 0)
     {
-        url = "4_accepted.html";
+        url = PAGE_4;
     } else if(strncmp(curr, "5_discarded.html ", 17) == 0)
     {
-        url = "5_discarded.html";
+        url = PAGE_5;
     } else if(strncmp(curr, "6_endgame.html ", 15) == 0)
     {
-        url = "6_endgame.html";
+        url = PAGE_6;
     } else if(strncmp(curr, "7_gameover.html ", 16) == 0)
     {
-        url = "7_gameover.html";
+        url = PAGE_7;
     } else {
 		url = " ";
 	}
@@ -131,19 +138,32 @@ static bool getRequest(int sockfd, char* buff, char* url, int n) {
 }
 
 static bool postRequest(int sockfd, char* buff, char* url, int n) {
-	char * file;
-	char * post = "hello";
-	if(strcmp(url,"1_intro.html")) {
-		file = "2_start.html";
-	} else if(strcmp(url,"2_start.html")) {
-		//file = "7_gameover.html";
-	} else if(strcmp(url,"3_first_turn.html")) {
-		if(addKeyword(post,1)) {
-			file = "6_endgame.html";
+	char* post;
+	char* file;
+	
+	if((post = strstr(buff,"user=")) != NULL) {
+		if(P1_GAME_STATE == 0) {
+			P1_NAME = post + 5;
+			P1_GAME_STATE++;
+		} else if (P2_GAME_STATE == 0) {
+			P2_NAME = post + 5;
+			P2_GAME_STATE++;
+		}
+		file = PAGE_2;
+	} else if((post = strstr(buff,"keyword=")) != NULL) {
+		if(P1_GAME_STATE == P2_GAME_STATE && P1_GAME_STATE == 2) {
+			if(addKeyword(post+8,1)) {
+				file = PAGE_6;
+			} else {
+				file = PAGE_4;
+			}
 		} else {
-			file = "4_accepted.html";
+			file = PAGE_5;
 		}
-	} 
+	} else if((post = strstr(buff,"quit=Quit")) != NULL) {
+		file = PAGE_7;
+	}
+	printf("post = %s and file = %s\n", post, file);
 	
 	struct stat st;
     stat(file, &st);
@@ -188,7 +208,7 @@ static bool handle_http_request(int sockfd)
 
     // terminate the string
     buff[n] = 0;
-	printf("%s /n", buff);
+	printf("%s\n", buff);
     char * curr = buff;
 
     // parse the method
-- 
GitLab