From 177d12b5b89f97039a5849d131b3524eecb3bf57 Mon Sep 17 00:00:00 2001 From: Jan Wolff Date: Sat, 16 May 2020 12:47:42 +0200 Subject: [PATCH] some error codes in handler --- src/main.rs | 4 +++- src/server/handler.rs | 14 ++++++++++++-- src/server/mod.rs | 13 +++++++++---- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index df4f4d2..59b6d00 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ mod server; +use std::path::Path; fn main() { - let server = server::Server::new("gemini://localhost"); + let wwwRoot = Path::new("/var/www/gemini/"); + let server = server::Server::new("gemini://localhost", &wwwRoot); server.serve(); } diff --git a/src/server/handler.rs b/src/server/handler.rs index fb07a4d..ceb6551 100644 --- a/src/server/handler.rs +++ b/src/server/handler.rs @@ -1,7 +1,17 @@ -use url::Url; use crate::server::response; use crate::server::ServerConfig; +use std::path::Path; +use url::Url; pub fn handle(config: &ServerConfig, url: Url) -> Option { - Some(response::invalid_protocol()) + if url.scheme() != "gemini" { + return Some(response::invalid_protocol()); + } + + let path = Path::new(url.path()); + if !path.has_root() { + return Some(response::not_understood()); + } + + None } diff --git a/src/server/mod.rs b/src/server/mod.rs index 5b7aedb..d7826f6 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -2,17 +2,19 @@ use crate::server::response::Response; use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod, SslStream}; use std::collections::HashMap; use std::net::{SocketAddr, TcpListener, TcpStream}; +use std::path::{Path, PathBuf}; +use std::sync::Arc; use std::thread; use std::vec::Vec; -use std::sync::Arc; use url::Url; -pub mod response; pub mod handler; +pub mod response; #[derive(Clone)] pub struct ServerConfig { defaultHost: Url, + wwwRoot: PathBuf, } pub struct Server { @@ -21,8 +23,11 @@ pub struct Server { } impl Server { - pub fn new(host: &str) -> Server { - let config = ServerConfig{defaultHost: Url::parse(host).unwrap()}; + pub fn new(host: &str, wwwRoot: &Path) -> Server { + let config = ServerConfig { + defaultHost: Url::parse(host).unwrap(), + wwwRoot: PathBuf::from(wwwRoot), + }; Server { acceptor: build_acceptor(),