add -e option for constant environment variables

This commit is contained in:
dece 2021-11-26 16:06:03 +01:00
parent 6f51c4b9c4
commit f93d63ee01
2 changed files with 25 additions and 2 deletions

View file

@ -31,6 +31,8 @@ can be configured from the command line.
- `-c, --cert <cert>`: server certificate path. - `-c, --cert <cert>`: server certificate path.
- `-k, --key <key>`: server private key path. - `-k, --key <key>`: server private key path.
- `-r, --root-path <root_path>`: path to CGI scripts root. - `-r, --root-path <root_path>`: path to CGI scripts root.
- `-e, --env <key=value>`: additional environment variables for CGI scripts;
this option can be used multiple times.

View file

@ -18,6 +18,7 @@ use openssl::{asn1, ssl, x509};
#[derive(Clone)] #[derive(Clone)]
struct CgiConfig { struct CgiConfig {
root: String, root: String,
envs: HashMap<String, String>,
} }
fn main() { fn main() {
@ -65,6 +66,14 @@ fn run() -> Result<(), i32> {
.help("Path to CGI scripts root") .help("Path to CGI scripts root")
.takes_value(true), .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(); .get_matches();
// Setup logging pretty much just like Agate. // Setup logging pretty much just like Agate.
@ -80,7 +89,18 @@ fn run() -> Result<(), i32> {
error!("Invalid CGI root path: {}", err); error!("Invalid CGI root path: {}", err);
1 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. // Setup TLS server.
let acceptor = create_ssl_acceptor( let acceptor = create_ssl_acceptor(
@ -339,7 +359,8 @@ fn get_response(
// Run the subprocess! // Run the subprocess!
let output = process::Command::new(script_path) let output = process::Command::new(script_path)
.env_clear() .env_clear()
.envs(envs) .envs(&envs)
.envs(&cgi_config.envs)
.output() .output()
.map_err(|err| { .map_err(|err| {
error!("Can't execute script: {}", err); error!("Can't execute script: {}", err);