elif-http 0.8.8

HTTP server core for the elif.rs LLM-friendly web framework
Documentation
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
//! Route matching engine for elif.rs
//!
//! This module provides the core route matching functionality that efficiently
//! resolves incoming requests to the appropriate route handlers.

use super::pattern::{CompiledRoute, RouteId, RouteMatch, RoutePattern, RoutePatternError};
use super::HttpMethod;
use std::collections::HashMap;
use thiserror::Error;

/// Errors that can occur during route matching
#[derive(Error, Debug)]
pub enum RouteMatchError {
    #[error("No matching route found")]
    NoMatch,
    #[error("Route pattern error: {0}")]
    PatternError(#[from] RoutePatternError),
    #[error("Conflicting routes: {0} conflicts with {1}")]
    RouteConflict(String, String),
}

/// Definition of a route to be compiled
#[derive(Debug, Clone)]
pub struct RouteDefinition {
    pub id: RouteId,
    pub method: HttpMethod,
    pub path: String,
}

/// High-performance route matcher
#[derive(Debug)]
pub struct RouteMatcher {
    /// Static routes for O(1) lookup - nested to avoid string allocation
    /// Structure: method -> path -> route_id
    static_routes: HashMap<HttpMethod, HashMap<String, RouteId>>,
    /// Dynamic routes sorted by priority
    dynamic_routes: Vec<CompiledRoute>,
    /// All route definitions for introspection
    route_definitions: HashMap<RouteId, RouteDefinition>,
}

impl RouteMatcher {
    /// Create a new empty route matcher
    pub fn new() -> Self {
        Self {
            static_routes: HashMap::new(),
            dynamic_routes: Vec::new(),
            route_definitions: HashMap::new(),
        }
    }

    /// Add a route to the matcher
    pub fn add_route(&mut self, definition: RouteDefinition) -> Result<(), RouteMatchError> {
        let pattern = RoutePattern::parse(&definition.path)?;

        // Check for route conflicts
        self.check_conflicts(&definition, &pattern)?;

        // Store the definition
        self.route_definitions
            .insert(definition.id.clone(), definition.clone());

        if pattern.is_static() {
            // Static route - add to nested lookup table
            self.static_routes
                .entry(definition.method.clone())
                .or_default()
                .insert(definition.path.clone(), definition.id);
        } else {
            // Dynamic route - compile and add to sorted list
            let compiled_route = CompiledRoute::new(definition.id, definition.method, pattern);

            // Insert in priority order (lower priority value = higher precedence)
            let insert_pos = self
                .dynamic_routes
                .binary_search_by_key(&compiled_route.priority, |r| r.priority)
                .unwrap_or_else(|pos| pos);

            self.dynamic_routes.insert(insert_pos, compiled_route);
        }

        Ok(())
    }

    /// Resolve an incoming request to a matching route
    pub fn resolve(&self, method: &HttpMethod, path: &str) -> Option<RouteMatch> {
        // Fast path: check static routes first (no allocation!)
        if let Some(method_routes) = self.static_routes.get(method) {
            if let Some(route_id) = method_routes.get(path) {
                return Some(RouteMatch {
                    route_id: route_id.clone(),
                    params: HashMap::new(),
                });
            }
        }

        // Dynamic route matching
        for compiled_route in &self.dynamic_routes {
            if compiled_route.matches(method, path) {
                let params = compiled_route.extract_params(path);
                return Some(RouteMatch {
                    route_id: compiled_route.id.clone(),
                    params,
                });
            }
        }

        None
    }

    /// Check for route conflicts before adding a new route
    fn check_conflicts(
        &self,
        new_route: &RouteDefinition,
        new_pattern: &RoutePattern,
    ) -> Result<(), RouteMatchError> {
        // Check against static routes
        if new_pattern.is_static() {
            if let Some(method_routes) = self.static_routes.get(&new_route.method) {
                if let Some(existing_id) = method_routes.get(&new_route.path) {
                    return Err(RouteMatchError::RouteConflict(
                        new_route.id.clone(),
                        existing_id.clone(),
                    ));
                }
            }
        }

        // Check against dynamic routes
        for existing_route in &self.dynamic_routes {
            if existing_route.method == new_route.method
                && self.patterns_conflict(new_pattern, &existing_route.pattern)
            {
                return Err(RouteMatchError::RouteConflict(
                    new_route.id.clone(),
                    existing_route.id.clone(),
                ));
            }
        }

        Ok(())
    }

