From f93d63ee0110dd85732fc00a484532bc47040283 Mon Sep 17 00:00:00 2001 From: dece Date: Fri, 26 Nov 2021 16:06:03 +0100 Subject: [PATCH] add -e option for constant environment variables --- README.md | 2 ++ src/main.rs | 25 +++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bc0a81d..13001c7 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,8 @@ can be configured from the command line. - `-c, --cert `: server certificate path. - `-k, --key `: server private key path. - `-r, --root-path `: path to CGI scripts root. +- `-e, --env `: additional environment variables for CGI scripts; + this option can be used multiple times. diff --git a/src/main.rs b/src/main.rs index 141be73..95fce0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ use openssl::{asn1, ssl, x509}; #[derive(Clone)] struct CgiConfig { root: String, + envs: HashMap, } fn main() { @@ -65,6 +66,14 @@ fn run() -> Result<(), i32> { .help("Path to CGI scripts root") .takes_value(true), ) + .arg( + clap::Arg::with_name("env") + .short("e") + .long("env") + .help("Environment variable for CGI scripts") + .takes_value(true) + .multiple(true), + ) .get_matches(); // Setup logging pretty much just like Agate. @@ -80,7 +89,18 @@ fn run() -> Result<(), i32> { error!("Invalid CGI root path: {}", err); 1 })?; - let cgi_config = CgiConfig { root: cgi_root }; + let mut cgi_envs = HashMap::new(); + if let Some(envs) = matches.values_of("env") { + envs.for_each(|env| { + if let Some((key, value)) = env.split_once("=") { + cgi_envs.insert(key.to_string(), value.to_string()); + } + }) + } + let cgi_config = CgiConfig { + root: cgi_root, + envs: cgi_envs, + }; // Setup TLS server. let acceptor = create_ssl_acceptor( @@ -339,7 +359,8 @@ fn get_response( // Run the subprocess! let output = process::Command::new(script_path) .env_clear() - .envs(envs) + .envs(&envs) + .envs(&cgi_config.envs) .output() .map_err(|err| { error!("Can't execute script: {}", err);