wx_rust_common/util/http/
http_response_proxy.rs1#[derive(Debug, Clone)]
9pub struct HttpResponseProxy {
10 pub status_code: u16,
12 pub headers: Vec<(String, String)>,
14 pub body: Vec<u8>,
16}
17
18impl HttpResponseProxy {
19 pub fn new(status_code: u16, headers: Vec<(String, String)>, body: Vec<u8>) -> Self {
21 Self {
22 status_code,
23 headers,
24 body,
25 }
26 }
27
28 pub fn extract_file_name_from_content_string(
43 content: &str,
44 ) -> Result<String, crate::error::WxErrorException> {
45 if content.is_empty() {
46 return Err(crate::error::WxErrorException::from_code(
47 -1,
48 "无法获取到文件名,content为空",
49 ));
50 }
51
52 if let Some(start) = content.find("filename*=utf-8''") {
54 let after = &content[start + "filename*=utf-8''".len()..];
55 let end = after.find([';', ' ', ',']).unwrap_or(after.len());
56 let encoded = &after[..end];
57 return percent_encoding::percent_decode_str(encoded)
59 .decode_utf8()
60 .map(|s| s.to_string())
61 .map_err(|e| {
62 crate::error::WxErrorException::from_code(-1, format!("文件名解码失败: {e}"))
63 });
64 }
65
66 let marker = "filename=\"";
68 if let Some(start) = content.find(marker) {
69 let after = &content[start + marker.len()..];
70 if let Some(end) = after.find('"') {
71 let raw = &after[..end];
72 let bytes: Vec<u8> = raw.chars().map(|c| c as u8).collect();
74 return Ok(String::from_utf8_lossy(&bytes).to_string());
75 }
76 }
77
78 Err(crate::error::WxErrorException::from_code(
79 -1,
80 "无法获取到文件名,header信息有问题",
81 ))
82 }
83}