Skip to content

Commit

Permalink
feature: check is valid local host params
Browse files Browse the repository at this point in the history
  • Loading branch information
Ando committed Aug 13, 2023
1 parent 32cbbac commit daa3cea
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ name = "core"
path = "src/lib.rs"

[dependencies]
regex = "1.9.3"
4 changes: 4 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ impl Server {
panic!("An error occured, parameters need to be not empty");
}

if !Params::is_valid_local_host(host) {
panic!("An error occured, parameters need to be valid local host");
}

addr.push_str(&host);

Check warning on line 18 in src/server.rs

View workflow job for this annotation

GitHub Actions / lint

this expression creates a reference which is immediately dereferenced by the compiler

Check warning on line 18 in src/server.rs

View workflow job for this annotation

GitHub Actions / lint

this expression creates a reference which is immediately dereferenced by the compiler
addr.push(':');
addr.push_str(&port.to_string());
Expand Down
11 changes: 11 additions & 0 deletions src/utils/params.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use regex::Regex;

pub struct Params {}

impl Params {
Expand All @@ -7,4 +9,13 @@ impl Params {

is_host_empty && is_port_empty
}

pub fn is_valid_local_host(host: &str) -> bool {
let pattern = r"^(localhost|127\.0\.0\.1)$";

match Regex::new(pattern) {
Ok(re) => re.is_match(host),
Err(_) => return false,

Check warning on line 18 in src/utils/params.rs

View workflow job for this annotation

GitHub Actions / lint

unneeded `return` statement

Check warning on line 18 in src/utils/params.rs

View workflow job for this annotation

GitHub Actions / lint

unneeded `return` statement
}
}
}

0 comments on commit daa3cea

Please sign in to comment.