Include request body in response
This commit is contained in:
31
src/main.rs
31
src/main.rs
@ -1,5 +1,10 @@
|
||||
use actix_web::{App, HttpRequest, HttpResponse, HttpServer, Responder, web};
|
||||
use actix_web::{App, Error, error, HttpRequest, HttpResponse, HttpServer, web};
|
||||
use actix_web::web::Payload;
|
||||
use clap::Parser;
|
||||
use futures::StreamExt;
|
||||
|
||||
const MAX_BODY_SIZE: usize = 262_144; // max payload size is 256k
|
||||
|
||||
|
||||
/// Simple HTTP responder
|
||||
#[derive(Parser, Debug)]
|
||||
@ -10,24 +15,36 @@ struct Args {
|
||||
listen_address: String,
|
||||
}
|
||||
|
||||
async fn handler(req: HttpRequest) -> impl Responder {
|
||||
async fn handler(req: HttpRequest, mut payload: Payload) -> Result<HttpResponse, Error> {
|
||||
let mut headers = req.headers().iter()
|
||||
.map(|h| format!("{}: {}", h.0.as_str(), h.1.to_str().unwrap_or_default()))
|
||||
.collect::<Vec<_>>();
|
||||
headers.sort();
|
||||
|
||||
let body = format!(
|
||||
"Remote Peer: {}\nVerb: {}\nPath: {}\nQuery: {}\n\n=== Headers ===\n{}",
|
||||
let mut body = web::BytesMut::new();
|
||||
while let Some(chunk) = payload.next().await {
|
||||
let chunk = chunk?;
|
||||
// limit max size of in-memory payload
|
||||
if (body.len() + chunk.len()) > MAX_BODY_SIZE {
|
||||
return Err(error::ErrorBadRequest("overflow"));
|
||||
}
|
||||
body.extend_from_slice(&chunk);
|
||||
}
|
||||
|
||||
|
||||
let response = format!(
|
||||
"Remote Peer: {}\nVerb: {}\nPath: {}\nQuery: {}\n\n=== Headers ===\n{}\n\n=== Body ===\n{}",
|
||||
req.peer_addr().expect("Missing remote peer address!"),
|
||||
req.head().method,
|
||||
req.uri().path(),
|
||||
req.uri().query().unwrap_or_default(),
|
||||
headers.join("\n")
|
||||
headers.join("\n"),
|
||||
String::from_utf8_lossy(&body)
|
||||
);
|
||||
|
||||
HttpResponse::Ok()
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("text/plain")
|
||||
.body(body)
|
||||
.body(response))
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
|
Reference in New Issue
Block a user