gdritter repos httputils / 1df44df
Some updated documentation + working on domain-matching Getty Ritter 8 years ago
7 changed file(s) with 49 addition(s) and 15 deletion(s). Collapse all Expand all
44 authors = ["Getty Ritter <gdritter@galois.com>"]
55
66 [dependencies]
7 hyper = "*"
7 hyper = "0.8.0"
3232 host: which host to forward to; defaults to "localhost"
3333 port: which port to forward to; defaults to "80"
3434 conf: which path to forward to; defaults to "/dev/null"
35 resp: which HTTP response to issue; defaults to 303
3536 ~~~
3637
3738 These are interpreted as follows:
4041 both of them default to accepting anything, and both of them
4142 allow their values to have the wildcard character `*`.
4243
43 - The `mode` field tells us _how_ to forward requests: right now,
44 it can only contain the values `http` or `aloys`. If the mode is
45 `http`, then Aloysius will forward the HTTP request to the server
46 listening on the host `host` and the port `port`. If the mode is
47 `aloys`, then Aloysius will recursively check the configuration
48 directory at `conf`.
44 - The `mode` field tells us _how_ to forward requests. There are
45 three possible forwarding modes:
46 - If the mode is `http`, then Aloysius will forward the HTTP
47 request to the server listening on the host `host` and the
48 port `port`.
49 - If the mode is `aloys`, then Aloysius will recursively check
50 the configuration directory at `conf`.
51 - If the mode is `redir`, then Aloysius will respond with an
52 HTTP response code as indicated in `resp` and redirect to
53 the host as indicated in `host`.
4954
5055 ## Example Setups
5156
1 /home/gdritter/projects/httputils/sample/secondary_conf
1 /home/gdritter/projects/personal/httputils/sample/secondary_conf
33
44 /// An `AloysError` is one of the errors that can occur in
55 /// the course of running an Aloys server. This might be
6 /// an `IOError` from
6 /// an `IOError` from the stdlib, a `HyperError` from
7 /// the hyper library, or an `AloysError` from us.
78 #[derive(Debug)]
89 pub enum AloysError {
910 IOError(std::io::Error),
1112 AloysError(String),
1213 }
1314
15 /// Convenience function for constructing an `AloysError`.
1416 pub fn aloys_error<A>(s: String) -> Result<A, AloysError> {
1517 Err(AloysError::AloysError(s))
1618 }
3535 }
3636 }
3737
38 pub fn path_for<'a>(req: &'a Request) -> Option<&'a str> {
38 /// Get the absolute path of a URI, if possible.
39 pub fn path_for<'a>(req: &'a Request) -> Option<String> {
3940 if let RequestUri::AbsolutePath(ref s) = req.uri {
40 Some(s)
41 Some(s.to_string())
42 } else if let RequestUri::AbsoluteUri(ref url) = req.uri {
43 url.serialize_path()
4144 } else {
4245 None
4346 }
4447 }
48
49 pub fn domain_for<'a>(req: &'a Request) -> Option<&'a str> {
50 if let RequestUri::AbsoluteUri(ref url) = req.uri {
51 url.domain()
52 } else {
53 None
54 }
55 }
1515
1616 use error::{AloysError,aloys_error};
1717 use matching::do_match;
18 use helper::{path_for,with_file};
18 use helper::{path_for,domain_for,with_file};
1919
2020 #[derive(Debug, Clone)]
2121 enum Delegate {
4343 }
4444
4545 impl Routes {
46 /// Take a path and load the route specifications contained there.
4647 fn load_paths(dir: &Path) -> Result<Routes,AloysError> {
4748 let mut routes = vec![];
4849 for entry in try!(read_dir(dir)) {
5253 Ok(Routes { routes: routes })
5354 }
5455
56 /// Given a request route it appropriately, or raise an error.
5557 fn dispatch(&self, req: &Request, res: Response<Fresh>) {
5658 match self.find_delegate(&req) {
5759 Some(delegate) => delegate.run(req, res),
6567 for d in self.routes.iter() {
6668 let mut matches = true;
6769 if let Some(ref d) = d.for_domain {
68 // matches = matches && do_match(d, domain_for(req));
70 if let Some(domain) = domain_for(req) {
71 matches = matches && do_match(d, domain);
72 }
6973 }
7074 if let Some(ref p) = d.for_path {
7175 if let Some(req_path) = path_for(req) {
72 matches = matches && do_match(p, req_path);
76 matches = matches && do_match(p, &req_path);
7377 } else {
7478 matches = false;
7579 }
7680 }
7781 if matches {
82 println!("using {:?}", d.conf_at);
7883 return Some(&d.delegate_to);
7984 }
8085 }
9095
9196 impl RouteSpec {
9297 fn from_path(dir: &Path) -> Result<RouteSpec,AloysError> {
98 println!("at dir {:?}", dir);
9399 let path = with_file(&dir.join("path"),
94100 None,
95101 |s| Some(s));
137143 &Delegate::Forward(ref fwd) => {
138144 let _ = res.send(format!("[forward to {:?}:{:?}]",
139145 fwd.domn,
140 fwd.port).as_bytes());
146 fwd.port,
147 ).as_bytes());
141148 },
142149 &Delegate::Defer(ref rts) => {
143150 rts.dispatch(req, res);
11 /// Find out whether the `spec_str` matches the `string_str`.
22 /// The former can contain the wildcard character `*`.
3 ///
4 /// # Examples
5 ///
6 /// ```
7 /// assert!(do_match("foo", "foo"));
8 /// assert!(do_match("*o", "foo"));
9 /// assert!(do_match("f*o", "foo"));
10 /// assert!(do_match("f*", "foo"));
11 /// ```
312 pub fn do_match(spec_str: &str, string_str: &str) -> bool {
413 let spec = spec_str.as_bytes();
514 let string = string_str.as_bytes();