dmsc 0.1.9

Ri - A high-performance Rust middleware framework with modular architecture
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
//! Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
//!
//! This file is part of Ri.
//! The Ri project belongs to the Dunimd Team.
//!
//! Licensed under the Apache License, Version 2.0 (the "License");
//! You may not use this file except in compliance with the License.
//! You may obtain a copy of the License at
//!
//!     http://www.apache.org/licenses/LICENSE-2.0
//!
//! Unless required by applicable law or agreed to in writing, software
//! distributed under the License is distributed on an "AS IS" BASIS,
//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//! See the License for the specific language governing permissions and
//! limitations under the License.

#![allow(non_snake_case)]

//! # Middleware Module
//! 
//! This module provides a flexible middleware system for the Ri gateway, allowing for
//! request processing and modification through a chain of middleware components.
//! 
//! ## Key Components
//! 
//! - **RiMiddleware**: Trait defining the middleware interface
//! - **RiMiddlewareChain**: Manages a chain of middleware components
//! - **Built-in Middleware**: Auth, CORS, Logging, Request ID, and Rate Limiting implementations
//! 
//! ## Design Principles
//! 
//! 1. **Async Trait**: Uses async_trait for async middleware execution
//! 2. **Flexible Chain**: Allows dynamic addition and removal of middleware
//! 3. **Extensible**: Easy to implement custom middleware
//! 4. **Thread Safe**: Uses Arc for safe sharing of middleware instances
//! 5. **Modular**: Built-in middleware implementations can be used independently
//! 6. **Request Modification**: Middleware can modify requests before they reach the target service
//! 7. **Error Handling**: Middleware can return errors to abort request processing
//! 8. **Order Matters**: Middleware is executed in the order they are added to the chain
//! 
//! ## Usage
//! 
//! ```rust
//! use ri::prelude::*;
//! use std::sync::Arc;
//! 
//! async fn example() -> RiResult<()> {
//!     // Create a middleware chain
//!     let mut chain = RiMiddlewareChain::new();
//!     
//!     // Add built-in middleware
//!     chain.add(Arc::new(RiLoggingMiddleware::new("info".to_string())));
//!     chain.add(Arc::new(RiAuthMiddleware::new("Authorization".to_string())));
//!     chain.add(Arc::new(RiCorsMiddleware::new(
//!         vec!["*".to_string()],
//!         vec!["GET".to_string(), "POST".to_string()],
//!         vec!["Content-Type".to_string(), "Authorization".to_string()]
//!     )));
//!     
//!     // Create a request and execute the middleware chain
//!     let mut request = RiGatewayRequest::new("GET".to_string(), "/api/v1/resource".to_string());
//!     chain.execute(&mut request).await?;
//!     
//!     Ok(())
//! }
//! ```

use super::RiGatewayRequest;
use crate::core::RiResult;
use async_trait::async_trait;
use std::sync::Arc;

/// Trait defining the middleware interface for request processing.
/// 
/// All middleware components must implement this trait, which provides methods for
/// executing middleware logic and identifying the middleware.
#[async_trait]
pub trait RiMiddleware: Send + Sync {
    /// Executes the middleware logic on a request.
    /// 
    /// This method is called for each request passing through the middleware chain.
    /// Middleware can modify the request, validate it, or return an error to abort processing.
    /// 
    /// # Parameters
    /// 
    /// - `request`: Mutable reference to the request being processed
    /// 
    /// # Returns
    /// 
    /// A `RiResult<()>` indicating success or failure
    async fn execute(&self, request: &mut RiGatewayRequest) -> RiResult<()>;
    
    /// Gets the name of the middleware.
    /// 
    /// This method returns a static string identifier for the middleware, useful for logging
    /// and debugging purposes.
    /// 
    /// # Returns
    /// 
    /// A static string containing the middleware name
    fn name(&self) -> &'static str;
}

/// Manages a chain of middleware components.
/// 
/// This struct maintains a list of middleware instances and provides methods for
/// adding, removing, and executing middleware in sequence.
pub struct RiMiddlewareChain {
    /// Vector of middleware instances in the order they should be executed
    middlewares: Vec<Arc<dyn RiMiddleware>>,
}

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

