From 38b817fdf8057f8023fcc0c67c480b0c9d2bcd0d Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 31 Mar 2024 21:23:22 -0500 Subject: [PATCH] new: Proper reply when invalid data is sent --- .gitignore | 2 +- actix/src/utils.rs | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index da49308..1dbf157 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,4 @@ urls.sqlite .vscode/ **/.directory .env - +cookie* diff --git a/actix/src/utils.rs b/actix/src/utils.rs index d9fb977..900f744 100644 --- a/actix/src/utils.rs +++ b/actix/src/utils.rs @@ -1,13 +1,13 @@ -use std::env; - -use crate::database; use nanoid::nanoid; use rand::seq::SliceRandom; use regex::Regex; use rusqlite::Connection; use serde::Deserialize; +use std::env; -#[derive(Deserialize)] +use crate::database; + +#[derive(Deserialize, Default, PartialEq)] struct URLPair { shortlink: String, longlink: String, @@ -32,7 +32,11 @@ pub fn getall(db: &Connection) -> String { } pub fn add_link(req: String, db: &Connection) -> (bool, String) { - let mut chunks: URLPair = serde_json::from_str(&req).unwrap(); + let mut chunks: URLPair = serde_json::from_str(&req).unwrap_or_default(); + + if chunks == URLPair::default() { + return (false, String::from("Invalid request!")); + } let style = env::var("slug_style").unwrap_or(String::from("Pair")); let len_str = env::var("slug_length").unwrap_or(String::from("8"));