fix chunked data
This commit is contained in:
parent
e06a2a0207
commit
c8a96f2616
@ -425,6 +425,7 @@ impl FlowgateServer {
|
|||||||
if read >= content_length { break }
|
if read >= content_length { break }
|
||||||
}
|
}
|
||||||
} else if is_chunked {
|
} else if is_chunked {
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut length = Vec::new();
|
let mut length = Vec::new();
|
||||||
{
|
{
|
||||||
@ -440,16 +441,17 @@ impl FlowgateServer {
|
|||||||
(1, b'\n') => break,
|
(1, b'\n') => break,
|
||||||
_ => 0,
|
_ => 0,
|
||||||
};
|
};
|
||||||
conn.stream.write_all(&buf).ok()?;
|
|
||||||
}
|
}
|
||||||
|
conn.stream.write_all(&length).ok()?;
|
||||||
|
|
||||||
length.truncate(length.len() - 2);
|
length.truncate(length.len() - 2);
|
||||||
}
|
}
|
||||||
let length = usize::from_str_radix(String::from_utf8(length).ok()?.as_str(), 16).ok()?;
|
let length = String::from_utf8(length).ok()?;
|
||||||
let mut data = vec![0; length+2];
|
let length = usize::from_str_radix(length.as_str(), 16).ok()?;
|
||||||
|
let mut data = vec![0u8; length+2];
|
||||||
stream.read_exact(&mut data).ok()?;
|
stream.read_exact(&mut data).ok()?;
|
||||||
|
|
||||||
conn.stream.write_all(&data).ok()?;
|
conn.stream.write_all(&data).ok()?;
|
||||||
data.truncate(length);
|
|
||||||
if length == 0 {
|
if length == 0 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -486,16 +488,27 @@ impl FlowgateServer {
|
|||||||
let head_str = String::from_utf8(head.clone()).ok()?;
|
let head_str = String::from_utf8(head.clone()).ok()?;
|
||||||
let head_str = head_str.trim_matches(char::from(0));
|
let head_str = head_str.trim_matches(char::from(0));
|
||||||
|
|
||||||
let content_length = head_str.split("\r\n")
|
let headers = head_str.split("\r\n")
|
||||||
.skip(1)
|
.skip(1)
|
||||||
.filter(|l| l.contains(": "))
|
.filter(|l| l.contains(": "))
|
||||||
.map(|l| l.split_once(": ").unwrap())
|
.map(|l| l.split_once(": ").unwrap())
|
||||||
.filter(|(k, _)| k.to_lowercase() == "content-length")
|
.map(|(k,v)| (k.to_lowercase(),v.to_string()))
|
||||||
.next()
|
.collect::<Vec<(String,String)>>();
|
||||||
|
|
||||||
|
let content_length = headers.iter()
|
||||||
|
.find(|(k, _)| k == "content-length")
|
||||||
.map(|o| o.1.parse().ok())
|
.map(|o| o.1.parse().ok())
|
||||||
.flatten()
|
.flatten()
|
||||||
.unwrap_or(0usize);
|
.unwrap_or(0usize);
|
||||||
|
|
||||||
|
let is_chunked = headers.iter()
|
||||||
|
.find(|o| o.0.to_lowercase() == "transfer-encoding")
|
||||||
|
.map(|o| o.1.split(",").map(|x| x.trim_matches(' ').to_string()).collect::<Vec<String>>())
|
||||||
|
.map(|o| o.contains(&"chunked".to_string()))
|
||||||
|
.unwrap_or(false);
|
||||||
|
|
||||||
|
println!("{content_length} {is_chunked}");
|
||||||
|
|
||||||
if content_length > 0 {
|
if content_length > 0 {
|
||||||
let mut read = 0usize;
|
let mut read = 0usize;
|
||||||
let mut buf = vec![0; 4096];
|
let mut buf = vec![0; 4096];
|
||||||
@ -507,6 +520,37 @@ impl FlowgateServer {
|
|||||||
buf = vec![0; 4096];
|
buf = vec![0; 4096];
|
||||||
if read == content_length { break }
|
if read == content_length { break }
|
||||||
}
|
}
|
||||||
|
} else if is_chunked {
|
||||||
|
loop {
|
||||||
|
let mut length = Vec::new();
|
||||||
|
{
|
||||||
|
let mut buf = [0; 1];
|
||||||
|
let mut counter = 0;
|
||||||
|
|
||||||
|
while let Ok(1) = conn.stream.read(&mut buf) {
|
||||||
|
let byte = buf[0];
|
||||||
|
length.push(byte);
|
||||||
|
|
||||||
|
counter = match (counter, byte) {
|
||||||
|
(0, b'\r') => 1,
|
||||||
|
(1, b'\n') => break,
|
||||||
|
_ => 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
stream.write_all(&length).ok()?;
|
||||||
|
|
||||||
|
length.truncate(length.len() - 2);
|
||||||
|
}
|
||||||
|
let length = String::from_utf8(length).ok()?;
|
||||||
|
let length = usize::from_str_radix(length.as_str(), 16).ok()?;
|
||||||
|
let mut data = vec![0u8; length+2];
|
||||||
|
conn.stream.read_exact(&mut data).ok()?;
|
||||||
|
|
||||||
|
stream.write_all(&data).ok()?;
|
||||||
|
if length == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let mut buf = vec![0;1024];
|
let mut buf = vec![0;1024];
|
||||||
@ -514,7 +558,6 @@ impl FlowgateServer {
|
|||||||
if n == 0 { break }
|
if n == 0 { break }
|
||||||
buf.truncate(n);
|
buf.truncate(n);
|
||||||
stream.write_all(&buf).ok()?;
|
stream.write_all(&buf).ok()?;
|
||||||
if n < 1024 { break }
|
|
||||||
buf = vec![0;1024];
|
buf = vec![0;1024];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user