proper url decoding, better hop resolution
This commit is contained in:
parent
23e4039b31
commit
165875b31c
3 changed files with 68 additions and 47 deletions
11
Cargo.lock
generated
11
Cargo.lock
generated
|
@ -363,6 +363,8 @@ dependencies = [
|
||||||
"actix-web 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
"actix-web 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"handlebars 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"handlebars 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"hotwatch 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"hotwatch 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"serde_yaml 0.8.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde_yaml 0.8.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
@ -778,6 +780,14 @@ dependencies = [
|
||||||
"winreg 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"winreg 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itertools"
|
||||||
|
version = "0.8.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itoa"
|
name = "itoa"
|
||||||
version = "0.4.4"
|
version = "0.4.4"
|
||||||
|
@ -1894,6 +1904,7 @@ dependencies = [
|
||||||
"checksum inotify-sys 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e74a1aa87c59aeff6ef2cc2fa62d41bc43f54952f55652656b18a02fd5e356c0"
|
"checksum inotify-sys 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e74a1aa87c59aeff6ef2cc2fa62d41bc43f54952f55652656b18a02fd5e356c0"
|
||||||
"checksum iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e"
|
"checksum iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e"
|
||||||
"checksum ipconfig 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "aa79fa216fbe60834a9c0737d7fcd30425b32d1c58854663e24d4c4b328ed83f"
|
"checksum ipconfig 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "aa79fa216fbe60834a9c0737d7fcd30425b32d1c58854663e24d4c4b328ed83f"
|
||||||
|
"checksum itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484"
|
||||||
"checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f"
|
"checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f"
|
||||||
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
|
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
|
||||||
"checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a"
|
"checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a"
|
||||||
|
|
|
@ -13,7 +13,9 @@ actix-web = "1.0"
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_yaml = "0.8"
|
serde_yaml = "0.8"
|
||||||
handlebars = "2.0"
|
handlebars = "2.0"
|
||||||
hotwatch = "0.4.3"
|
hotwatch = "0.4"
|
||||||
|
percent-encoding = "2.1"
|
||||||
|
itertools = "0.8"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = true
|
lto = true
|
||||||
|
|
100
src/main.rs
100
src/main.rs
|
@ -6,6 +6,8 @@ use actix_web::{
|
||||||
};
|
};
|
||||||
use handlebars::Handlebars;
|
use handlebars::Handlebars;
|
||||||
use hotwatch::{Event, Hotwatch};
|
use hotwatch::{Event, Hotwatch};
|
||||||
|
use itertools::Itertools;
|
||||||
|
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::collections::{BTreeMap, HashMap};
|
use std::collections::{BTreeMap, HashMap};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
@ -14,10 +16,14 @@ use std::io::Write;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
/// https://url.spec.whatwg.org/#fragment-percent-encode-set
|
||||||
|
static FRAGMENT_ENCODE_SET: &AsciiSet =
|
||||||
|
&CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
|
||||||
static DEFAULT_CONFIG: &[u8] = include_bytes!("../bunbun.default.toml");
|
static DEFAULT_CONFIG: &[u8] = include_bytes!("../bunbun.default.toml");
|
||||||
static CONFIG_FILE: &str = "bunbun.toml";
|
static CONFIG_FILE: &str = "bunbun.toml";
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
#[allow(clippy::enum_variant_names)]
|
||||||
enum BunBunError {
|
enum BunBunError {
|
||||||
IoError(std::io::Error),
|
IoError(std::io::Error),
|
||||||
ParseError(serde_yaml::Error),
|
ParseError(serde_yaml::Error),
|
||||||
|
@ -67,58 +73,60 @@ fn hop(
|
||||||
query: Query<SearchQuery>,
|
query: Query<SearchQuery>,
|
||||||
) -> impl Responder {
|
) -> impl Responder {
|
||||||
let data = data.read().unwrap();
|
let data = data.read().unwrap();
|
||||||
let mut raw_args = query.to.split_ascii_whitespace();
|
|
||||||
let command = raw_args.next();
|
|
||||||
|
|
||||||
if command.is_none() {
|
match resolve_hop(&query.to, &data.routes, &data.default_route) {
|
||||||
return HttpResponse::NotFound().body("not found");
|
(Some(path), args) => {
|
||||||
}
|
let mut template_args = HashMap::new();
|
||||||
|
template_args.insert(
|
||||||
|
"query",
|
||||||
|
utf8_percent_encode(&args, FRAGMENT_ENCODE_SET).to_string(),
|
||||||
|
);
|
||||||
|
|
||||||
// Reform args into url-safe string (probably want to go thru an actual parser)
|
HttpResponse::Found()
|
||||||
let mut args = String::new();
|
.header(
|
||||||
if let Some(first_arg) = raw_args.next() {
|
header::LOCATION,
|
||||||
args.push_str(first_arg);
|
data
|
||||||
for arg in raw_args {
|
.renderer
|
||||||
args.push_str("+");
|
.render_template(&path, &template_args)
|
||||||
args.push_str(arg);
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.finish()
|
||||||
}
|
}
|
||||||
|
(None, _) => HttpResponse::NotFound().body("not found"),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut template_args = HashMap::new();
|
/// Attempts to resolve the provided string into its route and its arguments.
|
||||||
template_args.insert("query", args);
|
/// If a default route was provided, then this will consider that route before
|
||||||
|
/// failing to resolve a route.
|
||||||
|
///
|
||||||
|
/// The first element in the tuple describes the route, while the second element
|
||||||
|
/// returns the remaining arguments. If none remain, an empty string is given.
|
||||||
|
fn resolve_hop(
|
||||||
|
query: &str,
|
||||||
|
routes: &BTreeMap<String, String>,
|
||||||
|
default_route: &Option<String>,
|
||||||
|
) -> (Option<String>, String) {
|
||||||
|
let mut split_args = query.split_ascii_whitespace().peekable();
|
||||||
|
let command = match split_args.peek() {
|
||||||
|
Some(command) => command,
|
||||||
|
None => return (None, String::new()),
|
||||||
|
};
|
||||||
|
|
||||||
match data.routes.get(command.unwrap()) {
|
match (routes.get(*command), default_route) {
|
||||||
Some(template) => HttpResponse::Found()
|
// Found a route
|
||||||
.header(
|
(Some(resolved), _) => (
|
||||||
header::LOCATION,
|
Some(resolved.clone()),
|
||||||
data
|
match split_args.next() {
|
||||||
.renderer
|
// Discard the first result, we found the route using the first arg
|
||||||
.render_template(template, &template_args)
|
Some(_) => split_args.join(" "),
|
||||||
.unwrap(),
|
None => String::new(),
|
||||||
)
|
},
|
||||||
.finish(),
|
),
|
||||||
None => match &data.default_route {
|
// Unable to find route, but had a default route
|
||||||
Some(route) => {
|
(None, Some(route)) => (routes.get(route).cloned(), split_args.join(" ")),
|
||||||
template_args.insert(
|
// No default route and no match
|
||||||
"query",
|
(None, None) => (None, String::new()),
|
||||||
format!(
|
|
||||||
"{}+{}",
|
|
||||||
command.unwrap(),
|
|
||||||
template_args.get("query").unwrap()
|
|
||||||
),
|
|
||||||
);
|
|
||||||
HttpResponse::Found()
|
|
||||||
.header(
|
|
||||||
header::LOCATION,
|
|
||||||
data
|
|
||||||
.renderer
|
|
||||||
.render_template(data.routes.get(route).unwrap(), &template_args)
|
|
||||||
.unwrap(),
|
|
||||||
)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
None => HttpResponse::NotFound().body("not found"),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue