Repository: tokio-rs/axum
File: axum-core/src/extract/request_parts.rs (Rust)

Code before:
    B::Data: Send,
    B::Error: Into<BoxError>,
    S: Send + Sync,
{
    type Rejection = BytesRejection;

    async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
        // update docs in `axum-core/src/extract/default_body_limit.rs` and
        // `axum/src/docs/extract.md` if this changes
        const DEFAULT_LIMIT: usize = 2_097_152; // 2 mb

        let limit_kind = req.extensions().get::<DefaultBodyLimitKind>().copied();
        let bytes = match limit_kind {
            Some(DefaultBodyLimitKind::Disable) => crate::body::to_bytes(req.into_body())
                .await
                .map_err(FailedToBufferBody::from_err)?,
            Some(DefaultBodyLimitKind::Limit(limit)) => {
                let content_length: usize = req
                    .headers()
                    .get(http::header::CONTENT_LENGTH)
                    .and_then(|val| val.to_str().ok()?.parse().ok())
                    .unwrap();

                if content_length > limit {
                    todo!()
                }

                let body = http_body::Limited::new(req.into_body(), limit);
                crate::body::to_bytes(body)
                    .await
                    .map_err(FailedToBufferBody::from_err)?
            }
            None => {
                let body = http_body::Limited::new(req.into_body(), DEFAULT_LIMIT);
                crate::body::to_bytes(body)
                    .await
                    .map_err(FailedToBufferBody::from_err)?
            }
        };

        Ok(bytes)
    }
}

#[async_trait]
impl<S, B> FromRequest<S, B> for String
where
    B: http_body::Body + Send + 'static,
    B::Data: Send,
    B::Error: Into<BoxError>,
    S: Send + Sync,

Review comment (suggestion):
Sorry but I don't think we should add this in this PR. I'd rather have that split up. I think if this PR just removes `ContentLengthLimit` then thats best.

Code after:
    B::Data: Send,
    B::Error: Into<BoxError>,
    S: Send + Sync,
{
    type Rejection = BytesRejection;

    async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
        // update docs in `axum-core/src/extract/default_body_limit.rs` and
        // `axum/src/docs/extract.md` if this changes
        const DEFAULT_LIMIT: usize = 2_097_152; // 2 mb

        let limit_kind = req.extensions().get::<DefaultBodyLimitKind>().copied();
        let bytes = match limit_kind {
            Some(DefaultBodyLimitKind::Disable) => crate::body::to_bytes(req.into_body())
                .await
                .map_err(FailedToBufferBody::from_err)?,
            Some(DefaultBodyLimitKind::Limit(limit)) => {
                let body = http_body::Limited::new(req.into_body(), limit);
                crate::body::to_bytes(body)
                    .await
                    .map_err(FailedToBufferBody::from_err)?
            }
            None => {
                let body = http_body::Limited::new(req.into_body(), DEFAULT_LIMIT);
                crate::body::to_bytes(body)
                    .await
                    .map_err(FailedToBufferBody::from_err)?
            }
        };

        Ok(bytes)
    }
}

#[async_trait]
impl<S, B> FromRequest<S, B> for String
where
    B: http_body::Body + Send + 'static,
    B::Data: Send,
    B::Error: Into<BoxError>,
    S: Send + Sync,
{
    type Rejection = StringRejection;

    async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
        let bytes = Bytes::from_request(req, state)
            .await
            .map_err(|err| match err {
                BytesRejection::FailedToBufferBody(inner) => {
                    StringRejection::FailedToBufferBody(inner)
                }