From 1fb5a17f1430840da4990ffa35a04aca96f06d0a Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Wed, 27 Jan 2021 12:52:38 +0100 Subject: feat(users/Profpatsch): add read-http reads a http request or response from stdin, and parses its headers into a netencoded record. Darn rust code took way too long to write. Change-Id: Ie99faa6d4bbd4996fa4e43fb119a11d85b611c99 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2447 Reviewed-by: Profpatsch Tested-by: BuildkiteCI --- users/Profpatsch/read-http/default.nix | 19 ++++ users/Profpatsch/read-http/read-http.rs | 191 ++++++++++++++++++++++++++++++++ users/Profpatsch/rust-crates.nix | 7 ++ 3 files changed, 217 insertions(+) create mode 100644 users/Profpatsch/read-http/default.nix create mode 100644 users/Profpatsch/read-http/read-http.rs (limited to 'users/Profpatsch') diff --git a/users/Profpatsch/read-http/default.nix b/users/Profpatsch/read-http/default.nix new file mode 100644 index 000000000000..20f8675b3fd4 --- /dev/null +++ b/users/Profpatsch/read-http/default.nix @@ -0,0 +1,19 @@ +{ 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.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 new file mode 100644 index 000000000000..6d419542c707 --- /dev/null +++ b/users/Profpatsch/read-http/read-http.rs @@ -0,0 +1,191 @@ +extern crate httparse; +extern crate netencode; +extern crate arglib_netencode; + +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 lowercase_headers<'a>(headers: &'a [httparse::Header]) -> Vec<(String, &'a [u8])> { + let mut res = vec![]; + for httparse::Header { name, value } in headers { + // lowercase the headers, since the standard doesn’t care + // and we want unique strings to match agains + res.push((name.to_lowercase(), *value)) + } + 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, &lowercase_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, &lowercase_headers(resp.headers)) + } + } +} + +fn write_dict_req<'buf>(method: &'buf str, path: &'buf str, headers: &[(String, &[u8])]) -> 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, &[u8])]) -> 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, &[u8])]) -> std::io::Result<()> { + http.push(("headers", U::Record( + headers.iter().map( + |(name, value)| + (name.as_str(), U::Binary(value)) + ).collect::>() + ))); + + netencode::encode( + &mut std::io::stderr(), + 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/rust-crates.nix b/users/Profpatsch/rust-crates.nix index ecf3c960775b..bb65625fc758 100644 --- a/users/Profpatsch/rust-crates.nix +++ b/users/Profpatsch/rust-crates.nix @@ -82,4 +82,11 @@ rec { sha256 = "0fcknyvknglwwk1pdzdlb4m0ry2dym1yx8r5prf2v00pxnjk0hv2"; }; + httparse = pkgs.buildRustCrate { + pname = "httparse"; + version = "1.3.4"; + crateName = "httparse"; + sha256 = "0dggj4s0cq69bn63q9nqzzay5acmwl33nrbhjjsh5xys8sk2x4jw"; + }; + } -- cgit 1.4.1