impl RiMiddlewareChain {
    /// Creates a new empty middleware chain.
    /// 
    /// # Returns
    /// 
    /// A new `RiMiddlewareChain` instance with no middleware
    pub fn new() -> Self {
        Self {
            middlewares: Vec::new(),
        }
    }

    /// Adds a middleware to the end of the chain.
    /// 
    /// # Parameters
    /// 
    /// - `middleware`: The middleware to add to the chain
    pub fn add(&mut self, middleware: Arc<dyn RiMiddleware>) {
        self.middlewares.push(middleware);
    }

    /// Executes all middleware in the chain on a request.
    /// 
    /// Middleware is executed in the order they were added to the chain.
    /// If any middleware returns an error, execution stops and the error is returned.
    /// 
    /// # Parameters
    /// 
    /// - `request`: Mutable reference to the request being processed
    /// 
    /// # Returns
    /// 
    /// A `RiResult<()>` indicating success or failure
    pub async fn execute(&self, request: &mut RiGatewayRequest) -> RiResult<()> {
        for middleware in &self.middlewares {
            // Record middleware execution time
            let start = std::time::Instant::now();
            
            middleware.execute(request).await?;
            
            let duration = start.elapsed();
            let _duration_ms = duration.as_secs_f64() * 1000.0;
            
            // Middleware execution time is tracked by the observability module
            // The metrics are automatically collected when the observability feature is enabled
        }
        Ok(())
    }

    /// Clears all middleware from the chain.
    pub fn clear(&mut self) {
        self.middlewares.clear();
    }

    /// Gets the number of middleware in the chain.
    /// 
    /// # Returns
    /// 
    /// The number of middleware in the chain
    pub fn len(&self) -> usize {
        self.middlewares.len()
    }

    /// Checks if the middleware chain is empty.
    /// 
    /// # Returns
    /// 
    /// `true` if the chain contains no middleware, `false` otherwise
    pub fn is_empty(&self) -> bool {
        self.middlewares.is_empty()
    }
}

// Built-in middleware implementations

/// Authentication middleware for validating request credentials.
/// 
/// This middleware checks for and validates authorization headers in requests.
pub struct RiAuthMiddleware {
    /// Name of the authorization header to check
    auth_header: String,
}

impl RiAuthMiddleware {
    /// Creates a new authentication middleware instance.
    /// 
    /// # Parameters
    /// 
    /// - `auth_header`: Name of the authorization header to check
    /// 
    /// # Returns
    /// 
    /// A new `RiAuthMiddleware` instance
    pub fn new(auth_header: String) -> Self {
        Self { auth_header }
    }
}

#[async_trait]
impl RiMiddleware for RiAuthMiddleware {
    /// Validates the authorization header in the request.
    /// 
    /// This implementation checks for a Bearer token in the specified authorization header.
    /// In a production environment, this would validate JWT tokens using the auth module.
    /// 
    /// # Parameters
    /// 
    /// - `request`: Mutable reference to the request being processed
    /// 
    /// # Returns
    /// 
    /// A `RiResult<()>` indicating success or failure
    async fn execute(&self, request: &mut RiGatewayRequest) -> RiResult<()> {
        // Check for authorization header
        if let Some(auth_header) = request.headers.get(&self.auth_header) {
            // Basic auth validation - in a real implementation, this would validate JWT tokens
            if let Some(token) = auth_header.strip_prefix("Bearer ") {
                if token.is_empty() {
                    return Err(crate::core::RiError::Other("Empty bearer token".to_string()));
                }
                // Here you would validate the JWT token using your auth module
            } else {
                return Err(crate::core::RiError::Other("Invalid authorization header format".to_string()));
            }
        } else {
            // Allow requests without auth for public endpoints
            // In a real implementation, you'd check if the route requires authentication
        }
        Ok(())
    }

    /// Gets the name of the middleware.
    /// 
    /// # Returns
    /// 
    /// The string "AuthMiddleware"
    fn name(&self) -> &'static str {
        "AuthMiddleware"
    }
}

