[fix] MORE mutex poisoning issues

should be all of them
This commit is contained in:
hkau 2024-02-25 21:47:38 -05:00
parent d5f6a739f9
commit 777381c3f6
6 changed files with 104 additions and 84 deletions

View file

@ -27,14 +27,15 @@ pub async fn register(
.body("This server requires has registration disabled");
}
let mut lock = match data.lock() {
Ok(lock) => lock,
// the poisoned guard tells us that something panicked while handling a locked guard
Err(poisoned) => poisoned.into_inner(),
};
// ...
let username = &body.username.trim();
let res = data
.lock()
.unwrap()
.db
.create_user(username.to_string())
.await;
let res = lock.db.create_user(username.to_string()).await;
let c = res.clone();
let set_cookie = if res.success && res.payload.is_some() {
@ -55,9 +56,13 @@ pub async fn login(body: web::Json<LoginInfo>, data: web::Data<Mutex<AppData>>)
let id = body.uid.trim();
let id_hashed = utility::hash(id.to_string());
let res = data
.lock()
.unwrap()
let mut lock = match data.lock() {
Ok(lock) => lock,
// the poisoned guard tells us that something panicked while handling a locked guard
Err(poisoned) => poisoned.into_inner(),
};
let res = lock
.db
.get_user_by_hashed(id_hashed) // if the user is returned, that means the ID is valid
.await;
@ -84,9 +89,13 @@ pub async fn logout(req: HttpRequest, data: web::Data<Mutex<AppData>>) -> impl R
return HttpResponse::NotAcceptable().body("Missing token");
}
let res = data
.lock()
.unwrap()
let mut lock = match data.lock() {
Ok(lock) => lock,
// the poisoned guard tells us that something panicked while handling a locked guard
Err(poisoned) => poisoned.into_inner(),
};
let res = lock
.db
.get_user_by_hashed(cookie.unwrap().value().to_string()) // if the user is returned, that means the ID is valid
.await;

View file

