chiark / gitweb /
response plumbing
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 16 Aug 2021 00:41:03 +0000 (01:41 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 16 Aug 2021 00:41:03 +0000 (01:41 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bin/server.rs

index 73f2de5495639841d3525e59bf39767a6aa7efae..15b5161c9203df0e7a6ea4d6579d4c14edf1727c 100644 (file)
@@ -46,7 +46,7 @@ struct WebResponse {
   data: Result<WebResponseData, AE>,
 }
 
   data: Result<WebResponseData, AE>,
 }
 
-type WebResponseData = ();
+type WebResponseData = Vec<u8>;
 
 #[throws(PacketError)]
 pub fn route_packet(packet: Box<[u8]>, daddr: IpAddr) {
 
 #[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(
 }
 
 async fn handle(
+  conn: Arc<String>,
   all_clients: Arc<AllClients>,
   req: hyper::Request<hyper::Body>
   all_clients: Arc<AllClients>,
   req: hyper::Request<hyper::Body>
-) -> Result<hyper::Response<hyper::Body>, Void> {
+) -> Result<hyper::Response<hyper::Body>, hyper::http::Error> {
   if req.method() == Method::GET {
     let mut resp = hyper::Response::new(hyper::Body::from("hippotat\r\n"));
     resp.headers_mut().insert(
   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();
 
 
   let mut warnings: Warnings = default();
 
-  match async {
+  async {
 
     let get_header = |hn: &str| {
       let mut values = req.headers().get_all(hn).iter();
 
     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];
 
     })().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
 
     // 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) {
     //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();
     }
 
     let client = client.unwrap();
@@ -206,19 +215,30 @@ async fn handle(
 
     let reply: WebResponse = reply_recv.await?;
     warnings = reply.warnings;
 
     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
 }
 
 #[allow(unused_variables)] // xxx
@@ -259,7 +279,7 @@ async fn run_client(ic: Arc<InstanceConfig>,
       }
     } {
       let response = WebResponse {
       }
     } {
       let response = WebResponse {
-        data: Ok(()),
+        data: Ok(vec![ /* xxx */ ]),
         warnings: default(),
       };
 
         warnings: default(),
       };
 
@@ -410,10 +430,11 @@ async fn main() {
 
     for addr in &global.addrs {
       let all_clients_ = all_clients.clone();
 
     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 all_clients_ = all_clients_.clone();
+        let conn = Arc::new(format!("[conn:{:?}]", conn));
         async { Ok::<_, Void>( hyper::service::service_fn(move |req| {
         async { Ok::<_, Void>( hyper::service::service_fn(move |req| {
-          handle(all_clients_.clone(), req)
+          handle(conn.clone(), all_clients_.clone(), req)
         }) ) } }
       );
 
         }) ) } }
       );