From 492b79ec7a1844700ff75e19b39e3bc21f93dc23 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sun, 31 Jan 2021 16:38:21 +0100 Subject: feat(users/Profpatsch): add die_* helpers for semantic exit errors There is this semantic exit code schema championed by execline and skaware tooling, and we refined and documented it a bit in lorri https://github.com/nix-community/lorri/blob/d1d673d42090f0cfe8ab9b92b465315a9e7d30a3/src/ops/mod.rs#L24-L35 in the past. This just transcribes the error messages into simple helper functions. Applies the functions to the places where we would panic or die `sys::exit()` instead. Change-Id: I15ca05cd6f99a25a3378518be94110eab416354e Reviewed-on: https://cl.tvl.fyi/c/depot/+/2475 Tested-by: BuildkiteCI Reviewed-by: Profpatsch --- users/Profpatsch/execline/exec_helpers.rs | 55 ++++++++- users/Profpatsch/netencode/default.nix | 9 +- users/Profpatsch/netencode/netencode.rs | 7 +- users/Profpatsch/read-http.nix | 17 +++ users/Profpatsch/read-http.rs | 192 +++++++++++++++++++++++++++++ users/Profpatsch/read-http/default.nix | 20 --- users/Profpatsch/read-http/read-http.rs | 194 ------------------------------ 7 files changed, 273 insertions(+), 221 deletions(-) create mode 100644 users/Profpatsch/read-http.nix create mode 100644 users/Profpatsch/read-http.rs delete mode 100644 users/Profpatsch/read-http/default.nix delete mode 100644 users/Profpatsch/read-http/read-http.rs (limited to 'users/Profpatsch') diff --git a/users/Profpatsch/execline/exec_helpers.rs b/users/Profpatsch/execline/exec_helpers.rs index 4e4149882b40..3e74ffc72210 100644 --- a/users/Profpatsch/execline/exec_helpers.rs +++ b/users/Profpatsch/execline/exec_helpers.rs @@ -36,6 +36,59 @@ pub fn exec_into_args<'a, 'b, Args, Arg, Env, Key, Val>(current_prog_name: &str, let env = env_additions.into_iter().collect::>(); let env = env.iter().map(|(k,v)| (OsStr::from_bytes(k.as_ref()), OsStr::from_bytes(v.as_ref()))); let err = std::process::Command::new(prog).args(args).envs(env).exec(); - panic!("{}: exec failed: {:?}", current_prog_name, err); + die_missing_executable(current_prog_name, format!("exec failed: {:?}", err)); } +/// Exit 1 to signify a generic expected error +/// (e.g. something that sometimes just goes wrong, like a nix build). +pub fn die_expected_error(current_prog_name: &str, msg: S) -> ! +where S: AsRef +{ + die_with(1, current_prog_name, msg) +} + +/// Exit 100 to signify a user error (“the user is holding it wrong”). +/// This is a permanent error, if the program is executed the same way +/// it should crash with 100 again. +pub fn die_user_error(current_prog_name: &str, msg: S) -> ! +where S: AsRef +{ + die_with(100, current_prog_name, msg) +} + +/// Exit 101 to signify an unexpected crash (failing assertion or panic). +/// This is the same exit code that `panic!()` emits. +pub fn die_panic(current_prog_name: &str, msg: S) -> ! +where S: AsRef +{ + die_with(101, current_prog_name, msg) +} + +/// Exit 111 to signify a temporary error (such as resource exhaustion) +pub fn die_temporary(current_prog_name: &str, msg: S) -> ! +where S: AsRef +{ + die_with(111, current_prog_name, msg) +} + +/// Exit 126 to signify an environment problem +/// (the user has set up stuff incorrectly so the program cannot work) +pub fn die_environment_problem(current_prog_name: &str, msg: S) -> ! +where S: AsRef +{ + die_with(126, current_prog_name, msg) +} + +/// Exit 127 to signify a missing executable. +pub fn die_missing_executable(current_prog_name: &str, msg: S) -> ! +where S: AsRef +{ + die_with(127, current_prog_name, msg) +} + +fn die_with(status: i32, current_prog_name: &str, msg: S) -> ! + where S: AsRef +{ + eprintln!("{}: {}", current_prog_name, msg.as_ref()); + std::process::exit(status) +} diff --git a/users/Profpatsch/netencode/default.nix b/users/Profpatsch/netencode/default.nix index fb1d2c2ef831..b9822d48225a 100644 --- a/users/Profpatsch/netencode/default.nix +++ b/users/Profpatsch/netencode/default.nix @@ -30,7 +30,10 @@ let netencode-rs-common = tests: imports.writers.rustSimpleLib { name = "netencode"; - dependencies = [ nom ]; + dependencies = [ + nom + depot.users.Profpatsch.execline.exec-helpers + ]; buildTests = tests; release = false; verbose = true; @@ -101,13 +104,13 @@ let use netencode::dec::{Record, ScalarAsBytes, Decoder, DecodeError}; fn main() { - let t = netencode::t_from_stdin_or_panic("record-splice-env"); + let t = netencode::t_from_stdin_or_die_user_error("record-splice-env"); let (_, prog) = exec_helpers::args_for_exec("record-splice-env", 0); match Record::::dec(t) { Ok(map) => { exec_helpers::exec_into_args("record-splice-env", prog, map); }, - Err(DecodeError(err)) => panic!("{}", err), + Err(DecodeError(err)) => exec_helpers::die_user_error("record-splice-env", err), } } ''; diff --git a/users/Profpatsch/netencode/netencode.rs b/users/Profpatsch/netencode/netencode.rs index 66f3245fcf99..f6158ae3e6a0 100644 --- a/users/Profpatsch/netencode/netencode.rs +++ b/users/Profpatsch/netencode/netencode.rs @@ -1,4 +1,5 @@ extern crate nom; +extern crate exec_helpers; use std::collections::HashMap; use std::io::{Write, Read}; @@ -116,15 +117,15 @@ pub fn text(s: String) -> T { T::Text(s) } -pub fn t_from_stdin_or_panic(prog_name: &str) -> T { +pub fn t_from_stdin_or_die_user_error(prog_name: &str) -> T { let mut buf = vec![]; std::io::stdin().lock().read_to_end(&mut buf); match parse::t_t(&buf) { Ok((rest, t)) => match rest { b"" => t, - _ => panic!("{}: stdin contained some soup after netencode value: {:?}", prog_name, rest) + _ => exec_helpers::die_user_error(prog_name, format!("stdin contained some soup after netencode value: {:?}", rest)) }, - Err(err) => panic!("{}: unable to parse netencode from stdin: {:?}", prog_name, err) + Err(err) => exec_helpers::die_user_error(prog_name, format!("unable to parse netencode from stdin: {:?}", err)) } } diff --git a/users/Profpatsch/read-http.nix b/users/Profpatsch/read-http.nix new file mode 100644 index 000000000000..aff1fa8662e1 --- /dev/null +++ b/users/Profpatsch/read-http.nix @@ -0,0 +1,17 @@ +{ depot, pkgs, ... }: + +let + + # reads a http request (stdin), and writes all headers to stdout, as netencoded dict + read-http = depot.users.Profpatsch.writers.rustSimple { + name = "read-http"; + dependencies = [ + depot.users.Profpatsch.rust-crates.ascii + depot.users.Profpatsch.rust-crates.httparse + depot.users.Profpatsch.netencode.netencode-rs + depot.users.Profpatsch.arglib.netencode.rust + depot.users.Profpatsch.execline.exec-helpers + ]; + } (builtins.readFile ./read-http.rs); + +in read-http diff --git a/users/Profpatsch/read-http.rs b/users/Profpatsch/read-http.rs new file mode 100644 index 000000000000..de112f4c772d --- /dev/null +++ b/users/Profpatsch/read-http.rs @@ -0,0 +1,192 @@ +extern crate httparse; +extern crate netencode; +extern crate arglib_netencode; +extern crate ascii; +extern crate exec_helpers; + +use std::os::unix::io::FromRawFd; +use std::io::Read; +use std::io::Write; +use exec_helpers::{die_user_error, die_expected_error, die_temporary}; + +use netencode::{U, T}; + +enum What { + Request, + Response +} + +fn main() -> std::io::Result<()> { + + let what : What = match arglib_netencode::arglib_netencode(None).unwrap() { + T::Record(rec) => match rec.get("what") { + Some(T::Text(t)) => match t.as_str() { + "request" => What::Request, + "response" => What::Response, + _ => die_user_error("read-http arglib", "`what` should be either t:request or t:response"), + }, + Some(o) => die_user_error("read-http arglib", format!("expected a record of text, got {:#?}", o)), + None => { + eprintln!("read-http arglib: no `what` given, defaulting to Response"); + What::Response + } + } + o => die_user_error("read-http arglib", format!("expected a record, got {:#?}", o)) + }; + + fn read_stdin_to_complete(mut parse: F) -> () + where F: FnMut(&[u8]) -> httparse::Result + { + let mut res = httparse::Status::Partial; + loop { + if let httparse::Status::Complete(_) = res { + return; + } + let mut buf = [0; 2048]; + match std::io::stdin().read(&mut buf[..]) { + Ok(size) => if size == 0 { + break; + }, + Err(err) => die_temporary("read-http", format!("could not read from stdin, {:?}", err)) + } + match parse(&buf) { + Ok(status) => { + res = status; + } + Err(err) => die_temporary("read-http", format!("httparse parsing failed: {:#?}", err)) + } + } + } + + + fn normalize_headers<'a>(headers: &'a [httparse::Header]) -> Vec<(String, &'a str)> { + let mut res = vec![]; + for httparse::Header { name, value } in headers { + let val = ascii::AsciiStr::from_ascii(*value) + .expect(&format!("read-http: we require header values to be ASCII, but the header {} was {:?}", name, value)); + // lowercase the headers, since the standard doesn’t care + // and we want unique strings to match agains + res.push((name.to_lowercase(), val.as_str())) + } + res + } + + // tries to read until the end of the http header (deliniated by two newlines "\r\n\r\n") + fn read_till_end_of_header(buf: &mut Vec, reader: R) -> Option<()> { + let mut chunker = Chunkyboi::new(reader, 4096); + loop { + match chunker.next() { + Some(Ok(chunk)) => { + buf.extend_from_slice(&chunk); + if chunk.windows(4).any(|c| c == b"\r\n\r\n" ) { + return Some(()); + } + }, + Some(Err(err)) => die_temporary("read-http", format!("error reading from stdin: {:?}", err)), + None => return None + } + } + } + + // max header size chosen arbitrarily + let mut headers = [httparse::EMPTY_HEADER; 128]; + let stdin = std::io::stdin(); + + match what { + Request => { + let mut req = httparse::Request::new(&mut headers); + let mut buf: Vec = vec![]; + match read_till_end_of_header(&mut buf, stdin.lock()) { + Some(()) => match req.parse(&buf) { + Ok(httparse::Status::Complete(_body_start)) => {}, + Ok(httparse::Status::Partial) => die_expected_error("read-http", "httparse should have gotten a full header"), + Err(err) => die_expected_error("read-http", format!("httparse response parsing failed: {:#?}", err)) + }, + None => die_expected_error("read-http", format!("httparse end of stdin reached before able to parse request headers")) + } + let method = req.method.expect("method must be filled on complete parse"); + let path = req.path.expect("path must be filled on complete parse"); + write_dict_req(method, path, &normalize_headers(req.headers)) + }, + Response => { + let mut resp = httparse::Response::new(&mut headers); + let mut buf: Vec = vec![]; + match read_till_end_of_header(&mut buf, stdin.lock()) { + Some(()) => match resp.parse(&buf) { + Ok(httparse::Status::Complete(_body_start)) => {}, + Ok(httparse::Status::Partial) => die_expected_error("read-http", "httparse should have gotten a full header"), + Err(err) => die_expected_error("read-http", format!("httparse response parsing failed: {:#?}", err)) + }, + None => die_expected_error("read-http", format!("httparse end of stdin reached before able to parse response headers")) + } + let code = resp.code.expect("code must be filled on complete parse"); + let reason = resp.reason.expect("reason must be filled on complete parse"); + write_dict_resp(code, reason, &normalize_headers(resp.headers)) + } + } +} + +fn write_dict_req<'buf>(method: &'buf str, path: &'buf str, headers: &[(String, &str)]) -> std::io::Result<()> { + let mut http = vec![ + ("method", U::Text(method.as_bytes())), + ("path", U::Text(path.as_bytes())), + ]; + write_dict(http, headers) +} + +fn write_dict_resp<'buf>(code: u16, reason: &'buf str, headers: &[(String, &str)]) -> std::io::Result<()> { + let mut http = vec![ + ("status", U::N6(code as u64)), + ("status-text", U::Text(reason.as_bytes())), + ]; + write_dict(http, headers) +} + + +fn write_dict<'buf, 'a>(mut http: Vec<(&str, U<'a>)>, headers: &'a[(String, &str)]) -> std::io::Result<()> { + http.push(("headers", U::Record( + headers.iter().map( + |(name, value)| + (name.as_str(), U::Text(value.as_bytes())) + ).collect::>() + ))); + + netencode::encode( + &mut std::io::stdout(), + U::Record(http) + )?; + Ok(()) +} + + +// iter helper + +struct Chunkyboi { + inner: T, + buf: Vec, +} + +impl Chunkyboi { + fn new(inner: R, chunksize: usize) -> Self { + let buf = vec![0; chunksize]; + Chunkyboi { + inner, + buf + } + } +} + +impl Iterator for Chunkyboi { + type Item = std::io::Result>; + + fn next(&mut self) -> Option>> { + match self.inner.read(&mut self.buf) { + Ok(0) => None, + Ok(read) => { + // clone a new buffer so we can reuse the internal one + Some(Ok(self.buf[..read].to_owned())) + } + Err(err) => Some(Err(err)) + } + } +} diff --git a/users/Profpatsch/read-http/default.nix b/users/Profpatsch/read-http/default.nix deleted file mode 100644 index 41fe1c7fedcc..000000000000 --- a/users/Profpatsch/read-http/default.nix +++ /dev/null @@ -1,20 +0,0 @@ -{ depot, pkgs, ... }: - -let - - # reads a http request (stdin), and writes all headers to stdout, as netencoded dict - read-http = depot.users.Profpatsch.writers.rustSimple { - name = "read-http"; - dependencies = [ - depot.users.Profpatsch.rust-crates.ascii - depot.users.Profpatsch.rust-crates.httparse - depot.users.Profpatsch.netencode.netencode-rs - depot.users.Profpatsch.arglib.netencode.rust - ]; - } (builtins.readFile ./read-http.rs); - -in { - inherit - read-http - ; -} diff --git a/users/Profpatsch/read-http/read-http.rs b/users/Profpatsch/read-http/read-http.rs deleted file mode 100644 index ab2c3887d7b5..000000000000 --- a/users/Profpatsch/read-http/read-http.rs +++ /dev/null @@ -1,194 +0,0 @@ -extern crate httparse; -extern crate netencode; -extern crate arglib_netencode; -extern crate ascii; - -use std::os::unix::io::FromRawFd; -use std::io::Read; -use std::io::Write; - -use netencode::{U, T}; - -enum What { - Request, - Response -} - -fn main() -> std::io::Result<()> { - fn die(msg: T) -> ! { - eprintln!("{}", msg); - std::process::exit(1); - } - - let what : What = match arglib_netencode::arglib_netencode(None).unwrap() { - T::Record(rec) => match rec.get("what") { - Some(T::Text(t)) => match t.as_str() { - "request" => What::Request, - "response" => What::Response, - _ => die("read-http arglib: what should be either t:request or t:response"), - }, - Some(o) => die(format!("read-http arglib: expected a record of text, got {:#?}", o)), - None => { - eprintln!("read-http arglib: no `what` given, defaulting to Response"); - What::Response - } - } - o => die(format!("read-http arglib: expected a record, got {:#?}", o)) - }; - - fn read_stdin_to_complete(mut parse: F) -> () - where F: FnMut(&[u8]) -> httparse::Result - { - let mut res = httparse::Status::Partial; - loop { - if let httparse::Status::Complete(_) = res { - return; - } - let mut buf = [0; 2048]; - match std::io::stdin().read(&mut buf[..]) { - Ok(size) => if size == 0 { - break; - }, - Err(err) => panic!("could not read from stdin, {:?}", err) - } - match parse(&buf) { - Ok(status) => { - res = status; - } - Err(err) => die(format!("httparse parsing failed: {:#?}", err)) - } - } - } - - - fn normalize_headers<'a>(headers: &'a [httparse::Header]) -> Vec<(String, &'a str)> { - let mut res = vec![]; - for httparse::Header { name, value } in headers { - let val = ascii::AsciiStr::from_ascii(*value) - .expect(&format!("read-http: we require header values to be ASCII, but the header {} was {:?}", name, value)); - // lowercase the headers, since the standard doesn’t care - // and we want unique strings to match agains - res.push((name.to_lowercase(), val.as_str())) - } - res - } - - // tries to read until the end of the http header (deliniated by two newlines "\r\n\r\n") - fn read_till_end_of_header(buf: &mut Vec, reader: R) -> Option<()> { - let mut chunker = Chunkyboi::new(reader, 4096); - loop { - match chunker.next() { - Some(Ok(chunk)) => { - buf.extend_from_slice(&chunk); - if chunk.windows(4).any(|c| c == b"\r\n\r\n" ) { - return Some(()); - } - }, - Some(Err(err)) => die(format!("error reading from stdin: {:?}", err)), - None => return None - } - } - } - - // max header size chosen arbitrarily - let mut headers = [httparse::EMPTY_HEADER; 128]; - let stdin = std::io::stdin(); - - match what { - Request => { - let mut req = httparse::Request::new(&mut headers); - let mut buf: Vec = vec![]; - match read_till_end_of_header(&mut buf, stdin.lock()) { - Some(()) => match req.parse(&buf) { - Ok(httparse::Status::Complete(_body_start)) => {}, - Ok(httparse::Status::Partial) => die("httparse should have gotten a full header"), - Err(err) => die(format!("httparse response parsing failed: {:#?}", err)) - }, - None => die(format!("httparse end of stdin reached before able to parse request headers")) - } - let method = req.method.expect("method must be filled on complete parse"); - let path = req.path.expect("path must be filled on complete parse"); - write_dict_req(method, path, &normalize_headers(req.headers)) - }, - Response => { - let mut resp = httparse::Response::new(&mut headers); - let mut buf: Vec = vec![]; - match read_till_end_of_header(&mut buf, stdin.lock()) { - Some(()) => match resp.parse(&buf) { - Ok(httparse::Status::Complete(_body_start)) => {}, - Ok(httparse::Status::Partial) => die("httparse should have gotten a full header"), - Err(err) => die(format!("httparse response parsing failed: {:#?}", err)) - }, - None => die(format!("httparse end of stdin reached before able to parse response headers")) - } - let code = resp.code.expect("code must be filled on complete parse"); - let reason = resp.reason.expect("reason must be filled on complete parse"); - write_dict_resp(code, reason, &normalize_headers(resp.headers)) - } - } -} - -fn write_dict_req<'buf>(method: &'buf str, path: &'buf str, headers: &[(String, &str)]) -> std::io::Result<()> { - let mut http = vec![ - ("method", U::Text(method.as_bytes())), - ("path", U::Text(path.as_bytes())), - ]; - write_dict(http, headers) -} - -fn write_dict_resp<'buf>(code: u16, reason: &'buf str, headers: &[(String, &str)]) -> std::io::Result<()> { - let mut http = vec![ - ("status", U::N6(code as u64)), - ("status-text", U::Text(reason.as_bytes())), - ]; - write_dict(http, headers) -} - - -fn write_dict<'buf, 'a>(mut http: Vec<(&str, U<'a>)>, headers: &'a[(String, &str)]) -> std::io::Result<()> { - http.push(("headers", U::Record( - headers.iter().map( - |(name, value)| - (name.as_str(), U::Text(value.as_bytes())) - ).collect::>() - ))); - - netencode::encode( - &mut std::io::stdout(), - U::Record(http) - )?; - Ok(()) -} - - -// iter helper - -struct Chunkyboi { - inner: T, - buf: Vec, -} - -impl Chunkyboi { - fn new(inner: R, chunksize: usize) -> Self { - let buf = vec![0; chunksize]; - Chunkyboi { - inner, - buf - } - } -} - -impl Iterator for Chunkyboi { - type Item = std::io::Result>; - - fn next(&mut self) -> Option>> { - match self.inner.read(&mut self.buf) { - Ok(0) => None, - Ok(read) => { - // clone a new buffer so we can reuse the internal one - Some(Ok(self.buf[..read].to_owned())) - } - Err(err) => Some(Err(err)) - } - } -} -- cgit 1.4.1