diff --git a/1_welcome.html b/1_welcome.html index a0d9e68386e121534dc4dfcffd1956fe7ded62d2..9f5fb94ffcb335faedca61b163dd5998625bea62 100644 --- a/1_welcome.html +++ b/1_welcome.html @@ -7,7 +7,7 @@ <h2>Image Tagger Game</h2> -<img src="http://203.101.227.119/image-3.jpg" alt="HTML5 Icon" style="width:700px;height:400px;"> +<img src="https://swift.rc.nectar.org.au/v1/AUTH_eab314456b624071ac5aecd721b977f0/comp30023-project/image-3.jpg" alt="HTML5 Icon" style="width:700px;height:400px;"> <p>Welcome to the image tagging game. Please enter your name below.</p> @@ -19,4 +19,3 @@ </body> </html> - diff --git a/2_start.html b/2_start.html index a36ed6a6d01521875cf5b7f859e1d9995359f120..d09da4d91be3fb9cd31aad792c0be37ef544236c 100644 --- a/2_start.html +++ b/2_start.html @@ -6,7 +6,7 @@ <h2>Image Tagger Game</h2> -<img src="http://203.101.227.119/image-3.jpg" alt="HTML5 Icon" style="width:700px;height:400px;"> +<img src="https://swift.rc.nectar.org.au/v1/AUTH_eab314456b624071ac5aecd721b977f0/comp30023-project/image-3.jpg" alt="HTML5 Icon" style="width:700px;height:400px;"> <form method="GET"> <input type="submit" class="button" name="start" value="Start"/> diff --git a/3_first_turn.html b/3_first_turn.html index bc6d1530c7b21eb39ec54d67e5fbeb4c958d5d31..92f3ca0e0494a222a06db8a29b77f764ce6b4e63 100644 --- a/3_first_turn.html +++ b/3_first_turn.html @@ -6,7 +6,7 @@ <h2>You are ready now!</h2> -<img src="http://203.101.227.119/image-2.jpg" alt="HTML5 Icon" style="width:700px;height:400px;"> +<img src="https://swift.rc.nectar.org.au/v1/AUTH_eab314456b624071ac5aecd721b977f0/comp30023-project/image-2.jpg" alt="HTML5 Icon" style="width:700px;height:400px;"> <p>Rule: Try to guess the above image by typing a keyword which describes it:</p> diff --git a/4_accepted.html b/4_accepted.html index d061a5853d00294fa9625a8d8a3e8270ffab601e..d3bdc8bccf55bc282500c4b054a353d256d1a92a 100644 --- a/4_accepted.html +++ b/4_accepted.html @@ -6,7 +6,7 @@ <h2>Keyword Accepted! Keep trying more.</h2> -<img src="http://203.101.227.119/image-2.jpg" alt="HTML5 Icon" style="width:700px;height:400px;"> +<img src="https://swift.rc.nectar.org.au/v1/AUTH_eab314456b624071ac5aecd721b977f0/comp30023-project/image-2.jpg" alt="HTML5 Icon" style="width:700px;height:400px;"> <p>Rule: Try to guess the above image by typing a keyword which describes it:</p> diff --git a/5_discarded.html b/5_discarded.html index e6b4b8f20c5cc6c7344470d1d3252f4711ae075e..bc5effaf68291a82bdd35e9e7a999ec3a5e08125 100644 --- a/5_discarded.html +++ b/5_discarded.html @@ -6,7 +6,7 @@ <h2>Keyword Discarded. The other player is not ready yet.</h2> -<img src="http://203.101.227.119/image-2.jpg" alt="HTML5 Icon" style="width:700px;height:400px;"> +<img src="https://swift.rc.nectar.org.au/v1/AUTH_eab314456b624071ac5aecd721b977f0/comp30023-project/image-2.jpg" alt="HTML5 Icon" style="width:700px;height:400px;"> <p>Rule: Try to guess the above image by typing a keyword which describes it:</p> diff --git a/hashtable.c b/hashtable.c index c4955f3b5110b557992c101d052197373418e44a..14c689036d9811e92ca2d9c71326fe8130aa45fa 100644 --- a/hashtable.c +++ b/hashtable.c @@ -174,3 +174,16 @@ bool hash_table_has(HashTable *table, char *key) { } return false; } +char* print_hash_map(HashTable *table){ + char* headers = malloc(100*sizeof(char)); + 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++){ + strcat(headers, table->buckets[i].keys[j]); + strcat(headers, table->buckets[i].values[j]); + strcat(headers, "\r\n"); + } + } + } + return headers; +} \ No newline at end of file diff --git a/hashtable.h b/hashtable.h index 9dbea8ad53f09d430fe208a01218e7c76fcdae44..eb041bfbd170732cc0c84a86e3eb4d16d47c48ec 100644 --- a/hashtable.h +++ b/hashtable.h @@ -22,3 +22,5 @@ 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 diff --git a/hashtable.o b/hashtable.o index 3d009356e879e27341f185114559a9f4dd7c78e1..181ffc4f2d1b1f243e3a17c0104bfeb5cda7f9f7 100644 Binary files a/hashtable.o and b/hashtable.o differ diff --git a/http-parser.c b/http-parser.c index ddbe63198e8c37fa69cf45de4777fcc006ca7a30..b55de391f0e245cba0a23660eff0454ee58c975d 100644 --- a/http-parser.c +++ b/http-parser.c @@ -7,7 +7,7 @@ #include "http-parser.h" #define MAX_URL_SIZE 60 -#define MAX_HEADER_SIZE 300 +#define MAX_HEADER_SIZE 600 #define MAX_VERSION_SIZE 10 Request* parse_request(char* request_message){ diff --git a/http-parser.o b/http-parser.o index 988eedd96577d49a833768472065914ea30d7f3d..66aae21511f7e3a278dab384323187d943ac15ab 100644 Binary files a/http-parser.o and b/http-parser.o differ diff --git a/http-response.c b/http-response.c index e7d1846a81d4d15fee6f9cfa70e987d7befe05d1..9b3c755df09c46d890478831961175460af82691 100644 --- a/http-response.c +++ b/http-response.c @@ -3,6 +3,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <stdlib.h> #include <assert.h> #include "http-parser.h" #include "http-response.h" @@ -18,7 +19,28 @@ Response* redirect(Request* request, char* url_string){ resp->version = request->version; resp->phrase = "Moved Permanently"; resp->header = new_hash_table(2); - hash_table_put(resp->header, "Location:", url_string); + hash_table_put(resp->header, "Location: ", url_string); + resp->body=""; + return resp; +} + +char* cookie_generator(){ + char* cookie_value; + cookie_value = (char*) malloc(sizeof(char)*60); + snprintf(cookie_value, 60, "sessionToken=%d; Expires=Wed, 01 Apr 2019 10:10:10 GMT", rand()); + return cookie_value; +} + + +Response* initialise_session(Request* request){ + Response *resp = malloc(sizeof *resp); + assert(resp); + resp->status_code=200; + 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); resp->body=""; return resp; } @@ -34,14 +56,14 @@ char* parse_response(Response* response){ strcat(response_string, " "); strcat(response_string, response->phrase); strcat(response_string, "\r\n"); - strcat(response_string, "Location: "); - strcat(response_string,hash_table_get(response->header, "Location:")); - strcat(response_string, "\r\n\r\n"); + strcat(response_string, print_hash_map(response->header)); + strcat(response_string, "\r\n"); strcat(response_string, response->body); return response_string; } + void free_response(Response* resp){ free_hash_table(resp->header); free(resp); diff --git a/http-response.h b/http-response.h index 14216960f9b3b79fd55e27754df6289cb1fd8060..adbcb3ade2780f8d4161958625ccda8668e85a6a 100644 --- a/http-response.h +++ b/http-response.h @@ -13,4 +13,9 @@ typedef struct Response { //Creates a new hash table of input size Response *redirect(Request *req, char* url_string); -char* parse_response(Response* response); \ No newline at end of file +char* parse_response(Response* response); + +char* cookie_generator(); + + +Response* initialise_session(Request* request); \ No newline at end of file diff --git a/http-response.o b/http-response.o index 5ab883eb71fd341af4e8733938c009aefa145748..55b6aae6b41e473941b9be6f93416a2973bd89f6 100644 Binary files a/http-response.o and b/http-response.o differ diff --git a/http-server.c b/http-server.c index 9b7244d877a37da75eaeef420ce149bab2727978..1e8b3e9fb28695f34a5b2038a11e1768d9a6a188 100644 --- a/http-server.c +++ b/http-server.c @@ -33,7 +33,37 @@ static char const * const HTTP_404 = "HTTP/1.1 404 Not Found\r\nContent-Length: static int const HTTP_404_LENGTH = 45; - +bool player_session(char* buff, int sockfd, char* file_name, char* response){ + // get the size of the file + printf("runnning player session functin\n"); + struct stat st; + stat(file_name, &st); + int n = sprintf(buff, response, st.st_size); + // send the header first + printf("sending header\n"); + if (write(sockfd, buff, n) < 0) + { + perror("write"); + return false; + } + // send the file + printf("sending file\n"); + int filefd = open(file_name, O_RDONLY); + do + { + n = sendfile(sockfd, filefd, NULL, 2048); + } + while (n > 0); + if (n < 0) + { + perror("sendfile"); + close(filefd); + return false; + } + printf("about to close"); + close(filefd); + return true; +} bool get_request(char* buff, int sockfd, char* file_name){ // get the size of the file struct stat st; @@ -165,14 +195,8 @@ static bool handle_http_request(int sockfd, User_list* users) free(response); } } - // assume the only valid request URI is "/" but it can be modified to accept more files - - else if (*req->url == '/') - if (req->method == GET) - { - get_request(buff,sockfd, "1_welcome.html"); - } - else if (req->method == POST) + else if (*req->url == '/' && (strlen(req->url) == 1)){ + if (req->method == POST) { char *name = strchr(req->body, '=')+1; printf("**%s**\n", name); @@ -189,9 +213,21 @@ static bool handle_http_request(int sockfd, User_list* users) free(response); } } + else if (req->method == GET) + { + printf("matches a / url but url isize is %zu\n\n", strlen(req->url)); + Response* resp = initialise_session(req); + char* resp_string = parse_response(resp); + printf("COOKIE CREATING RESP %s\n", resp_string); + // player_session(buff, sockfd, "1_welcome.html", resp_string); + free(resp_string); + free(resp); + get_request(buff,sockfd, "1_welcome.html"); + } else // never used, just for completeness - fprintf(stderr, "no other methods supported"); + fprintf(stderr, "no other methods supported"); + } // send 404 else if (write(sockfd, HTTP_404, HTTP_404_LENGTH) < 0) { diff --git a/http-server.o b/http-server.o index 093df8e7e2a3ddabcd0698b5ad7665901308b55f..85a765b9a537f150bbd39e98668b57dcc9742cdc 100644 Binary files a/http-server.o and b/http-server.o differ diff --git a/image_tagger b/image_tagger index 0032bce3a0d8b40dcdcd0f00cb256e60db02cc9b..246fb881cbe59963a6c0f1db99e86b9f6a69cf6a 100644 Binary files a/image_tagger and b/image_tagger differ