diff --git a/martin/src/srv/fonts.rs b/martin/src/srv/fonts.rs index a8808c17b..47d2d7514 100644 --- a/martin/src/srv/fonts.rs +++ b/martin/src/srv/fonts.rs @@ -1,3 +1,4 @@ +use std::str::FromStr; use std::string::ToString; use actix_web::error::{ErrorBadRequest, ErrorNotFound}; @@ -11,19 +12,39 @@ use crate::srv::server::map_internal_error; #[derive(Deserialize, Debug)] struct FontRequest { fontstack: String, - start: u32, - end: u32, + start: String, + end: String, +} + +impl FontRequest { + fn parse(&self) -> Result<(u32, u32), &'static str> { + let start = u32::from_str(&self.start).map_err(|_| "Invalid start value")?; + let end = FontRequest::parse_leading_digits(&self.end)?; + + Ok((start, end)) + } + + fn parse_leading_digits(input: &str) -> Result { + let digits: String = input.chars().take_while(char::is_ascii_digit).collect(); + if digits.is_empty() { + Err("No leading digits found") + } else { + digits.parse::().map_err(|_| "Failed to parse number") + } + } } #[route( - "/font/{fontstack}/{start}-{end}", + "/font/{fontstack}/{start}-{end}*", method = "GET", wrap = "middleware::Compress::default()" )] #[allow(clippy::unused_async)] async fn get_font(path: Path, fonts: Data) -> ActixResult { + let (start, end) = path.parse().map_err(|e| ErrorBadRequest(e.to_string()))?; + let data = fonts - .get_font_range(&path.fontstack, path.start, path.end) + .get_font_range(&path.fontstack, start, end) .map_err(map_font_error)?; Ok(HttpResponse::Ok() .content_type("application/x-protobuf")