diff --git a/Cargo.toml b/Cargo.toml index 73424ac..be657b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,12 @@ mime = "0.3.16" log = "0.4" simple_logger = "1" +[features] +default = [ "cgi", "scgi", "proxy" ] +cgi = [] +scgi = [] +proxy = [] + [profile.release] lto = true codegen-units = 1 diff --git a/src/cgi.rs b/src/cgi.rs index 3c74ce9..e8b7dd8 100644 --- a/src/cgi.rs +++ b/src/cgi.rs @@ -1,10 +1,18 @@ +#![cfg(any(feature = "cgi", feature = "scgi"))] use std::collections::HashMap; use std::io; -use std::path::PathBuf; +use std::net::SocketAddr; + +#[cfg(feature = "cgi")] use tokio::process::Command; -use url::Url; -use std::net::{SocketAddr, ToSocketAddrs}; +#[cfg(feature = "cgi")] +use std::path::PathBuf; + +#[cfg(feature = "scgi")] +use std::net::ToSocketAddrs; +#[cfg(feature = "scgi")] use tokio::io::{AsyncReadExt, AsyncWriteExt}; +#[cfg(feature = "scgi")] use tokio::net::TcpStream; use crate::config; @@ -12,6 +20,7 @@ use crate::conn; use crate::logger; use crate::status::Status; +#[cfg(any(feature = "cgi", feature = "scgi"))] fn envs(peer_addr: SocketAddr, srv: &config::ServerCfg, url: &url::Url) -> HashMap { let mut envs = HashMap::new(); envs.insert("GATEWAY_INTERFACE".to_string(), "CGI/1.1".to_string()); @@ -40,6 +49,7 @@ fn envs(peer_addr: SocketAddr, srv: &config::ServerCfg, url: &url::Url) -> HashM envs } +#[cfg(any(feature = "cgi", feature = "scgi"))] fn check(byt: u8, peer_addr: SocketAddr, u: url::Url) -> bool { match byt { 49 => { @@ -57,11 +67,12 @@ fn check(byt: u8, peer_addr: SocketAddr, u: url::Url) -> bool { true } +#[cfg(feature = "cgi")] pub async fn cgi( mut con: conn::Connection, srv: &config::ServerCfg, path: PathBuf, - url: Url, + url: url::Url, ) -> Result<(), io::Error> { let mut envs = envs(con.peer_addr, srv, &url); envs.insert("SCRIPT_NAME".to_string(), path.file_name().unwrap().to_str().unwrap().to_string()); @@ -112,6 +123,7 @@ pub async fn cgi( return Ok(()); } +#[cfg(feature = "scgi")] pub async fn scgi(addr: String, u: url::Url, mut con: conn::Connection, srv: &config::ServerCfg) -> Result<(), io::Error> { let addr = addr .to_socket_addrs()? diff --git a/src/config.rs b/src/config.rs index f3a2658..85cbd40 100644 --- a/src/config.rs +++ b/src/config.rs @@ -19,12 +19,17 @@ pub struct Server { pub key: String, pub cert: String, pub index: Option, + #[cfg(feature = "cgi")] pub cgi: Option, + #[cfg(feature = "cgi")] pub cgipath: Option, + #[cfg(any(feature = "cgi", feature = "scgi"))] pub cgienv: Option>, pub usrdir: Option, + #[cfg(feature = "proxy")] pub proxy: Option>, pub redirect: Option>, + #[cfg(feature = "scgi")] pub scgi: Option>, } diff --git a/src/main.rs b/src/main.rs index c6ed7e5..c8ce3aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -176,6 +176,8 @@ async fn handle_connection( } None => {} } + + #[cfg(feature = "proxy")] match &srv.server.proxy { Some(pr) => match url.path_segments().map(|c| c.collect::>()) { Some(s) => match pr.get(s[0]) { @@ -190,6 +192,7 @@ async fn handle_connection( None => {} } + #[cfg(feature = "scgi")] match &srv.server.scgi { Some(sc) => { let u = url.path().trim_end_matches("/"); @@ -258,6 +261,7 @@ async fn handle_connection( } } + #[cfg(feature = "cgi")] if srv.server.cgi.unwrap_or(false) { match &srv.server.cgipath { Some(c) => { diff --git a/src/revproxy.rs b/src/revproxy.rs index 9cf5ed2..0aebd54 100644 --- a/src/revproxy.rs +++ b/src/revproxy.rs @@ -1,3 +1,4 @@ +#![cfg(feature = "proxy")] use openssl::ssl::{SslConnector, SslMethod}; use std::io; use std::net::ToSocketAddrs;