Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set a Standard User Agent #186

Merged
merged 7 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lune/builtins/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use super::serde::encode_decode::{EncodeDecodeConfig, EncodeDecodeFormat};

pub fn create(lua: &Lua) -> LuaResult<LuaTable> {
NetClientBuilder::new()
.headers(&[("User-Agent", create_user_agent_header())])?
.headers(&[("User-Agent", create_user_agent_header(lua)?)])?
.build()?
.into_registry(lua);
TableBuilder::new(lua)?
Expand Down
19 changes: 13 additions & 6 deletions src/lune/builtins/net/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ use mlua::prelude::*;

use crate::lune::util::TableBuilder;

pub fn create_user_agent_header() -> String {
let (github_owner, github_repo) = env!("CARGO_PKG_REPOSITORY")
.trim_start_matches("https://github.com/")
.split_once('/')
.unwrap();
format!("{github_owner}-{github_repo}-cli")
pub fn create_user_agent_header(lua: &Lua) -> LuaResult<String> {
let version_global = lua
.globals()
.get::<_, LuaString>("_VERSION")
.expect("Missing _VERSION global");

let version_global_str = version_global
.to_str()
.context("Invalid utf8 found in _VERSION global")?;

let (package_name, full_version) = version_global_str.split_once(' ').unwrap();

Ok(format!("{}/{}", package_name.to_lowercase(), full_version))
}

pub fn header_map_to_table(
Expand Down
5 changes: 3 additions & 2 deletions src/lune/globals/version.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use mlua::prelude::*;

pub fn create(lua: &Lua) -> LuaResult<impl IntoLua<'_>> {
let lune_version = format!("Lune {}", env!("CARGO_PKG_VERSION"));
let lune_version = format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
CompeyDev marked this conversation as resolved.
Show resolved Hide resolved

let luau_version_full = lua
.globals()
.get::<_, LuaString>("_VERSION")
.expect("Missing _VERSION global");

let luau_version_str = luau_version_full
.to_str()
.context("Invalid utf8 found in _VERSION global")?;

// If this function runs more than once, we
// may get an already formatted lune version.
if luau_version_str.starts_with(&lune_version) {
if luau_version_str.starts_with(lune_version.as_str()) {
return Ok(luau_version_full);
}

Expand Down
9 changes: 9 additions & 0 deletions tests/net/request/user_agent.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local net = require("@lune/net")

local runtime, version = table.unpack(_VERSION:split(" "))
local expectedUserAgent = runtime:lower() .. "/" .. version

local userAgent: string =
net.jsonDecode(net.request("https://www.whatsmyua.info/api/v1/ua").body)[1].ua.rawUa

assert(userAgent == expectedUserAgent, "Expected user agent to be " .. expectedUserAgent)
Loading