mirror of
https://github.com/lune-org/lune.git
synced 2025-04-16 18:03:57 +01:00
Add back util and config files
This commit is contained in:
parent
0d0b4cbd6c
commit
65def158d2
3 changed files with 287 additions and 3 deletions
195
src/lune/builtins/net/config.rs
Normal file
195
src/lune/builtins/net/config.rs
Normal file
|
@ -0,0 +1,195 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use mlua::prelude::*;
|
||||||
|
|
||||||
|
use reqwest::Method;
|
||||||
|
|
||||||
|
use super::util::table_to_hash_map;
|
||||||
|
|
||||||
|
// Net request config
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct RequestConfigOptions {
|
||||||
|
pub decompress: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for RequestConfigOptions {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self { decompress: true }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'lua> FromLua<'lua> for RequestConfigOptions {
|
||||||
|
fn from_lua(value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult<Self> {
|
||||||
|
// Nil means default options, table means custom options
|
||||||
|
if let LuaValue::Nil = value {
|
||||||
|
return Ok(Self::default());
|
||||||
|
} else if let LuaValue::Table(tab) = value {
|
||||||
|
// Extract flags
|
||||||
|
let decompress = match tab.raw_get::<_, Option<bool>>("decompress") {
|
||||||
|
Ok(decomp) => Ok(decomp.unwrap_or(true)),
|
||||||
|
Err(_) => Err(LuaError::RuntimeError(
|
||||||
|
"Invalid option value for 'decompress' in request config options".to_string(),
|
||||||
|
)),
|
||||||
|
}?;
|
||||||
|
return Ok(Self { decompress });
|
||||||
|
}
|
||||||
|
// Anything else is invalid
|
||||||
|
Err(LuaError::FromLuaConversionError {
|
||||||
|
from: value.type_name(),
|
||||||
|
to: "RequestConfigOptions",
|
||||||
|
message: Some(format!(
|
||||||
|
"Invalid request config options - expected table or nil, got {}",
|
||||||
|
value.type_name()
|
||||||
|
)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct RequestConfig {
|
||||||
|
pub url: String,
|
||||||
|
pub method: Method,
|
||||||
|
pub query: HashMap<String, Vec<String>>,
|
||||||
|
pub headers: HashMap<String, Vec<String>>,
|
||||||
|
pub body: Option<Vec<u8>>,
|
||||||
|
pub options: RequestConfigOptions,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromLua<'_> for RequestConfig {
|
||||||
|
fn from_lua(value: LuaValue, lua: &Lua) -> LuaResult<Self> {
|
||||||
|
// If we just got a string we assume its a GET request to a given url
|
||||||
|
if let LuaValue::String(s) = value {
|
||||||
|
return Ok(Self {
|
||||||
|
url: s.to_string_lossy().to_string(),
|
||||||
|
method: Method::GET,
|
||||||
|
query: HashMap::new(),
|
||||||
|
headers: HashMap::new(),
|
||||||
|
body: None,
|
||||||
|
options: Default::default(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// If we got a table we are able to configure the entire request
|
||||||
|
if let LuaValue::Table(tab) = value {
|
||||||
|
// Extract url
|
||||||
|
let url = match tab.raw_get::<_, LuaString>("url") {
|
||||||
|
Ok(config_url) => Ok(config_url.to_string_lossy().to_string()),
|
||||||
|
Err(_) => Err(LuaError::runtime("Missing 'url' in request config")),
|
||||||
|
}?;
|
||||||
|
// Extract method
|
||||||
|
let method = match tab.raw_get::<_, LuaString>("method") {
|
||||||
|
Ok(config_method) => config_method.to_string_lossy().trim().to_ascii_uppercase(),
|
||||||
|
Err(_) => "GET".to_string(),
|
||||||
|
};
|
||||||
|
// Extract query
|
||||||
|
let query = match tab.raw_get::<_, LuaTable>("query") {
|
||||||
|
Ok(tab) => table_to_hash_map(tab, "query")?,
|
||||||
|
Err(_) => HashMap::new(),
|
||||||
|
};
|
||||||
|
// Extract headers
|
||||||
|
let headers = match tab.raw_get::<_, LuaTable>("headers") {
|
||||||
|
Ok(tab) => table_to_hash_map(tab, "headers")?,
|
||||||
|
Err(_) => HashMap::new(),
|
||||||
|
};
|
||||||
|
// Extract body
|
||||||
|
let body = match tab.raw_get::<_, LuaString>("body") {
|
||||||
|
Ok(config_body) => Some(config_body.as_bytes().to_owned()),
|
||||||
|
Err(_) => None,
|
||||||
|
};
|
||||||
|
// Convert method string into proper enum
|
||||||
|
let method = method.trim().to_ascii_uppercase();
|
||||||
|
let method = match method.as_ref() {
|
||||||
|
"GET" => Ok(Method::GET),
|
||||||
|
"POST" => Ok(Method::POST),
|
||||||
|
"PUT" => Ok(Method::PUT),
|
||||||
|
"DELETE" => Ok(Method::DELETE),
|
||||||
|
"HEAD" => Ok(Method::HEAD),
|
||||||
|
"OPTIONS" => Ok(Method::OPTIONS),
|
||||||
|
"PATCH" => Ok(Method::PATCH),
|
||||||
|
_ => Err(LuaError::RuntimeError(format!(
|
||||||
|
"Invalid request config method '{}'",
|
||||||
|
&method
|
||||||
|
))),
|
||||||
|
}?;
|
||||||
|
// Parse any extra options given
|
||||||
|
let options = match tab.raw_get::<_, LuaValue>("options") {
|
||||||
|
Ok(opts) => RequestConfigOptions::from_lua(opts, lua)?,
|
||||||
|
Err(_) => RequestConfigOptions::default(),
|
||||||
|
};
|
||||||
|
// All good, validated and we got what we need
|
||||||
|
return Ok(Self {
|
||||||
|
url,
|
||||||
|
method,
|
||||||
|
query,
|
||||||
|
headers,
|
||||||
|
body,
|
||||||
|
options,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
// Anything else is invalid
|
||||||
|
Err(LuaError::FromLuaConversionError {
|
||||||
|
from: value.type_name(),
|
||||||
|
to: "RequestConfig",
|
||||||
|
message: Some(format!(
|
||||||
|
"Invalid request config - expected string or table, got {}",
|
||||||
|
value.type_name()
|
||||||
|
)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Net serve config
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ServeConfig<'a> {
|
||||||
|
pub handle_request: LuaFunction<'a>,
|
||||||
|
pub handle_web_socket: Option<LuaFunction<'a>>,
|
||||||
|
pub address: Option<LuaString<'a>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'lua> FromLua<'lua> for ServeConfig<'lua> {
|
||||||
|
fn from_lua(value: LuaValue<'lua>, lua: &'lua Lua) -> LuaResult<Self> {
|
||||||
|
let message = match &value {
|
||||||
|
LuaValue::Function(f) => {
|
||||||
|
return Ok(ServeConfig {
|
||||||
|
handle_request: f.clone(),
|
||||||
|
handle_web_socket: None,
|
||||||
|
address: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
LuaValue::Table(t) => {
|
||||||
|
let handle_request: Option<LuaFunction> = t.raw_get("handleRequest")?;
|
||||||
|
let handle_web_socket: Option<LuaFunction> = t.raw_get("handleWebSocket")?;
|
||||||
|
let address: Option<LuaString> = t.raw_get("address")?;
|
||||||
|
if handle_request.is_some() || handle_web_socket.is_some() {
|
||||||
|
return Ok(ServeConfig {
|
||||||
|
handle_request: handle_request.unwrap_or_else(|| {
|
||||||
|
let chunk = r#"
|
||||||
|
return {
|
||||||
|
status = 426,
|
||||||
|
body = "Upgrade Required",
|
||||||
|
headers = {
|
||||||
|
Upgrade = "websocket",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
lua.load(chunk)
|
||||||
|
.into_function()
|
||||||
|
.expect("Failed to create default http responder function")
|
||||||
|
}),
|
||||||
|
handle_web_socket,
|
||||||
|
address,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
Some("Missing handleRequest and / or handleWebSocket".to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
Err(LuaError::FromLuaConversionError {
|
||||||
|
from: value.type_name(),
|
||||||
|
to: "ServeConfig",
|
||||||
|
message,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,8 +2,13 @@
|
||||||
|
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
|
|
||||||
|
mod config;
|
||||||
|
mod util;
|
||||||
|
|
||||||
use crate::lune::util::TableBuilder;
|
use crate::lune::util::TableBuilder;
|
||||||
|
|
||||||
|
use self::config::{RequestConfig, ServeConfig};
|
||||||
|
|
||||||
use super::serde::encode_decode::{EncodeDecodeConfig, EncodeDecodeFormat};
|
use super::serde::encode_decode::{EncodeDecodeConfig, EncodeDecodeFormat};
|
||||||
|
|
||||||
pub fn create(lua: &Lua) -> LuaResult<LuaTable> {
|
pub fn create(lua: &Lua) -> LuaResult<LuaTable> {
|
||||||
|
@ -38,15 +43,18 @@ fn net_json_decode<'lua>(lua: &'lua Lua, json: LuaString<'lua>) -> LuaResult<Lua
|
||||||
EncodeDecodeConfig::from(EncodeDecodeFormat::Json).deserialize_from_string(lua, json)
|
EncodeDecodeConfig::from(EncodeDecodeFormat::Json).deserialize_from_string(lua, json)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn net_request<'lua>(lua: &'lua Lua, config: ()) -> LuaResult<LuaTable<'lua>> {
|
async fn net_request<'lua>(lua: &'lua Lua, config: RequestConfig) -> LuaResult<LuaTable<'lua>> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn net_socket<'lua>(lua: &'lua Lua, url: String) -> LuaResult<LuaTable> {
|
async fn net_socket<'lua>(lua: &'lua Lua, url: String) -> LuaResult<LuaTable<'lua>> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn net_serve<'lua>(lua: &'lua Lua, (port, config): (u16, ())) -> LuaResult<LuaTable<'lua>> {
|
async fn net_serve<'lua>(
|
||||||
|
lua: &'lua Lua,
|
||||||
|
(port, config): (u16, ServeConfig<'lua>),
|
||||||
|
) -> LuaResult<LuaTable<'lua>> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
81
src/lune/builtins/net/util.rs
Normal file
81
src/lune/builtins/net/util.rs
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use hyper::{
|
||||||
|
header::{CONTENT_ENCODING, CONTENT_LENGTH},
|
||||||
|
HeaderMap,
|
||||||
|
};
|
||||||
|
|
||||||
|
use mlua::prelude::*;
|
||||||
|
|
||||||
|
use crate::lune::util::TableBuilder;
|
||||||
|
|
||||||
|
pub fn header_map_to_table(
|
||||||
|
lua: &Lua,
|
||||||
|
headers: HeaderMap,
|
||||||
|
remove_content_headers: bool,
|
||||||
|
) -> LuaResult<LuaTable> {
|
||||||
|
let mut res_headers: HashMap<String, Vec<String>> = HashMap::new();
|
||||||
|
for (name, value) in headers.iter() {
|
||||||
|
let name = name.as_str();
|
||||||
|
let value = value.to_str().unwrap().to_owned();
|
||||||
|
if let Some(existing) = res_headers.get_mut(name) {
|
||||||
|
existing.push(value);
|
||||||
|
} else {
|
||||||
|
res_headers.insert(name.to_owned(), vec![value]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if remove_content_headers {
|
||||||
|
let content_encoding_header_str = CONTENT_ENCODING.as_str();
|
||||||
|
let content_length_header_str = CONTENT_LENGTH.as_str();
|
||||||
|
res_headers.retain(|name, _| {
|
||||||
|
name != content_encoding_header_str && name != content_length_header_str
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut builder = TableBuilder::new(lua)?;
|
||||||
|
for (name, mut values) in res_headers {
|
||||||
|
if values.len() == 1 {
|
||||||
|
let value = values.pop().unwrap().into_lua(lua)?;
|
||||||
|
builder = builder.with_value(name, value)?;
|
||||||
|
} else {
|
||||||
|
let values = TableBuilder::new(lua)?
|
||||||
|
.with_sequential_values(values)?
|
||||||
|
.build_readonly()?
|
||||||
|
.into_lua(lua)?;
|
||||||
|
builder = builder.with_value(name, values)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.build_readonly()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn table_to_hash_map(
|
||||||
|
tab: LuaTable,
|
||||||
|
tab_origin_key: &'static str,
|
||||||
|
) -> LuaResult<HashMap<String, Vec<String>>> {
|
||||||
|
let mut map = HashMap::new();
|
||||||
|
|
||||||
|
for pair in tab.pairs::<String, LuaValue>() {
|
||||||
|
let (key, value) = pair?;
|
||||||
|
match value {
|
||||||
|
LuaValue::String(s) => {
|
||||||
|
map.insert(key, vec![s.to_str()?.to_owned()]);
|
||||||
|
}
|
||||||
|
LuaValue::Table(t) => {
|
||||||
|
let mut values = Vec::new();
|
||||||
|
for value in t.sequence_values::<LuaString>() {
|
||||||
|
values.push(value?.to_str()?.to_owned());
|
||||||
|
}
|
||||||
|
map.insert(key, values);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return Err(LuaError::runtime(format!(
|
||||||
|
"Value for '{tab_origin_key}' must be a string or array of strings",
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(map)
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue