about summary refs log tree commit diff
path: root/parrot/src/main.rs
blob: d3fe60fce713cbf3387e4fb25a53c8bf29a31449 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::{
	env,
	fs::File,
	io::{BufWriter, Read, Write},
};

fn main() {
	println!("Content-Type: text/plain\n");

	let mut bw = BufWriter::new(File::create("/tmp/parrot-debug").unwrap());

	for (key, value) in env::vars() {
		let log = format!("{key}: {value}\n");
		print!("{log}");
		bw.write_all(log.as_bytes()).unwrap();
	}

	let mut buf = vec![];
	std::io::stdin().read_to_end(&mut buf).unwrap();

	match std::str::from_utf8(&buf) {
		Err(_e) => {
			let log = format!("\n(binary data; {} bytes)\n", buf.len());
			print!("{log}");
			bw.write_all(log.as_bytes()).unwrap();
		}
		Ok(s) => {
			let log = format!("\n{s}\n");
			print!("{log}");
			bw.write_all(log.as_bytes()).unwrap();
		}
	}
}