Use percent_encode and decode to allow accents in paths

Fixes #1
This commit is contained in:
Thomas Forgione 2018-11-13 10:46:50 +01:00
parent f395b82680
commit f65602fb71
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
4 changed files with 35 additions and 17 deletions

View File

@ -7,6 +7,7 @@ authors = ["Thomas Forgione <thomas@forgione.fr>"]
dirs = "1.0.4"
notify-rust = "3.4.2"
colored = "1.6.1"
percent-encoding = "1.0.1"
hyper = "0.12.10"
[[bin]]

View File

@ -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::<hyper::Uri>().unwrap();
rt::run(fetch_url(url));
}
fn fetch_url(url: hyper::Uri) -> impl Future<Item=(), Error=()> {
fn fetch_url(uri: hyper::Uri) -> impl Future<Item=(), Error=()> {
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);
})
}

View File

@ -91,6 +91,19 @@ pub fn destroy<T, E>(result: Result<T, E>) -> Result<(), ()> {
}
}
/// Converts a string to a pair (PathBuf, Vec<String>).
pub fn builder_arguments_from_string(uri: &str) -> (PathBuf, Vec<String>) {
let split = uri.split('?').collect::<Vec<_>>();
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::<Vec<_>>()
} else {
vec![]
};
let result = build(&PathBuf::from(path), &args, &self.builders);
pub fn build(&self, path: &PathBuf, args: &Vec<String>) -> Result<(), Error> {
let result = build(path, args, &self.builders);
match result {
Ok(_) => self.notify_success(),

View File

@ -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());
},