Add back url encoding and decoding functions

This commit is contained in:
Filip Tibell 2025-04-27 13:17:39 +02:00
parent 611a9a92d8
commit 9157ed9d33
6 changed files with 58 additions and 2 deletions

7
Cargo.lock generated
View file

@ -1870,6 +1870,7 @@ dependencies = [
"rustls 0.23.26",
"rustls-pki-types",
"url",
"urlencoding",
"webpki",
"webpki-roots 0.26.8",
]
@ -3742,6 +3743,12 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "urlencoding"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
[[package]]
name = "ustr"
version = "1.1.0"

View file

@ -30,6 +30,7 @@ pin-project-lite = "0.2"
rustls = "0.23"
rustls-pki-types = "1.11"
url = "2.5"
urlencoding = "2.1"
webpki = "0.22"
webpki-roots = "0.26"

View file

@ -37,11 +37,29 @@ pub fn module(lua: Lua) -> LuaResult<LuaTable> {
.with_async_function("request", net_request)?
// .with_async_function("socket", net_socket)?
// .with_async_function("serve", net_serve)?
// .with_function("urlEncode", net_url_encode)?
// .with_function("urlDecode", net_url_decode)?
.with_function("urlEncode", net_url_encode)?
.with_function("urlDecode", net_url_decode)?
.build_readonly()
}
async fn net_request(lua: Lua, config: RequestConfig) -> LuaResult<Response> {
self::client::send_request(Request::try_from(config)?, lua).await
}
fn net_url_encode(
lua: &Lua,
(lua_string, as_binary): (LuaString, Option<bool>),
) -> LuaResult<LuaString> {
let as_binary = as_binary.unwrap_or_default();
let bytes = self::url::encode(lua_string, as_binary)?;
lua.create_string(bytes)
}
fn net_url_decode(
lua: &Lua,
(lua_string, as_binary): (LuaString, Option<bool>),
) -> LuaResult<LuaString> {
let as_binary = as_binary.unwrap_or_default();
let bytes = self::url::decode(lua_string, as_binary)?;
lua.create_string(bytes)
}

View file

@ -0,0 +1,12 @@
use mlua::prelude::*;
pub fn decode(lua_string: LuaString, as_binary: bool) -> LuaResult<Vec<u8>> {
if as_binary {
Ok(urlencoding::decode_binary(&lua_string.as_bytes()).into_owned())
} else {
Ok(urlencoding::decode(&lua_string.to_str()?)
.map_err(|e| LuaError::RuntimeError(format!("Encountered invalid encoding - {e}")))?
.into_owned()
.into_bytes())
}
}

View file

@ -0,0 +1,13 @@
use mlua::prelude::*;
pub fn encode(lua_string: LuaString, as_binary: bool) -> LuaResult<Vec<u8>> {
if as_binary {
Ok(urlencoding::encode_binary(&lua_string.as_bytes())
.into_owned()
.into_bytes())
} else {
Ok(urlencoding::encode(&lua_string.to_str()?)
.into_owned()
.into_bytes())
}
}

View file

@ -0,0 +1,5 @@
mod decode;
mod encode;
pub use self::decode::decode;
pub use self::encode::encode;