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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
use core::panic;
use std::{net::SocketAddr, pin::Pin};
use confindent::Confindent;
use http_body_util::{BodyExt, Full};
use hyper::{
Request, Response, StatusCode,
body::{Body, Bytes, Incoming},
server::conn::http1,
service::Service,
};
use hyper_util::rt::TokioIo;
use tokio::{net::TcpListener, process::Command, runtime::Runtime};
#[derive(Clone, Debug)]
pub struct Settings {
script_filename: String,
env: Vec<(String, String)>,
}
const CONF_DEFAULT: &str = "/etc/corgi.conf";
fn main() {
let conf_path = std::env::args().nth(1).unwrap_or(String::from(CONF_DEFAULT));
let conf = Confindent::from_file(conf_path).expect("failed to open conf");
let script = conf.child("Script").expect("no 'Script' key in conf");
let settings = Settings {
script_filename: script.value_owned().expect("'Script' key has no value'"),
env: vec![],
};
let rt = Runtime::new().unwrap();
rt.block_on(async { run(settings).await });
}
async fn run(settings: Settings) {
let addr = SocketAddr::from(([0, 0, 0, 0], 2562));
let listen = TcpListener::bind(addr).await.unwrap();
let svc = Svc { settings };
loop {
let (stream, _caddr) = listen.accept().await.unwrap();
let io = TokioIo::new(stream);
let svc_clone = svc.clone();
tokio::task::spawn(
async move { http1::Builder::new().serve_connection(io, svc_clone).await },
);
}
}
#[derive(Clone, Debug)]
struct Svc {
settings: Settings,
}
impl Service<Request<Incoming>> for Svc {
type Response = Response<Full<Bytes>>;
type Error = hyper::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn call(&self, req: Request<Incoming>) -> Self::Future {
fn make<B: Into<Bytes>>(b: B) -> Result<Response<Full<Bytes>>, hyper::Error> {
Ok(Response::builder().body(Full::new(b.into())).unwrap())
}
let settings = self.settings.clone();
Box::pin(async { Ok(Self::handle(settings, req).await) })
}
}
impl Svc {
async fn handle(settings: Settings, req: Request<Incoming>) -> Response<Full<Bytes>> {
let method = req.method().as_str().to_ascii_uppercase();
let path = req.uri().path().to_owned();
let headers = req.headers().clone();
let body = req.into_body().collect().await.unwrap().to_bytes();
let content_length = body.len();
let mut cmd = Command::new(&settings.script_filename);
cmd.env("SCRIPT_NAME", settings.script_filename)
.env("PATH_INFO", path)
.env("REQUEST_METHOD", method);
for (header, value) in headers {
if let Some(header) = header {
let hname = header.as_str();
let hvalue = value.to_str().unwrap();
cmd.env(hname.to_ascii_uppercase(), hvalue);
if hname.to_ascii_lowercase() == "user-agent" {
println!("USER_AGENT: {hvalue}");
}
}
}
/*if content_length > 0 {
cmd.env("CONTENT_LENGTH", content_length.to_string());
}*/
let output = cmd.output().await.unwrap();
let response_raw = output.stdout;
let mut response = Response::builder();
println!("{}", String::from_utf8_lossy(&response_raw));
let mut curr = response_raw.as_slice();
let mut status = None;
let mut headers = vec![];
let body = loop {
let nl = curr.iter().position(|b| *b == b'\n').expect("no nl in header");
let line = &curr[..nl];
let colon = line.iter().position(|b| *b == b':').expect("no colon in header");
let key = &line[..colon];
let value = &line[colon + 1..];
headers.push((key, value));
let key_string = String::from_utf8_lossy(key);
if key_string == "Status" {
let value_string = String::from_utf8_lossy(value);
if let Some((raw_code, raw_msg)) = value_string.trim().split_once(' ') {
let code: usize = raw_code.parse().unwrap();
status = Some((code, raw_msg.to_owned()));
}
}
// Body next
if curr[nl + 1] == b'\n' || (curr[nl + 1] == b'\r' && curr[nl + 2] == b'\n') {
break &curr[nl + 2..];
} else {
curr = &curr[nl + 1..];
}
};
match status {
None => response = response.status(StatusCode::OK),
Some((code, _status)) => {
response = response.status(StatusCode::from_u16(code as u16).unwrap());
}
}
for (key, value) in headers {
response = response.header(key.to_vec(), value.to_vec());
}
response.body(Full::new(Bytes::from(body.to_vec()))).unwrap()
}
fn make<B: Into<Bytes>>(b: B) -> Response<Full<Bytes>> {
Response::builder().body(Full::new(b.into())).unwrap()
}
}
|