From 7562961093e2998788fa7595c4a079647291c74b Mon Sep 17 00:00:00 2001 From: Magnus Hoff Date: Sun, 1 Oct 2017 23:24:16 +0200 Subject: [PATCH] Percent decode path segments of URL --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 1 + src/wiki_lookup.rs | 9 +++++++-- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2181ba5..8ff3b38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -580,6 +580,7 @@ dependencies = [ "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "libsqlite3-sys 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", "num 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "percent-encoding 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "pulldown-cmark 0.0.11 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index d0b15e3..8d1d24d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ chrono = "0.4" clap = "2.26" bart = "0.1.4" bart_derive = "0.1.4" +percent-encoding = "1.0.0" [dependencies.libsqlite3-sys] features = ["bundled"] diff --git a/src/main.rs b/src/main.rs index 86544d2..dafed6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ extern crate clap; extern crate futures; extern crate futures_cpupool; extern crate hyper; +extern crate percent_encoding; extern crate pulldown_cmark; extern crate r2d2; extern crate r2d2_diesel; diff --git a/src/wiki_lookup.rs b/src/wiki_lookup.rs index 10aeecc..55d58a9 100644 --- a/src/wiki_lookup.rs +++ b/src/wiki_lookup.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; -use futures::{Future, finished}; +use futures::{Future, finished, failed}; +use percent_encoding::percent_decode; use assets::*; use article_resource::ArticleResource; @@ -66,7 +67,11 @@ impl Lookup for WikiLookup { let mut split = path[1..].split('/'); - let slug = split.next().expect("Always at least one element").to_owned(); + let slug = split.next().expect("Always at least one element"); + let slug = match percent_decode(slug.as_bytes()).decode_utf8() { + Ok(x) => x, + Err(x) => return Box::new(failed(x.into())) + }.to_string(); if split.next() != None { // Currently disallow any URLs of the form /slug/...