Skip to content

Commit

Permalink
start
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb committed Oct 31, 2024
1 parent 52b3332 commit a865e37
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,28 @@ use std::net::IpAddr;
#[cfg(feature = "tls")]
use std::path::PathBuf;

#[derive(clap::ValueEnum, Clone, Debug, PartialEq)]
pub enum PrintOption {
#[clap(name = "B")]
RequestBody,
#[clap(name = "H")]
RequestHeaders,
#[clap(name = "b")]
ResponseBody,
#[clap(name = "h")]
ResponseHeaders,
}

#[derive(Debug, Clone, Parser)]
#[command(name = "dummyhttp", author, about, version)]
pub struct Args {
/// Be quiet (log nothing)
#[arg(short, long)]
pub quiet: bool,

#[arg(long, value_delimiter = ',', num_args = 1..)]
pub print: Vec<PrintOption>,

/// Be verbose (log data of incoming and outgoing requests). If given twice it will also log
/// the body data.
#[arg(short, long, action = clap::ArgAction::Count)]
Expand Down
53 changes: 52 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use hyper::{header::CONTENT_TYPE, HeaderMap};
use inflector::Inflector;
use tokio::time::{sleep, Duration};

use crate::args::Args;
use crate::args::{Args, PrintOption};

mod args;

Expand Down Expand Up @@ -215,6 +215,57 @@ async fn print_request_response(
resp_banner = "┌─Outgoing response".red().bold(),
resp_info = resp_info,
);
} else if !args.print.is_empty() {
let mut incoming_headers_vec = vec![];
for (hk, hv) in &req_headers {
incoming_headers_vec.push(format!(
"{key}: {value}",
key = Inflector::to_train_case(hk.as_str()).cyan(),
value = hv.to_str().unwrap_or("<unprintable>")
));
}
incoming_headers_vec.sort();
if !incoming_headers_vec.is_empty() {
incoming_headers_vec.insert(0, "".to_string());
}
let request_headers_text = incoming_headers_vec.join("\n");

let request_body_text = String::from_utf8_lossy(&bytes2);

let mut outgoing_headers_vec = vec![];
for (hk, hv) in resp.headers() {
outgoing_headers_vec.push(format!(
"{key}: {value}",
key = Inflector::to_train_case(hk.as_str()).cyan(),
value = hv.to_str().unwrap_or("<unprintable>")
));
}
if !outgoing_headers_vec.is_empty() {
outgoing_headers_vec.insert(0, "".to_string());
}
outgoing_headers_vec.sort();
let response_headers_text = outgoing_headers_vec.join("\n");

let response_body_text =
if args.print.contains(&PrintOption::ResponseBody) && !args.body.is_empty() {
args.body
} else {
"".to_string()
};

if args.print.contains(&PrintOption::RequestHeaders) {
println!("{request_headers_text}");
}
if args.print.contains(&PrintOption::RequestBody) {
println!("{request_body_text}");
}

if args.print.contains(&PrintOption::ResponseHeaders) {
println!("{response_headers_text}");
}
if args.print.contains(&PrintOption::ResponseBody) {
println!("{response_body_text}");
}
} else if !args.quiet {
println!("{connect_line}",);
}
Expand Down

0 comments on commit a865e37

Please sign in to comment.