-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.rs
More file actions
182 lines (171 loc) · 7.61 KB
/
Copy pathhttp.rs
File metadata and controls
182 lines (171 loc) · 7.61 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use crate::{StdFunction, StdlibModule, StdlibRegistry};
use indexmap::IndexMap;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use techscript_runtime::{
context::Capability,
error::{RuntimeError, RuntimeErrorKind},
value::RuntimeValue,
};
impl StdlibRegistry {
pub fn register_http(&mut self) {
let mut exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
HashMap::new();
exports.insert(
"get".to_string(),
Rc::new(StdFunction {
name: "get".to_string(),
arity: 1,
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::Network) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Network capability is denied"
.to_string(),
),
None,
None,
));
}
let url = args[0].try_into_string()?;
let response = ureq::get(&url).call().map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!(
"HTTP GET request failed: {}",
e
)),
None,
None,
)
})?;
let status = response.status();
let body = response.into_string().map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!(
"Failed to read HTTP response body: {}",
e
)),
None,
None,
)
})?;
let mut res_map = IndexMap::new();
res_map.insert("status".to_string(), RuntimeValue::Int(status as i64));
res_map.insert("body".to_string(), RuntimeValue::Str(body));
Ok(RuntimeValue::Map {
entries: Rc::new(RefCell::new(res_map)),
is_const: false,
})
},
}),
);
exports.insert(
"post".to_string(),
Rc::new(StdFunction {
name: "post".to_string(),
arity: 2,
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::Network) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Network capability is denied"
.to_string(),
),
None,
None,
));
}
let url = args[0].try_into_string()?;
let body = args[1].try_into_string()?;
let response = ureq::post(&url).send_string(&body).map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!(
"HTTP POST request failed: {}",
e
)),
None,
None,
)
})?;
let status = response.status();
let res_body = response.into_string().map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!(
"Failed to read HTTP response body: {}",
e
)),
None,
None,
)
})?;
let mut res_map = IndexMap::new();
res_map.insert("status".to_string(), RuntimeValue::Int(status as i64));
res_map.insert("body".to_string(), RuntimeValue::Str(res_body));
Ok(RuntimeValue::Map {
entries: Rc::new(RefCell::new(res_map)),
is_const: false,
})
},
}),
);
exports.insert(
"listen".to_string(),
Rc::new(StdFunction {
name: "listen".to_string(),
arity: 2,
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::Network) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Network capability is denied".to_string(),
),
None,
None,
));
}
let port = args[0].try_into_int()?;
let callback = args[1].clone();
if let RuntimeValue::Function(func) = callback {
let listener = std::net::TcpListener::bind(format!("127.0.0.1:{}", port)).map_err(|e| {
RuntimeError::new(RuntimeErrorKind::InvalidOperation(format!("HTTP listen bind error: {}", e)), None, None)
})?;
listener.set_nonblocking(true).ok();
if let Ok((mut stream, _)) = listener.accept() {
use std::io::{Read, Write};
let mut buf = [0; 1024];
if let Ok(n) = stream.read(&mut buf) {
let request_text = String::from_utf8_lossy(&buf[..n]);
let mut req_map = IndexMap::new();
req_map.insert("raw".to_string(), RuntimeValue::Str(request_text.to_string()));
let req_val = RuntimeValue::Map {
entries: Rc::new(RefCell::new(req_map)),
is_const: false,
};
if let Ok(res) = func.call(ctx, vec![req_val]) {
let body = res.try_into_string().unwrap_or_else(|_| "Hello".to_string());
let response_str = format!(
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\nContent-Type: text/plain\r\n\r\n{}",
body.len(),
body
);
stream.write_all(response_str.as_bytes()).ok();
}
}
}
}
Ok(RuntimeValue::Null)
},
}),
);
self.register_module(
"std.http",
StdlibModule {
name: "std.http".to_string(),
version: "1.0.0".to_string(),
exports,
required_capabilities: vec![Capability::Network],
},
);
}
}