gdritter repos httputils / e79eabc
Split project into smaller chunks Getty Ritter 8 years ago
5 changed file(s) with 80 addition(s) and 70 deletion(s). Collapse all Expand all
1 extern crate hyper;
2 extern crate std;
3
4 #[derive(Debug)]
5 pub enum AloysError {
6 IOError(std::io::Error),
7 HyperError(hyper::error::Error),
8 AloysError(String),
9 }
10
11 pub fn aloys_error<A>(s: String) -> Result<A, AloysError> {
12 Err(AloysError::AloysError(s))
13 }
14
15 impl From<std::io::Error> for AloysError {
16 fn from(e: std::io::Error) -> AloysError {
17 AloysError::IOError(e)
18 }
19 }
20
21 impl From<hyper::error::Error> for AloysError {
22 fn from(e: hyper::error::Error) -> AloysError {
23 AloysError::HyperError(e)
24 }
25 }
1 use std::fs::File;
2 use std::io::prelude::Read;
3 use std::path::Path;
4
5 pub fn with_file_contents<A, F>(path: &Path, default: A, callback: F) -> A
6 where F: FnOnce(String) -> A + 'static {
7 match File::open(path) {
8 Ok(mut f) => {
9 let mut s = String::new();
10 match f.read_to_string(&mut s) {
11 Ok(_) => callback(s),
12 Err(_) => default,
13 }
14 },
15 Err(_) => default,
16 }
17 }
11 extern crate hyper;
2
3 mod error;
4 mod helper;
5 mod matching;
26
37 use hyper::Server;
48 use hyper::server::{Handler,Request,Response};
59 use hyper::net::Fresh;
610
711 use std::env::{args,current_dir,set_current_dir};
8 use std::fs::{File,read_dir};
9 use std::io::prelude::Read;
12 use std::fs::{read_dir};
1013 use std::iter::Iterator;
1114 use std::path::Path;
1215
13 mod matching;
14
15 #[derive(Debug)]
16 enum AloysError {
17 IOError(std::io::Error),
18 HyperError(hyper::error::Error),
19 }
20
21 impl From<std::io::Error> for AloysError {
22 fn from(e: std::io::Error) -> AloysError {
23 AloysError::IOError(e)
24 }
25 }
26
27 impl From<hyper::error::Error> for AloysError {
28 fn from(e: hyper::error::Error) -> AloysError {
29 AloysError::HyperError(e)
30 }
31 }
16 use error::{AloysError,aloys_error};
17 use helper::with_file_contents;
3218
3319 #[derive(Debug, Clone)]
3420 enum Delegate {
5440 routes: Vec<RouteSpec>,
5541 }
5642
57 fn with_file_contents<A, F>(path: &Path, default: A, callback: F) -> A
58 where F: FnOnce(String) -> A + 'static {
59 match File::open(path) {
60 Ok(mut f) => {
61 let mut s = String::new();
62 match f.read_to_string(&mut s) {
63 Ok(_) => callback(s),
64 Err(_) => default,
65 }
66 },
67 Err(_) => default,
68 }
69 }
70
71 impl RouteSpec {
72 fn from_path(dir: &Path) -> Result<RouteSpec,AloysError> {
73 let path = with_file_contents(&dir.join("path"), None, |s| Some(s));
74 let domain = with_file_contents(&dir.join("domain"), None, |s| Some(s));
75 let mode = with_file_contents(&dir.join("mode"), "http", |s| s);
76 if mode == "http" {
77 } else if mode == "alyos" {
78 let fwd = with_file_contents(&dir.join("conf"),
79 Path::new(&"/dev/null"),
80 |s| Path::new(&s));
81 let rts = try!(Routes::load_paths(fwd));
82 Ok(RouteSpec { for_path: path,
83 for_domain: domain,
84 delegate_to: Delegate::
85 } else {
86
87 }
88 // let forPath = match File::open(dir.join("path")) {
89 // Ok(f) => {};
90 // Err
91 // };
92 Ok(RouteSpec { for_path: None,
93 for_domain: None,
94 delegate_to: Delegate::Defer(Routes { routes: vec![] }),
95 })
96 }
97 }
98
9943 impl Routes {
10044 fn load_paths(dir: &Path) -> Result<Routes,AloysError> {
10145 let mut routes = vec![];
11761
11862 fn find_delegate(&self, req: &Request) -> Option<&Delegate> {
11963 for d in self.routes.iter() {
64 let mut matches = true;
12065 if let Some(ref d) = d.for_domain {
12166 println!("Matching against {:?}", d);
12267 }
13277 impl Handler for Routes {
13378 fn handle(&self, req: Request, res: Response<Fresh>) {
13479 self.dispatch(&req, res);
80 }
81 }
82
83 impl RouteSpec {
84 fn from_path(dir: &Path) -> Result<RouteSpec,AloysError> {
85 let path = with_file_contents(&dir.join("path"), None, |s| Some(s));
86 let domain = with_file_contents(&dir.join("domain"), None, |s| Some(s));
87 let mode = with_file_contents(&dir.join("mode"), "http".to_string(), |s| s);
88 if mode == "http" {
89 aloys_error("Unimplemented".to_string())
90 } else if mode == "alyos" {
91 let fwd = with_file_contents(&dir.join("conf"),
92 "/dev/null".to_string(),
93 |s| s);
94 let rts = try!(Routes::load_paths(Path::new(&fwd)));
95 Ok(RouteSpec { for_path: path,
96 for_domain: domain,
97 delegate_to: Delegate::Defer(rts) })
98 } else {
99 aloys_error(format!("Unknown mode `{:?}` in `{:?}", mode, dir))
100 }
135101 }
136102 }
137103
1919
2020 #[test]
2121 fn test_match() {
22 assert!(do_match("foo", "foo"));
23 assert!(do_match("*oo", "foo"));
24 assert!(do_match("f*o", "foo"));
25 assert!(do_match("fo*", "foo"));
26 assert!(do_match("f*", "foo"));
27 assert!(do_match("*o", "foo"));
28 assert!(do_match("*", "foo"));
22 assert!(do_match("foo", "foo"));
23 assert!(do_match("*oo", "foo"));
24 assert!(do_match("f*o", "foo"));
25 assert!(do_match("fo*", "foo"));
26 assert!(do_match("f*", "foo"));
27 assert!(do_match("*o", "foo"));
28 assert!(do_match("*", "foo"));
2929 assert!(!do_match("foo", "bar"));
30 assert!(!do_match("f*", "bar"));
3031 }