Skip to content

Commit

Permalink
--header-to-env
Browse files Browse the repository at this point in the history
  • Loading branch information
vi committed Jul 14, 2019
1 parent 6c07260 commit 8024156
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ impl WebsocatConfiguration2 {
}
}

if !self.opts.headers_to_env.is_empty() && !self.opts.exec_set_env {
on_warning("--header-to-env is meaningless without -e (--set-environment)");
}

Ok(())
}
fn l_closebug(&mut self, on_warning: &OnWarning) -> Result<()> {
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ struct Opt {
)]
custom_reply_headers: Vec<(String, Vec<u8>)>,

/// Forward specified incoming request header to
/// H_* environment variable for `exec:`-like specifiers.
#[structopt(
long = "header-to-env",
)]
headers_to_env: Vec<String>,

#[structopt(
long = "websocket-version",
help = "Override the Sec-WebSocket-Version value"
Expand Down Expand Up @@ -536,6 +543,7 @@ fn run() -> Result<()> {
origin
custom_headers
custom_reply_headers
headers_to_env
websocket_version
websocket_dont_close
one_message
Expand Down
2 changes: 2 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub struct Options {
pub pkcs12_passwd: Option<String>,
pub tls_insecure: bool,

pub headers_to_env: Vec<String>,

pub max_parallel_conns: Option<usize>,
pub ws_ping_interval: Option<u64>,
pub ws_ping_timeout: Option<u64>,
Expand Down
25 changes: 20 additions & 5 deletions src/ws_server_peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl<T: Specifier> Specifier for WsServer<T> {
//let l2r = cp.left_to_right;
let rdh = cp.program_options.read_debt_handling;
inner.map(move |p, l2r| {
// FIXME: attack of `Vec::clone`s.
ws_upgrade_peer(
p,
mode1,
Expand All @@ -44,6 +45,7 @@ impl<T: Specifier> Specifier for WsServer<T> {
cp.program_options.ws_ping_timeout,
cp.program_options.websocket_reply_protocol.clone(),
cp.program_options.custom_reply_headers.clone(),
cp.program_options.headers_to_env.clone(),
l2r,
)
})
Expand Down Expand Up @@ -129,6 +131,7 @@ pub fn ws_upgrade_peer(
ping_timeout: Option<u64>,
websocket_protocol: Option<String>,
custom_reply_headers: Vec<(String, Vec<u8>)>,
forward_these_headers: Vec<String>,
l2r: L2rUser,
) -> BoxedNewPeerFuture {
let step1 = PeerForWs(inner_peer);
Expand Down Expand Up @@ -218,11 +221,23 @@ pub fn ws_upgrade_peer(
z.uri = Some(format!("{}", uri));

let h : &websocket::header::Headers = &x.request.headers;
for q in h.iter() {
z.headers.push((
q.name().to_string(),
q.value_string(),
));
for q in forward_these_headers.iter() {
if let Some(v) = h.get_raw(q) {
if v.is_empty() { continue }
if v.len() > 1 {
warn!("Extra request header for {} ignored", q);
}
if let Ok(val) = String::from_utf8(v[0].clone()) {
z.headers.push((
q.clone(),
val,
));
} else {
warn!("Header {} value contains invalid UTF-8", q);
}
} else {
warn!("No request header {}, so no envvar H_{}", q, q);
}
}
},
L2rUser::ReadFrom(_) => {},
Expand Down

0 comments on commit 8024156

Please sign in to comment.