diff --git a/Cargo.toml b/Cargo.toml index fab5225..1b99976 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ authors = ["Thomas Forgione "] dirs = "1.0.4" notify-rust = "3.4.2" colored = "1.6.1" +percent-encoding = "1.0.1" hyper = "0.12.10" [[bin]] diff --git a/src/client.rs b/src/client.rs index 1007403..d054093 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,4 +1,5 @@ extern crate colored; +extern crate percent_encoding; extern crate hyper; extern crate mars; @@ -7,10 +8,12 @@ use std::process::exit; use colored::*; +use percent_encoding::{percent_decode, percent_encode, DEFAULT_ENCODE_SET}; + use hyper::Client; use hyper::rt::{self, Future}; -use mars::GeneralBuilder; +use mars::{GeneralBuilder, builder_arguments_from_string}; fn main() { @@ -33,19 +36,22 @@ fn main() { format!("http://localhost:1500{}?{}", current_dir, args) }; + let url = percent_encode(url.as_bytes(), DEFAULT_ENCODE_SET).to_string(); let url = url.parse::().unwrap(); rt::run(fetch_url(url)); } -fn fetch_url(url: hyper::Uri) -> impl Future { +fn fetch_url(uri: hyper::Uri) -> impl Future { let client = Client::new(); - client.get(url.clone()) + client.get(uri.clone()) .map(|_| ()) // If there was an error, build in client .map_err(move |_| { eprintln!("{}", "Server not listening, building in client...".bold().yellow()); let builder = GeneralBuilder::new(); - let _ = builder.build(&url); + let uri = percent_decode(uri.path().as_bytes()).decode_utf8().unwrap(); + let (path, args) = builder_arguments_from_string(&*uri); + let _ = builder.build(&path, &args); }) } diff --git a/src/lib.rs b/src/lib.rs index 37176bd..3c8ee83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,6 +91,19 @@ pub fn destroy(result: Result) -> Result<(), ()> { } } +/// Converts a string to a pair (PathBuf, Vec). +pub fn builder_arguments_from_string(uri: &str) -> (PathBuf, Vec) { + let split = uri.split('?').collect::>(); + let path = PathBuf::from(split[0]); + let args = if split.len() > 1 { + split[1].split('&').map(|x| x.to_owned()).collect() + } else { + vec![] + }; + + (path, args) +} + /// A general builders that contains many builders and can build any type of code. pub struct GeneralBuilder { /// The path to the success icon. @@ -150,16 +163,8 @@ impl GeneralBuilder { } /// Triggers a build. - pub fn build(&self, uri: &hyper::Uri) -> Result<(), Error> { - let path = uri.path(); - - let args = if let Some(query) = uri.query() { - query.split("&").map(|x| x.to_owned()).collect::>() - } else { - vec![] - }; - - let result = build(&PathBuf::from(path), &args, &self.builders); + pub fn build(&self, path: &PathBuf, args: &Vec) -> Result<(), Error> { + let result = build(path, args, &self.builders); match result { Ok(_) => self.notify_success(), diff --git a/src/server.rs b/src/server.rs index 683b08f..377f5fb 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,4 +1,5 @@ extern crate colored; +extern crate percent_encoding; extern crate hyper; extern crate mars; @@ -7,11 +8,13 @@ use std::sync::{Mutex, Arc, mpsc}; use colored::*; +use percent_encoding::percent_decode; + use hyper::{Body, Response, Server}; use hyper::service::service_fn_ok; use hyper::rt::Future; -use mars::GeneralBuilder; +use mars::{GeneralBuilder, builder_arguments_from_string}; fn main() { @@ -50,8 +53,11 @@ fn main() { while let Some(uri) = tasks.pop() { - println!("{}", format!("---- STARTING BUILD ---- from {}", uri.path()).bold().green()); - match builder.build(&uri) { + let uri = percent_decode(uri.path().as_bytes()).decode_utf8().unwrap(); + let (path, args) = builder_arguments_from_string(&*uri); + + println!("{}", format!("---- STARTING BUILD ---- from {}", uri).bold().green()); + match builder.build(&path, &args) { Err(_) => { println!("{}", "--------- FAIL ---------".bold().red()); },