mirror of
https://github.com/actix/actix-web.git
synced 2025-01-03 05:48:45 +00:00
Relax MessageBody limit to 2048kb (#2110)
* relax MessageBody limit to 2048kb * fix clippy * Update awc/src/response.rs Co-authored-by: Rob Ede <robjtede@icloud.com> * fix default body limit Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
parent
1be54efbeb
commit
9704beddf8
1 changed files with 38 additions and 45 deletions
|
@ -228,12 +228,13 @@ impl<S> fmt::Debug for ClientResponse<S> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DEFAULT_BODY_LIMIT: usize = 2 * 1024 * 1024;
|
||||||
|
|
||||||
/// Future that resolves to a complete HTTP message body.
|
/// Future that resolves to a complete HTTP message body.
|
||||||
pub struct MessageBody<S> {
|
pub struct MessageBody<S> {
|
||||||
length: Option<usize>,
|
length: Option<usize>,
|
||||||
err: Option<PayloadError>,
|
|
||||||
timeout: ResponseTimeout,
|
timeout: ResponseTimeout,
|
||||||
fut: Option<ReadBody<S>>,
|
body: Result<ReadBody<S>, Option<PayloadError>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> MessageBody<S>
|
impl<S> MessageBody<S>
|
||||||
|
@ -242,41 +243,38 @@ where
|
||||||
{
|
{
|
||||||
/// Create `MessageBody` for request.
|
/// Create `MessageBody` for request.
|
||||||
pub fn new(res: &mut ClientResponse<S>) -> MessageBody<S> {
|
pub fn new(res: &mut ClientResponse<S>) -> MessageBody<S> {
|
||||||
let mut len = None;
|
let length = match res.headers().get(&header::CONTENT_LENGTH) {
|
||||||
if let Some(l) = res.headers().get(&header::CONTENT_LENGTH) {
|
Some(value) => {
|
||||||
if let Ok(s) = l.to_str() {
|
let len = value.to_str().ok().and_then(|s| s.parse::<usize>().ok());
|
||||||
if let Ok(l) = s.parse::<usize>() {
|
|
||||||
len = Some(l)
|
match len {
|
||||||
} else {
|
None => return Self::err(PayloadError::UnknownLength),
|
||||||
return Self::err(PayloadError::UnknownLength);
|
len => len,
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return Self::err(PayloadError::UnknownLength);
|
|
||||||
}
|
}
|
||||||
}
|
None => None,
|
||||||
|
};
|
||||||
|
|
||||||
MessageBody {
|
MessageBody {
|
||||||
length: len,
|
length,
|
||||||
err: None,
|
|
||||||
timeout: std::mem::take(&mut res.timeout),
|
timeout: std::mem::take(&mut res.timeout),
|
||||||
fut: Some(ReadBody::new(res.take_payload(), 262_144)),
|
body: Ok(ReadBody::new(res.take_payload(), DEFAULT_BODY_LIMIT)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Change max size of payload. By default max size is 256kB
|
/// Change max size of payload. By default max size is 2048kB
|
||||||
pub fn limit(mut self, limit: usize) -> Self {
|
pub fn limit(mut self, limit: usize) -> Self {
|
||||||
if let Some(ref mut fut) = self.fut {
|
if let Ok(ref mut body) = self.body {
|
||||||
fut.limit = limit;
|
body.limit = limit;
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
fn err(e: PayloadError) -> Self {
|
fn err(e: PayloadError) -> Self {
|
||||||
MessageBody {
|
MessageBody {
|
||||||
fut: None,
|
|
||||||
err: Some(e),
|
|
||||||
length: None,
|
length: None,
|
||||||
timeout: ResponseTimeout::default(),
|
timeout: ResponseTimeout::default(),
|
||||||
|
body: Err(Some(e)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -290,19 +288,20 @@ where
|
||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
let this = self.get_mut();
|
let this = self.get_mut();
|
||||||
|
|
||||||
if let Some(err) = this.err.take() {
|
match this.body {
|
||||||
return Poll::Ready(Err(err));
|
Err(ref mut err) => Poll::Ready(Err(err.take().unwrap())),
|
||||||
}
|
Ok(ref mut body) => {
|
||||||
|
if let Some(len) = this.length.take() {
|
||||||
|
if len > body.limit {
|
||||||
|
return Poll::Ready(Err(PayloadError::Overflow));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(len) = this.length.take() {
|
this.timeout.poll_timeout(cx)?;
|
||||||
if len > this.fut.as_ref().unwrap().limit {
|
|
||||||
return Poll::Ready(Err(PayloadError::Overflow));
|
Pin::new(body).poll(cx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.timeout.poll_timeout(cx)?;
|
|
||||||
|
|
||||||
Pin::new(&mut this.fut.as_mut().unwrap()).poll(cx)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -415,7 +414,7 @@ impl<S> ReadBody<S> {
|
||||||
fn new(stream: Payload<S>, limit: usize) -> Self {
|
fn new(stream: Payload<S>, limit: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
stream,
|
stream,
|
||||||
buf: BytesMut::with_capacity(std::cmp::min(limit, 32768)),
|
buf: BytesMut::new(),
|
||||||
limit,
|
limit,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -430,20 +429,14 @@ where
|
||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
let this = self.get_mut();
|
let this = self.get_mut();
|
||||||
|
|
||||||
loop {
|
while let Some(chunk) = ready!(Pin::new(&mut this.stream).poll_next(cx)?) {
|
||||||
return match Pin::new(&mut this.stream).poll_next(cx)? {
|
if (this.buf.len() + chunk.len()) > this.limit {
|
||||||
Poll::Ready(Some(chunk)) => {
|
return Poll::Ready(Err(PayloadError::Overflow));
|
||||||
if (this.buf.len() + chunk.len()) > this.limit {
|
}
|
||||||
Poll::Ready(Err(PayloadError::Overflow))
|
this.buf.extend_from_slice(&chunk);
|
||||||
} else {
|
|
||||||
this.buf.extend_from_slice(&chunk);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Poll::Ready(None) => Poll::Ready(Ok(this.buf.split().freeze())),
|
|
||||||
Poll::Pending => Poll::Pending,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Poll::Ready(Ok(this.buf.split().freeze()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -462,7 +455,7 @@ mod tests {
|
||||||
_ => unreachable!("error"),
|
_ => unreachable!("error"),
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut req = TestResponse::with_header(header::CONTENT_LENGTH, "1000000").finish();
|
let mut req = TestResponse::with_header(header::CONTENT_LENGTH, "10000000").finish();
|
||||||
match req.body().await.err().unwrap() {
|
match req.body().await.err().unwrap() {
|
||||||
PayloadError::Overflow => {}
|
PayloadError::Overflow => {}
|
||||||
_ => unreachable!("error"),
|
_ => unreachable!("error"),
|
||||||
|
|
Loading…
Reference in a new issue