diff --git a/hashtable.c b/hashtable.c index 14c689036d9811e92ca2d9c71326fe8130aa45fa..a02d701a184cc1ad77aca95678480583a7b5b54a 100644 --- a/hashtable.c +++ b/hashtable.c @@ -139,7 +139,8 @@ void move_to_front(Bucket *bucket, int index){ /************************************************************************/ char* hash_table_get(HashTable *table, char *key) { /*Checks if key in hash table and returns value if present*/ - char* freq = malloc(60*sizeof(char)); + char* freq = calloc(60,sizeof(char)); + assert(freq); assert(table != NULL); assert(key != NULL); @@ -149,7 +150,7 @@ char* hash_table_get(HashTable *table, char *key) { Bucket *bucket = &table->buckets[hash_value]; for (int i=0; i < bucket->n_elems; i++){ if (equal(bucket->keys[i], key)){ - freq = bucket->values[i]; + memcpy(freq, bucket->values[i], strlen(bucket->values[i])); move_to_front(bucket, i); break; } @@ -175,7 +176,8 @@ bool hash_table_has(HashTable *table, char *key) { return false; } char* print_hash_map(HashTable *table){ - char* headers = malloc(100*sizeof(char)); + char* headers = calloc(100,sizeof(char)); + assert(headers); for (int i = 0; i < table->size; i++) { if (table->buckets[i].n_elems > 0){ for (int j=0; j< table->buckets[i].n_elems; j++){ @@ -186,4 +188,20 @@ char* print_hash_map(HashTable *table){ } } return headers; +} + +void free_cookie(HashTable *headers) { + /*Checks if key in hash table and returns value if present*/ + + assert(headers != NULL); + + int hash_value = xor_hash("Set-cookie: ", headers->size); + + // look for existing key + Bucket *bucket = &headers->buckets[hash_value]; + for (int i=0; i < bucket->n_elems; i++){ + if (equal(bucket->keys[i], "Set-cookie: ")){ + free(bucket->values[i]); + } + } } \ No newline at end of file diff --git a/hashtable.h b/hashtable.h index eb041bfbd170732cc0c84a86e3eb4d16d47c48ec..4f503861db0dfb7cb9a084f685148586a1352ab5 100644 --- a/hashtable.h +++ b/hashtable.h @@ -23,4 +23,6 @@ char* hash_table_get(HashTable *table, char *key); //Checks if the key exists in hash table bool hash_table_has(HashTable *table, char *key); -char* print_hash_map(HashTable *table); \ No newline at end of file +char* print_hash_map(HashTable *table); + +void free_cookie(HashTable *headers); \ No newline at end of file diff --git a/hashtable.o b/hashtable.o index 181ffc4f2d1b1f243e3a17c0104bfeb5cda7f9f7..e645c3024283be1594fc9b89e8ea70900f41bc1d 100644 Binary files a/hashtable.o and b/hashtable.o differ diff --git a/http-parser.o b/http-parser.o index 2b8275ce460326732ac11963bb8fce87c42ee2ee..991595bdb34393e6913860faf4886e396ea822c7 100644 Binary files a/http-parser.o and b/http-parser.o differ diff --git a/http-response.c b/http-response.c index ae80e7e2cf769e8d937de9196fb2577ed9e32e1a..679a02d86dea39855f05dec5d39ac4879055c28a 100644 --- a/http-response.c +++ b/http-response.c @@ -27,8 +27,8 @@ Response* initialise_session(Request* request){ resp->version = request->version; resp->phrase = "OK"; resp->header = new_hash_table(2); - char *cook = cookie_generator(); - hash_table_put(resp->header, "Set-cookie: ", cook); + char *cookie = cookie_generator(); + hash_table_put(resp->header, "Set-cookie: ", cookie); resp->body=""; return resp; } @@ -44,14 +44,17 @@ char* parse_response(Response* response){ strcat(response_string, " "); strcat(response_string, response->phrase); strcat(response_string, "\r\n"); - strcat(response_string, print_hash_map(response->header)); + char* headers = print_hash_map(response->header); + memcpy(response_string, headers, strlen(headers)); strcat(response_string, "\r\n"); strcat(response_string, response->body); + free(headers); return response_string; } void free_response(Response* resp){ + free_cookie(resp->header); free_hash_table(resp->header); free(resp); -} \ No newline at end of file +} diff --git a/http-response.h b/http-response.h index adbcb3ade2780f8d4161958625ccda8668e85a6a..11a55c2c670be3afbe20c2dc46892d3291d134da 100644 --- a/http-response.h +++ b/http-response.h @@ -17,5 +17,6 @@ char* parse_response(Response* response); char* cookie_generator(); +void free_response(Response* resp); Response* initialise_session(Request* request); \ No newline at end of file diff --git a/http-response.o b/http-response.o index 55b6aae6b41e473941b9be6f93416a2973bd89f6..4b2466c739a994873f2cb6feae255a160a83ea93 100644 Binary files a/http-response.o and b/http-response.o differ diff --git a/http-server.c b/http-server.c index 5a857bbf363dacd7bb15c296cbe8b6dc50ef4f8f..94429feab5a21914302a488444941bdc2c377516 100644 --- a/http-server.c +++ b/http-server.c @@ -22,8 +22,14 @@ #include <unistd.h> #include "http-parser.h" #include "http-response.h" +#include "hashtable.h" #include "user.h" +#define ROUND_1 3 +#define ROUND_2 4 +#define ROUND_3 1 +#define ROUND_4 2 + // constants static char const * const HTTP_200_FORMAT = "HTTP/1.1 200 OK\r\n\ Content-Type: text/html\r\n\ @@ -33,6 +39,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; + + char *substring(char *string, int position, int length) { char *p; @@ -287,8 +295,13 @@ static bool handle_http_request(int sockfd, User_list *users) // char* resp_string = parse_response(resp); // printf("COOKIE CREATING RESP %s\n", resp_string); // player_session(buff, sockfd, "1_welcome.html", resp_string); + // User* new_player = new_user(sockfd); + // add_user(new_player, users); + // char* cookie = hash_table_get(resp->header, "Set-cookie: ")+13; + // cookie = strtok(cookie, ";"); + // printf("the cookie token is %s*****\n", cookie); // free(resp_string); - // free(resp); + // free_response(resp); User* new_player = new_user(sockfd); add_user(new_player, users); get_request(buff,sockfd, "1_welcome.html"); diff --git a/http-server.o b/http-server.o index 2f79510ca1627adfe514713bbe53a0d37c8f3fe4..57139f56959c935e10194905dff25c3ecae120c1 100644 Binary files a/http-server.o and b/http-server.o differ diff --git a/image_tagger b/image_tagger index 759d813b60e17389ec90cab269c2df6094a4ef22..0a73c5e2bf39bfe9fdf2382d2d6ad6921343eddc 100644 Binary files a/image_tagger and b/image_tagger differ diff --git a/user.c b/user.c index 40129546ab9ea24b2a4f012a44543c4bf924d57a..9af591675f28887fd5e62f205f89ab6f37cc6841 100644 --- a/user.c +++ b/user.c @@ -18,6 +18,7 @@ User* new_user(int id){ user->n_keywords = 0; user->status = WAIT; user->keywords = calloc(user->n_capacity,sizeof(char*)); + user->round = 0; assert(user->keywords); return user; } @@ -108,6 +109,20 @@ void change_player_status(int user_id, User_list* users, STATUS status){ } } +void change_player_round(int user_id, User_list* users){ + for(int i=0; i < users->n_users; i++){ + if (users->users[i]->id == user_id){ + if (users->users[i]->round < 4){ + users->users[i]->round++; + } + else{ + users->users[i]->round = 1; + } + } + } +} + + bool keyword_match(User* user, char* keyword){ for(int i=0; i <user->n_keywords;i++){ printf("words being matched is with %s and %s\n", keyword, user->keywords[i]); diff --git a/user.h b/user.h index 025b02683b3781652e6dc0bb5afa2a58671df7ee..08588449f80322ba54fb810622d6af105c1fe339 100644 --- a/user.h +++ b/user.h @@ -15,6 +15,7 @@ typedef struct User { int n_keywords; STATUS status; int n_capacity; + int round; }User; typedef struct User_list{ @@ -42,6 +43,8 @@ bool players_ready(User_list* users); void change_player_status(int user_id, User_list* users, STATUS status); +void change_player_round(int user_id, User_list* users); + bool keyword_match(User* user, char* keyword); void reset_players(User_list *users); diff --git a/user.o b/user.o index 49a932a02a49d25a7030cd94a522b1b939675585..0af93b19e77ba6c543082c2de78573050b49a3fb 100644 Binary files a/user.o and b/user.o differ