Skip to content

Commit

Permalink
fix read body
Browse files Browse the repository at this point in the history
  • Loading branch information
cn-kali-team committed Oct 21, 2024
1 parent cc058a1 commit 202aff6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "slinger" #改这个
version = "0.1.10"
version = "0.1.11"
edition = "2021"
description = "An HTTP Client for Rust designed for hackers."
homepage = "https://github.com/emo-crab/slinger"
Expand Down
19 changes: 12 additions & 7 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,14 @@ impl<T: Read> ResponseBuilder<T> {
if te == "chunked" {
body = self.read_chunked_body()?;
}
} else if let Some(mut cl) = content_length {
// 如果有最大读取限制,取一个最小的长度
if let Some(max_read) = self.config.max_read {
cl = std::cmp::min(cl, max_read);
}
} else {
let limits = content_length.map(|x| {
if let Some(max) = self.config.max_read {
std::cmp::min(x, max)
} else {
x
}
});
let mut buffer = vec![0; 12]; // 定义一个缓冲区
let mut total_bytes_read = 0;
let mut start = Instant::now();
Expand Down Expand Up @@ -545,8 +548,10 @@ impl<T: Read> ResponseBuilder<T> {
Err(_err) => break,
}
// 检查是否读取到了全部数据,如果是,则退出循环
if total_bytes_read >= cl as usize {
break;
if let Some(limit) = limits {
if total_bytes_read >= limit as usize {
break;
}
}
}
}
Expand Down

0 comments on commit 202aff6

Please sign in to comment.