    /// Check if two patterns would conflict (ambiguous matching)
    fn patterns_conflict(&self, pattern1: &RoutePattern, pattern2: &RoutePattern) -> bool {
        // Two patterns conflict if they are structurally identical.
        // This means they have the same number of segments, and each corresponding
        // segment is of the same type with the same static value or constraint.
        if pattern1.segments.len() != pattern2.segments.len() {
            return false;
        }

        for (seg1, seg2) in pattern1.segments.iter().zip(pattern2.segments.iter()) {
            match (seg1, seg2) {
                (
                    super::pattern::PathSegment::Static(s1),
                    super::pattern::PathSegment::Static(s2),
                ) if s1 == s2 => continue,
                (
                    super::pattern::PathSegment::Parameter { constraint: c1, .. },
                    super::pattern::PathSegment::Parameter { constraint: c2, .. },
                ) if c1 == c2 => continue,
                (
                    super::pattern::PathSegment::CatchAll { .. },
                    super::pattern::PathSegment::CatchAll { .. },
                ) => continue,
                _ => return false, // Segments are not structurally identical
            }
        }

        true // All segments are structurally identical, so the patterns conflict.
    }

    /// Get all route definitions for introspection
    pub fn all_routes(&self) -> &HashMap<RouteId, RouteDefinition> {
        &self.route_definitions
    }

    /// Get a specific route definition
    pub fn get_route(&self, route_id: &RouteId) -> Option<&RouteDefinition> {
        self.route_definitions.get(route_id)
    }

    /// Get statistics about the matcher
    pub fn stats(&self) -> MatcherStats {
        let static_routes_count = self
            .static_routes
            .values()
            .map(|method_routes| method_routes.len())
            .sum();

        MatcherStats {
            static_routes: static_routes_count,
            dynamic_routes: self.dynamic_routes.len(),
            total_routes: self.route_definitions.len(),
        }
    }

    /// Clear all routes
    pub fn clear(&mut self) {
        self.static_routes.clear();
        self.dynamic_routes.clear();
        self.route_definitions.clear();
    }
}

impl Default for RouteMatcher {
    fn default() -> Self {
        Self::new()
    }
}

/// Statistics about the route matcher
#[derive(Debug, Clone)]
pub struct MatcherStats {
    pub static_routes: usize,
    pub dynamic_routes: usize,
    pub total_routes: usize,
}

/// Builder for creating route matchers
pub struct RouteMatcherBuilder {
    routes: Vec<RouteDefinition>,
}

impl RouteMatcherBuilder {
    /// Create a new builder
    pub fn new() -> Self {
        Self { routes: Vec::new() }
    }

    /// Add a route to the builder
    pub fn route(mut self, id: String, method: HttpMethod, path: String) -> Self {
        self.routes.push(RouteDefinition { id, method, path });
        self
    }

    /// Add a GET route
    pub fn get(self, id: String, path: String) -> Self {
        self.route(id, HttpMethod::GET, path)
    }

    /// Add a POST route
    pub fn post(self, id: String, path: String) -> Self {
        self.route(id, HttpMethod::POST, path)
    }

    /// Add a PUT route
    pub fn put(self, id: String, path: String) -> Self {
        self.route(id, HttpMethod::PUT, path)
    }

    /// Add a DELETE route
    pub fn delete(self, id: String, path: String) -> Self {
        self.route(id, HttpMethod::DELETE, path)
    }

    /// Add a PATCH route
    pub fn patch(self, id: String, path: String) -> Self {
        self.route(id, HttpMethod::PATCH, path)
    }

    /// Build the route matcher
    pub fn build(self) -> Result<RouteMatcher, RouteMatchError> {
        let mut matcher = RouteMatcher::new();

        for route_def in self.routes {
            matcher.add_route(route_def)?;
        }

        Ok(matcher)
    }
}

impl Default for RouteMatcherBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_static_route_matching() {
        let mut matcher = RouteMatcher::new();

        let route_def = RouteDefinition {
            id: "home".to_string(),
            method: HttpMethod::GET,
            path: "/".to_string(),
        };

