From 130be08584bef3a3e54143783aba20af83e35e1a Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 23 Mar 2025 00:14:43 +0000 Subject: [PATCH] refactor(actix): simplify body_from_http --- Cargo.lock | 1 + sentry-actix/Cargo.toml | 3 ++- sentry-actix/src/lib.rs | 31 ++++++++++++------------------- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9457e131e..e62fa91bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4029,6 +4029,7 @@ version = "0.36.0" dependencies = [ "actix-http", "actix-web", + "bytes 1.10.1", "futures", "futures-util", "sentry", diff --git a/sentry-actix/Cargo.toml b/sentry-actix/Cargo.toml index 56aa7b90e..081db9f70 100644 --- a/sentry-actix/Cargo.toml +++ b/sentry-actix/Cargo.toml @@ -14,11 +14,12 @@ rust-version = "1.81" [dependencies] actix-web = { version = "4", default-features = false } +bytes = "1.2" futures-util = { version = "0.3.5", default-features = false } sentry-core = { version = "0.36.0", path = "../sentry-core", default-features = false, features = [ "client", ] } -actix-http = "3.9.0" +actix-http = "3.10" [dev-dependencies] actix-web = { version = "4" } diff --git a/sentry-actix/src/lib.rs b/sentry-actix/src/lib.rs index be0f21c3e..eea9a03a9 100644 --- a/sentry-actix/src/lib.rs +++ b/sentry-actix/src/lib.rs @@ -78,12 +78,11 @@ use std::sync::Arc; use actix_http::header::{self, HeaderMap}; use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform}; -use actix_web::error::PayloadError; use actix_web::http::StatusCode; -use actix_web::web::BytesMut; -use actix_web::{Error, HttpMessage}; +use actix_web::Error; +use bytes::{Bytes, BytesMut}; use futures_util::future::{ok, Future, Ready}; -use futures_util::FutureExt; +use futures_util::{FutureExt as _, TryStreamExt as _}; use sentry_core::protocol::{self, ClientSdkPackage, Event, Request}; use sentry_core::MaxRequestBodySize; @@ -239,26 +238,20 @@ fn should_capture_request_body( } /// Extract a body from the HTTP request -async fn body_from_http(req: &mut ServiceRequest) -> Result { - let mut stream = req.take_payload(); +async fn body_from_http(req: &mut ServiceRequest) -> actix_web::Result { + let stream = req.extract::().await?; + let body = stream.try_collect::().await?.freeze(); - let mut body = BytesMut::new(); - while let Some(chunk) = futures_util::StreamExt::next(&mut stream).await { - let chunk = chunk?; - body.extend_from_slice(&chunk); - } - let (_, mut orig_payload) = actix_http::h1::Payload::create(true); - orig_payload.unread_data(body.clone().freeze()); - req.set_payload(actix_web::dev::Payload::from(orig_payload)); + // put copy of payload back into request for downstream to read + req.set_payload(actix_web::dev::Payload::from(body.clone())); - Ok::<_, PayloadError>(body) + Ok(body) } async fn capture_request_body(req: &mut ServiceRequest) -> String { - if let Ok(request_body) = body_from_http(req).await { - String::from_utf8_lossy(&request_body).into_owned() - } else { - String::new() + match body_from_http(req).await { + Ok(request_body) => String::from_utf8_lossy(&request_body).into_owned(), + Err(_) => String::new(), } }