1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
use super::validation::{
MAX_PATH_SEGMENTS, MAX_PATTERN_LENGTH, MAX_REGEX_SIZE, type_spec_to_regex, validate_path_param,
validate_reverse_param,
};
use aho_corasick::AhoCorasick;
use regex::Regex;
use std::collections::{HashMap, HashSet};
/// Path pattern for URL matching
/// Similar to Django's URL patterns but using composition
#[derive(Clone, Debug)]
pub struct PathPattern {
/// Original pattern string (may contain type specifiers)
pattern: String,
/// Pattern normalized to `{name}` format for URL reversal
normalized_pattern: String,
pub(super) regex: Regex,
pub(super) param_names: Vec<String>,
/// Parameter names that use the `path` type specifier.
/// These require post-match validation to reject directory traversal.
pub(super) path_type_params: HashSet<String>,
/// Pre-built Aho-Corasick automaton for efficient URL reversal
/// This is constructed once during pattern creation for O(n+m+z) reversal
aho_corasick: Option<AhoCorasick>,
}
/// Parse result containing regex, param names, and normalized pattern for URL reversal
struct ParsePatternResult {
regex_str: String,
param_names: Vec<String>,
/// Parameter names that use the `path` type specifier
path_type_params: HashSet<String>,
/// Pattern normalized to `{name}` format for URL reversal
/// e.g., "/users/{<int:id>}/" -> "/users/{id}/"
normalized_pattern: String,
}
impl PathPattern {
/// Create a new path pattern
/// Patterns like "/users/{id}/" are converted to regex
///
/// # Examples
///
/// ```
/// use reinhardt_urls::routers::{PathPattern, path};
///
/// // Create a simple pattern without parameters
/// let pattern = PathPattern::new(path!("/users/")).unwrap();
/// assert_eq!(pattern.pattern(), "/users/");
///
/// // Create a pattern with a parameter
/// let pattern = PathPattern::new(path!("/users/{id}/")).unwrap();
/// assert_eq!(pattern.param_names(), &["id"]);
/// ```
pub fn new(pattern: impl Into<String>) -> Result<Self, String> {
let pattern = pattern.into();
// Reject patterns exceeding the maximum length to prevent ReDoS
if pattern.len() > MAX_PATTERN_LENGTH {
return Err(format!(
"Pattern length {} exceeds maximum allowed length of {} bytes",
pattern.len(),
MAX_PATTERN_LENGTH
));
}
// Reject patterns with excessive path segments to prevent resource exhaustion
let segment_count = pattern.split('/').count();
if segment_count > MAX_PATH_SEGMENTS {
return Err(format!(
"Pattern has {} path segments, exceeding maximum of {}",
segment_count, MAX_PATH_SEGMENTS
));
}
let parse_result = Self::parse_pattern(&pattern)?;
// Use RegexBuilder with size limits to prevent memory exhaustion
let regex = regex::RegexBuilder::new(&parse_result.regex_str)
.size_limit(MAX_REGEX_SIZE)
.build()
.map_err(|e| format!("Failed to compile pattern regex: {}", e))?;
// Build Aho-Corasick automaton for URL reversal if there are parameters
let aho_corasick = if !parse_result.param_names.is_empty() {
let placeholders: Vec<String> = parse_result
.param_names
.iter()
.map(|name| format!("{{{}}}", name))
.collect();
AhoCorasick::new(&placeholders)
.map(Some)
.map_err(|e| format!("Failed to build Aho-Corasick automaton: {}", e))?
} else {
None
};
Ok(Self {
pattern,
normalized_pattern: parse_result.normalized_pattern,
regex,
param_names: parse_result.param_names,
path_type_params: parse_result.path_type_params,
aho_corasick,
})
}
fn parse_pattern(pattern: &str) -> Result<ParsePatternResult, String> {
let mut regex_str = String::from("^");
let mut param_names = Vec::new();
let mut path_type_params = HashSet::new();
let mut normalized_pattern = String::new();
let mut chars = pattern.chars().peekable();
while let Some(ch) = chars.next() {
match ch {
'{' => {
// Extract parameter content (everything between { and })
let mut param_content = String::new();
while let Some(&next_ch) = chars.peek() {
if next_ch == '}' {
chars.next(); // consume '}'
break;
}
param_content.push(chars.next().unwrap());
}
if param_content.is_empty() {
return Err("Empty parameter name".to_string());
}
// Check for typed parameter syntax: {<type:name>}
let (param_name, regex_pattern) =
if param_content.starts_with('<') && param_content.ends_with('>') {
// Parse {<type:name>}
let inner = ¶m_content[1..param_content.len() - 1]; // Remove < >
if let Some(colon_pos) = inner.find(':') {
let type_spec = &inner[..colon_pos];
let name = &inner[colon_pos + 1..];
if name.is_empty() {
return Err(format!(
"Empty parameter name in typed parameter: {{<{}:>}}",
type_spec
));
}
if type_spec == "path" {
path_type_params.insert(name.to_string());
}
(name.to_string(), type_spec_to_regex(type_spec))
} else {
return Err(format!(
"Invalid typed parameter syntax: {{<{}>}}. Expected {{<type:name>}}",
inner
));
}
} else {
// Simple {name} parameter - use default [^/]+
(param_content, "[^/]+")
};
param_names.push(param_name.clone());
regex_str.push_str(&format!("(?P<{}>{})", param_name, regex_pattern));
// Write normalized placeholder for URL reversal
normalized_pattern.push_str(&format!("{{{}}}", param_name));
}
_ => {
// Escape special regex characters
if ".*+?^${}()|[]\\".contains(ch) {
regex_str.push('\\');
}
regex_str.push(ch);
// Copy literal characters to normalized pattern
normalized_pattern.push(ch);
}
}
}
regex_str.push('$');
Ok(ParsePatternResult {
regex_str,
param_names,
path_type_params,
normalized_pattern,
})
}
/// Get the original pattern string
///
/// # Examples
///
/// ```
/// use reinhardt_urls::routers::{PathPattern, path};
///
/// let pattern = PathPattern::new(path!("/users/{id}/")).unwrap();
/// assert_eq!(pattern.pattern(), "/users/{id}/");
/// ```
pub fn pattern(&self) -> &str {
&self.pattern
}
/// Convert pattern to matchit-compatible format
///
/// Transforms path-type parameters from `{<path:name>}` to `{*name}`
/// for use with the matchit radix router. Non-path parameters remain
/// as `{name}`.
pub(crate) fn to_matchit_pattern(&self) -> String {
let mut result = String::new();
let mut chars = self.pattern.chars().peekable();
while let Some(ch) = chars.next() {
if ch == '{' {
let mut param_content = String::new();
while let Some(&next_ch) = chars.peek() {
if next_ch == '}' {
chars.next();
break;
}
param_content.push(chars.next().unwrap());
}
// Check for typed parameter: {<type:name>}
if param_content.starts_with('<') && param_content.ends_with('>') {
let inner = ¶m_content[1..param_content.len() - 1];
if let Some(colon_pos) = inner.find(':') {
let type_spec = &inner[..colon_pos];
let name = &inner[colon_pos + 1..];
if type_spec == "path" {
// Convert path type to matchit catch-all: {*name}
result.push_str(&format!("{{*{}}}", name));
} else {
// Other typed params use simple {name}
result.push_str(&format!("{{{}}}", name));
}
} else {
result.push_str(&format!("{{{}}}", param_content));
}
} else {
// Simple {name} parameter
result.push_str(&format!("{{{}}}", param_content));
}
} else {
result.push(ch);
}
}
result
}
/// Get the list of parameter names in the pattern
///
/// # Examples
///
/// ```
/// use reinhardt_urls::routers::{PathPattern, path};
///
/// let pattern = PathPattern::new(path!("/users/{user_id}/posts/{post_id}/")).unwrap();
/// assert_eq!(pattern.param_names(), &["user_id", "post_id"]);
/// ```
pub fn param_names(&self) -> &[String] {
&self.param_names
}
/// Test if the pattern matches a given path
///
/// # Examples
///
/// ```
/// use reinhardt_urls::routers::{PathPattern, path};
///
/// let pattern = PathPattern::new(path!("/users/{id}/")).unwrap();
/// assert!(pattern.is_match("/users/123/"));
/// assert!(!pattern.is_match("/users/"));
/// ```
pub fn is_match(&self, path: &str) -> bool {
self.regex.is_match(path)
}
/// Match a path and extract parameters
///
/// # Examples
///
/// ```
/// use reinhardt_urls::routers::{PathPattern, path};
///
/// let pattern = PathPattern::new(path!("/users/{id}/")).unwrap();
/// let params = pattern.extract_params("/users/123/").unwrap();
/// assert_eq!(params.get("id"), Some(&"123".to_string()));
/// ```
pub fn extract_params(&self, path: &str) -> Option<HashMap<String, String>> {
self.regex.captures(path).and_then(|captures| {
let mut params = HashMap::new();
for name in self.param_names() {
if let Some(value) = captures.name(name) {
let val = value.as_str();
// Validate path-type parameters against directory traversal
if self.path_type_params.contains(name) && !validate_path_param(val) {
return None;
}
params.insert(name.clone(), val.to_string());
}
}
Some(params)
})
}
/// Reverse URL pattern with parameters using Aho-Corasick algorithm
///
/// This method uses pre-built Aho-Corasick automaton for efficient
/// multi-pattern matching with O(n+m+z) complexity where:
/// - n: pattern length
/// - m: total parameter values length
/// - z: number of placeholder matches
///
/// # Arguments
///
/// * `params` - HashMap of parameter names to values
///
/// # Returns
///
/// * `Ok(String)` - Reversed URL with parameters substituted
/// * `Err(String)` - If required parameters are missing
///
/// # Examples
///
/// ```
/// use reinhardt_urls::routers::{PathPattern, path};
/// use std::collections::HashMap;
///
/// let pattern = PathPattern::new(path!("/users/{id}/posts/{post_id}/")).unwrap();
///
/// let mut params = HashMap::new();
/// params.insert("id".to_string(), "123".to_string());
/// params.insert("post_id".to_string(), "456".to_string());
///
/// let url = pattern.reverse(¶ms).unwrap();
/// assert_eq!(url, "/users/123/posts/456/");
/// ```
pub fn reverse(&self, params: &HashMap<String, String>) -> Result<String, String> {
// Validate all required parameters are present
for param_name in &self.param_names {
if !params.contains_key(param_name) {
return Err(format!("Missing required parameter: {}", param_name));
}
}
// Validate parameter values against injection attacks
for (name, value) in params {
if !validate_reverse_param(value) {
return Err(format!(
"Invalid parameter value for '{}': contains dangerous characters",
name
));
}
}
// If no parameters, return normalized pattern as-is
if self.param_names.is_empty() {
return Ok(self.normalized_pattern.clone());
}
// Use Aho-Corasick if available, otherwise fallback to single-pass
match &self.aho_corasick {
Some(ac) => {
// Find all matches using Aho-Corasick on normalized pattern
let mut replacements = Vec::new();
for mat in ac.find_iter(&self.normalized_pattern) {
let param_name = &self.param_names[mat.pattern()];
// We already validated all params exist above
let value = params.get(param_name).unwrap();
replacements.push((mat.start(), mat.end(), value.clone()));
}
// Apply replacements from right to left to avoid position shifts
let mut result = self.normalized_pattern.clone();
for (start, end, value) in replacements.into_iter().rev() {
result.replace_range(start..end, &value);
}
Ok(result)
}
None => {
// Fallback: no parameters, just return normalized pattern
Ok(self.normalized_pattern.clone())
}
}
}
}