From d5dfcd465e308c72f93a4d7247874826be33e4bb Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Mon, 27 Jul 2026 03:10:14 +0800 Subject: [PATCH] feat(astrbot): record the attachment request and the server's reply MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Probing the server settled that the endpoints exist: unauthenticated, /api/v1/files/content?filename= and /api/v1/files/{id}/content both answer 401 JSON, while an unknown /api/v1 path answers 404 JSON. AstrBot does not serve an HTML shell for API paths at all. Our authenticated request nevertheless logs 200 with text/html and 201 bytes, and 201 bytes is exactly the length of AstrBot's plain-text 404 body. That combination cannot be explained from the code: the base URL has its trailing slashes stripped and is written back, redirects are not followed, and a non-2xx status returns before the log line. So record the URL actually requested, the status before the status checks, and — when the body is not an image — the first 160 bytes of it, which is the server explaining itself rather than anything the user wrote. Refs: N-6. Co-Authored-By: Claude Opus 5 --- src-tauri/src/networking/astrbot_client.rs | 52 ++++++++++++++-------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/src-tauri/src/networking/astrbot_client.rs b/src-tauri/src/networking/astrbot_client.rs index 1b97e44..6520fe4 100644 --- a/src-tauri/src/networking/astrbot_client.rs +++ b/src-tauri/src/networking/astrbot_client.rs @@ -480,26 +480,32 @@ impl AstrBotClient { { return Err(AstrBotClientError::InvalidConfig("invalid attachment id")); } - let response = self - .http - // AstrBot's dashboard fetches a chat image by name via - // `/api/v1/files/content?filename=`. The by-id form - // `/api/v1/files/{id}/content` also exists, but the id carried by - // `attachment_saved` does not address the file — asking for it - // returned the dashboard's own HTML shell with HTTP 200. - .get(self.url(&format!( - "/api/v1/files/content?filename={}", - urlencoding_username(attachment_id) - ))) - .send() - .await?; - if response.status().as_u16() == 401 || response.status().as_u16() == 403 { - return Err(AstrBotClientError::HttpStatus(response.status())); - } - if !response.status().is_success() { - return Err(AstrBotClientError::HttpStatus(response.status())); - } + // AstrBot's dashboard fetches a chat image by name via + // `/api/v1/files/content?filename=`. The by-id form + // `/api/v1/files/{id}/content` also exists, but the id carried by + // `attachment_saved` does not address the file. + let request_url = self.url(&format!( + "/api/v1/files/content?filename={}", + urlencoding_username(attachment_id) + )); + let response = self.http.get(&request_url).send().await?; let status = response.status(); + // Logged before the status checks: a rejected request is exactly the + // case where the path being asked for is the thing in question. The URL + // holds a file name, not a credential — the bearer token lives in a + // header. + tracing::debug!( + target: FRAME_TRACE_TARGET, + url = request_url.as_str(), + status = status.as_u16(), + "astrbot attachment request" + ); + if status.as_u16() == 401 || status.as_u16() == 403 { + return Err(AstrBotClientError::HttpStatus(status)); + } + if !status.is_success() { + return Err(AstrBotClientError::HttpStatus(status)); + } let header_mime = response .headers() .get(CONTENT_TYPE) @@ -528,6 +534,13 @@ impl AstrBotClient { } else { header_mime.clone() }; + // When the body is not an image it is the server explaining why, which + // is diagnostic text rather than anything the user wrote. + let server_note = if sniffed.is_none() { + String::from_utf8_lossy(&bytes[..bytes.len().min(160)]).into_owned() + } else { + String::new() + }; tracing::debug!( target: FRAME_TRACE_TARGET, status = status.as_u16(), @@ -535,6 +548,7 @@ impl AstrBotClient { sniffed = sniffed.unwrap_or(""), resolved_mime = mime_type.as_str(), bytes = bytes.len(), + server_note = server_note.as_str(), "astrbot attachment download" );