new: Proper reply when invalid data is sent

This commit is contained in:
SinTan1729 2024-03-31 21:23:22 -05:00
parent a9168e3459
commit 38b817fdf8
No known key found for this signature in database
GPG Key ID: EB3E68BFBA25C85F
2 changed files with 10 additions and 6 deletions

2
.gitignore vendored
View File

@ -8,4 +8,4 @@ urls.sqlite
.vscode/ .vscode/
**/.directory **/.directory
.env .env
cookie*

View File

@ -1,13 +1,13 @@
use std::env;
use crate::database;
use nanoid::nanoid; use nanoid::nanoid;
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use regex::Regex; use regex::Regex;
use rusqlite::Connection; use rusqlite::Connection;
use serde::Deserialize; use serde::Deserialize;
use std::env;
#[derive(Deserialize)] use crate::database;
#[derive(Deserialize, Default, PartialEq)]
struct URLPair { struct URLPair {
shortlink: String, shortlink: String,
longlink: String, longlink: String,
@ -32,7 +32,11 @@ pub fn getall(db: &Connection) -> String {
} }
pub fn add_link(req: String, db: &Connection) -> (bool, 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 style = env::var("slug_style").unwrap_or(String::from("Pair"));
let len_str = env::var("slug_length").unwrap_or(String::from("8")); let len_str = env::var("slug_length").unwrap_or(String::from("8"));