Skip to content

Commit 509b198

Browse files
authored
Merge pull request #108 from rusty-sec/extract_regex
Extract regex
2 parents 33b5306 + 6d1f86b commit 509b198

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ thiserror = "1.0"
4040

4141
# REGEX AND MATCHES
4242
fancy-regex = "0.10.0"
43+
regex = "1.7.3"
4344
scraper = "0.13.0"
4445

4546
# Dirs

src/lua/parsing/text.rs

+28-18
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,41 @@
1414

1515
use mlua::UserData;
1616
use tealr::TypeName;
17+
use regex::Regex;
1718

1819
#[derive(TypeName, Debug)]
1920
pub struct ResponseMatcher {}
2021

2122
impl ResponseMatcher {
22-
pub fn match_and_body(&self, body: String, text: Vec<String>) -> bool {
23+
pub fn match_and_body(&self, body: &str, text: Vec<String>, is_regex: Option<bool>) -> bool {
2324
let mut counter = 0;
24-
text.iter().for_each(|x| {
25-
if body.contains(x) {
25+
for x in text.iter() {
26+
if is_regex.unwrap_or(false) {
27+
if let Ok(re_pattern) = Regex::new(x) {
28+
if re_pattern.is_match(body) {
29+
counter += 1;
30+
}
31+
}
32+
} else if body.contains(x) {
2633
counter += 1;
2734
}
28-
});
29-
if counter == text.len() {
30-
true
31-
} else {
32-
false
3335
}
36+
counter == text.len()
3437
}
35-
pub fn match_once_body(&self, body: String, text: Vec<String>) -> String {
36-
let mut matched_data = "".into();
37-
text.iter().for_each(|x| {
38-
if body.contains(x) {
39-
matched_data = x.to_string();
38+
39+
pub fn match_once_body(&self, body: String, text: Vec<String>, is_regex: Option<bool>) -> Vec<String> {
40+
let mut matched_data = Vec::new();
41+
for pattern in text {
42+
if is_regex.unwrap_or(false) {
43+
if let Ok(re) = Regex::new(&pattern) {
44+
if re.is_match(&body) {
45+
matched_data.push(pattern);
46+
}
47+
}
48+
} else if body.contains(&pattern) {
49+
matched_data.push(pattern);
4050
}
41-
});
51+
}
4252
matched_data
4353
}
4454
}
@@ -47,14 +57,14 @@ impl UserData for ResponseMatcher {
4757
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
4858
methods.add_method(
4959
"match_body",
50-
|_, this, (response, text_list): (String, Vec<String>)| {
51-
Ok(this.match_and_body(response, text_list))
60+
|_, this, (response, text_list, is_regex): (String, Vec<String>, Option<bool>)| {
61+
Ok(this.match_and_body(&response, text_list,is_regex))
5262
},
5363
);
5464
methods.add_method(
5565
"match_body_once",
56-
|_, this, (response, text_list): (String, Vec<String>)| {
57-
let is_match = this.match_once_body(response, text_list);
66+
|_, this, (response, text_list, is_regex): (String, Vec<String>, Option<bool>)| {
67+
let is_match = this.match_once_body(response, text_list,is_regex);
5868
Ok(is_match)
5969
},
6070
)

0 commit comments

Comments
 (0)