From 9b98c65060be726f831ba5b3573590e9af9af04c Mon Sep 17 00:00:00 2001
From: meehl <12648828+meehl@users.noreply.github.com>
Date: Sat, 17 Oct 2026 00:14:23 +0200
Subject: [PATCH] Register `luacurl` module
This removes the runtime dependency on the external `Lua-cURLv3` C library.
---
Cargo.lock | 69 ++++++-
Cargo.toml | 1 +
src/pob.rs | 3 +-
src/pob/modules.rs | 1 +
src/pob/modules/luacurl.rs | 29 +++
src/pob/modules/luacurl/constants.rs | 25 +++
src/pob/modules/luacurl/easy.rs | 290 +++++++++++++++++++++++++++
src/pob/subscript.rs | 6 +-
8 files changed, 421 insertions(+), 3 deletions(-)
create mode 100644 src/pob/modules/luacurl.rs
create mode 100644 src/pob/modules/luacurl/constants.rs
create mode 100644 src/pob/modules/luacurl/easy.rs
diff --git a/Cargo.lock b/Cargo.lock
index 0d32a28..b068173 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -735,6 +735,36 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5"
+[[package]]
+name = "curl"
+version = "0.4.51"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ef3ce06df3ac230a4f5c91bfd3e3961cd66f177bd9964c87deb3cce59a54fb7b"
+dependencies = [
+ "curl-sys",
+ "libc",
+ "openssl-probe 0.1.6",
+ "openssl-sys",
+ "schannel",
+ "socket2",
+ "windows-sys 0.61.2",
+]
+
+[[package]]
+name = "curl-sys"
+version = "0.4.91+curl-8.22.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cd39579123e45bf762716bb893d43886ed1cabe6289455be29a4f1d4e37afb06"
+dependencies = [
+ "cc",
+ "libc",
+ "libz-sys",
+ "openssl-sys",
+ "pkg-config",
+ "vcpkg",
+ "windows-sys 0.61.2",
+]
+
[[package]]
name = "cursor-icon"
version = "1.2.0"
@@ -1840,6 +1870,18 @@ dependencies = [
"redox_syscall 0.9.3",
]
+[[package]]
+name = "libz-sys"
+version = "1.1.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f710a23e6dbf193214fd46ca56a9d6864e550abe86202184532ae7275e46de19"
+dependencies = [
+ "cc",
+ "libc",
+ "pkg-config",
+ "vcpkg",
+]
+
[[package]]
name = "linebender_resource_handle"
version = "0.1.1"
@@ -2513,12 +2555,30 @@ dependencies = [
"libc",
]
+[[package]]
+name = "openssl-probe"
+version = "0.1.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e"
+
[[package]]
name = "openssl-probe"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe"
+[[package]]
+name = "openssl-sys"
+version = "0.9.117"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b47e7e6bb2c38cd930d25a23b40fa52e068c10e85f3e03a7f5ba5aaca5713695"
+dependencies = [
+ "cc",
+ "libc",
+ "pkg-config",
+ "vcpkg",
+]
+
[[package]]
name = "option-ext"
version = "0.2.0"
@@ -3307,7 +3367,7 @@ version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dab5152771c58876a2146916e53e35057e1a4dfa2b9df0f0305b07f611fdea4d"
dependencies = [
- "openssl-probe",
+ "openssl-probe 0.2.1",
"rustls-pki-types",
"schannel",
"security-framework",
@@ -3377,6 +3437,7 @@ dependencies = [
"arboard",
"bytemuck",
"clap",
+ "curl",
"dds",
"directories",
"env_logger",
@@ -4078,6 +4139,12 @@ dependencies = [
"wasm-bindgen",
]
+[[package]]
+name = "vcpkg"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
+
[[package]]
name = "version_check"
version = "0.9.5"
diff --git a/Cargo.toml b/Cargo.toml
index 280c379..e2cc2cc 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -14,6 +14,7 @@ anyhow = "1.0"
arboard = { version = "3.6.1", default-features = false }
bytemuck = "1.23.2"
clap = { version = "4.5.46", features = ["derive"] }
+curl = "0.4.51"
dds = "0.2.0"
directories = "6.0.0"
env_logger = "0.11"
diff --git a/src/pob.rs b/src/pob.rs
index 159a226..0831534 100644
--- a/src/pob.rs
+++ b/src/pob.rs
@@ -11,7 +11,7 @@ use crate::{
fonts::Fonts,
input::InputState,
pob::{
- modules::luasocket,
+ modules::{luacurl, luasocket},
subscript::{NativeMultiValue, SubscriptEvent, SubscriptManager},
},
renderer::textures::TextureManager,
@@ -130,6 +130,7 @@ impl PathOfBuilding {
// register modules
lua.register_module("lua-utf8", luautf8::create_module(&lua)?)?;
lua.register_module("socket", luasocket::create_module(&lua)?)?;
+ lua.register_module("lcurl.safe", luacurl::create_module(&lua)?)?;
Ok(lua)
}
diff --git a/src/pob/modules.rs b/src/pob/modules.rs
index 48a86c1..44a7a39 100644
--- a/src/pob/modules.rs
+++ b/src/pob/modules.rs
@@ -1 +1,2 @@
+pub mod luacurl;
pub mod luasocket;
diff --git a/src/pob/modules/luacurl.rs b/src/pob/modules/luacurl.rs
new file mode 100644
index 0000000..5a41627
--- /dev/null
+++ b/src/pob/modules/luacurl.rs
@@ -0,0 +1,29 @@
+use mlua::{Lua, Result as LuaResult, Table};
+
+use crate::pob::modules::luacurl::easy::EasyHandle;
+
+mod constants;
+mod easy;
+
+pub fn create_module(lua: &Lua) -> LuaResult
{
+ let exports = lua.create_table()?;
+
+ exports.set("easy", lua.create_function(|_, ()| Ok(EasyHandle::new()))?)?;
+
+ exports.set("OPT_ACCEPT_ENCODING", constants::OPT_ACCEPT_ENCODING)?;
+ exports.set("OPT_FOLLOWLOCATION", constants::OPT_FOLLOWLOCATION)?;
+ exports.set("OPT_HTTPHEADER", constants::OPT_HTTPHEADER)?;
+ exports.set("OPT_IPRESOLVE", constants::OPT_IPRESOLVE)?;
+ exports.set("OPT_POST", constants::OPT_POST)?;
+ exports.set("OPT_POSTFIELDS", constants::OPT_POSTFIELDS)?;
+ exports.set("OPT_PROXY", constants::OPT_PROXY)?;
+ exports.set("OPT_SSL_VERIFYHOST", constants::OPT_SSL_VERIFYHOST)?;
+ exports.set("OPT_SSL_VERIFYPEER", constants::OPT_SSL_VERIFYPEER)?;
+ exports.set("OPT_USERAGENT", constants::OPT_USERAGENT)?;
+
+ exports.set("INFO_RESPONSE_CODE", constants::INFO_RESPONSE_CODE)?;
+ exports.set("INFO_SIZE_DOWNLOAD", constants::INFO_SIZE_DOWNLOAD)?;
+ exports.set("INFO_REDIRECT_URL", constants::INFO_REDIRECT_URL)?;
+
+ Ok(exports)
+}
diff --git a/src/pob/modules/luacurl/constants.rs b/src/pob/modules/luacurl/constants.rs
new file mode 100644
index 0000000..fa1e426
--- /dev/null
+++ b/src/pob/modules/luacurl/constants.rs
@@ -0,0 +1,25 @@
+pub type CURLoption = i64;
+pub type CURLinfo = i64;
+
+const CURLOPTTYPE_LONG: CURLoption = 0;
+const CURLOPTTYPE_OBJECTPOINT: CURLoption = 10_000;
+
+pub const OPT_URL: CURLoption = CURLOPTTYPE_OBJECTPOINT + 2;
+pub const OPT_PROXY: CURLoption = CURLOPTTYPE_OBJECTPOINT + 4;
+pub const OPT_POSTFIELDS: CURLoption = CURLOPTTYPE_OBJECTPOINT + 15;
+pub const OPT_USERAGENT: CURLoption = CURLOPTTYPE_OBJECTPOINT + 18;
+pub const OPT_HTTPHEADER: CURLoption = CURLOPTTYPE_OBJECTPOINT + 23;
+pub const OPT_POST: CURLoption = CURLOPTTYPE_LONG + 47;
+pub const OPT_FOLLOWLOCATION: CURLoption = CURLOPTTYPE_LONG + 52;
+pub const OPT_SSL_VERIFYPEER: CURLoption = CURLOPTTYPE_LONG + 64;
+pub const OPT_SSL_VERIFYHOST: CURLoption = CURLOPTTYPE_LONG + 81;
+pub const OPT_ACCEPT_ENCODING: CURLoption = CURLOPTTYPE_OBJECTPOINT + 102;
+pub const OPT_IPRESOLVE: CURLoption = CURLOPTTYPE_LONG + 113;
+
+const CURLINFO_STRING: CURLinfo = 0x100000;
+const CURLINFO_LONG: CURLinfo = 0x200000;
+const CURLINFO_DOUBLE: CURLinfo = 0x300000;
+
+pub const INFO_RESPONSE_CODE: CURLinfo = CURLINFO_LONG + 2;
+pub const INFO_SIZE_DOWNLOAD: CURLinfo = CURLINFO_DOUBLE + 8;
+pub const INFO_REDIRECT_URL: CURLinfo = CURLINFO_STRING + 31;
diff --git a/src/pob/modules/luacurl/easy.rs b/src/pob/modules/luacurl/easy.rs
new file mode 100644
index 0000000..7c67eab
--- /dev/null
+++ b/src/pob/modules/luacurl/easy.rs
@@ -0,0 +1,290 @@
+use curl::easy::{Easy, IpResolve};
+use mlua::{
+ Error as LuaError, IntoLua, Lua, LuaString, RegistryKey, Result as LuaResult, UserData,
+ UserDataMethods,
+};
+
+use crate::pob::modules::luacurl::constants;
+
+/// Helper for constructing a `mlua::Error::BadArgument`
+fn bad_argument(to: impl Into, pos: usize, cause: &'static str) -> LuaError {
+ #[allow(clippy::arc_with_non_send_sync)]
+ LuaError::BadArgument {
+ to: Some(to.into()),
+ pos: pos + 1,
+ name: None,
+ cause: std::sync::Arc::new(LuaError::runtime(cause)),
+ }
+}
+
+#[derive(Default)]
+pub struct EasyHandle {
+ easy: Option,
+ write_callback: Option,
+ header_callback: Option,
+}
+
+impl EasyHandle {
+ pub fn new() -> Self {
+ Self {
+ easy: Some(Easy::new()),
+ write_callback: None,
+ header_callback: None,
+ }
+ }
+
+ fn curl(&mut self) -> LuaResult<&mut Easy> {
+ self.easy
+ .as_mut()
+ .ok_or_else(|| LuaError::runtime("curl handle is closed"))
+ }
+
+ pub fn close(&mut self, lua: &Lua) -> LuaResult<()> {
+ if let Some(key) = self.write_callback.take() {
+ lua.remove_registry_value(key)?;
+ }
+
+ if let Some(key) = self.header_callback.take() {
+ lua.remove_registry_value(key)?;
+ }
+
+ self.easy = None;
+ Ok(())
+ }
+
+ // TODO: handling of `value` might not be 100% correct w.r.t. casting and mapping
+ // of number values to arguments of `easy` methods. extract to helper functions.
+ pub fn set_option(&mut self, option: i64, value: mlua::Value) -> LuaResult<()> {
+ let easy = self.curl()?;
+
+ match option {
+ constants::OPT_ACCEPT_ENCODING => {
+ let encoding = match value {
+ mlua::Value::String(s) => s.to_str(),
+ _ => Err(bad_argument("setopt", 1, "string expected")),
+ }?;
+ easy.accept_encoding(&encoding)
+ .map_err(LuaError::external)?;
+ }
+ constants::OPT_FOLLOWLOCATION => {
+ let enable = match value {
+ mlua::Value::Boolean(b) => Ok(b),
+ mlua::Value::Integer(n) => Ok(n != 0),
+ mlua::Value::Number(n) => Ok(n != 0.0),
+ _ => Err(bad_argument("setopt", 1, "number or boolean expected")),
+ }?;
+ easy.follow_location(enable).map_err(LuaError::external)?;
+ }
+ constants::OPT_HTTPHEADER => {
+ let headers = match value {
+ mlua::Value::Table(t) => Ok(t),
+ _ => Err(bad_argument("setopt", 1, "array expected")),
+ }?;
+
+ let mut list = curl::easy::List::new();
+ for header in headers.sequence_values::() {
+ list.append(&header?).map_err(LuaError::external)?;
+ }
+
+ easy.http_headers(list).map_err(LuaError::external)?;
+ }
+ constants::OPT_IPRESOLVE => {
+ let resolve = match value {
+ mlua::Value::Integer(0) => Ok(IpResolve::Any),
+ mlua::Value::Integer(1) => Ok(IpResolve::V4),
+ mlua::Value::Integer(2) => Ok(IpResolve::V6),
+ _ => Err(bad_argument("setopt", 1, "number or boolean expected")),
+ }?;
+ easy.ip_resolve(resolve).map_err(LuaError::external)?;
+ }
+ constants::OPT_POST => {
+ let enable = match value {
+ mlua::Value::Boolean(b) => Ok(b),
+ mlua::Value::Integer(n) => Ok(n != 0),
+ mlua::Value::Number(n) => Ok(n != 0.0),
+ _ => Err(bad_argument("setopt", 1, "number or boolean expected")),
+ }?;
+ easy.post(enable).map_err(LuaError::external)?;
+ }
+ constants::OPT_POSTFIELDS => {
+ let data = match value {
+ mlua::Value::String(s) => Ok(s.as_bytes()),
+ _ => Err(bad_argument("setopt", 1, "string expected")),
+ }?;
+ easy.post_fields_copy(&data).map_err(LuaError::external)?;
+ }
+ constants::OPT_PROXY => {
+ let proxy = match value {
+ mlua::Value::String(s) => s.to_str(),
+ _ => Err(bad_argument("setopt", 1, "string expected")),
+ }?;
+ easy.proxy(&proxy).map_err(LuaError::external)?;
+ }
+ constants::OPT_SSL_VERIFYHOST => {
+ let verify = match value {
+ mlua::Value::Boolean(b) => Ok(b),
+ mlua::Value::Integer(n) => Ok(n != 0),
+ mlua::Value::Number(n) => Ok(n != 0.0),
+ _ => Err(bad_argument("setopt", 1, "number or boolean expected")),
+ }?;
+ easy.ssl_verify_host(verify).map_err(LuaError::external)?;
+ }
+ constants::OPT_SSL_VERIFYPEER => {
+ let verify = match value {
+ mlua::Value::Boolean(b) => Ok(b),
+ mlua::Value::Integer(n) => Ok(n != 0),
+ mlua::Value::Number(n) => Ok(n != 0.0),
+ _ => Err(bad_argument("setopt", 1, "number or boolean expected")),
+ }?;
+ easy.ssl_verify_peer(verify).map_err(LuaError::external)?;
+ }
+ constants::OPT_USERAGENT => {
+ let useragent = match value {
+ mlua::Value::String(s) => s.to_str(),
+ _ => Err(bad_argument("setopt", 1, "string expected")),
+ }?;
+ easy.useragent(&useragent).map_err(LuaError::external)?;
+ }
+ constants::OPT_URL => {
+ let url = match value {
+ mlua::Value::String(s) => s.to_str(),
+ _ => Err(bad_argument("setopt", 1, "string expected")),
+ }?;
+ easy.url(&url).map_err(LuaError::external)?;
+ }
+ _ => unimplemented!("Unsupported `setopt` option: {option}"),
+ }
+
+ Ok(())
+ }
+
+ fn set_callback(
+ lua: &Lua,
+ slot: &mut Option,
+ value: mlua::Value,
+ ) -> LuaResult<()> {
+ let new_key = match value {
+ mlua::Value::Nil => None,
+ mlua::Value::Function(f) => Some(lua.create_registry_value(f)?),
+ _ => return Err(LuaError::runtime("invalid object type")),
+ };
+
+ if let Some(old_key) = slot.take() {
+ lua.remove_registry_value(old_key)?;
+ }
+
+ *slot = new_key;
+
+ Ok(())
+ }
+
+ fn perform(&mut self, lua: &Lua) -> LuaResult<()> {
+ let write_fn = self
+ .write_callback
+ .as_ref()
+ .map(|key| lua.registry_value::(key))
+ .transpose()?;
+
+ let header_fn = self
+ .header_callback
+ .as_ref()
+ .map(|key| lua.registry_value::(key))
+ .transpose()?;
+
+ let transfer_result = {
+ let mut transfer = self.curl()?.transfer();
+
+ if let Some(function) = write_fn {
+ transfer
+ .write_function(move |data| {
+ let Ok(argument) = lua.create_string(data) else {
+ return Ok(0);
+ };
+
+ match function.call::<()>(argument) {
+ Ok(()) => Ok(data.len()),
+ Err(_) => Ok(0),
+ }
+ })
+ .map_err(LuaError::external)?;
+ }
+
+ if let Some(function) = header_fn {
+ transfer
+ .header_function(move |data| {
+ let Ok(argument) = lua.create_string(data) else {
+ return false;
+ };
+
+ function.call::(argument).unwrap_or(false)
+ })
+ .map_err(LuaError::external)?;
+ }
+
+ transfer.perform()
+ };
+
+ transfer_result.map_err(LuaError::external)?;
+ Ok(())
+ }
+
+ fn get_info(&mut self, lua: &Lua, info: i64) -> LuaResult {
+ let easy = self.curl()?;
+
+ match info {
+ constants::INFO_REDIRECT_URL => {
+ let url = easy.redirect_url().map_err(LuaError::external)?;
+ url.into_lua(lua)
+ }
+ constants::INFO_RESPONSE_CODE => {
+ let code = easy.response_code().map_err(LuaError::external)?;
+ code.into_lua(lua)
+ }
+ constants::INFO_SIZE_DOWNLOAD => {
+ let size = easy.download_size().map_err(LuaError::external)?;
+ size.into_lua(lua)
+ }
+ _ => unimplemented!("Unsupported `getinfo` key: {info}"),
+ }
+ }
+}
+
+impl UserData for EasyHandle {
+ fn add_methods>(methods: &mut M) {
+ methods.add_method("escape", |lua, _, value: LuaString| {
+ let input = value.as_bytes();
+ let mut easy = Easy::new();
+ let escaped = easy.url_encode(input.as_ref());
+ lua.create_string(escaped)
+ });
+
+ methods.add_method_mut("close", |lua, this, ()| this.close(lua));
+
+ methods.add_method_mut("setopt", |_, this, (option, value): (i64, mlua::Value)| {
+ this.set_option(option, value)
+ });
+
+ methods.add_method_mut("setopt_url", |_, this, value: mlua::Value| {
+ this.set_option(constants::OPT_URL, value)
+ });
+ methods.add_method_mut("setopt_useragent", |_, this, value: mlua::Value| {
+ this.set_option(constants::OPT_USERAGENT, value)
+ });
+
+ methods.add_method_mut("setopt_writefunction", |lua, this, value: mlua::Value| {
+ Self::set_callback(lua, &mut this.write_callback, value)
+ });
+
+ methods.add_method_mut("setopt_headerfunction", |lua, this, value: mlua::Value| {
+ Self::set_callback(lua, &mut this.header_callback, value)
+ });
+
+ methods.add_method_mut("perform", |lua, this, ()| this.perform(lua));
+
+ methods.add_method_mut("getinfo", |lua, this, info: i64| this.get_info(lua, info));
+
+ methods.add_method_mut("getinfo_response_code", |lua, this, ()| {
+ this.get_info(lua, constants::INFO_RESPONSE_CODE)
+ });
+ }
+}
diff --git a/src/pob/subscript.rs b/src/pob/subscript.rs
index 2579a54..e6c426f 100644
--- a/src/pob/subscript.rs
+++ b/src/pob/subscript.rs
@@ -1,4 +1,7 @@
-use crate::{pob::modules::luasocket, util::append_lua_package_dir};
+use crate::{
+ pob::modules::{luacurl, luasocket},
+ util::append_lua_package_dir,
+};
use anyhow::{Result, anyhow};
use mlua::{Integer, IntoLuaMulti, Lua, MultiValue, Number, Value};
use std::{
@@ -144,6 +147,7 @@ impl Subscript {
// register modules
lua.register_module("lua-utf8", luautf8::create_module(&lua)?)?;
lua.register_module("socket", luasocket::create_module(&lua)?)?;
+ lua.register_module("lcurl.safe", luacurl::create_module(&lua)?)?;
// register a proxy for each allowed blocking call.
// forwards the call to the main thread and blocks on `reply_rx` until the main thread