From 0fe4bac2f0092121e735142d65cfa9da52abfc12 Mon Sep 17 00:00:00 2001 From: Anqi Chen <a.chen49@student.unimelb.edu.au> Date: Mon, 29 Apr 2019 20:17:48 +1000 Subject: [PATCH] edit_load_html --- img.c | 74 +++++++++++------------------------------------------------ 1 file changed, 13 insertions(+), 61 deletions(-) diff --git a/img.c b/img.c index 8e01421..3fec01b 100644 --- a/img.c +++ b/img.c @@ -69,7 +69,6 @@ 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[]); @@ -174,6 +173,7 @@ struct User* the_other_user(struct User* curr_user, struct User* user_arr[]){ bool match(char* w1, char** w2, int n_word){ for (int i=0; i<n_word; i++){ if (w2[i]==w1){ + printf("all good"); return true; } } @@ -205,25 +205,7 @@ void simple_load_html(char* html, int sockfd, char* buff, int n, struct User* cu close(filefd); } - /* adds the new word in the html (between the <body> */ -/*void plug_in_html(char* newword, char* old_buffer, char* new_buffer){ - // slice the html string into 2 parts - char start[MAX_BUFF]; - char* end = strstr(old_buffer, "</body>"); - - // front part - strncpy(start, old_buffer, strlen(old_buffer)-strlen(end)); - start[strlen(start)] = '\0'; - - // combine - strcpy(new_buffer, start); - strcat(new_buffer, newword); - strcat(new_buffer, end); - - 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){ @@ -252,24 +234,31 @@ void edit_load_html(char* html, int sockfd, char* buff, char* newword,struct Use } close(filefd); - + // create two buffers to read the thing before and after </body> char front_buff[MAX_BUFF], back_buff[MAX_BUFF]; int endlen=strstr(buff, "</body>")-buff; + // close teh string strcpy(back_buff,"\n\r\r<p> "); back_buff[strlen(back_buff)]='\0'; + + //add new word at the back_buff strcat(back_buff, newword); back_buff[strlen(back_buff)]='\0'; + + // close back_buff 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'; + //merge strcpy(buff, front_buff); buff[strlen(buff)]='\0'; strcat(buff, back_buff); buff[strlen(buff)]='\0'; + //write if (write(sockfd, buff, size) < 0){ perror("write"); return; @@ -278,46 +267,7 @@ void edit_load_html(char* html, int sockfd, char* buff, char* newword,struct Use } -/* -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); - - // makes new buffer, copies the username into it - char* new_buffer = (char *) malloc(sizeof(MAX_BUFF)); - plug_in_html(newword, buff, new_buffer); - - if (write(sockfd, new_buffer, size) < 0) - { - perror("write"); - return; - } - free(new_buffer); -} -*/ +// read the words into the curr_user->keywords void record_keyword(struct User* curr_user, char *new_guess){ for (int i=0; i<=curr_user->n_word; i++){ @@ -331,6 +281,8 @@ void record_keyword(struct User* curr_user, char *new_guess){ } } + +// handle with every single thread from the client requests static bool handle_http_request(int sockfd, struct User* user_arr[]) { // try to read the request @@ -412,7 +364,7 @@ static bool handle_http_request(int sockfd, struct User* user_arr[]) { char* username = strstr(buff, "user=")+5; char u[MAX_BUFF]; - strcpy(u,"<br>"); + strcpy(u,","); strcat(u, username); u[strlen(username)]='\0'; curr_user->isOnline = 1; -- GitLab