/// CORS (Cross-Origin Resource Sharing) middleware.
/// 
/// This middleware validates CORS headers and ensures requests come from allowed origins.
pub struct RiCorsMiddleware {
    /// List of allowed origins for CORS requests
    allowed_origins: Vec<String>,
    /// List of allowed HTTP methods for CORS requests
    #[allow(dead_code)]
    allowed_methods: Vec<String>,
    /// List of allowed headers for CORS requests
    #[allow(dead_code)]
    allowed_headers: Vec<String>,
}

impl RiCorsMiddleware {
    /// Creates a new CORS middleware instance.
    /// 
    /// # Parameters
    /// 
    /// - `allowed_origins`: List of allowed origins for CORS requests
    /// - `allowed_methods`: List of allowed HTTP methods for CORS requests
    /// - `allowed_headers`: List of allowed headers for CORS requests
    /// 
    /// # Returns
    /// 
    /// A new `RiCorsMiddleware` instance
    pub fn new(
        allowed_origins: Vec<String>,
        allowed_methods: Vec<String>,
        allowed_headers: Vec<String>,
    ) -> Self {
        Self {
            allowed_origins,
            allowed_methods,
            allowed_headers,
        }
    }
    
    /// Checks if an origin is allowed.
    ///
    /// # Security Note
    ///
    /// Wildcard origin "*" is NOT treated as matching any origin here.
    /// Wildcard handling is done at the response header level, not validation level.
    /// This prevents bypass attacks where any origin is falsely considered valid.
    ///
    /// # Parameters
    ///
    /// - `origin`: The origin to check
    ///
    /// # Returns
    ///
    /// `true` if the origin is allowed, `false` otherwise
    fn is_origin_allowed(&self, origin: &str) -> bool {
        // Wildcard should never match specific origins - it's only for response headers
        if self.allowed_origins.contains(&"*".to_string()) {
            // If wildcard is set, only exact matches or the wildcard itself is valid
            // But we don't treat wildcard as matching everything
            return false;
        }
        self.allowed_origins.iter().any(|allowed| allowed == origin)
    }

    /// Checks if wildcard origin is configured.
    ///
    /// # Returns
    ///
    /// `true` if wildcard origin is allowed
    fn is_wildcard_allowed(&self) -> bool {
        self.allowed_origins.contains(&"*".to_string())
    }
}

#[async_trait]
impl RiMiddleware for RiCorsMiddleware {
    /// Validates CORS headers in the request.
    ///
    /// This implementation checks if the request origin is in the list of allowed origins.
    /// If wildcard is configured, any origin is allowed but will be handled at response time.
    ///
    /// # Parameters
    ///
    /// - `request`: Mutable reference to the request being processed
    ///
    /// # Returns
    ///
    /// A `RiResult<()>` indicating success or failure
    async fn execute(&self, request: &mut RiGatewayRequest) -> RiResult<()> {
        // CORS preflight handling would be done at the response level
        // This middleware just validates the request

        if let Some(origin) = request.headers.get("origin") {
            // If wildcard is allowed, origin validation passes here
            // Actual wildcard response will be handled at response header level
            if !self.is_wildcard_allowed() && !self.is_origin_allowed(origin) {
                return Err(crate::core::RiError::Other("Origin not allowed".to_string()));
            }
        }

        Ok(())
    }

    /// Gets the name of the middleware.
    /// 
    /// # Returns
    /// 
    /// The string "CorsMiddleware"
    fn name(&self) -> &'static str {
        "CorsMiddleware"
    }
}

/// Logging middleware for recording request details.
/// 
/// This middleware logs request information such as method, path, and remote address.
pub struct RiLoggingMiddleware {
    /// Log level for the middleware
    #[allow(dead_code)]
    log_level: String,
}

impl RiLoggingMiddleware {
    /// Creates a new logging middleware instance.
    /// 
    /// # Parameters
    /// 
    /// - `log_level`: Log level for the middleware
    /// 
    /// # Returns
    /// 
    /// A new `RiLoggingMiddleware` instance
    pub fn new(log_level: String) -> Self {
        Self { log_level }
    }
}

#[async_trait]
impl RiMiddleware for RiLoggingMiddleware {
    /// Logs request details.
    /// 
    /// This implementation prints request information to the console.
    /// In a production environment, this would use a proper logging framework.
    /// 
    /// # Parameters
    /// 
    /// - `request`: Mutable reference to the request being processed
    /// 
    /// # Returns
    /// 
    /// A `RiResult<()>` indicating success or failure
    async fn execute(&self, request: &mut RiGatewayRequest) -> RiResult<()> {
        // In a real implementation, this would log the request details
        // For now, we'll just allow it through
        log::info!("[{}] {} {} from {}", 
            chrono::Utc::now().format("%Y-%m-%d %H:%M:%S"),
            request.method,
            request.path,
            request.remote_addr
        );
        Ok(())
    }

    /// Gets the name of the middleware.
    /// 
    /// # Returns
    /// 
    /// The string "LoggingMiddleware"
    fn name(&self) -> &'static str {
        "LoggingMiddleware"
    }
}

/// Request ID middleware for processing request IDs.
/// 
/// This middleware handles request ID generation and processing.
/// Note: Request IDs are already generated in `RiGatewayRequest::new`.
pub struct RiRequestIdMiddleware;

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

impl RiRequestIdMiddleware {
    /// Creates a new request ID middleware instance.
    /// 
    /// # Returns
    /// 
    /// A new `RiRequestIdMiddleware` instance
    pub fn new() -> Self {
        Self
    }
}

#[async_trait]
impl RiMiddleware for RiRequestIdMiddleware {
    /// Processes the request ID in the request.
    /// 
    /// This implementation is a no-op since request IDs are generated in `RiGatewayRequest::new`.
    /// It can be extended for additional request ID processing.
    /// 
    /// # Parameters
    /// 
    /// - `_request`: Mutable reference to the request being processed
    /// 
    /// # Returns
    /// 
    /// A `RiResult<()>` indicating success or failure
    async fn execute(&self, _request: &mut RiGatewayRequest) -> RiResult<()> {
        // Request ID is already generated in RiGatewayRequest::new
        // This middleware can be used for additional request ID processing
        Ok(())
    }

    /// Gets the name of the middleware.
    /// 
    /// # Returns
    /// 
    /// The string "RequestIdMiddleware"
    fn name(&self) -> &'static str {
        "RequestIdMiddleware"
    }
}

/// Rate limiting middleware for controlling request rates.
/// 
/// This middleware limits the number of requests from a client within a specified time window.
pub struct RiRateLimitMiddleware {
    /// Rate limiter instance for enforcing rate limits
    rate_limiter: Arc<crate::gateway::RiRateLimiter>,
}

impl RiRateLimitMiddleware {
    /// Creates a new rate limiting middleware instance.
    /// 
    /// # Parameters
    /// 
    /// - `rate_limiter`: Rate limiter instance for enforcing rate limits
    /// 
    /// # Returns
    /// 
    /// A new `RiRateLimitMiddleware` instance
    pub fn new(rate_limiter: Arc<crate::gateway::RiRateLimiter>) -> Self {
        Self {
            rate_limiter,
        }
    }
}

#[async_trait]
impl RiMiddleware for RiRateLimitMiddleware {
    /// Applies rate limiting to the request.
    /// 
    /// This implementation uses the RiRateLimiter to check if the request should be allowed
    /// based on rate limiting rules. If the request exceeds the rate limit, an error is returned.
    /// 
    /// # Parameters
    /// 
    /// - `request`: Mutable reference to the request being processed
    /// 
    /// # Returns
    /// 
    /// A `RiResult<()>` indicating success or failure. Returns error if rate limit exceeded.
    async fn execute(&self, request: &mut RiGatewayRequest) -> RiResult<()> {
        // Check rate limit using the rate limiter
        if !self.rate_limiter.check_request(request).await {
            return Err(crate::core::RiError::Other("Rate limit exceeded".to_string()));
        }
        
        Ok(())
    }

    /// Gets the name of the middleware.
    /// 
    /// # Returns
    /// 
    /// The string "RateLimitMiddleware"
    fn name(&self) -> &'static str {
        "RateLimitMiddleware"
    }
}