From 7a565f377203ba8e01574952014a23dfa8d94fa3 Mon Sep 17 00:00:00 2001 From: salmonik Date: Thu, 30 Jul 2026 15:09:51 +0200 Subject: fix: auto completion timing out before ready + fix preexisting warns --- src/copyparty/mod.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'src/copyparty') diff --git a/src/copyparty/mod.rs b/src/copyparty/mod.rs index 00bfd53..164c9f8 100644 --- a/src/copyparty/mod.rs +++ b/src/copyparty/mod.rs @@ -1,3 +1,5 @@ +use std::time::Duration; + use reqwest::Client; use serde::Deserialize; use tracing::warn; @@ -5,6 +7,10 @@ use tracing::warn; use crate::error::Result; use crate::lang; +/// Timeout for copyparty search requests; autocomplete has a 3s Discord budget, +/// so this must be comfortably lower. +const SEARCH_HTTP_TIMEOUT: Duration = Duration::from_secs(4); + #[derive(Clone)] pub struct CopypartyClient { pub base_url: String, @@ -20,13 +26,12 @@ pub fn percent_decode(s: &str) -> String { let mut i = 0; while i < bytes.len() { let b = bytes[i]; - if b == b'%' && i + 2 < bytes.len() { - if let (Some(hi), Some(lo)) = (hex_nibble(bytes[i + 1]), hex_nibble(bytes[i + 2])) { + if b == b'%' && i + 2 < bytes.len() + && let (Some(hi), Some(lo)) = (hex_nibble(bytes[i + 1]), hex_nibble(bytes[i + 2])) { buf.push(hi << 4 | lo); i += 3; continue; } - } if b == b'+' { buf.push(b' '); } else { @@ -56,7 +61,7 @@ pub fn html_decode(s: &str) -> String { """ => buf.extend_from_slice(b"\""), "'" => buf.extend_from_slice(b"'"), " " => buf.extend_from_slice(b" "), - _ if entity.as_bytes().len() > 3 && entity.as_bytes()[1] == b'#' => { + _ if entity.len() > 3 && entity.as_bytes()[1] == b'#' => { let (num_str, radix) = if entity.as_bytes()[2] == b'x' || entity.as_bytes()[2] == b'X' { (&entity[3..entity.len() - 1], 16u32) @@ -130,7 +135,7 @@ impl SearchHit { /// Best-effort URL path for building a full download URL. /// Tries `url`, then `rp`. pub fn url_path(&self) -> Option<&str> { - self.url.as_deref().or_else(|| self.rp.as_deref()) + self.url.as_deref().or(self.rp.as_deref()) } } @@ -203,7 +208,8 @@ impl CopypartyClient { let mut req = self .client .post(format!("{}/?srch", self.base_url)) - .json(&payload); + .json(&payload) + .timeout(SEARCH_HTTP_TIMEOUT); if let Some(pw) = &self.password { req = req.header("PW", pw); } -- cgit v1.2.3