diff --git a/hashtable.c b/hashtable.c index c85a4c03f1c7b5a9de74e74d112b7701ddfedc45..4238967ba680b9bac7e68eedbc90db66818c5f27 100644 --- a/hashtable.c +++ b/hashtable.c @@ -110,7 +110,6 @@ void hash_table_put(HashTable *table, char *key, char *value) { int hash_value = xor_hash(key, table->size); // look for existing key Bucket *bucket = &table->buckets[hash_value]; - printf("the number of elements in bucket is %d\n", bucket->n_elems); for (int i=0; i < bucket->n_elems; i++){ if (equal(bucket->keys[i], key)){ bucket->values[i] = value; @@ -120,7 +119,6 @@ void hash_table_put(HashTable *table, char *key, char *value) { // adds key and value as not in hash table bucket = add_to_bucket(bucket, key, value); table->buckets[hash_value] = *bucket; - printf("the number of elements in bucket is %d\n", bucket->n_elems); } /************************************************************************/ void move_to_front(Bucket *bucket, int index){ diff --git a/http-parser.c b/http-parser.c index 9d2f9d2aea973005f55ca68ab06d6a1a7a4898cc..4be000b70ee86d935b1b00d9a155e48d639d1df0 100644 --- a/http-parser.c +++ b/http-parser.c @@ -97,5 +97,5 @@ void free_request(Request* req){ * */ free_hash_map(req->header); free_hash_table(req->header); - // free(req); + free(req); } diff --git a/http-server.c b/http-server.c index 36212c2bf8d71a9882d5c80a025164d3b593ad9e..49123759ff4260644324d65da93476ebb4ff3804 100644 --- a/http-server.c +++ b/http-server.c @@ -54,7 +54,6 @@ bool player_session(char* buff, int sockfd, char* file_name, char* response){ stat(file_name, &st); // printf("pre add %s\n", buff); int n = sprintf(buff, response, st.st_size); - printf("post add %s\n", buff); // send the header first if (write(sockfd, buff, n) < 0) { @@ -124,9 +123,7 @@ bool response(char *buff, int sockfd, char* file_name){ * */ struct stat st; stat(file_name, &st); - printf("pre add %s\n", buff); int n = sprintf(buff, HTTP_200_FORMAT, st.st_size); - printf("post add %s\n", buff); // send the header first if (write(sockfd, buff, n) < 0) { @@ -252,23 +249,24 @@ static bool handle_http_request(int sockfd, User_list *user_list){ // parse the method // printf("REQUEST IS \n\n%s\n", buff); req = parse_request(curr); - + char* cookie = hash_table_get(req->header, "Cookie:")+3; + int id = atoi(cookie+3); + User* user = get_current_user(user_list, id); // user should quit game if(strncmp(req->body, "quit=Quit", 9) == 0){ - change_player_status(sockfd,user_list, QUIT); - response(buff,sockfd, "7_gameover.html"); - // free_request(req); - // return false; + if (user != NULL){ + change_player_status(id,user_list, QUIT); + response(buff,sockfd, "7_gameover.html"); + // free_request(req); + // return false; + } } // user starting game else if (strncmp(req->url, "/?start=Start", 24) == 0){ // printf("THE COOKIE IS %s\n", hash_table_get(req->header, "Cookie: ")); - int id = atoi(hash_table_get(req->header, "Cookie:")+3); - User* user = get_current_user(user_list, id); // user on first turn if (user != NULL){ - printf("user aint null"); if (req->method == GET){ change_player_status(id, user_list, READY); int round = change_player_round(id, user_list); @@ -303,8 +301,8 @@ static bool handle_http_request(int sockfd, User_list *user_list){ //request(buff,sockfd, "5_discarded.html"); } else{ - char* keyword = add_keyword(sockfd, user_list, req->body); - + char* keyword = add_keyword(id, user_list, req->body); + printf("keyword is %s\n", keyword); // game ends as submitted keyword has been submitted by other player if(has_match_ended(user_list, keyword, id)){ response(buff,sockfd, "6_endgame.html"); @@ -330,12 +328,16 @@ static bool handle_http_request(int sockfd, User_list *user_list){ // user's name stored and rendered on screen if (req->method == POST) { - char *name = strchr(req->body, '=')+1; - // printf("**%s**\n", name); + size_t name_len = strlen(req->body+5); + printf("name string is: %s, size/len is: %ld\n\n", req->body+5, name_len); + char *name = calloc(name_len+1, sizeof(char)); + assert(name); + memcpy(name,req->body+5, name_len); if (name != NULL){ for (int i=0; i < user_list->n_users; i++){ - if(user_list->users[i]->id == sockfd){ - user_list->users[i]->name = name; + if(user_list->users[i]->id == id){ + user_list->users[i]->name = name; + printf("name is %s\n", user_list->users[i]->name); } } // get_request(buff,sockfd, "2_start.html"); @@ -371,9 +373,8 @@ static bool handle_http_request(int sockfd, User_list *user_list){ // response(buff,sockfd, "1_welcome.html"); } else if (req->method == GET){ - int id = atoi(hash_table_get(req->header, "Cookie:")+3); - User* user = get_current_user(user_list, id); if (user != NULL){ + printf("name of user is %s\n", user->name); text_render_response(buff,sockfd, "2_start.html", user->name); } } @@ -385,6 +386,7 @@ static bool handle_http_request(int sockfd, User_list *user_list){ else if (write(sockfd, HTTP_404, HTTP_404_LENGTH) < 0) { free_request(req); + free(cookie); perror("write"); return false; } @@ -407,6 +409,7 @@ static bool handle_http_request(int sockfd, User_list *user_list){ printf("is restart\n"); } } + free(cookie); free_request(req); return true; } @@ -416,9 +419,6 @@ static void exit_handler(int sig){ * Closes TCP Connection */ keep_alive = 0; - if (req){ - free_request(req); - } if (user_list){ free_users(user_list); } diff --git a/user.c b/user.c index bc884a3c7483c4985b1e27cc9a835643bcb35178..93d925999fc11b9d9ce9262f47f7476bcb815e37 100644 --- a/user.c +++ b/user.c @@ -108,6 +108,7 @@ void free_users(User_list* user_list){ // free(&users->users[i]->keywords[j]); // } free(user_list->users[i]->keywords); + free(user_list->users[i]->name); free(user_list->users[i]); } free(user_list->users); @@ -200,11 +201,8 @@ bool keyword_match(User* user, char* keyword){ /** * Checks if keyword has already been submitted * */ - printf("keyword is %s\n\n", keyword); for(int i=0; i <user->n_keywords;i++){ - printf("words being matched is with %s and %s\n", keyword, user->keywords[i]); if(strcmp(keyword, user->keywords[i]) == 0){ - printf("match is with %s and %s\n", keyword, user->keywords[i]); return true; } }