/// Parse the request's body into raw bytes, applying the configured size
/// limits and decoding any content encoding.
Parse the request's body into raw bytes, applying the configured size
limits and decoding any content encoding.
Parse the request ' s body into raw bytes applying the configured size limits and decoding any content encoding .
async fn read_body(&self, req: hyper::Request<Body>) -> Result<Bytes, Error> {
        let encoding = req
            .headers()
            .get(&CONTENT_ENCODING)
            .map(|v| v.to_str().map_err(Error::NonUtf8ContentHeader))
            .transpose()?;
        let ungzip = match encoding {
            None => false,
            Some("gzip") => true,
            Some(v) => return Err(Error::InvalidContentEncoding(v.to_string())),
        };
        let mut payload = req.into_body();
        let mut body = BytesMut::new();
        while let Some(chunk) = payload.next().await {
            let chunk = chunk.map_err(Error::ClientHangup)?;
            // limit max size of in-memory payload
            if (body.len() + chunk.len()) > self.max_request_bytes {
                return Err(Error::RequestSizeExceeded(self.max_request_bytes));
            }
            body.extend_from_slice(&chunk);
        }
        let body = body.freeze();
        // If the body is not compressed, return early.
        if !ungzip {
            return Ok(body);
        }
        // Unzip the gzip-encoded content
        use std::io::Read;
        let decoder = flate2::read::GzDecoder::new(&body[..]);
        // Read at most max_request_bytes bytes to prevent a decompression bomb
        // based DoS.
        //
        // In order to detect if the entire stream ahs been read, or truncated,
        // read an extra byte beyond the limit and check the resulting data
        // length - see the max_request_size_truncation test.
        let mut decoder = decoder.take(self.max_request_bytes as u64 + 1);
        let mut decoded_data = Vec::new();
        decoder
            .read_to_end(&mut decoded_data)
            .map_err(Error::InvalidGzip)?;
        // If the length is max_size+1, the body is at least max_size+1 bytes in
        // length, and possibly longer, but truncated.
        if decoded_data.len() > self.max_request_bytes {
            return Err(Error::RequestSizeExceeded(self.max_request_bytes));
        }
        Ok(decoded_data.into())
    }
async fn read_body ( & self , req : hyper :: Request < Body > ) -> Result < Bytes , Error > { let encoding = req . headers ( ) . get ( & CONTENT_ENCODING ) . map ( | v | v . to_str ( ) . map_err ( Error :: NonUtf8ContentHeader ) ) . transpose ( ) ? ; let ungzip = match encoding { None => false , Some ( " " ) => true , Some ( v ) => return Err ( Error :: InvalidContentEncoding ( v . to_string ( ) ) ) , } ; let mut payload = req . into_body ( ) ; let mut body = BytesMut :: new ( ) ; while let Some ( chunk ) = payload . next ( ) . await { let chunk = chunk . map_err ( Error :: ClientHangup ) ? ; // limit max size of in-memory payload if ( body . len ( ) + chunk . len ( ) ) > self . max_request_bytes { return Err ( Error :: RequestSizeExceeded ( self . max_request_bytes ) ) ; } body . extend_from_slice ( & chunk ) ; } let body = body . freeze ( ) ; // If the body is not compressed, return early. if ! ungzip { return Ok ( body ) ; } // Unzip the gzip-encoded content use std :: io :: Read ; let decoder = flate2 :: read :: GzDecoder :: new ( & body [ .. ] ) ; // Read at most max_request_bytes bytes to prevent a decompression bomb // based DoS. // // In order to detect if the entire stream ahs been read, or truncated, // read an extra byte beyond the limit and check the resulting data // length - see the max_request_size_truncation test. let mut decoder = decoder . take ( self . max_request_bytes as u64 + 1 ) ; let mut decoded_data = Vec :: new ( ) ; decoder . read_to_end ( & mut decoded_data ) . map_err ( Error :: InvalidGzip ) ? ; // If the length is max_size+1, the body is at least max_size+1 bytes in // length, and possibly longer, but truncated. if decoded_data . len ( ) > self . max_request_bytes { return Err ( Error :: RequestSizeExceeded ( self . max_request_bytes ) ) ; } Ok ( decoded_data . into ( ) ) }
Parse the request's body into raw bytes, applying the configured size
limits and decoding any content encoding.
Parse the request ' s body into raw bytes applying the configured size limits and decoding any content encoding .
&self
req
hyper::Request<Body>