Skip to content

Commit 7a57d46

Browse files
committed
Handle chunked http response
1 parent 67305be commit 7a57d46

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ http-body-util = "0.1.1"
1414
httparse = "1.8.0"
1515
hyper = { version = "1.3.1", features = ["server", "http1"] }
1616
hyper-util = { version = "0.1.5", features = ["tokio"] }
17-
litep2p = "0.6.0"
17+
litep2p = { git = "https://github.com/Ma233/litep2p.git", branch = "keepalive" }
1818
# Do not upgrade multiaddr, see: https://github.com/paritytech/litep2p/pull/91
1919
multiaddr = "0.17.1"
2020
percent-encoding = "2.3.1"

src/lib.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub struct PProxyHandle {
6666
pub enum FullLength {
6767
NotParsed,
6868
NotSet,
69+
Chunked,
6970
Parsed(usize),
7071
}
7172

@@ -74,8 +75,8 @@ impl FullLength {
7475
matches!(self, FullLength::NotParsed)
7576
}
7677

77-
pub fn not_set(&self) -> bool {
78-
matches!(self, FullLength::NotSet)
78+
pub fn chunked(&self) -> bool {
79+
matches!(self, FullLength::Chunked)
7980
}
8081
}
8182

@@ -139,7 +140,7 @@ impl PProxy {
139140
return Err(Error::IncompleteHttpRequest);
140141
}
141142

142-
let mut stream = tcp_connect_with_timeout(&proxy_addr, 1).await?;
143+
let mut stream = tcp_connect_with_timeout(&proxy_addr, 2).await?;
143144
stream.write_all(request).await?;
144145

145146
let mut response = Vec::new();
@@ -149,7 +150,7 @@ impl PProxy {
149150
let mut buf = [0u8; 30000];
150151

151152
let Ok(Ok(n)) =
152-
timeout(std::time::Duration::from_secs(5), stream.read(&mut buf)).await
153+
timeout(std::time::Duration::from_secs(2), stream.read(&mut buf)).await
153154
else {
154155
break;
155156
};
@@ -175,12 +176,25 @@ impl PProxy {
175176
value.parse::<usize>().ok()
176177
});
177178

178-
match content_length {
179-
Some(content_length) => {
179+
let transfor_encoding = resp_checker.headers.iter().find_map(|h| {
180+
if h.name.to_lowercase() != "transfer-encoding" {
181+
return None;
182+
}
183+
let Ok(value) = std::str::from_utf8(h.value) else {
184+
return None;
185+
};
186+
Some(value)
187+
});
188+
189+
match (content_length, transfor_encoding) {
190+
(Some(content_length), _) => {
180191
let header_length = res.unwrap();
181192
full_length = FullLength::Parsed(header_length + content_length)
182193
}
183-
None => {
194+
(None, Some(value)) if value.to_lowercase().contains("chunked") => {
195+
full_length = FullLength::Chunked;
196+
}
197+
_ => {
184198
full_length = FullLength::NotSet;
185199
}
186200
}
@@ -192,6 +206,10 @@ impl PProxy {
192206
break;
193207
}
194208
}
209+
210+
if full_length.chunked() && response.ends_with(b"0\r\n\r\n") {
211+
break;
212+
}
195213
}
196214

197215
if response.is_empty() {

0 commit comments

Comments
 (0)