1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-28 20:41:48 +00:00

refactor(multipart): clean up InnerField::poll

This commit is contained in:
Rob Ede 2024-07-07 20:19:35 +01:00
parent e0e4d1e661
commit e9ccfbc866
No known key found for this signature in database
GPG key ID: 97C636207D3EF933

View file

@ -209,8 +209,14 @@ impl fmt::Debug for Field {
pub(crate) struct InnerField { pub(crate) struct InnerField {
/// Payload is initialized as Some and is `take`n when the field stream finishes. /// Payload is initialized as Some and is `take`n when the field stream finishes.
payload: Option<PayloadRef>, payload: Option<PayloadRef>,
/// Field boundary (without "--" prefix).
boundary: String, boundary: String,
/// True if request payload has been exhausted.
eof: bool, eof: bool,
/// Field data's stated size according to it's Content-Length header.
length: Option<u64>, length: Option<u64>,
} }
@ -286,6 +292,7 @@ impl InnerField {
let mut pos = 0; let mut pos = 0;
let len = payload.buf.len(); let len = payload.buf.len();
if len == 0 { if len == 0 {
return if payload.eof { return if payload.eof {
Poll::Ready(Some(Err(Error::Incomplete))) Poll::Ready(Some(Err(Error::Incomplete)))
@ -296,7 +303,7 @@ impl InnerField {
// check boundary // check boundary
if len > 4 && payload.buf[0] == b'\r' { if len > 4 && payload.buf[0] == b'\r' {
let b_len = if &payload.buf[..2] == b"\r\n" && &payload.buf[2..4] == b"--" { let b_len = if payload.buf.starts_with(b"\r\n") && &payload.buf[2..4] == b"--" {
Some(4) Some(4)
} else if &payload.buf[1..3] == b"--" { } else if &payload.buf[1..3] == b"--" {
Some(3) Some(3)
@ -357,28 +364,30 @@ impl InnerField {
return Poll::Ready(None); return Poll::Ready(None);
} }
let result = if let Some(mut payload) = self let Some(mut payload) = self
.payload .payload
.as_ref() .as_ref()
.expect("Field should not be polled after completion") .expect("Field should not be polled after completion")
.get_mut(safety) .get_mut(safety)
{ else {
if !self.eof { return Poll::Pending;
let res = if let Some(ref mut len) = self.length {
InnerField::read_len(&mut payload, len)
} else {
InnerField::read_stream(&mut payload, &self.boundary)
}; };
match res { if !self.eof {
Poll::Pending => return Poll::Pending, let res = if let Some(ref mut len) = self.length {
Poll::Ready(Some(Ok(bytes))) => return Poll::Ready(Some(Ok(bytes))), Self::read_len(&mut payload, len)
Poll::Ready(Some(Err(err))) => return Poll::Ready(Some(Err(err))), } else {
Poll::Ready(None) => self.eof = true, Self::read_stream(&mut payload, &self.boundary)
};
match ready!(res) {
Some(Ok(bytes)) => return Poll::Ready(Some(Ok(bytes))),
Some(Err(err)) => return Poll::Ready(Some(Err(err))),
None => self.eof = true,
} }
} }
match payload.readline() { let result = match payload.readline() {
Ok(None) => Poll::Pending, Ok(None) => Poll::Pending,
Ok(Some(line)) => { Ok(Some(line)) => {
if line.as_ref() != b"\r\n" { if line.as_ref() != b"\r\n" {
@ -387,11 +396,10 @@ impl InnerField {
Poll::Ready(None) Poll::Ready(None)
} }
Err(err) => Poll::Ready(Some(Err(err))), Err(err) => Poll::Ready(Some(Err(err))),
}
} else {
Poll::Pending
}; };
drop(payload);
if let Poll::Ready(None) = result { if let Poll::Ready(None) = result {
// drop payload buffer and make future un-poll-able // drop payload buffer and make future un-poll-able
let _ = self.payload.take(); let _ = self.payload.take();