From e2c508f3733ebdef16873228b6bfd2624638c0ed Mon Sep 17 00:00:00 2001
From: Anqi Chen <a.chen49@student.unimelb.edu.au>
Date: Mon, 29 Apr 2019 19:54:45 +1000
Subject: [PATCH] edit_load_html

---
 img.c | 135 ++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 98 insertions(+), 37 deletions(-)

diff --git a/img.c b/img.c
index 7daae2e..8e01421 100644
--- a/img.c
+++ b/img.c
@@ -1,7 +1,7 @@
 /*
 ** http-server.c
 */
-
+/********************************************Libraries**********************************************/
 #include <errno.h>
 #include <stdbool.h>
 #include <stdio.h>
@@ -20,11 +20,13 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+/********************************************constants**********************************************/
+
 #define GUESS_SUFFIX "&guess=Guess"
 #define MAX_BUFF 2049
 
 
-
+/********************************************statics**********************************************/
 // cookie for user, formtted with userID
 static char const * const HTTP_200_FORMAT = "HTTP/1.1 200 OK\r\n\
 Content-Length: %ld\r\n\
@@ -40,8 +42,8 @@ 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;
 
-
-
+/********************************************Structs**********************************************/
+// enumerate all the methods
 typedef enum
 {
     GET,
@@ -53,21 +55,33 @@ typedef enum
 
 // struct User
 struct User {
-    int userID;
-    char* username;
-    char** keywords;
-    int isOnline; // Online=1, OffLine=0
-    int stage;
-    int n_word;
+    int userID;        // userID is the identification of the user, used for cookie  
+    char** keywords;   // an array of guessing words
+    int isOnline;      // Online=1, OffLine=0
+    int stage;         // record the stage of the user
+    int n_word;        // number of guessing words
 };
+/********************************************Function Signitures**********************************************/
+struct User* newUser();
+void clear_user_keyword(struct User* user);
+int find_cookie(char* buff);
+struct User* find_curr(struct User* user_arr[], char* buff);
+struct User* the_other_user(struct User* curr_user, struct User* user_arr[]);
+bool match(char* w1, char** w2, int n_word);
+void simple_load_html(char* html, int sockfd, char* buff, int n, struct User* curr_user);
+//void plug_in_html(char* newword, char* old_buffer, char* new_buffer);
+void edit_load_html(char* html, int sockfd, char* buff, char* newword,struct User* curr_user);
+void record_keyword(struct User* curr_user, char *new_guess);
+static bool handle_http_request(int sockfd, struct User* user_arr[]);
+/********************************************Functions**********************************************/
+
+
 
 
 // malloc a newUser
 struct User* newUser() {
     struct User* newUser = malloc(sizeof (struct User));
     newUser->userID = 0;
-    //newUser->username = malloc(MAX_BUFF*sizeof(char));
-    //newUser->keywords = NULL;
     newUser->keywords =malloc(MAX_BUFF*sizeof(char*));
     for (int i = 0; i < MAX_BUFF; i++)
     {
@@ -79,6 +93,7 @@ struct User* newUser() {
     return newUser;
 }
 
+// clear user: point the array to NULL, reset the int to 0
 void clear_user_keyword(struct User* user){
     user->stage=0;
     user->isOnline=0;
@@ -89,6 +104,8 @@ void clear_user_keyword(struct User* user){
     }
 }
 
+
+//find the cookie according to id, in buff
 int find_cookie(char* buff)
 {
     char* cookie_id=strstr(buff, "cookie_id=");
@@ -103,27 +120,22 @@ int find_cookie(char* buff)
         strcpy(temp, cookie_id);
         temp+=10;
     }
-    printf("find_cookie in buff %s\n", cookie_id);
-    //int cookie_length = strlen(username);
     temp[strlen(temp)-3]='\0';
-    printf("find_cookie in buff %s\n", cookie_id);
-
-
     return  atoi(temp);
 }
 
-
+// find the current user by analyse buffer
 struct User* find_curr(struct User* user_arr[], char* buff)
 {
     int cookie_id = find_cookie(buff);
 
-    printf("the kmimimji%d  %d\n", cookie_id,user_arr[0]->userID);
-    if(cookie_id==0){
+    // if the cookie has not set up, assign it with a new id and malloc the space
+        if(cookie_id==0){
         
         struct User* curr_user = newUser();
         
-
-
+        //if never accessed before, create new user
+        //check according to cookie and id
         if ((cookie_id!=1||cookie_id!=2)&& user_arr[0]->userID!=1)
         {    
             curr_user->userID = 1;
@@ -136,7 +148,7 @@ struct User* find_curr(struct User* user_arr[], char* buff)
         cookie_id=curr_user->userID;
         printf(" func curr: cookie_id is %d\n%d\n\n\n", cookie_id,curr_user->userID);
     }
-    
+    // get the user from array
     for (int i=0; i<2; i++) {
         if (user_arr[i]->userID == cookie_id)
         {               
@@ -148,6 +160,8 @@ struct User* find_curr(struct User* user_arr[], char* buff)
 
 }
 
+
+// the user in arrsy if not this one, then should be that one
 struct User* the_other_user(struct User* curr_user, struct User* user_arr[]){
     for(int i=0; i<2; i++){
         if (user_arr[i]!=curr_user){
@@ -156,7 +170,7 @@ struct User* the_other_user(struct User* curr_user, struct User* user_arr[]){
     }
 }
 
-
+// try to match the new guess to the other user keywords
 bool match(char* w1, char** w2, int n_word){
     for (int i=0; i<n_word; i++){
         if (w2[i]==w1){
@@ -166,11 +180,11 @@ bool match(char* w1, char** w2, int n_word){
     return false;
 }
 
-
+// only load page
 void simple_load_html(char* html, int sockfd, char* buff, int n, struct User* curr_user){
     struct stat st;
     stat(html, &st);
-    n = sprintf(buff, HTTP_200_FORMAT, st.st_size+6,curr_user->userID);
+    n = sprintf(buff, HTTP_200_FORMAT, st.st_size,curr_user->userID);
     if (write(sockfd, buff, n) < 0)
             {
                 perror("write");
@@ -192,7 +206,7 @@ void simple_load_html(char* html, int sockfd, char* buff, int n, struct User* cu
 }
 
     /* adds the new word in the html (between the <body> */
-void plug_in_html(char* newword, char* old_buffer, char* new_buffer){
+/*void plug_in_html(char* newword, char* old_buffer, char* new_buffer){
 
     // slice the html string into 2 parts
     char start[MAX_BUFF];
@@ -209,9 +223,62 @@ void plug_in_html(char* newword, char* old_buffer, char* new_buffer){
 
     new_buffer[strlen(new_buffer)] = '\0';
 }
+*/
+
+// load page with new words 
+void edit_load_html(char* html, int sockfd, char* buff, char* newword,struct User* curr_user){
+    // get the size of the file
+    struct stat st;
+    stat(html, &st);
+
+    // increase file size to accommodate the new guess
+    int added_length= strlen(newword);
+    long size = st.st_size + added_length;
+    int n = sprintf(buff, HTTP_200_FORMAT, size, curr_user->userID);
+    // send the header first
+    if (write(sockfd, buff, n) < 0)
+    {
+        perror("write");
+        return;
+    }
+    // read the content of the HTML file
+    int filefd = open(html, O_RDONLY);
+    n = read(filefd, buff, st.st_size);
+    if (n < 0)
+    {
+        perror("read");
+        close(filefd);
+        return;
+    }
+    close(filefd);
+
 
+    char front_buff[MAX_BUFF], back_buff[MAX_BUFF];
+    int endlen=strstr(buff, "</body>")-buff;
+    strcpy(back_buff,"\n\r\r<p> ");
+    back_buff[strlen(back_buff)]='\0';
+    strcat(back_buff, newword);
+    back_buff[strlen(back_buff)]='\0';
+    strcat(back_buff, "\r\r</p>\r</body> </html>");
+    back_buff[strlen(back_buff)]='\0';
+    strncpy(front_buff, buff, endlen);
+    front_buff[strlen(front_buff)]='\0';
+
+    strcpy(buff, front_buff);
+    buff[strlen(buff)]='\0';
+    strcat(buff, back_buff);
+  
+    buff[strlen(buff)]='\0';
+
+    if (write(sockfd, buff, size) < 0){
+      perror("write");
+      return;
+    }
 
 
+}
+
+/*
 void edit_load_html(char* html, int sockfd, char* buff, char* newword,struct User* curr_user){
 
     // get the size of the file
@@ -221,7 +288,7 @@ void edit_load_html(char* html, int sockfd, char* buff, char* newword,struct Use
     // increase file size to accommodate the new guess
     int added_length= strlen(newword);
     long size = st.st_size + added_length;
-    int n = sprintf(buff, HTTP_200_FORMAT, size+6, curr_user->userID);
+    int n = sprintf(buff, HTTP_200_FORMAT, size, curr_user->userID);
     // send the header first
     if (write(sockfd, buff, n) < 0)
     {
@@ -250,12 +317,14 @@ void edit_load_html(char* html, int sockfd, char* buff, char* newword,struct Use
     }
     free(new_buffer);
 }
-
+*/
 void record_keyword(struct User* curr_user, char *new_guess){
     for (int i=0; i<=curr_user->n_word; i++){
+
         if(curr_user->keywords[curr_user->n_word]==NULL){
             curr_user->keywords[curr_user->n_word] = malloc((1 + strlen(new_guess)) * sizeof(char*));
             strcpy(curr_user->keywords[curr_user->n_word],new_guess);
+            printf("record_keyword :curr_user->userID %d the last word stored is %s", curr_user->userID, curr_user->keywords[curr_user->n_word]);
             curr_user->n_word++;
             break;
         }
@@ -412,15 +481,7 @@ static bool handle_http_request(int sockfd, struct User* user_arr[])
 
 
         }
-        
 
-
-
-            
-
-
-
-        
         else
             // never used, just for completeness
             fprintf(stderr, "no other methods supported");
-- 
GitLab