@ -120,13 +120,17 @@ pub async fn create_request(
""
};
let mut lock = match data.lock() {
Ok(lock) => lock,
// the poisoned guard tells us that something panicked while handling a locked guard
Err(poisoned) => poisoned.into_inner(),
};
// get owner
let token_cookie = req.cookie("__Secure-Token");
let token_user = if token_cookie.is_some() {
Option::Some(
data.lock()
.unwrap()
.db
lock.db
.get_user_by_hashed(token_cookie.as_ref().unwrap().value().to_string()) // if the user is returned, that means the ID is valid
.await,
)
@ -150,9 +154,7 @@ pub async fn create_request(
}
// create paste
let res = data
.lock()
.unwrap()
let res = lock
.db
.create_paste(
&mut bundlesdb::Paste {
@ -194,13 +196,17 @@ pub async fn edit_request(
let new_url: Option<String> = body.new_custom_url.to_owned();
let new_edit_password: Option<String> = body.new_edit_password.to_owned();
let mut lock = match data.lock() {
Ok(lock) => lock,
// the poisoned guard tells us that something panicked while handling a locked guard
Err(poisoned) => poisoned.into_inner(),
};
// get owner
let token_cookie = req.cookie("__Secure-Token");
let token_user = if token_cookie.is_some() {
Option::Some(
data.lock()
.unwrap()
.db
lock.db
.get_user_by_hashed(token_cookie.as_ref().unwrap().value().to_string()) // if the user is returned, that means the ID is valid
.await,
)
@ -216,9 +222,7 @@ pub async fn edit_request(
}
// ...
let res = data
.lock()
.unwrap()
let res = lock
.db
.edit_paste_by_url(
custom_url,
@ -253,13 +257,17 @@ pub async fn edit_atomic_request(
let path: String = body.path.trim().to_string();
let content: String = body.content.trim().to_string();
let mut lock = match data.lock() {
Ok(lock) => lock,
// the poisoned guard tells us that something panicked while handling a locked guard
Err(poisoned) => poisoned.into_inner(),
};
// get owner
let token_cookie = req.cookie("__Secure-Token");
let token_user = if token_cookie.is_some() {
Option::Some(
data.lock()
.unwrap()
.db
lock.db
.get_user_by_hashed(token_cookie.as_ref().unwrap().value().to_string()) // if the user is returned, that means the ID is valid
.await,
)
@ -275,12 +283,8 @@ pub async fn edit_atomic_request(
}
// get paste
let paste: bundlesdb::DefaultReturn<Option<bundlesdb::Paste<String>>> = data
.lock()
.unwrap()
.db
.get_paste_by_url(custom_url.clone())
.await;
let paste: bundlesdb::DefaultReturn<Option<bundlesdb::Paste<String>>> =
lock.db.get_paste_by_url(custom_url.clone()).await;
if paste.success == false {
return HttpResponse::Ok()
@ -320,9 +324,7 @@ pub async fn edit_atomic_request(
});
// ...
let res = data
.lock()
.unwrap()
let res = lock
.db
.edit_paste_by_url(
custom_url,
@ -354,13 +356,17 @@ pub async fn delete_request(
let custom_url: String = body.custom_url.trim().to_string();
let edit_password: String = body.edit_password.to_owned();
let mut lock = match data.lock() {
Ok(lock) => lock,
// the poisoned guard tells us that something panicked while handling a locked guard
Err(poisoned) => poisoned.into_inner(),
};
// get owner
let token_cookie = req.cookie("__Secure-Token");
let token_user = if token_cookie.is_some() {
Option::Some(
data.lock()
.unwrap()
.db
lock.db
.get_user_by_hashed(token_cookie.as_ref().unwrap().value().to_string()) // if the user is returned, that means the ID is valid
.await,
)
@ -376,9 +382,7 @@ pub async fn delete_request(
}
// delete
let res = data
.lock()
.unwrap()
let res = lock
.db
.delete_paste_by_url(
custom_url,
@ -410,13 +414,17 @@ pub async fn metadata_request(
let m = body.metadata.to_owned();
let metadata: bundlesdb::PasteMetadata = m;
let mut lock = match data.lock() {
Ok(lock) => lock,
// the poisoned guard tells us that something panicked while handling a locked guard
Err(poisoned) => poisoned.into_inner(),
};
// get owner
let token_cookie = req.cookie("__Secure-Token");
let token_user = if token_cookie.is_some() {
Option::Some(
data.lock()
.unwrap()
.db
lock.db
.get_user_by_hashed(token_cookie.as_ref().unwrap().value().to_string()) // if the user is returned, that means the ID is valid
.await,
)
@ -432,9 +440,7 @@ pub async fn metadata_request(
}
// ...
let res = data
.lock()
.unwrap()
let res = lock
.db
.edit_paste_metadata_by_url(
custom_url,

View file

@ -84,15 +84,19 @@ pub async fn dashboard_request(
req: HttpRequest,
data: web::Data<Mutex<db::bundlesdb::AppData>>,
) -> impl Responder {
let mut lock = match data.lock() {
Ok(lock) => lock,
// the poisoned guard tells us that something panicked while handling a locked guard
Err(poisoned) => poisoned.into_inner(),
};
// verify auth status
let token_cookie = req.cookie("__Secure-Token");
let mut set_cookie: &str = "";
let token_user = if token_cookie.is_some() {
Option::Some(
data.lock()
.unwrap()
.db
lock.db
.get_user_by_hashed(token_cookie.as_ref().unwrap().value().to_string()) // if the user is returned, that means the ID is valid
.await,
)
@ -116,9 +120,7 @@ You can create an account at: /d/auth/register",
}
// fetch pastes
let pastes = data
.lock()
.unwrap()
let pastes = lock
.db
.get_atomic_pastes_by_owner(token_user.clone().unwrap().payload.unwrap().username)
.await;
@ -191,15 +193,19 @@ pub async fn new_request(
req: HttpRequest,
data: web::Data<Mutex<db::bundlesdb::AppData>>,
) -> impl Responder {
let mut lock = match data.lock() {
Ok(lock) => lock,
// the poisoned guard tells us that something panicked while handling a locked guard
Err(poisoned) => poisoned.into_inner(),
};
// verify auth status
let token_cookie = req.cookie("__Secure-Token");
let mut set_cookie: &str = "";
let token_user = if token_cookie.is_some() {
Option::Some(
data.lock()
.unwrap()
.db
lock.db
.get_user_by_hashed(token_cookie.as_ref().unwrap().value().to_string()) // if the user is returned, that means the ID is valid
.await,
)
@ -313,15 +319,19 @@ pub async fn edit_request(
data: web::Data<Mutex<db::bundlesdb::AppData>>,
info: web::Query<EditQueryProps>,
) -> impl Responder {
let mut lock = match data.lock() {
Ok(lock) => lock,
// the poisoned guard tells us that something panicked while handling a locked guard
Err(poisoned) => poisoned.into_inner(),
};
// verify auth status
let token_cookie = req.cookie("__Secure-Token");
let mut set_cookie: &str = "";
let token_user = if token_cookie.is_some() {
Option::Some(
data.lock()
.unwrap()
.db
lock.db
.get_user_by_hashed(token_cookie.as_ref().unwrap().value().to_string()) // if the user is returned, that means the ID is valid
.await,
)
@ -353,8 +363,7 @@ You can create an account at: /d/auth/register",
Err(poisoned) => poisoned.into_inner(),
};
let paste: bundlesdb::DefaultReturn<Option<Paste<String>>> =
lock.db.get_paste_by_id(id).await;
let paste: bundlesdb::DefaultReturn<Option<Paste<String>>> = lock.db.get_paste_by_id(id).await;
if paste.success == false {
let renderer = ServerRenderer::<crate::pages::errors::_404Page>::new();

View file

@ -212,9 +212,7 @@ pub async fn profile_view_request(
let token_user = if token_cookie.is_some() {
Option::Some(
data.lock()
.unwrap()
.db
lock.db
.get_user_by_hashed(token_cookie.as_ref().unwrap().value().to_string()) // if the user is returned, that means the ID is valid
.await,
)
@ -230,12 +228,8 @@ pub async fn profile_view_request(
}
// ...
let pastes_res: bundlesdb::DefaultReturn<Option<Vec<bundlesdb::PasteIdentifier>>> = data
.lock()
.unwrap()
.db
.get_pastes_by_owner(username_c.clone())
.await;
let pastes_res: bundlesdb::DefaultReturn<Option<Vec<bundlesdb::PasteIdentifier>>> =
lock.db.get_pastes_by_owner(username_c.clone()).await;
let renderer = build_renderer_with_props(Props {
user: unwrap.clone(),

View file

@ -201,15 +201,19 @@ pub async fn home_request(
data: web::Data<Mutex<db::bundlesdb::AppData>>,
info: web::Query<Props>,
) -> impl Responder {
let mut lock = match data.lock() {
Ok(lock) => lock,
// the poisoned guard tells us that something panicked while handling a locked guard
Err(poisoned) => poisoned.into_inner(),
};
// verify auth status
let token_cookie = req.cookie("__Secure-Token");
let mut set_cookie: &str = "";
let token_user = if token_cookie.is_some() {
Option::Some(
data.lock()
.unwrap()
.db
lock.db
.get_user_by_hashed(token_cookie.as_ref().unwrap().value().to_string()) // if the user is returned, that means the ID is valid
.await,
)
@ -228,13 +232,7 @@ pub async fn home_request(
let str: &Option<String> = &info.editing;
let paste = if str.is_some() {
Option::Some(
data.lock()
.unwrap()
.db
.get_paste_by_url(str.to_owned().unwrap())
.await,
)
Option::Some(lock.db.get_paste_by_url(str.to_owned().unwrap()).await)
} else {
Option::None
};

View file

@ -94,15 +94,19 @@ pub async fn user_settings_request(
req: HttpRequest,
data: web::Data<Mutex<AppData>>,
) -> impl Responder {
let mut lock = match data.lock() {
Ok(lock) => lock,
// the poisoned guard tells us that something panicked while handling a locked guard
Err(poisoned) => poisoned.into_inner(),
};
// verify auth status
let token_cookie = req.cookie("__Secure-Token");
let mut set_cookie: &str = "";
let token_user = if token_cookie.is_some() {
Option::Some(
data.lock()
.unwrap()
.db
lock.db
.get_user_by_hashed(token_cookie.as_ref().unwrap().value().to_string()) // if the user is returned, that means the ID is valid
.await,
)