canonicalize relative paths

This commit is contained in:
Edward Shen 2019-12-31 19:23:51 -05:00
parent c990aef0e9
commit 71df3394ad
Signed by: edward
GPG key ID: F350507060ED6C90

View file

@ -11,6 +11,7 @@ use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
use serde::{de::Visitor, Deserialize, Deserializer, Serialize, Serializer}; use serde::{de::Visitor, Deserialize, Deserializer, Serialize, Serializer};
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
use std::path::PathBuf;
use std::process::Command; use std::process::Command;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
@ -78,8 +79,8 @@ impl<'de> Deserialize<'de> for Route {
impl std::fmt::Display for Route { impl std::fmt::Display for Route {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Self::External(s) => write!(f, "raw path ({})", s), Self::External(s) => write!(f, "raw ({})", s),
Self::Path(s) => write!(f, "file path ({})", s), Self::Path(s) => write!(f, "file ({})", s),
} }
} }
} }
@ -115,7 +116,7 @@ pub async fn hop(
match resolve_hop(&query.to, &data.routes, &data.default_route) { match resolve_hop(&query.to, &data.routes, &data.default_route) {
(Some(path), args) => { (Some(path), args) => {
let resolved_template = match path { let resolved_template = match path {
Route::Path(path) => resolve_path(path, &args), Route::Path(path) => resolve_path(PathBuf::from(path), &args),
Route::External(path) => Ok(path.to_owned().into_bytes()), Route::External(path) => Ok(path.to_owned().into_bytes()),
}; };
@ -214,8 +215,17 @@ pub async fn index(data: StateData, req: HttpRequest) -> impl Responder {
) )
} }
fn resolve_path(path: &str, args: &str) -> Result<Vec<u8>, crate::BunBunError> { fn resolve_path(
Ok(Command::new(path).arg(args).output()?.stdout) path: PathBuf,
args: &str,
) -> Result<Vec<u8>, crate::BunBunError> {
Ok(
// Unwrap is OK, we validated the path exists already
Command::new(path.canonicalize().unwrap())
.arg(args)
.output()?
.stdout,
)
} }
#[get("/bunbunsearch.xml")] #[get("/bunbunsearch.xml")]