From 04a0c8e9883b480e568a24cfb6a05e1bbcf3c0bd Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Mon, 27 Jul 2020 21:03:50 -0400 Subject: [PATCH] more helpers --- site/src/main.rs | 21 +++++++++++++++------ src/response.rs | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/site/src/main.rs b/site/src/main.rs index cf0667d..9f28dfa 100644 --- a/site/src/main.rs +++ b/site/src/main.rs @@ -5,7 +5,9 @@ use maj::{ split, Response, }; use rustls::internal::pemfile::{certs, rsa_private_keys}; -use rustls::{Certificate, NoClientAuth, PrivateKey, ServerConfig}; +use rustls::{ + AllowAnyAnonymousOrAuthenticatedClient, Certificate, PrivateKey, RootCertStore, ServerConfig, +}; use std::fs::File; use std::io::{self, BufReader}; use std::path::{Path, PathBuf}; @@ -62,7 +64,9 @@ fn main() -> Result<(), maj::server::Error> { opts.port ); - let mut config = ServerConfig::new(NoClientAuth::new()); + let mut config = ServerConfig::new(AllowAnyAnonymousOrAuthenticatedClient::new( + RootCertStore::empty(), + )); config .set_single_cert(certs, keys.remove(0)) .map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?; @@ -83,16 +87,20 @@ struct Handler { hostname: String, } -fn index(_req: Request) -> Result { +async fn index() -> Result { let msg = include_bytes!("index.gmi"); Ok(Response::gemini(msg.to_vec())) } -fn majc(_req: Request) -> Result { +async fn majc() -> Result { let msg = include_bytes!("majc.gmi"); Ok(Response::gemini(msg.to_vec())) } +async fn need_cert() -> Result { + Ok(Response::need_cert("test")) +} + #[async_trait::async_trait] impl MajHandler for Handler { async fn handle(&self, req: Request) -> Result { @@ -108,8 +116,9 @@ impl MajHandler for Handler { } route!(req.url.path(), { - (/) => index(req); - (/"majc") => majc(req); + (/) => index().await; + (/"cert") => need_cert().await; + (/"majc") => majc().await; }); Ok(Response::not_found()) diff --git a/src/response.rs b/src/response.rs index d70409e..61c4fcb 100644 --- a/src/response.rs +++ b/src/response.rs @@ -53,6 +53,22 @@ impl Response { body: vec![], } } + + pub fn input>(msg: T) -> Response { + Response { + status: StatusCode::Input, + meta: msg.into(), + body: vec![], + } + } + + pub fn need_cert>(msg: T) -> Response { + Response { + status: StatusCode::ClientCertificateRequired, + meta: msg.into(), + body: vec![], + } + } } /// The parser state.