fix: 修复 YAML 块语法 body 匹配失败问题
- normalize_yaml_body 函数在解析 JSON 前添加 trim() 处理,解决 YAML `|` 和 `>` 语法产生的前导空格问题 - 修复 multiple_login.yaml 中 response body 格式错误(YAML 对象改为 JSON 字符串)
This commit is contained in:
147
src/handler.rs
147
src/handler.rs
@@ -8,6 +8,7 @@ use std::collections::HashMap;
|
||||
use std::sync::{Arc, RwLock}; // 必须引入 RwLock
|
||||
use tokio_util::io::ReaderStream;
|
||||
|
||||
use crate::models::Payload;
|
||||
use crate::router::MockRouter;
|
||||
|
||||
/// 共享的应用状态,router 现在由 RwLock 保护以支持热重载
|
||||
@@ -15,6 +16,131 @@ pub struct AppState {
|
||||
pub router: RwLock<MockRouter>,
|
||||
}
|
||||
|
||||
/// 提取请求的 Content-Type(去掉参数部分,如 boundary)
|
||||
fn extract_content_type(headers: &HeaderMap) -> Option<String> {
|
||||
headers
|
||||
.get(axum::http::header::CONTENT_TYPE)
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.map(|s| s.split(';').next().unwrap_or(s).trim().to_lowercase())
|
||||
}
|
||||
|
||||
/// 根据 Content-Type 解析 Body(始终以请求的 Content-Type 为准)
|
||||
fn parse_body(content_type: Option<&str>, bytes: &[u8]) -> Payload {
|
||||
if bytes.is_empty() {
|
||||
return Payload::None;
|
||||
}
|
||||
|
||||
match content_type {
|
||||
Some(ct) if ct.contains("application/json") => {
|
||||
serde_json::from_slice(bytes)
|
||||
.map(Payload::Json)
|
||||
.unwrap_or_else(|_| {
|
||||
// JSON 解析失败,降级为文本
|
||||
Payload::Text(String::from_utf8_lossy(bytes).to_string())
|
||||
})
|
||||
}
|
||||
Some(ct) if ct.contains("xml") => {
|
||||
Payload::Xml(String::from_utf8_lossy(bytes).to_string())
|
||||
}
|
||||
Some(ct) if ct.contains("form-urlencoded") => {
|
||||
Payload::Form(parse_urlencoded(bytes))
|
||||
}
|
||||
Some(ct) if ct.contains("multipart/form-data") => {
|
||||
Payload::Multipart(extract_multipart_data(bytes))
|
||||
}
|
||||
_ => {
|
||||
Payload::Text(String::from_utf8_lossy(bytes).to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 解析 urlencoded 格式
|
||||
fn parse_urlencoded(bytes: &[u8]) -> HashMap<String, String> {
|
||||
let body = String::from_utf8_lossy(bytes);
|
||||
let mut map = HashMap::new();
|
||||
for pair in body.split('&') {
|
||||
if let Some((key, value)) = pair.split_once('=') {
|
||||
let decoded_key = urlencoding_decode(key);
|
||||
let decoded_value = urlencoding_decode(value);
|
||||
map.insert(decoded_key, decoded_value);
|
||||
}
|
||||
}
|
||||
map
|
||||
}
|
||||
|
||||
/// URL 解码(简单实现)
|
||||
fn urlencoding_decode(s: &str) -> String {
|
||||
let mut result = String::new();
|
||||
let mut chars = s.chars().peekable();
|
||||
while let Some(c) = chars.next() {
|
||||
if c == '+' {
|
||||
result.push(' ');
|
||||
} else if c == '%' {
|
||||
let hex: String = chars.by_ref().take(2).collect();
|
||||
if let Ok(byte) = u8::from_str_radix(&hex, 16) {
|
||||
result.push(byte as char);
|
||||
} else {
|
||||
result.push('%');
|
||||
result.push_str(&hex);
|
||||
}
|
||||
} else {
|
||||
result.push(c);
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// 从 multipart body 中提取键值对
|
||||
fn extract_multipart_data(bytes: &[u8]) -> HashMap<String, String> {
|
||||
let body = String::from_utf8_lossy(bytes);
|
||||
let mut map = HashMap::new();
|
||||
|
||||
// 分割 boundary
|
||||
let lines: Vec<&str> = body.lines().collect();
|
||||
let mut current_name: Option<String> = None;
|
||||
let mut current_value = String::new();
|
||||
let mut in_value = false;
|
||||
|
||||
for line in &lines {
|
||||
// 检测 Content-Disposition 行,提取 name
|
||||
if line.contains("Content-Disposition") && line.contains("name=") {
|
||||
// 保存上一个字段的值
|
||||
if let Some(name) = current_name.take() {
|
||||
map.insert(name, current_value.trim().to_string());
|
||||
current_value.clear();
|
||||
}
|
||||
|
||||
// 提取 name 属性
|
||||
if let Some(start) = line.find("name=\"") {
|
||||
let start = start + 6;
|
||||
if let Some(end) = line[start..].find('"') {
|
||||
current_name = Some(line[start..start + end].to_string());
|
||||
in_value = false;
|
||||
}
|
||||
}
|
||||
} else if line.starts_with("Content-Type") {
|
||||
// 跳过 Content-Type 行
|
||||
continue;
|
||||
} else if line.is_empty() {
|
||||
// 空行后面是值
|
||||
in_value = true;
|
||||
} else if in_value {
|
||||
// 收集值内容
|
||||
if !current_value.is_empty() {
|
||||
current_value.push('\n');
|
||||
}
|
||||
current_value.push_str(line);
|
||||
}
|
||||
}
|
||||
|
||||
// 保存最后一个字段
|
||||
if let Some(name) = current_name {
|
||||
map.insert(name, current_value.trim().to_string());
|
||||
}
|
||||
|
||||
map
|
||||
}
|
||||
|
||||
/// 全局统一请求处理函数
|
||||
pub async fn mock_handler(
|
||||
State(state): State<Arc<AppState>>, // State 必须是第一个或靠前的参数
|
||||
@@ -27,7 +153,10 @@ pub async fn mock_handler(
|
||||
let path = req.uri().path().to_string();
|
||||
let method_str = method.as_str().to_string();
|
||||
|
||||
// 2. 读取请求 body(用于 body 字段匹配)
|
||||
// 2. 提取请求的 Content-Type
|
||||
let req_content_type = extract_content_type(&headers);
|
||||
|
||||
// 3. 读取请求 body
|
||||
let body_bytes = match axum::body::to_bytes(req.into_body(), 10 * 1024 * 1024).await {
|
||||
Ok(bytes) => bytes,
|
||||
Err(_) => {
|
||||
@@ -37,9 +166,11 @@ pub async fn mock_handler(
|
||||
.unwrap();
|
||||
}
|
||||
};
|
||||
let incoming_json: Option<serde_json::Value> = serde_json::from_slice(&body_bytes).ok();
|
||||
|
||||
// 3. 将 Axum HeaderMap 转换为简单的 HashMap
|
||||
// 4. 根据【请求的 Content-Type】解析 body
|
||||
let parsed_body = parse_body(req_content_type.as_deref(), &body_bytes);
|
||||
|
||||
// 5. 将 Axum HeaderMap 转换为简单的 HashMap
|
||||
let mut req_headers = HashMap::new();
|
||||
for (name, value) in headers.iter() {
|
||||
if let Ok(v) = value.to_str() {
|
||||
@@ -47,22 +178,22 @@ pub async fn mock_handler(
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 执行匹配逻辑:先获取读锁 (Read Lock)
|
||||
// 6. 执行匹配逻辑:先获取读锁 (Read Lock)
|
||||
let maybe_rule = {
|
||||
let router = state.router.read().expect("Failed to acquire read lock");
|
||||
router.match_rule(&method_str, &path, ¶ms, &req_headers, &incoming_json).cloned()
|
||||
router.match_rule(&method_str, &path, ¶ms, &req_headers, &parsed_body).cloned()
|
||||
// 此处使用 .cloned() 以便尽早释放读锁,避免阻塞热重载写锁
|
||||
};
|
||||
|
||||
if let Some(rule) = maybe_rule {
|
||||
// 5. 处理模拟延迟
|
||||
// 7. 处理模拟延迟
|
||||
if let Some(ref settings) = rule.settings {
|
||||
if let Some(delay) = settings.delay_ms {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(delay)).await;
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 构建响应
|
||||
// 8. 构建响应
|
||||
let status = StatusCode::from_u16(rule.response.status).unwrap_or(StatusCode::OK);
|
||||
let mut response_builder = Response::builder().status(status);
|
||||
|
||||
@@ -72,7 +203,7 @@ pub async fn mock_handler(
|
||||
}
|
||||
}
|
||||
|
||||
// 7. Smart Body 逻辑
|
||||
// 9. Smart Body 逻辑
|
||||
if let Some(file_path) = rule.response.get_file_path() {
|
||||
match tokio::fs::File::open(file_path).await {
|
||||
Ok(file) => {
|
||||
|
||||
Reference in New Issue
Block a user