From 355066b197a41920b85bff75ff0225c286cff177 Mon Sep 17 00:00:00 2001 From: RaspberryProgramming Date: Fri, 19 Jul 2024 14:20:02 +0000 Subject: [PATCH 1/4] Fixed cookie bug When connecting for the first time, if a request uses the cookie jar the cookie may be pending. Made a new function that will account for that and return the pending cookie if it exists. Otherwise it will return the cookie using the .get function of the cookiejar. --- src/lib.rs | 10 ++++++ src/main.rs | 102 +++++++++++++++++++++++++--------------------------- 2 files changed, 58 insertions(+), 54 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e835b44..65fbbd8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ pub mod schema; use chrono::offset::Utc; use diesel::prelude::*; +use rocket::http::CookieJar; use std::env; use dotenvy::dotenv; use models::{ @@ -129,3 +130,12 @@ pub fn ownership_exists(client_id: &str, owner_id: &str, connection: &mut PgConn ownerships.len() > 0 } + +/// Looks for a cookie in the cookiejar. If it is a pending cookie, the pending will be returned as a String. +/// This accounts for if this client hasn't connected before and is making its first request +pub fn get_cookie(key: &str, cookies: &CookieJar<'_>) -> String { + match cookies.get_pending(key) { + Some(v) => v.value().to_string(), + None => cookies.get(key).unwrap().value().to_string() + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 69a19b5..57d953a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -152,30 +152,27 @@ fn index(_info: RequestInfo) -> Template { #[get("/test")] fn test_get(request: RequestInfo, cookies: &CookieJar<'_>) -> Template { - let client_id = cookies.get("mirror-id"); + let client_id = get_cookie("mirror-id", cookies); - // If the cookie exists, create new database records for this request - if client_id.is_some() { - let connection = &mut establish_connection(); + // create new database records for this request + let connection = &mut establish_connection(); - // Create new history record and retrieve new history_id - let history_id = create_history_record(connection, client_id.unwrap().value(), "Get"); + // Create new history record and retrieve new history_id + let history_id = create_history_record(connection, &client_id, "Get"); - // Create header pair records - for row in &request.header { - create_pair_record(connection, history_id, PairType::Header, &row.key, &row.value); - } - - // Create cookie pair records - for row in &request.cookies { - create_pair_record(connection, history_id, PairType::Cookie, &row.key, &row.value); - } - - // Create query pair records - for row in &request.query { - create_pair_record(connection, history_id, PairType::Query, &row.key, &row.value); - } + // Create header pair records + for row in &request.header { + create_pair_record(connection, history_id, PairType::Header, &row.key, &row.value); + } + // Create cookie pair records + for row in &request.cookies { + create_pair_record(connection, history_id, PairType::Cookie, &row.key, &row.value); + } + + // Create query pair records + for row in &request.query { + create_pair_record(connection, history_id, PairType::Query, &row.key, &row.value); } // Define context for the template @@ -204,36 +201,33 @@ fn test_get(request: RequestInfo, cookies: &CookieJar<'_>) -> Template { #[post("/test", data = "")] fn test_post(body: RequestBody, request: RequestInfo, cookies: &CookieJar<'_>) -> Template { - let client_id = cookies.get("mirror-id"); + let client_id = get_cookie("mirror-id", cookies); - // If the cookie exists, create new database records for this request - if client_id.is_some() { - let connection = &mut establish_connection(); - - // Create new history record and retrieve new history_id - let history_id = create_history_record(connection, client_id.unwrap().value(), "Post"); - - // Create header pair records - for row in &request.header { - create_pair_record(connection, history_id, PairType::Header, &row.key, &row.value); - } - - // Create cookie pair records - for row in &request.cookies { - create_pair_record(connection, history_id, PairType::Cookie, &row.key, &row.value); - } - - // Create query pair records - for row in &request.query { - create_pair_record(connection, history_id, PairType::Query, &row.key, &row.value); - } - - // Create a pair record for body of the request - println!("Creating body Records for {history_id}"); - create_pair_record(connection, history_id, PairType::Body, "body", &body.0.clone()); - + // Create new database records for this request + let connection = &mut establish_connection(); + + // Create new history record and retrieve new history_id + let history_id = create_history_record(connection, &client_id, "Post"); + + // Create header pair records + for row in &request.header { + create_pair_record(connection, history_id, PairType::Header, &row.key, &row.value); } + // Create cookie pair records + for row in &request.cookies { + create_pair_record(connection, history_id, PairType::Cookie, &row.key, &row.value); + } + + // Create query pair records + for row in &request.query { + create_pair_record(connection, history_id, PairType::Query, &row.key, &row.value); + } + + // Create a pair record for body of the request + println!("Creating body Records for {history_id}"); + create_pair_record(connection, history_id, PairType::Body, "body", &body.0.clone()); + // Define context for the template #[derive(Serialize)] struct Context<'a> { @@ -260,10 +254,10 @@ fn test_post(body: RequestBody, request: RequestInfo, cookies: &CookieJar<'_>) - /// Request function that returns a history of requests that the current client has made /// The user can click a history_id and view the request itself #[get("/history")] -fn history_req(cookies: &CookieJar<'_>) -> Template { +fn history_req(_info: RequestInfo, cookies: &CookieJar<'_>) -> Template { // Get the client_id from the cookies - let client_id = cookies.get("mirror-id").unwrap().value(); + let client_id = get_cookie("mirror-id", cookies); let connection = &mut establish_connection(); @@ -275,7 +269,7 @@ fn history_req(cookies: &CookieJar<'_>) -> Template { .expect("Error loading clients"); // Get ownership relationships - let ownerships: Vec = get_ownerships(client_id, connection); + let ownerships: Vec = get_ownerships(&client_id, connection); // Add any records that owned clients have for ownership in ownerships { @@ -325,14 +319,14 @@ fn history_req(cookies: &CookieJar<'_>) -> Template { /// request that was recorded to the database. /// This includes the body of a post request, headers, cookies and query parameters. #[get("/history/")] -fn history_details(history_id: i64, cookies: &CookieJar<'_>) -> Template { +fn history_details(history_id: i64, _info: RequestInfo, cookies: &CookieJar<'_>) -> Template { - let client_id = cookies.get("mirror-id").unwrap().value(); + let client_id = get_cookie("mirror-id", cookies); let connection: &mut PgConnection = &mut establish_connection(); // Get owned client ids - let owned_clients: Vec = get_ownerships(client_id, connection) + let owned_clients: Vec = get_ownerships(&client_id, connection) .iter() .map(|x|x.client_id.to_string()) .collect::>(); @@ -415,7 +409,7 @@ fn history_details(history_id: i64, cookies: &CookieJar<'_>) -> Template { #[get("/ownership_registration")] fn ownership_registration(info: RequestInfo, cookies: &CookieJar<'_>) -> Template { - let client_id = cookies.get("mirror-id").unwrap().value().to_string(); + let client_id = get_cookie("mirror-id", cookies); let owner_id_pair = info.find_query_key("owner_id"); let mut disp_owner_reg = false; let re = Regex::new(r"^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$").unwrap(); From d5f13d63e1d1bcd9f9d2f7490feaccaa8f9438d6 Mon Sep 17 00:00:00 2001 From: RaspberryProgramming Date: Fri, 19 Jul 2024 14:21:16 +0000 Subject: [PATCH 2/4] Updated navbar highlight for each page --- templates/error.html.hbs | 2 +- templates/history.html.hbs | 4 ++-- templates/ownership_registration.html.hbs | 4 ++-- templates/request_details.html.hbs | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/templates/error.html.hbs b/templates/error.html.hbs index 41c5872..9dd3ae0 100644 --- a/templates/error.html.hbs +++ b/templates/error.html.hbs @@ -19,7 +19,7 @@