        matcher.add_route(route_def).unwrap();

        let result = matcher.resolve(&HttpMethod::GET, "/");
        assert!(result.is_some());

        let route_match = result.unwrap();
        assert_eq!(route_match.route_id, "home");
        assert!(route_match.params.is_empty());

        // Should not match different method
        assert!(matcher.resolve(&HttpMethod::POST, "/").is_none());

        // Should not match different path
        assert!(matcher.resolve(&HttpMethod::GET, "/users").is_none());
    }

    #[test]
    fn test_dynamic_route_matching() {
        let mut matcher = RouteMatcher::new();

        let route_def = RouteDefinition {
            id: "user_show".to_string(),
            method: HttpMethod::GET,
            path: "/users/{id}".to_string(),
        };

        matcher.add_route(route_def).unwrap();

        let result = matcher.resolve(&HttpMethod::GET, "/users/123");
        assert!(result.is_some());

        let route_match = result.unwrap();
        assert_eq!(route_match.route_id, "user_show");
        assert_eq!(route_match.params.get("id"), Some(&"123".to_string()));
    }

    #[test]
    fn test_route_priority() {
        let mut matcher = RouteMatcher::new();

        // Add in reverse priority order to test sorting
        matcher
            .add_route(RouteDefinition {
                id: "catch_all".to_string(),
                method: HttpMethod::GET,
                path: "/files/*path".to_string(),
            })
            .unwrap();

        matcher
            .add_route(RouteDefinition {
                id: "specific".to_string(),
                method: HttpMethod::GET,
                path: "/files/config.json".to_string(),
            })
            .unwrap();

        matcher
            .add_route(RouteDefinition {
                id: "param".to_string(),
                method: HttpMethod::GET,
                path: "/files/{name}".to_string(),
            })
            .unwrap();

        // Static route should match first
        let result = matcher.resolve(&HttpMethod::GET, "/files/config.json");
        assert_eq!(result.unwrap().route_id, "specific");

        // Parameter route should match before catch-all
        let result = matcher.resolve(&HttpMethod::GET, "/files/other.txt");
        assert_eq!(result.unwrap().route_id, "param");

        // Catch-all should match multi-segment paths
        let result = matcher.resolve(&HttpMethod::GET, "/files/docs/readme.md");
        assert_eq!(result.unwrap().route_id, "catch_all");
    }

    #[test]
    fn test_route_conflict_detection() {
        let mut matcher = RouteMatcher::new();

        // Add first route
        matcher
            .add_route(RouteDefinition {
                id: "route1".to_string(),
                method: HttpMethod::GET,
                path: "/users".to_string(),
            })
            .unwrap();

        // Try to add conflicting static route
        let result = matcher.add_route(RouteDefinition {
            id: "route2".to_string(),
            method: HttpMethod::GET,
            path: "/users".to_string(),
        });

        assert!(result.is_err());
        assert!(matches!(result, Err(RouteMatchError::RouteConflict(_, _))));
    }

    #[test]
    fn test_advanced_conflict_detection() {
        let mut matcher = RouteMatcher::new();

        // Test 1: Parameter routes with same structure should conflict
        matcher
            .add_route(RouteDefinition {
                id: "users_by_id".to_string(),
                method: HttpMethod::GET,
                path: "/users/{id}".to_string(),
            })
            .unwrap();

        let result = matcher.add_route(RouteDefinition {
            id: "users_by_name".to_string(),
            method: HttpMethod::GET,
            path: "/users/{name}".to_string(),
        });
        assert!(
            result.is_err(),
            "Parameters with different names should conflict"
        );

        // Test 2: Different methods should not conflict
        let result = matcher.add_route(RouteDefinition {
            id: "users_post".to_string(),
            method: HttpMethod::POST,
            path: "/users/{id}".to_string(),
        });
        assert!(result.is_ok(), "Different methods should not conflict");

        // Test 3: Different static segments should not conflict
        let result = matcher.add_route(RouteDefinition {
            id: "posts_by_id".to_string(),
            method: HttpMethod::GET,
            path: "/posts/{id}".to_string(),
        });
        assert!(
            result.is_ok(),
            "Different static segments should not conflict"
        );

        // Test 4: Catch-all routes with same structure should conflict
        matcher
            .add_route(RouteDefinition {
                id: "files_serve".to_string(),
                method: HttpMethod::GET,
                path: "/files/*path".to_string(),
            })
            .unwrap();

        let result = matcher.add_route(RouteDefinition {
            id: "files_download".to_string(),
            method: HttpMethod::GET,
            path: "/files/*file_path".to_string(),
        });
        assert!(
            result.is_err(),
            "Catch-all routes with same structure should conflict"
        );

        // Test 5: Different segment types should not conflict
        let result = matcher.add_route(RouteDefinition {
            id: "admin_static".to_string(),
            method: HttpMethod::GET,
            path: "/admin/dashboard".to_string(),
        });
        assert!(
            result.is_ok(),
            "Static vs parameter segments should not conflict"
        );
    }

