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.
This commit is contained in:
parent
8eccd1ed24
commit
355066b197
10
src/lib.rs
10
src/lib.rs
|
|
@ -3,6 +3,7 @@ pub mod schema;
|
||||||
|
|
||||||
use chrono::offset::Utc;
|
use chrono::offset::Utc;
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
use rocket::http::CookieJar;
|
||||||
use std::env;
|
use std::env;
|
||||||
use dotenvy::dotenv;
|
use dotenvy::dotenv;
|
||||||
use models::{
|
use models::{
|
||||||
|
|
@ -129,3 +130,12 @@ pub fn ownership_exists(client_id: &str, owner_id: &str, connection: &mut PgConn
|
||||||
|
|
||||||
ownerships.len() > 0
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/main.rs
32
src/main.rs
|
|
@ -152,14 +152,13 @@ fn index(_info: RequestInfo) -> Template {
|
||||||
#[get("/test")]
|
#[get("/test")]
|
||||||
fn test_get(request: RequestInfo, cookies: &CookieJar<'_>) -> Template {
|
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
|
// create new database records for this request
|
||||||
if client_id.is_some() {
|
|
||||||
let connection = &mut establish_connection();
|
let connection = &mut establish_connection();
|
||||||
|
|
||||||
// Create new history record and retrieve new history_id
|
// Create new history record and retrieve new history_id
|
||||||
let history_id = create_history_record(connection, client_id.unwrap().value(), "Get");
|
let history_id = create_history_record(connection, &client_id, "Get");
|
||||||
|
|
||||||
// Create header pair records
|
// Create header pair records
|
||||||
for row in &request.header {
|
for row in &request.header {
|
||||||
|
|
@ -176,8 +175,6 @@ fn test_get(request: RequestInfo, cookies: &CookieJar<'_>) -> Template {
|
||||||
create_pair_record(connection, history_id, PairType::Query, &row.key, &row.value);
|
create_pair_record(connection, history_id, PairType::Query, &row.key, &row.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Define context for the template
|
// Define context for the template
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Context<'a> {
|
struct Context<'a> {
|
||||||
|
|
@ -204,14 +201,13 @@ fn test_get(request: RequestInfo, cookies: &CookieJar<'_>) -> Template {
|
||||||
#[post("/test", data = "<body>")]
|
#[post("/test", data = "<body>")]
|
||||||
fn test_post(body: RequestBody, request: RequestInfo, cookies: &CookieJar<'_>) -> Template {
|
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
|
// Create new database records for this request
|
||||||
if client_id.is_some() {
|
|
||||||
let connection = &mut establish_connection();
|
let connection = &mut establish_connection();
|
||||||
|
|
||||||
// Create new history record and retrieve new history_id
|
// Create new history record and retrieve new history_id
|
||||||
let history_id = create_history_record(connection, client_id.unwrap().value(), "Post");
|
let history_id = create_history_record(connection, &client_id, "Post");
|
||||||
|
|
||||||
// Create header pair records
|
// Create header pair records
|
||||||
for row in &request.header {
|
for row in &request.header {
|
||||||
|
|
@ -232,8 +228,6 @@ fn test_post(body: RequestBody, request: RequestInfo, cookies: &CookieJar<'_>) -
|
||||||
println!("Creating body Records for {history_id}");
|
println!("Creating body Records for {history_id}");
|
||||||
create_pair_record(connection, history_id, PairType::Body, "body", &body.0.clone());
|
create_pair_record(connection, history_id, PairType::Body, "body", &body.0.clone());
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Define context for the template
|
// Define context for the template
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Context<'a> {
|
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
|
/// 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
|
/// The user can click a history_id and view the request itself
|
||||||
#[get("/history")]
|
#[get("/history")]
|
||||||
fn history_req(cookies: &CookieJar<'_>) -> Template {
|
fn history_req(_info: RequestInfo, cookies: &CookieJar<'_>) -> Template {
|
||||||
|
|
||||||
// Get the client_id from the cookies
|
// 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();
|
let connection = &mut establish_connection();
|
||||||
|
|
||||||
|
|
@ -275,7 +269,7 @@ fn history_req(cookies: &CookieJar<'_>) -> Template {
|
||||||
.expect("Error loading clients");
|
.expect("Error loading clients");
|
||||||
|
|
||||||
// Get ownership relationships
|
// Get ownership relationships
|
||||||
let ownerships: Vec<Ownership> = get_ownerships(client_id, connection);
|
let ownerships: Vec<Ownership> = get_ownerships(&client_id, connection);
|
||||||
|
|
||||||
// Add any records that owned clients have
|
// Add any records that owned clients have
|
||||||
for ownership in ownerships {
|
for ownership in ownerships {
|
||||||
|
|
@ -325,14 +319,14 @@ fn history_req(cookies: &CookieJar<'_>) -> Template {
|
||||||
/// request that was recorded to the database.
|
/// request that was recorded to the database.
|
||||||
/// This includes the body of a post request, headers, cookies and query parameters.
|
/// This includes the body of a post request, headers, cookies and query parameters.
|
||||||
#[get("/history/<history_id>")]
|
#[get("/history/<history_id>")]
|
||||||
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();
|
let connection: &mut PgConnection = &mut establish_connection();
|
||||||
|
|
||||||
// Get owned client ids
|
// Get owned client ids
|
||||||
let owned_clients: Vec<String> = get_ownerships(client_id, connection)
|
let owned_clients: Vec<String> = get_ownerships(&client_id, connection)
|
||||||
.iter()
|
.iter()
|
||||||
.map(|x|x.client_id.to_string())
|
.map(|x|x.client_id.to_string())
|
||||||
.collect::<Vec<String>>();
|
.collect::<Vec<String>>();
|
||||||
|
|
@ -415,7 +409,7 @@ fn history_details(history_id: i64, cookies: &CookieJar<'_>) -> Template {
|
||||||
#[get("/ownership_registration")]
|
#[get("/ownership_registration")]
|
||||||
fn ownership_registration(info: RequestInfo, cookies: &CookieJar<'_>) -> Template {
|
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 owner_id_pair = info.find_query_key("owner_id");
|
||||||
let mut disp_owner_reg = false;
|
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();
|
let re = Regex::new(r"^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$").unwrap();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue