From 19403983b71e426278467e15805ddb0665d0b32f Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Mon, 20 Jan 2025 14:12:49 -0800 Subject: [PATCH] Better reading of response body --- src-tauri/Cargo.lock | 5 +++-- src-tauri/Cargo.toml | 1 + src-tauri/src/encoding.rs | 16 ++++++++++++++++ src-tauri/src/lib.rs | 14 ++++++++------ 4 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 src-tauri/src/encoding.rs diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 78c1250a..823d58d9 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1379,9 +1379,9 @@ checksum = "4ef6b89e5b37196644d8796de5268852ff179b44e96276cf4290264843743bb7" [[package]] name = "encoding_rs" -version = "0.8.34" +version = "0.8.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" +checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3" dependencies = [ "cfg-if", ] @@ -7492,6 +7492,7 @@ dependencies = [ "chrono", "cocoa 0.26.0", "datetime", + "encoding_rs", "eventsource-client", "hex_color", "http", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 636cdc89..89e0bb40 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -76,6 +76,7 @@ yaak-plugins = { workspace = true } yaak-sse = { workspace = true } yaak-sync = { path = "yaak-sync" } yaak-templates = { path = "yaak-templates" } +encoding_rs = "0.8.35" [workspace.dependencies] yaak-models = { path = "yaak-models" } diff --git a/src-tauri/src/encoding.rs b/src-tauri/src/encoding.rs new file mode 100644 index 00000000..e5aec36a --- /dev/null +++ b/src-tauri/src/encoding.rs @@ -0,0 +1,16 @@ +use encoding_rs::SHIFT_JIS; +use tokio::fs; +use yaak_models::models::HttpResponse; + +pub async fn read_response_body<'a>( + response: HttpResponse, +) -> Option { + let body_path = match response.body_path { + None => return None, + Some(p) => p, + }; + + let body = fs::read(body_path).await.unwrap(); + let (s, _, _) = SHIFT_JIS.decode(body.as_slice()); + Some(s.to_string()) +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 720289a1..443ce7fc 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -2,6 +2,7 @@ extern crate core; #[cfg(target_os = "macos")] extern crate objc; use crate::analytics::{AnalyticsAction, AnalyticsResource}; +use crate::encoding::read_response_body; use crate::grpc::metadata_to_map; use crate::http_request::send_http_request; use crate::notifications::YaakNotifier; @@ -77,6 +78,7 @@ use yaak_templates::format::format_json; use yaak_templates::{Parser, Tokens}; mod analytics; +mod encoding; mod grpc; mod http_request; mod notifications; @@ -796,7 +798,7 @@ async fn cmd_filter_response( } } - let body = read_to_string(response.body_path.unwrap()).await.unwrap(); + let body = read_response_body(response).await.unwrap(); // TODO: Have plugins register their own content type (regex?) plugin_manager @@ -808,11 +810,11 @@ async fn cmd_filter_response( #[tauri::command] async fn cmd_get_sse_events(file_path: &str) -> Result, String> { let body = fs::read(file_path).map_err(|e| e.to_string())?; - let mut p = EventParser::new(); - p.process_bytes(body.into()).map_err(|e| e.to_string())?; + let mut event_parser = EventParser::new(); + event_parser.process_bytes(body.into()).map_err(|e| e.to_string())?; let mut events = Vec::new(); - while let Some(e) = p.get_event() { + while let Some(e) = event_parser.get_event() { if let SSE::Event(e) = e { events.push(ServerSentEvent { event_type: e.event_type, @@ -2178,8 +2180,8 @@ async fn call_frontend( } window.unlisten(event_id); - let foo = rx.borrow(); - foo.clone() + let v = rx.borrow(); + v.clone() } async fn handle_plugin_event(