p0: eliminate body bytes double-copy — keep body as Bytes, avoid unnecessary to_vec()
swadhinbiswas committed 3 months ago
bdd9f32
1 parent 105aa17
commit bdd9f32
1 file changed
+21 -22
Showing 1 changed file+21-22
▼crates/justapi-py/src/native/handlers.rs
+21-22
| @@ -729,7 +729,7 @@ where | ||
729 | 729 | let (body_bytes, multipart_form_res) = if is_multipart { |
730 | 730 | let ct = content_type.unwrap(); |
731 | 731 | match justapi_core::multipart::parse_multipart(req_body, &ct).await { |
732 | - Ok(form) => (vec![], Some(Ok::<_, anyhow::Error>(form))), | |
732 | + Ok(form) => (Bytes::new(), Some(Ok::<_, anyhow::Error>(form))), | |
733 | 733 | Err(e) => { |
734 | 734 | let msg = e.to_string(); |
735 | 735 | if msg.contains("exceeds maximum size") { |
| @@ -745,30 +745,27 @@ where | ||
745 | 745 | } |
746 | 746 | } |
747 | 747 | } else { |
748 | - let b = match http_body_util::Limited::new(req_body, max_body_size).collect().await | |
749 | - { | |
750 | - Ok(c) => c.to_bytes(), | |
751 | - Err(e) if e.to_string().contains("length limit") => { | |
752 | - return Ok(json_response( | |
753 | - StatusCode::PAYLOAD_TOO_LARGE, | |
754 | - r#"{"detail":"payload too large"}"#, | |
755 | - )); | |
756 | - } | |
757 | - Err(e) => return Err(anyhow::anyhow!("Body error: {}", e)), | |
758 | - }; | |
759 | - let mut body = b.to_vec(); | |
748 | + let body = | |
749 | + match http_body_util::Limited::new(req_body, max_body_size).collect().await { | |
750 | + Ok(c) => c.to_bytes(), | |
751 | + Err(e) if e.to_string().contains("length limit") => { | |
752 | + return Ok(json_response( | |
753 | + StatusCode::PAYLOAD_TOO_LARGE, | |
754 | + r#"{"detail":"payload too large"}"#, | |
755 | + )); | |
756 | + } | |
757 | + Err(e) => return Err(anyhow::anyhow!("Body error: {}", e)), | |
758 | + }; | |
760 | 759 | // Normalize an XML request body into JSON so downstream |
761 | 760 | // JSON-speaking validation/handlers receive a uniform shape. |
762 | 761 | let ct_lower = content_type.as_deref().unwrap_or("").to_ascii_lowercase(); |
763 | - if ct_lower.contains("application/xml") || ct_lower.contains("text/xml") { | |
762 | + let body = if ct_lower.contains("application/xml") || ct_lower.contains("text/xml") | |
763 | + { | |
764 | 764 | match xml::xml_to_json(&body) { |
765 | - Ok(value) => { | |
766 | - if let Ok(json_bytes) = serde_json::to_vec(&value) { | |
767 | - body = json_bytes; | |
768 | - } | |
769 | - // On JSON serialization failure, fall through with | |
770 | - // the original bytes. | |
771 | - } | |
765 | + Ok(value) => match serde_json::to_vec(&value) { | |
766 | + Ok(json_bytes) => Bytes::from(json_bytes), | |
767 | + Err(_) => body, | |
768 | + }, | |
772 | 769 | Err(e) => { |
773 | 770 | return Ok(json_response( |
774 | 771 | StatusCode::BAD_REQUEST, |
| @@ -776,7 +773,9 @@ where | ||
776 | 773 | )); |
777 | 774 | } |
778 | 775 | } |
779 | - } | |
776 | + } else { | |
777 | + body | |
778 | + }; | |
780 | 779 | (body, None) |
781 | 780 | }; |
782 | 781 | |
783 | 782 | |