2021-01-27 12:52:38 +01:00
|
|
|
|
extern crate arglib_netencode;
|
2021-01-29 16:35:02 +01:00
|
|
|
|
extern crate ascii;
|
2021-01-31 16:38:21 +01:00
|
|
|
|
extern crate exec_helpers;
|
2022-02-07 16:49:59 +01:00
|
|
|
|
extern crate httparse;
|
|
|
|
|
extern crate netencode;
|
2021-01-27 12:52:38 +01:00
|
|
|
|
|
2022-02-07 16:49:59 +01:00
|
|
|
|
use exec_helpers::{die_expected_error, die_temporary, die_user_error};
|
2021-02-06 22:33:39 +01:00
|
|
|
|
use std::collections::HashMap;
|
2022-02-07 16:49:59 +01:00
|
|
|
|
use std::io::{Read, Write};
|
|
|
|
|
use std::os::unix::io::FromRawFd;
|
2021-01-27 12:52:38 +01:00
|
|
|
|
|
2021-02-09 01:38:08 +01:00
|
|
|
|
use netencode::dec::Decoder;
|
2022-02-07 16:49:59 +01:00
|
|
|
|
use netencode::{dec, T, U};
|
2021-01-27 12:52:38 +01:00
|
|
|
|
|
|
|
|
|
enum What {
|
|
|
|
|
Request,
|
2022-02-07 16:49:59 +01:00
|
|
|
|
Response,
|
2021-01-27 12:52:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-06 22:33:39 +01:00
|
|
|
|
// reads a http request (stdin), and writes all headers to stdout, as netencoded record.
|
|
|
|
|
// The keys are text, but can be lists of text iff headers appear multiple times, so beware.
|
2021-01-27 12:52:38 +01:00
|
|
|
|
fn main() -> std::io::Result<()> {
|
2021-02-09 21:06:07 +01:00
|
|
|
|
exec_helpers::no_args("read-http");
|
|
|
|
|
|
2021-02-09 01:38:08 +01:00
|
|
|
|
let args = dec::RecordDot {
|
|
|
|
|
field: "what",
|
|
|
|
|
inner: dec::OneOf {
|
|
|
|
|
list: vec!["request", "response"],
|
2022-02-07 16:49:59 +01:00
|
|
|
|
inner: dec::Text,
|
|
|
|
|
},
|
2021-02-09 01:38:08 +01:00
|
|
|
|
};
|
2022-02-07 16:49:59 +01:00
|
|
|
|
let what: What = match args.dec(arglib_netencode::arglib_netencode("read-http", None).to_u()) {
|
2021-02-09 01:38:08 +01:00
|
|
|
|
Ok("request") => What::Request,
|
|
|
|
|
Ok("response") => What::Response,
|
|
|
|
|
Ok(v) => panic!("shouldn’t happen!, value was: {}", v),
|
|
|
|
|
Err(dec::DecodeError(err)) => die_user_error("read-http", err),
|
2021-01-27 12:52:38 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fn read_stdin_to_complete<F>(mut parse: F) -> ()
|
2022-02-07 16:49:59 +01:00
|
|
|
|
where
|
|
|
|
|
F: FnMut(&[u8]) -> httparse::Result<usize>,
|
2021-01-27 12:52:38 +01:00
|
|
|
|
{
|
|
|
|
|
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[..]) {
|
2022-02-07 16:49:59 +01:00
|
|
|
|
Ok(size) => {
|
|
|
|
|
if size == 0 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
die_temporary("read-http", format!("could not read from stdin, {:?}", err))
|
|
|
|
|
}
|
2021-01-27 12:52:38 +01:00
|
|
|
|
}
|
|
|
|
|
match parse(&buf) {
|
|
|
|
|
Ok(status) => {
|
|
|
|
|
res = status;
|
|
|
|
|
}
|
2022-02-07 16:49:59 +01:00
|
|
|
|
Err(err) => {
|
|
|
|
|
die_temporary("read-http", format!("httparse parsing failed: {:#?}", err))
|
|
|
|
|
}
|
2021-01-27 12:52:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-06 22:33:39 +01:00
|
|
|
|
fn normalize_headers<'a>(headers: &'a [httparse::Header]) -> HashMap<String, U<'a>> {
|
|
|
|
|
let mut res = HashMap::new();
|
2021-01-27 12:52:38 +01:00
|
|
|
|
for httparse::Header { name, value } in headers {
|
2021-01-29 16:35:02 +01:00
|
|
|
|
let val = ascii::AsciiStr::from_ascii(*value)
|
2022-02-07 16:49:59 +01:00
|
|
|
|
.expect(&format!(
|
|
|
|
|
"read-http: we require header values to be ASCII, but the header {} was {:?}",
|
|
|
|
|
name, value
|
|
|
|
|
))
|
2021-02-06 22:33:39 +01:00
|
|
|
|
.as_str();
|
|
|
|
|
// lowercase the header names, since the standard doesn’t care
|
|
|
|
|
// and we want unique strings to match against
|
|
|
|
|
let name_lower = name.to_lowercase();
|
|
|
|
|
match res.insert(name_lower, U::Text(val)) {
|
|
|
|
|
None => (),
|
|
|
|
|
Some(U::Text(t)) => {
|
|
|
|
|
let name_lower = name.to_lowercase();
|
|
|
|
|
let _ = res.insert(name_lower, U::List(vec![U::Text(t), U::Text(val)]));
|
|
|
|
|
()
|
2022-02-07 16:49:59 +01:00
|
|
|
|
}
|
2021-02-06 22:33:39 +01:00
|
|
|
|
Some(U::List(mut l)) => {
|
|
|
|
|
let name_lower = name.to_lowercase();
|
|
|
|
|
l.push(U::Text(val));
|
|
|
|
|
let _ = res.insert(name_lower, U::List(l));
|
|
|
|
|
()
|
2022-02-07 16:49:59 +01:00
|
|
|
|
}
|
2021-02-06 22:33:39 +01:00
|
|
|
|
Some(o) => panic!("read-http: header not text nor list: {:?}", o),
|
|
|
|
|
}
|
2021-01-27 12:52:38 +01:00
|
|
|
|
}
|
|
|
|
|
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<R: Read>(buf: &mut Vec<u8>, reader: R) -> Option<()> {
|
2021-02-06 22:33:39 +01:00
|
|
|
|
let mut chonker = Chunkyboi::new(reader, 4096);
|
2021-01-27 12:52:38 +01:00
|
|
|
|
loop {
|
2021-02-06 22:33:39 +01:00
|
|
|
|
// TODO: attacker can send looooong input, set upper maximum
|
|
|
|
|
match chonker.next() {
|
2021-01-27 12:52:38 +01:00
|
|
|
|
Some(Ok(chunk)) => {
|
|
|
|
|
buf.extend_from_slice(&chunk);
|
2022-02-07 16:49:59 +01:00
|
|
|
|
if chunk.windows(4).any(|c| c == b"\r\n\r\n") {
|
2021-01-27 12:52:38 +01:00
|
|
|
|
return Some(());
|
|
|
|
|
}
|
2022-02-07 16:49:59 +01:00
|
|
|
|
}
|
|
|
|
|
Some(Err(err)) => {
|
|
|
|
|
die_temporary("read-http", format!("error reading from stdin: {:?}", err))
|
|
|
|
|
}
|
|
|
|
|
None => return None,
|
2021-01-27 12:52:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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<u8> = vec![];
|
|
|
|
|
match read_till_end_of_header(&mut buf, stdin.lock()) {
|
|
|
|
|
Some(()) => match req.parse(&buf) {
|
2022-02-07 16:49:59 +01:00
|
|
|
|
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),
|
|
|
|
|
),
|
2021-01-27 12:52:38 +01:00
|
|
|
|
},
|
2022-02-07 16:49:59 +01:00
|
|
|
|
None => die_expected_error(
|
|
|
|
|
"read-http",
|
|
|
|
|
format!("httparse end of stdin reached before able to parse request headers"),
|
|
|
|
|
),
|
2021-01-27 12:52:38 +01:00
|
|
|
|
}
|
|
|
|
|
let method = req.method.expect("method must be filled on complete parse");
|
|
|
|
|
let path = req.path.expect("path must be filled on complete parse");
|
2021-01-29 15:45:30 +01:00
|
|
|
|
write_dict_req(method, path, &normalize_headers(req.headers))
|
2022-02-07 16:49:59 +01:00
|
|
|
|
}
|
2021-01-27 12:52:38 +01:00
|
|
|
|
Response => {
|
|
|
|
|
let mut resp = httparse::Response::new(&mut headers);
|
|
|
|
|
let mut buf: Vec<u8> = vec![];
|
|
|
|
|
match read_till_end_of_header(&mut buf, stdin.lock()) {
|
|
|
|
|
Some(()) => match resp.parse(&buf) {
|
2022-02-07 16:49:59 +01:00
|
|
|
|
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),
|
|
|
|
|
),
|
2021-01-27 12:52:38 +01:00
|
|
|
|
},
|
2022-02-07 16:49:59 +01:00
|
|
|
|
None => die_expected_error(
|
|
|
|
|
"read-http",
|
|
|
|
|
format!("httparse end of stdin reached before able to parse response headers"),
|
|
|
|
|
),
|
2021-01-27 12:52:38 +01:00
|
|
|
|
}
|
|
|
|
|
let code = resp.code.expect("code must be filled on complete parse");
|
2022-02-07 16:49:59 +01:00
|
|
|
|
let reason = resp
|
|
|
|
|
.reason
|
|
|
|
|
.expect("reason must be filled on complete parse");
|
2021-01-29 15:45:30 +01:00
|
|
|
|
write_dict_resp(code, reason, &normalize_headers(resp.headers))
|
2021-01-27 12:52:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-07 16:49:59 +01:00
|
|
|
|
fn write_dict_req<'a, 'buf>(
|
|
|
|
|
method: &'buf str,
|
|
|
|
|
path: &'buf str,
|
|
|
|
|
headers: &'a HashMap<String, U<'a>>,
|
|
|
|
|
) -> std::io::Result<()> {
|
|
|
|
|
let mut http = vec![("method", U::Text(method)), ("path", U::Text(path))]
|
|
|
|
|
.into_iter()
|
|
|
|
|
.collect();
|
2021-01-27 12:52:38 +01:00
|
|
|
|
write_dict(http, headers)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-07 16:49:59 +01:00
|
|
|
|
fn write_dict_resp<'a, 'buf>(
|
|
|
|
|
code: u16,
|
|
|
|
|
reason: &'buf str,
|
|
|
|
|
headers: &'a HashMap<String, U<'a>>,
|
|
|
|
|
) -> std::io::Result<()> {
|
2021-01-27 12:52:38 +01:00
|
|
|
|
let mut http = vec![
|
|
|
|
|
("status", U::N6(code as u64)),
|
2021-02-01 09:16:14 +01:00
|
|
|
|
("status-text", U::Text(reason)),
|
2022-02-07 16:49:59 +01:00
|
|
|
|
]
|
|
|
|
|
.into_iter()
|
|
|
|
|
.collect();
|
2021-01-27 12:52:38 +01:00
|
|
|
|
write_dict(http, headers)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-07 16:49:59 +01:00
|
|
|
|
fn write_dict<'buf, 'a>(
|
|
|
|
|
mut http: HashMap<&str, U<'a>>,
|
|
|
|
|
headers: &'a HashMap<String, U<'a>>,
|
|
|
|
|
) -> std::io::Result<()> {
|
|
|
|
|
match http.insert(
|
|
|
|
|
"headers",
|
|
|
|
|
U::Record(
|
|
|
|
|
headers
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(k, v)| (k.as_str(), v.clone()))
|
|
|
|
|
.collect(),
|
|
|
|
|
),
|
|
|
|
|
) {
|
2021-02-06 22:33:39 +01:00
|
|
|
|
None => (),
|
|
|
|
|
Some(_) => panic!("read-http: headers already in dict"),
|
|
|
|
|
};
|
2022-02-07 16:49:59 +01:00
|
|
|
|
netencode::encode(&mut std::io::stdout(), &U::Record(http))?;
|
2021-01-27 12:52:38 +01:00
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// iter helper
|
2022-09-18 12:37:30 +02:00
|
|
|
|
// TODO: put into its own module
|
2021-01-27 12:52:38 +01:00
|
|
|
|
struct Chunkyboi<T> {
|
|
|
|
|
inner: T,
|
|
|
|
|
buf: Vec<u8>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<R: Read> Chunkyboi<R> {
|
|
|
|
|
fn new(inner: R, chunksize: usize) -> Self {
|
|
|
|
|
let buf = vec![0; chunksize];
|
2022-02-07 16:49:59 +01:00
|
|
|
|
Chunkyboi { inner, buf }
|
2021-01-27 12:52:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<R: Read> Iterator for Chunkyboi<R> {
|
|
|
|
|
type Item = std::io::Result<Vec<u8>>;
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<std::io::Result<Vec<u8>>> {
|
|
|
|
|
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()))
|
|
|
|
|
}
|
2022-02-07 16:49:59 +01:00
|
|
|
|
Err(err) => Some(Err(err)),
|
2021-01-27 12:52:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|