    #[test]
    fn test_constraint_based_conflicts() {
        let mut matcher = RouteMatcher::new();

        // Test 1: Same constraints should conflict
        matcher
            .add_route(RouteDefinition {
                id: "user_by_int_id".to_string(),
                method: HttpMethod::GET,
                path: "/users/{id:int}".to_string(),
            })
            .unwrap();

        let result = matcher.add_route(RouteDefinition {
            id: "user_by_int_uid".to_string(),
            method: HttpMethod::GET,
            path: "/users/{uid:int}".to_string(),
        });
        assert!(result.is_err(), "Same constraints should conflict");

        // Test 2: Different constraints should not conflict (they have different precedence)
        let result = matcher.add_route(RouteDefinition {
            id: "user_by_uuid".to_string(),
            method: HttpMethod::GET,
            path: "/users/{id:uuid}".to_string(),
        });
        assert!(result.is_ok(), "Different constraints should not conflict");

        // Test 3: Constrained vs unconstrained should not conflict
        let result = matcher.add_route(RouteDefinition {
            id: "user_by_string".to_string(),
            method: HttpMethod::GET,
            path: "/users/{name}".to_string(),
        });
        assert!(
            result.is_ok(),
            "Constrained vs unconstrained should not conflict"
        );
    }

    #[test]
    fn test_complex_pattern_conflicts() {
        let mut matcher = RouteMatcher::new();

        // Test complex multi-segment patterns
        matcher
            .add_route(RouteDefinition {
                id: "api_user_posts".to_string(),
                method: HttpMethod::GET,
                path: "/api/v1/users/{user_id}/posts/{post_id}".to_string(),
            })
            .unwrap();

        // Same structure should conflict
        let result = matcher.add_route(RouteDefinition {
            id: "api_member_articles".to_string(),
            method: HttpMethod::GET,
            path: "/api/v1/users/{member_id}/posts/{article_id}".to_string(),
        });
        assert!(
            result.is_err(),
            "Structurally identical complex patterns should conflict"
        );

        // Different static segment should not conflict
        let result = matcher.add_route(RouteDefinition {
            id: "api_user_comments".to_string(),
            method: HttpMethod::GET,
            path: "/api/v1/users/{user_id}/comments/{comment_id}".to_string(),
        });
        assert!(
            result.is_ok(),
            "Different static segments should not conflict"
        );

        // Different segment count should not conflict
        let result = matcher.add_route(RouteDefinition {
            id: "api_user_profile".to_string(),
            method: HttpMethod::GET,
            path: "/api/v1/users/{user_id}/profile".to_string(),
        });
        assert!(
            result.is_ok(),
            "Different segment count should not conflict"
        );
    }

    #[test]
    fn test_matcher_builder() {
        let matcher = RouteMatcherBuilder::new()
            .get("home".to_string(), "/".to_string())
            .post("users_create".to_string(), "/users".to_string())
            .get("users_show".to_string(), "/users/{id}".to_string())
            .build()
            .unwrap();

        let stats = matcher.stats();
        assert_eq!(stats.total_routes, 3);
        assert_eq!(stats.static_routes, 2); // "/" and "/users" for POST
        assert_eq!(stats.dynamic_routes, 1); // "/users/{id}"

        // Test that routes work
        assert!(matcher.resolve(&HttpMethod::GET, "/").is_some());
        assert!(matcher.resolve(&HttpMethod::POST, "/users").is_some());
        assert!(matcher.resolve(&HttpMethod::GET, "/users/123").is_some());
    }

