Skip to content
Snippets Groups Projects
Commit 914da89e authored by Alexander Cock's avatar Alexander Cock
Browse files

Fixed postRequest function, made page names into static strings

parent e43be603
Branches
No related tags found
No related merge requests found
...@@ -30,16 +30,23 @@ static int const HTTP_400_LENGTH = 47; ...@@ -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 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 int const HTTP_404_LENGTH = 45;
//static char * PLAYER_1_NAME; static char* P1_NAME;
//static char * PLAYER_2_NAME; static char* P2_NAME;
//static int PLAYER_1_GAME_STATE = 0; static int P1_GAME_STATE = 0;
//static int PLAYER_2_GAME_STATE = 0; static int P2_GAME_STATE = 0;
static char* KEYWORD_LIST_P1[100]; static char* KEYWORD_LIST_P1[100];
static char* KEYWORD_LIST_P2[100]; static char* KEYWORD_LIST_P2[100];
static int NUM_KEYWORD_P1 = 0; static int NUM_KEYWORD_P1 = 0;
static int NUM_KEYWORD_P2 = 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 // represents the types of method
typedef enum typedef enum
{ {
...@@ -52,25 +59,25 @@ static char* getURL(char* curr) { ...@@ -52,25 +59,25 @@ static char* getURL(char* curr) {
char* url; char* url;
if (strncmp(curr, "1_intro.html ", 13) == 0 || strncmp(curr, " HTTP/1.1", 9) == 0) 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) } 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) } 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) } 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) } 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) } 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) } else if(strncmp(curr, "7_gameover.html ", 16) == 0)
{ {
url = "7_gameover.html"; url = PAGE_7;
} else { } else {
url = " "; url = " ";
} }
...@@ -131,19 +138,32 @@ static bool getRequest(int sockfd, char* buff, char* url, int n) { ...@@ -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) { static bool postRequest(int sockfd, char* buff, char* url, int n) {
char* post;
char* file; char* file;
char * post = "hello";
if(strcmp(url,"1_intro.html")) { if((post = strstr(buff,"user=")) != NULL) {
file = "2_start.html"; if(P1_GAME_STATE == 0) {
} else if(strcmp(url,"2_start.html")) { P1_NAME = post + 5;
//file = "7_gameover.html"; P1_GAME_STATE++;
} else if(strcmp(url,"3_first_turn.html")) { } else if (P2_GAME_STATE == 0) {
if(addKeyword(post,1)) { P2_NAME = post + 5;
file = "6_endgame.html"; 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 { } 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; struct stat st;
stat(file, &st); stat(file, &st);
...@@ -188,7 +208,7 @@ static bool handle_http_request(int sockfd) ...@@ -188,7 +208,7 @@ static bool handle_http_request(int sockfd)
// terminate the string // terminate the string
buff[n] = 0; buff[n] = 0;
printf("%s /n", buff); printf("%s\n", buff);
char * curr = buff; char * curr = buff;
// parse the method // parse the method
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment