Return header information
This commit is contained in:
		
							
								
								
									
										1318
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1318
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -6,3 +6,7 @@ edition = "2021"
 | 
			
		||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
actix-web = "4"
 | 
			
		||||
log = "0.4.17"
 | 
			
		||||
env_logger = "0.9.0"
 | 
			
		||||
clap = { version = "3.1.15", features = ["derive", "env"] }
 | 
			
		||||
							
								
								
									
										49
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										49
									
								
								src/main.rs
									
									
									
									
									
								
							@@ -1,3 +1,48 @@
 | 
			
		||||
fn main() {
 | 
			
		||||
    println!("Hello, world!");
 | 
			
		||||
use actix_web::{App, HttpRequest, HttpResponse, HttpServer, Responder, web};
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user