    #[test]
    fn test_constraint_validation_in_matching() {
        let mut matcher = RouteMatcher::new();

        matcher
            .add_route(RouteDefinition {
                id: "user_by_id".to_string(),
                method: HttpMethod::GET,
                path: "/users/{id:int}".to_string(),
            })
            .unwrap();

        // Should match valid integer
        let result = matcher.resolve(&HttpMethod::GET, "/users/123");
        assert!(result.is_some());
        assert_eq!(result.unwrap().route_id, "user_by_id");

        // Should not match invalid integer
        let result = matcher.resolve(&HttpMethod::GET, "/users/abc");
        assert!(result.is_none());
    }

    #[test]
    fn test_mixed_static_and_dynamic_routes() {
        let mut matcher = RouteMatcher::new();

        // Mix of static and dynamic routes
        matcher
            .add_route(RouteDefinition {
                id: "api_status".to_string(),
                method: HttpMethod::GET,
                path: "/api/status".to_string(),
            })
            .unwrap();

        matcher
            .add_route(RouteDefinition {
                id: "api_user".to_string(),
                method: HttpMethod::GET,
                path: "/api/users/{id}".to_string(),
            })
            .unwrap();

        matcher
            .add_route(RouteDefinition {
                id: "root".to_string(),
                method: HttpMethod::GET,
                path: "/".to_string(),
            })
            .unwrap();

        // Test static route lookup
        let result = matcher.resolve(&HttpMethod::GET, "/api/status");
        assert_eq!(result.unwrap().route_id, "api_status");

        // Test root route
        let result = matcher.resolve(&HttpMethod::GET, "/");
        assert_eq!(result.unwrap().route_id, "root");

        // Test dynamic route
        let result = matcher.resolve(&HttpMethod::GET, "/api/users/456");
        let route_match = result.unwrap();
        assert_eq!(route_match.route_id, "api_user");
        assert_eq!(route_match.params.get("id"), Some(&"456".to_string()));
    }

    #[test]
    fn test_no_match() {
        let matcher = RouteMatcherBuilder::new()
            .get("home".to_string(), "/".to_string())
            .build()
            .unwrap();

        assert!(matcher.resolve(&HttpMethod::GET, "/nonexistent").is_none());
        assert!(matcher.resolve(&HttpMethod::POST, "/").is_none());
    }

    #[test]
    fn test_static_route_lookup_performance() {
        // Create a matcher with many static routes across different methods
        let mut builder = RouteMatcherBuilder::new();

        for i in 0..100 {
            builder = builder
                .get(format!("get_{}", i), format!("/static/path/{}", i))
                .post(format!("post_{}", i), format!("/api/v1/{}", i))
                .put(format!("put_{}", i), format!("/resource/{}", i));
        }

        let matcher = builder.build().unwrap();
        let stats = matcher.stats();

        // Verify we have static routes
        assert_eq!(stats.static_routes, 300); // 100 routes × 3 methods
        assert_eq!(stats.dynamic_routes, 0);

        // Test lookup performance - these should be O(1) with no allocations
        let start = std::time::Instant::now();

        // Perform many lookups
        for i in 0..1000 {
            let test_index = i % 100;

            // These lookups should not allocate strings
            let result = matcher.resolve(&HttpMethod::GET, &format!("/static/path/{}", test_index));
            assert!(result.is_some());

            let result = matcher.resolve(&HttpMethod::POST, &format!("/api/v1/{}", test_index));
            assert!(result.is_some());

            let result = matcher.resolve(&HttpMethod::PUT, &format!("/resource/{}", test_index));
            assert!(result.is_some());

            // Test non-existent path
            let result = matcher.resolve(&HttpMethod::GET, "/nonexistent/path");
            assert!(result.is_none());
        }

        let elapsed = start.elapsed();

        // This test primarily verifies that the optimization doesn't break functionality
        // The performance benefit (no string allocation) can't be directly tested in a unit test,
        // but the nested HashMap structure ensures we only do &str lookups

        // Should complete very quickly due to O(1) lookups
        assert!(
            elapsed.as_millis() < 100,
            "Static route lookups took too long: {}ms",
            elapsed.as_millis()
        );

        println!(
            "3000 static route lookups completed in {}μs",
            elapsed.as_micros()
        );
    }
}