@@ -10,6 +10,7 @@ use pyo3::types::{PyBytes, PyDict, PyList, PyString, PyTuple};
1010use pyo3:: IntoPyObjectExt ;
1111use serde_json:: Value as JsonValue ;
1212
13+ use crate :: buffer_pool:: PooledBuffer ;
1314use crate :: config;
1415use crate :: form:: { self , ParsedFile } ;
1516use crate :: params:: { build_request_context, header_get_lax, parse_query, value_for_path_param} ;
@@ -699,22 +700,26 @@ pub async fn run_rsgi(
699700 routes_arc[ route_idx] . extra . path_template . clone ( ) ,
700701 )
701702 } ) ;
702- let mut body_bytes: Vec < u8 > = if should_read_body {
703+ let mut body_bytes: PooledBuffer = if should_read_body {
703704 let read_fut = Python :: with_gil ( |py| {
704705 let p = protocol. bind ( py) ;
705706 let aw: Bound < PyAny > = p. call0 ( ) ?;
706707 pyo3_async_runtimes:: tokio:: into_future ( aw)
707708 } ) ?;
708709 let body_obj: PyObject = read_fut. await ?;
709- let body = Python :: with_gil ( |py| -> PyResult < Vec < u8 > > {
710+ let mut body = PooledBuffer :: new ( ) ;
711+ Python :: with_gil ( |py| -> PyResult < ( ) > {
710712 let b = body_obj. bind ( py) ;
711- if let Ok ( x) = b. extract :: < Vec < u8 > > ( ) {
712- return Ok ( x) ;
713- }
714- if let Ok ( s) = b. str ( ) {
715- return Ok ( s. to_string ( ) . into_bytes ( ) ) ;
713+ if let Ok ( py_bytes) = b. downcast :: < pyo3:: types:: PyBytes > ( ) {
714+ body. extend_from_slice ( py_bytes. as_bytes ( ) ) ;
715+ } else if let Ok ( py_str) = b. downcast :: < pyo3:: types:: PyString > ( ) {
716+ if let Ok ( s) = py_str. to_str ( ) {
717+ body. extend_from_slice ( s. as_bytes ( ) ) ;
718+ }
719+ } else if let Ok ( bytes_vec) = b. extract :: < Vec < u8 > > ( ) {
720+ body. extend_from_slice ( & bytes_vec) ;
716721 }
717- Ok ( Vec :: new ( ) )
722+ Ok ( ( ) )
718723 } ) ?;
719724 let max = form:: max_body_bytes ( ) ;
720725 if ( body. len ( ) as u64 ) > max {
@@ -728,7 +733,7 @@ pub async fn run_rsgi(
728733 }
729734 body
730735 } else {
731- Vec :: new ( )
736+ PooledBuffer :: new ( )
732737 } ;
733738 let ( auth, cookie_raw) : ( Option < String > , Option < String > ) = if require_jwt {
734739 Python :: with_gil ( |py| -> PyResult < ( Option < String > , Option < String > ) > {
@@ -886,7 +891,7 @@ pub async fn run_rsgi(
886891 . await
887892 }
888893 } ;
889- let multipart_body = std :: mem :: take ( & mut body_bytes ) ;
894+ let multipart_body = body_bytes . take ( ) ;
890895 let parsed = match form:: parse_multipart ( multipart_body, & boundary) . await {
891896 Ok ( p) => p,
892897 Err ( e) => {
0 commit comments