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 deleted file mode 100644 index 006163fa4be2a66e79f11cc80bcc1cee4acdae9b..0000000000000000000000000000000000000000 Binary files a/hashtable.o and /dev/null differ diff --git a/http-parser.o b/http-parser.o deleted file mode 100644 index feeecba7d45e8a54a1ce6497414c0d2ab8aca3bd..0000000000000000000000000000000000000000 Binary files a/http-parser.o and /dev/null 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 deleted file mode 100644 index 57bc34d925ed688f24eeacd72ac0ac1abd5bdbdb..0000000000000000000000000000000000000000 Binary files a/http-response.o and /dev/null differ diff --git a/http-server.c b/http-server.c index 3c3eca7eac8e8905ac696c728033cdc7a431b9aa..efd66f006802e462d6e13291f58b8fe5c01675f1 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 deleted file mode 100644 index b01a947c7792a7ea3df9585f7c5ce2caa7d9ae8b..0000000000000000000000000000000000000000 Binary files a/http-server.o and /dev/null differ diff --git a/server b/server deleted file mode 100644 index c63bbc29bdba00c9da7a64d718dba72f1c414565..0000000000000000000000000000000000000000 Binary files a/server and /dev/null differ diff --git a/user.c b/user.c index b95e67f28b53c3bbf29e53ac6a49bec6f38d576d..3ecf95e2e5636889977cd6905ed81348f928c3ea 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){ printf("keyword is %s\n\n", keyword); for(int i=0; i <user->n_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 deleted file mode 100644 index 2753a832f6660fb872898e5e75f4541b5336c52d..0000000000000000000000000000000000000000 Binary files a/user.o and /dev/null differ