gdritter repos httputils / b9afbad
Successfully reading configuration Getty Ritter 9 years ago
3 changed file(s) with 67 addition(s) and 23 deletion(s). Collapse all Expand all
1 extern crate hyper;
2
3 use hyper::server::Request;
4 use hyper::uri::RequestUri;
5
16 use std::fs::File;
27 use std::io::prelude::Read;
38 use std::path::Path;
49
5 pub fn with_file_contents<A, F>(path: &Path, default: A, callback: F) -> A
10 pub fn with_file<A, F>(path: &Path, default: A, callback: F) -> A
611 where F: FnOnce(String) -> A + 'static {
712 match File::open(path) {
813 Ok(mut f) => {
1520 Err(_) => default,
1621 }
1722 }
23
24 pub fn path_for<'a>(req: &'a Request) -> Option<&'a str> {
25 if let RequestUri::AbsolutePath(ref s) = req.uri {
26 Some(s)
27 } else {
28 None
29 }
30 }
1414 use std::path::Path;
1515
1616 use error::{AloysError,aloys_error};
17 use helper::with_file_contents;
17 use matching::do_match;
18 use helper::{path_for,with_file};
1819
1920 #[derive(Debug, Clone)]
2021 enum Delegate {
3031
3132 #[derive(Debug, Clone)]
3233 struct RouteSpec {
34 conf_at: String,
3335 for_path: Option<String>,
3436 for_domain: Option<String>,
3537 delegate_to: Delegate,
6365 for d in self.routes.iter() {
6466 let mut matches = true;
6567 if let Some(ref d) = d.for_domain {
66 println!("Matching against {:?}", d);
68 // matches = matches && do_match(d, domain_for(req));
6769 }
6870 if let Some(ref p) = d.for_path {
69 println!("Matching against {:?}", p);
71 if let Some(req_path) = path_for(req) {
72 matches = matches && do_match(p, req_path);
73 } else {
74 matches = false;
75 }
7076 }
71 return Some(&d.delegate_to);
77 if matches {
78 return Some(&d.delegate_to);
79 }
7280 }
7381 return None;
7482 }
8290
8391 impl RouteSpec {
8492 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 }
93 let path = with_file(&dir.join("path"),
94 None,
95 |s| Some(s));
96 let domain = with_file(&dir.join("domain"),
97 None,
98 |s| Some(s));
99 let mode = with_file(&dir.join("mode"),
100 "http".to_string(),
101 |s| s);
102 let delegate =
103 if mode == "http" {
104 let domn = with_file(&dir.join("host"),
105 "localhost".to_string(),
106 |s| s);
107 let port = with_file(&dir.join("port"),
108 Ok(80),
109 |s| s.trim().parse::<u32>() );
110 let port_num = match port {
111 Ok(n) => n,
112 Err(e) => return aloys_error(format!("Bad port number in `{:?}: {:?}`", dir, e)),
113 };
114 Delegate::Forward(HTTPForward { domn: domn,
115 port: port_num })
116 } else if mode == "aloys" {
117 let fwd = with_file(&dir.join("conf"),
118 "/dev/null".to_string(),
119 |s| s);
120 let rts = try!(Routes::load_paths(Path::new(&fwd)));
121 Delegate::Defer(rts)
122 } else {
123 return aloys_error(format!("Unknown mode `{:?}` in `{:?}", mode, dir))
124 };
125 Ok(RouteSpec {
126 conf_at: dir.to_string_lossy().to_string(),
127 for_path: path,
128 for_domain: domain,
129 delegate_to: delegate
130 })
101131 }
102132 }
103133
116146
117147
118148 fn run_server() -> Result<(), AloysError> {
119 if let Some(target_dir) = args().next() {
149 if let Some(target_dir) = args().skip(1).next() {
120150 println!("switching to directory {:}", target_dir);
121151 try!(set_current_dir(Path::new(&target_dir)));
122152 }
123153 let cwd = try!(current_dir());
124154 let paths = try!(Routes::load_paths(cwd.as_path()));
155 println!("Read routes: {:?}", paths);
125156 let srv = try!(Server::http("127.0.0.1:8080"));
126157 let _ = srv.handle(paths);
127158 Ok(())
55 while let Some((i, j)) = stack.pop() {
66 if i >= spec.len() && j >= spec.len() {
77 return true;
8 } else if i >= spec.len() || j >= spec.len() {
8 } else if i >= spec.len() || j >= string_str.len() {
99 continue;
1010 } else if spec[i] == 0x2a {
1111 stack.push((i, j+1));