-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhttp_ext.rs
154 lines (149 loc) · 4.74 KB
/
http_ext.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use crate::{
lua::{
model::LuaRunTime,
output::report::AllReports,
parsing::{files::filename_to_string, url::HttpMessage},
},
CliErrors,
};
use log::{debug, error, info, warn};
use mlua::ExternalError;
use std::collections::HashMap;
use std::path::Path;
use std::time::Duration;
use tokio::time::sleep;
use url::Url;
pub trait HTTPEXT {
fn add_httpfuncs(&self, target_url: Option<&str>);
}
impl HTTPEXT for LuaRunTime<'_> {
fn add_httpfuncs(&self, target_url: Option<&str>) {
self.lua
.globals()
.set(
"JOIN_SCRIPT_DIR",
self.lua
.create_function(|c_lua, new_path: String| {
let script_path = c_lua.globals().get::<_, String>("SCRIPT_PATH").unwrap();
let the_path = Path::new(&script_path);
Ok(the_path
.parent()
.unwrap()
.join(new_path)
.to_str()
.unwrap()
.to_string())
})
.unwrap(),
)
.unwrap();
let log_info = self
.lua
.create_function(|_, log_msg: String| {
info!("{}", log_msg);
Ok(())
})
.unwrap();
let log_warn = self
.lua
.create_function(|_, log_msg: String| {
warn!("{}", log_msg);
Ok(())
})
.unwrap();
let log_debug = self
.lua
.create_function(|_, log_msg: String| {
debug!("{}", log_msg);
Ok(())
})
.unwrap();
let log_error = self
.lua
.create_function(|_, log_msg: String| {
error!("{}", log_msg);
Ok(())
})
.unwrap();
let headers_converter = self
.lua
.create_function(|_, headers_txt: String| {
let mut result = HashMap::new();
for line in headers_txt.lines() {
if let Some((name, value)) = line.split_once(":") {
result.insert(name.trim().to_string(), value.trim().to_string());
}
}
Ok(result)
})
.unwrap();
self.lua
.globals()
.set("make_headers", headers_converter)
.unwrap();
self.lua
.globals()
.set(
"sleep",
self.lua
.create_async_function(|_, time: u64| async move {
sleep(Duration::from_secs(time)).await;
Ok(())
})
.unwrap(),
)
.unwrap();
self.lua
.globals()
.set(
"pathjoin",
self.lua
.create_function(|_, (current_path, new_path): (String, String)| {
let the_path = std::path::Path::new(¤t_path).join(new_path);
Ok(the_path.to_str().unwrap().to_string())
})
.unwrap(),
)
.unwrap();
self.lua
.globals()
.set(
"readfile",
self.lua
.create_function(|_ctx, file_path: String| {
if Path::new(&file_path).exists() {
let file_content = filename_to_string(&file_path)?;
Ok(file_content)
} else {
Err(CliErrors::ReadingError.to_lua_err())
}
})
.unwrap(),
)
.unwrap();
self.lua.globals().set("log_info", log_info).unwrap();
self.lua.globals().set("log_error", log_error).unwrap();
self.lua.globals().set("log_debug", log_debug).unwrap();
self.lua.globals().set("log_warn", log_warn).unwrap();
if let Some(url) = target_url {
self.lua
.globals()
.set("HttpMessage", HttpMessage { url: Some(Url::parse(&url).unwrap()) })
.unwrap();
} else {
self.lua
.globals()
.set("HttpMessage", HttpMessage { url: None })
.unwrap();
}
self.lua
.globals()
.set(
"Reports",
AllReports {
reports: Vec::new(),
},
)
.unwrap();
}
}