Fixed https support in net client + update changelog

This commit is contained in:
Filip Tibell 2025-04-30 15:28:29 +02:00
parent 9c9b90d70d
commit d2a89f41c8
No known key found for this signature in database
6 changed files with 71 additions and 9 deletions

View file

@ -12,7 +12,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### Changed
- Significantly improved performance of `net.request` and `net.serve` when handling large request bodies - Improved performance of `net.request` and `net.serve` when handling large request bodies
- Improved performance and memory usage of `task.spawn`, `task.defer`, and `task.delay`
### Fixed
- Fixed accidental breakage of `net.request` in version `0.9.1`
## `0.9.1` - April 29th, 2025 ## `0.9.1` - April 29th, 2025

View file

@ -1,8 +1,22 @@
use std::sync::{Arc, LazyLock}; use std::sync::{
atomic::{AtomicBool, Ordering},
Arc, LazyLock,
};
use rustls::ClientConfig; use rustls::{crypto::ring, ClientConfig};
static PROVIDER_INITIALIZED: AtomicBool = AtomicBool::new(false);
pub fn initialize_provider() {
if !PROVIDER_INITIALIZED.load(Ordering::Relaxed) {
PROVIDER_INITIALIZED.store(true, Ordering::Relaxed);
// Only errors if already installed, which is fine
ring::default_provider().install_default().ok();
}
}
pub static CLIENT_CONFIG: LazyLock<Arc<ClientConfig>> = LazyLock::new(|| { pub static CLIENT_CONFIG: LazyLock<Arc<ClientConfig>> = LazyLock::new(|| {
initialize_provider();
rustls::ClientConfig::builder() rustls::ClientConfig::builder()
.with_root_certificates(rustls::RootCertStore { .with_root_certificates(rustls::RootCertStore {
roots: webpki_roots::TLS_SERVER_ROOTS.to_vec(), roots: webpki_roots::TLS_SERVER_ROOTS.to_vec(),

View file

@ -33,6 +33,8 @@ pub fn typedefs() -> String {
Errors when out of memory. Errors when out of memory.
*/ */
pub fn module(lua: Lua) -> LuaResult<LuaTable> { pub fn module(lua: Lua) -> LuaResult<LuaTable> {
// No initial rustls setup is necessary, the respective
// functions lazily initialize anything there as needed
TableBuilder::new(lua)? TableBuilder::new(lua)?
.with_async_function("request", net_request)? .with_async_function("request", net_request)?
.with_async_function("socket", net_socket)? .with_async_function("socket", net_socket)?

View file

@ -126,6 +126,7 @@ create_tests! {
create_tests! { create_tests! {
net_request_codes: "net/request/codes", net_request_codes: "net/request/codes",
net_request_compression: "net/request/compression", net_request_compression: "net/request/compression",
net_request_https: "net/request/https",
net_request_methods: "net/request/methods", net_request_methods: "net/request/methods",
net_request_query: "net/request/query", net_request_query: "net/request/query",
net_request_redirect: "net/request/redirect", net_request_redirect: "net/request/redirect",

View file

@ -0,0 +1,24 @@
local task = require("@lune/task")
local util = require("./util")
local pass = util.pass
-- These are some public APIs that have, or most likely have, different
-- certificate authorities (CAs), plus are both free to use and stable.
-- This should be enough to ensure that rustls is configured correctly.
local servers = {
"https://www.googleapis.com/discovery/v1/apis",
"https://api.cloudflare.com/client/v4/ips",
"https://azure.microsoft.com/en-us/updates/feed/",
"https://acme-v02.api.letsencrypt.org/directory",
"https://ip-ranges.amazonaws.com/ip-ranges.json",
"https://api.github.com/zen",
"https://en.wikipedia.org/w/api.php",
"https://status.godaddy.com/api/v2/summary.json",
}
for _, server in servers do
task.spawn(function()
pass("GET", server, server)
end)
end

View file

@ -4,22 +4,38 @@ local stdio = require("@lune/stdio")
local util = {} local util = {}
function util.pass(method, url, message) function util.pass(method, url, message)
local response = net.request({ local success, response = pcall(net.request, {
method = method, method = method,
url = url, url = url,
}) })
if not response.ok then if not success then
error(string.format("%s failed!\nResponse: %s", message, stdio.format(response))) error(`{message} errored!\nError message: {tostring(response)}`)
elseif not response.ok then
error(
`{message} failed, but should have passed!`
.. `\nStatus code: {response.statusCode}`
.. `\nStatus message: {response.statusMessage}`
.. `\nResponse headers: {stdio.format(response.headers)}`
.. `\nResponse body: {response.body}`
)
end end
end end
function util.fail(method, url, message) function util.fail(method, url, message)
local response = net.request({ local success, response = pcall(net.request, {
method = method, method = method,
url = url, url = url,
}) })
if response.ok then if not success then
error(string.format("%s passed!\nResponse: %s", message, stdio.format(response))) error(`{message} errored!\nError message: {tostring(response)}`)
elseif response.ok then
error(
`{message} passed, but should have failed!`
.. `\nStatus code: {response.statusCode}`
.. `\nStatus message: {response.statusMessage}`
.. `\nResponse headers: {stdio.format(response.headers)}`
.. `\nResponse body: {response.body}`
)
end end
end end