Skip to content

refactor(actix): simplify body_from_http #757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion sentry-actix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ rust-version = "1.81"

[dependencies]
actix-web = { version = "4", default-features = false }
bytes = "1.2"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

min version for .try_collect::<BytesMut>()

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"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

min version for Payload::from(Bytes)


[dev-dependencies]
actix-web = { version = "4" }
Expand Down
31 changes: 12 additions & 19 deletions sentry-actix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BytesMut, PayloadError> {
let mut stream = req.take_payload();
async fn body_from_http(req: &mut ServiceRequest) -> actix_web::Result<Bytes> {
let stream = req.extract::<actix_web::web::Payload>().await?;
let body = stream.try_collect::<BytesMut>().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(),
}
}

Expand Down