From bd5938b5a00519fe8731d038195fe65f87209183 Mon Sep 17 00:00:00 2001 From: Benjamin Zeiss Date: Sun, 28 Jul 2024 14:58:14 +0200 Subject: [PATCH 1/4] Improved glyph url handling: accept arbitrary suffixes after {end} #1443 --- martin/src/srv/fonts.rs | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/martin/src/srv/fonts.rs b/martin/src/srv/fonts.rs index a8808c17b..820fb09f7 100644 --- a/martin/src/srv/fonts.rs +++ b/martin/src/srv/fonts.rs @@ -1,4 +1,5 @@ use std::string::ToString; +use std::str::FromStr; use actix_web::error::{ErrorBadRequest, ErrorNotFound}; use actix_web::web::{Data, Path}; @@ -11,19 +12,41 @@ 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(|c| c.is_digit(10)).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") From b5e83d3ba5887ec1a72b070ccb42dfd3aa4724eb Mon Sep 17 00:00:00 2001 From: Benjamin Zeiss Date: Sun, 28 Jul 2024 15:24:26 +0200 Subject: [PATCH 2/4] Improved glyph url handling: accept arbitrary suffixes after {end} #1443 - Fixed rust formatting --- martin/src/srv/fonts.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/martin/src/srv/fonts.rs b/martin/src/srv/fonts.rs index 820fb09f7..5ab4c03d8 100644 --- a/martin/src/srv/fonts.rs +++ b/martin/src/srv/fonts.rs @@ -1,5 +1,5 @@ -use std::string::ToString; use std::str::FromStr; +use std::string::ToString; use actix_web::error::{ErrorBadRequest, ErrorNotFound}; use actix_web::web::{Data, Path}; @@ -41,9 +41,7 @@ impl FontRequest { )] #[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 (start, end) = path.parse().map_err(|e| ErrorBadRequest(e.to_string()))?; let data = fonts .get_font_range(&path.fontstack, start, end) From 895dbe8f8ea557cf092e2804c57cc74494110822 Mon Sep 17 00:00:00 2001 From: Benjamin Zeiss Date: Sun, 28 Jul 2024 15:47:35 +0200 Subject: [PATCH 3/4] Improved glyph url handling: accept arbitrary suffixes after {end} #1443 - Fixed clippy error --- martin/src/srv/fonts.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/martin/src/srv/fonts.rs b/martin/src/srv/fonts.rs index 5ab4c03d8..990d2ed68 100644 --- a/martin/src/srv/fonts.rs +++ b/martin/src/srv/fonts.rs @@ -25,7 +25,7 @@ impl FontRequest { } fn parse_leading_digits(input: &str) -> Result { - let digits: String = input.chars().take_while(|c| c.is_digit(10)).collect(); + let digits: String = input.chars().take_while(|c| c.is_ascii_digit()).collect(); if digits.is_empty() { Err("No leading digits found") } else { From b9f1739dc306a7ac99f4c8ffe0f3397dd3524074 Mon Sep 17 00:00:00 2001 From: Benjamin Zeiss Date: Sun, 28 Jul 2024 16:02:29 +0200 Subject: [PATCH 4/4] Improved glyph url handling: accept arbitrary suffixes after {end} #1443 - Fixed follow up clippy error --- martin/src/srv/fonts.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/martin/src/srv/fonts.rs b/martin/src/srv/fonts.rs index 990d2ed68..47d2d7514 100644 --- a/martin/src/srv/fonts.rs +++ b/martin/src/srv/fonts.rs @@ -25,7 +25,7 @@ impl FontRequest { } fn parse_leading_digits(input: &str) -> Result { - let digits: String = input.chars().take_while(|c| c.is_ascii_digit()).collect(); + let digits: String = input.chars().take_while(char::is_ascii_digit).collect(); if digits.is_empty() { Err("No leading digits found") } else {