Return header information

This commit is contained in:
Pierre HUBERT 2022-05-04 15:20:51 +02:00
parent d17b6402fd
commit 65d54a9264
3 changed files with 1369 additions and 2 deletions

1318
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -6,3 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
actix-web = "4"
log = "0.4.17"
env_logger = "0.9.0"
clap = { version = "3.1.15", features = ["derive", "env"] }

View File

@ -1,3 +1,48 @@
fn main() { use actix_web::{App, HttpRequest, HttpResponse, HttpServer, Responder, web};
println!("Hello, world!"); use clap::Parser;
/// Simple HTTP responder
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
/// Listen address
#[clap(short, long, env, default_value = "0.0.0.0:8000")]
listen_address: String,
}
async fn handler(req: HttpRequest) -> impl Responder {
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{}",
req.peer_addr().expect("Missing remote peer address!"),
req.head().method,
req.uri().path(),
req.uri().query().unwrap_or_default(),
headers.join("\n")
);
HttpResponse::Ok()
.content_type("text/plain")
.body(body)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
let args: Args = Args::parse();
log::info!("Will listen on {}...", args.listen_address);
HttpServer::new(|| {
App::new()
.route("{tail:.*}", web::route().to(handler))
})
.bind(args.listen_address)?
.run()
.await
} }