From: Ian Jackson Date: Mon, 16 Aug 2021 00:41:03 +0000 (+0100) Subject: response plumbing X-Git-Tag: hippotat/1.0.0~147 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=38012115b303b610ef672a12119c41b48fb3b396;p=hippotat.git response plumbing Signed-off-by: Ian Jackson --- diff --git a/src/bin/server.rs b/src/bin/server.rs index 73f2de5..15b5161 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -46,7 +46,7 @@ struct WebResponse { data: Result, } -type WebResponseData = (); +type WebResponseData = Vec; #[throws(PacketError)] pub fn route_packet(packet: Box<[u8]>, daddr: IpAddr) { @@ -54,9 +54,10 @@ pub fn route_packet(packet: Box<[u8]>, daddr: IpAddr) { } async fn handle( + conn: Arc, all_clients: Arc, req: hyper::Request -) -> Result, Void> { +) -> Result, hyper::http::Error> { if req.method() == Method::GET { let mut resp = hyper::Response::new(hyper::Body::from("hippotat\r\n")); resp.headers_mut().insert( @@ -68,7 +69,7 @@ async fn handle( let mut warnings: Warnings = default(); - match async { + async { let get_header = |hn: &str| { let mut values = req.headers().get_all(hn).iter(); @@ -150,7 +151,8 @@ async fn handle( })().context("token")?; let hmac_got = &hmac_got[0..hmac_got_l]; - let client = all_clients.get(&client); + let client_name = client; + let client = all_clients.get(&client_name); // We attempt to hide whether the client exists we don't try to // hide the hash lookup computationgs, but we do try to hide the @@ -167,7 +169,14 @@ async fn handle( //dbg!(DumpHex(&hmac_exp), client.is_some()); //dbg!(DumpHex(hmac_got), hmac_ok, client_exists); if ! bool::from(hmac_ok & client_exists) { - throw!(anyhow!("xxx should be a 403 error")); + debug!("{} rejected client {}", &conn, &client_name); + let body = hyper::Body::from("Not authorised\r\n"); + return Ok( + hyper::Response::builder() + .status(hyper::StatusCode::FORBIDDEN) + .header("Content-Type", r#"text/plain; charset="utf-8""#) + .body(body) + ) } let client = client.unwrap(); @@ -206,19 +215,30 @@ async fn handle( let reply: WebResponse = reply_recv.await?; warnings = reply.warnings; + let data = reply.data?; - reply.data - }.await { - Ok(()) => { - }, - Err(e) => { - eprintln!("error={}", e); + if ! warnings.warnings.is_empty() + && log::log_enabled!(log::Level::Trace) { + trace!("{} {}: warnings {:?}", &conn, &client_name, &warnings.warnings); } - } - - eprintln!("warnings={:?}", &warnings); - Ok(hyper::Response::new(hyper::Body::from("Hello World"))) + let data = hyper::Body::from(data); + Ok::<_,AE>( + hyper::Response::builder() + .header("Content-Type", r#"application/octet-stream"#) + .body(data) + ) + }.await.unwrap_or_else(|e| { + debug!("{} error {}", &conn, &e); + let mut errmsg = format!("ERROR\n\n{:?}\n\n", &e); + for w in warnings.warnings { + write!(errmsg, "warning: {}\n", w).unwrap(); + } + hyper::Response::builder() + .status(hyper::StatusCode::BAD_REQUEST) + .header("Content-Type", r#"text/plain; charset="utf-8""#) + .body(errmsg.into()) + }) } #[allow(unused_variables)] // xxx @@ -259,7 +279,7 @@ async fn run_client(ic: Arc, } } { let response = WebResponse { - data: Ok(()), + data: Ok(vec![ /* xxx */ ]), warnings: default(), }; @@ -410,10 +430,11 @@ async fn main() { for addr in &global.addrs { let all_clients_ = all_clients.clone(); - let make_service = hyper::service::make_service_fn(move |_conn| { + let make_service = hyper::service::make_service_fn(move |conn| { let all_clients_ = all_clients_.clone(); + let conn = Arc::new(format!("[conn:{:?}]", conn)); async { Ok::<_, Void>( hyper::service::service_fn(move |req| { - handle(all_clients_.clone(), req) + handle(conn.clone(), all_clients_.clone(), req) }) ) } } );