Skip to content

Commit e108792

Browse files
author
richardson
committed
Merge branch 'main' of github-iaiuse:iaiuse/Desky
2 parents 9d34f1c + 57c4b33 commit e108792

8 files changed

+666
-91
lines changed

src-tauri/src/commands.rs

+28
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,31 @@ pub async fn check_server_status(
144144
error_msg
145145
})
146146
}
147+
148+
// 添加新的命令处理函数
149+
#[tauri::command]
150+
pub async fn proxy_request_with_headers(
151+
_window: tauri::Window,
152+
target_url: String,
153+
method: String,
154+
headers: std::collections::HashMap<String, String>,
155+
body: Vec<u8>
156+
) -> Result<String, String> {
157+
let function_name = "proxy_request_with_headers";
158+
log_message(
159+
format!("[{}] Received request for URL: {}", function_name, target_url),
160+
"INFO".to_string(),
161+
MODEL_NAME.to_string(),
162+
);
163+
164+
HTTP_CLIENT.send_request_with_headers(&target_url, &method, headers, body).await
165+
.map_err(|e| {
166+
let error_msg = format!("[{}] Request failed: {}", function_name, e);
167+
log_message(
168+
error_msg.clone(),
169+
"ERROR".to_string(),
170+
MODEL_NAME.to_string(),
171+
);
172+
error_msg
173+
})
174+
}

src-tauri/src/http_client.rs

+79
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,83 @@ impl HttpClient {
123123
// 返回状态码是否为200
124124
Ok(response.status().as_u16() == 200)
125125
}
126+
127+
pub async fn proxy_request_with_headers(
128+
&self,
129+
target_url: &str,
130+
method: &str,
131+
headers: std::collections::HashMap<String, String>,
132+
body: Vec<u8>,
133+
) -> Result<Response> {
134+
log_message(
135+
format!("Proxying {} request to {} with headers", method, target_url),
136+
"DEBUG".to_string(),
137+
MODEL_NAME.to_string(),
138+
);
139+
140+
// 根据HTTP方法构建请求
141+
let mut request_builder = match method {
142+
"GET" => self.client.get(target_url),
143+
"POST" => self.client.post(target_url),
144+
_ => {
145+
log_message(
146+
format!("Unsupported HTTP method: {}", method),
147+
"ERROR".to_string(),
148+
MODEL_NAME.to_string(),
149+
);
150+
return Err(anyhow::anyhow!("Unsupported HTTP method"));
151+
}
152+
};
153+
154+
// 添加headers
155+
for (key, value) in headers {
156+
// 在日志记录前先克隆值
157+
let key_clone = key.clone();
158+
let value_clone = value.clone();
159+
160+
log_message(
161+
format!("Adding header: {} = {}", key_clone, value_clone),
162+
"DEBUG".to_string(),
163+
MODEL_NAME.to_string(),
164+
);
165+
166+
request_builder = request_builder.header(key, value);
167+
}
168+
169+
// 添加body并发送请求
170+
let response = request_builder.body(body).send().await?;
171+
172+
log_message(
173+
format!(
174+
"Received response: Status={}, Content-Length={:?}",
175+
response.status(),
176+
response.headers().get("content-length")
177+
),
178+
"INFO".to_string(),
179+
MODEL_NAME.to_string(),
180+
);
181+
182+
Ok(response)
183+
}
184+
185+
pub async fn send_request_with_headers(
186+
&self,
187+
target_url: &str,
188+
method: &str,
189+
headers: std::collections::HashMap<String, String>,
190+
body: Vec<u8>
191+
) -> Result<String, String> {
192+
let function_name = "send_request_with_headers";
193+
log_message(
194+
format!("[{}] Sending {} request to {}", function_name, method, target_url),
195+
"DEBUG".to_string(),
196+
MODEL_NAME.to_string(),
197+
);
198+
199+
let response = self.proxy_request_with_headers(target_url, method, headers, body).await
200+
.map_err(|e| format!("Request failed: {}", e))?;
201+
202+
response.text().await
203+
.map_err(|e| format!("Failed to parse response: {}", e))
204+
}
126205
}

src-tauri/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ mod http_client;
1212
use crate::logger::setup_logging;
1313
use crate::device_manager::DeviceManager;
1414
use std::sync::Arc;
15-
use tauri::Manager;
1615

1716
#[tokio::main]
1817
async fn main() -> Result<(), Box<dyn std::error::Error>> {
1918
// 创建线程安全的组件实例
2019
let device_manager = Arc::new(DeviceManager::new());
2120
// 创建应用状态
2221
let app_state = commands::AppState {
23-
device_manager: device_manager.clone(),
22+
device_manager: device_manager.clone(),
2423
};
2524

2625
tauri::Builder::default()
@@ -43,6 +42,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4342
commands::clear_logs,
4443
commands::get_serial_ports,
4544
commands::proxy_request,
45+
commands::proxy_request_with_headers,
4646
commands::check_server_status,
4747
])
4848
.run(tauri::generate_context!())

0 commit comments

Comments
 (0)