a3s-gateway 0.2.1

A3S Gateway - AI-native API gateway with reverse proxy, routing, and agent orchestration
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
//! Compress middleware — gzip/deflate response compression
//!
//! Compresses response bodies based on the client's Accept-Encoding header.
//! Supports gzip and deflate compression algorithms.

#![allow(dead_code)]
use crate::error::Result;
use crate::middleware::{Middleware, RequestContext};
use async_trait::async_trait;
use flate2::write::{DeflateEncoder, GzEncoder};
use flate2::Compression;
use http::Response;
use std::io::Write;

/// Supported compression encoding
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Encoding {
    Brotli,
    Gzip,
    Deflate,
    Identity,
}

impl Encoding {
    /// Content-Encoding header value
    pub fn header_value(&self) -> &'static str {
        match self {
            Self::Brotli => "br",
            Self::Gzip => "gzip",
            Self::Deflate => "deflate",
            Self::Identity => "identity",
        }
    }
}

impl std::fmt::Display for Encoding {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.header_value())
    }
}

/// Compression middleware configuration
#[derive(Debug, Clone)]
pub struct CompressConfig {
    /// Minimum response size to compress (in bytes)
    pub min_size: usize,
    /// Compression level (0-9, higher = better compression, slower)
    pub level: u32,
}

impl Default for CompressConfig {
    fn default() -> Self {
        Self {
            min_size: 1024, // Don't compress responses < 1KB
            level: 6,       // Default compression level
        }
    }
}

/// Compress middleware — handles Accept-Encoding negotiation
pub struct CompressMiddleware {
    config: CompressConfig,
}

impl CompressMiddleware {
    /// Create with default configuration
    pub fn new() -> Self {
        Self {
            config: CompressConfig::default(),
        }
    }

    /// Create with custom configuration
    pub fn with_config(config: CompressConfig) -> Self {
        Self { config }
    }

    /// Get the configuration
    pub fn config(&self) -> &CompressConfig {
        &self.config
    }

    /// Parse Accept-Encoding header and return the best supported encoding
    ///
    /// Preference order: br > gzip > deflate
    pub fn negotiate_encoding(accept_encoding: &str) -> Encoding {
        let lower = accept_encoding.to_lowercase();
        // Prefer brotli over gzip over deflate
        if lower.contains("br") {
            Encoding::Brotli
        } else if lower.contains("gzip") {
            Encoding::Gzip
        } else if lower.contains("deflate") {
            Encoding::Deflate
        } else {
            Encoding::Identity
        }
    }

    /// Compress data with the given encoding
    pub fn compress(
        data: &[u8],
        encoding: Encoding,
        level: u32,
    ) -> std::result::Result<Vec<u8>, String> {
        match encoding {
            Encoding::Brotli => {
                let quality = level.min(11); // Brotli quality: 0-11
                let mut output = Vec::new();
                let params = brotli::enc::BrotliEncoderParams {
                    quality: quality as i32,
                    ..Default::default()
                };
                brotli::BrotliCompress(&mut std::io::Cursor::new(data), &mut output, &params)
                    .map_err(|e| format!("Brotli compression failed: {}", e))?;
                Ok(output)
            }
            Encoding::Gzip => {
                let compression = Compression::new(level);
                let mut encoder = GzEncoder::new(Vec::new(), compression);
                encoder
                    .write_all(data)
                    .map_err(|e| format!("Gzip compression failed: {}", e))?;
                encoder
                    .finish()
                    .map_err(|e| format!("Gzip finalize failed: {}", e))
            }
            Encoding::Deflate => {
                let compression = Compression::new(level);
                let mut encoder = DeflateEncoder::new(Vec::new(), compression);
                encoder
                    .write_all(data)
                    .map_err(|e| format!("Deflate compression failed: {}", e))?;
                encoder
                    .finish()
                    .map_err(|e| format!("Deflate finalize failed: {}", e))
            }
            Encoding::Identity => Ok(data.to_vec()),
        }
    }

    /// Check if a content type should be compressed
    pub fn is_compressible(content_type: &str) -> bool {
        let ct = content_type.to_lowercase();
        ct.starts_with("text/")
            || ct.contains("json")
            || ct.contains("xml")
            || ct.contains("javascript")
            || ct.contains("css")
            || ct.contains("svg")
            || ct.contains("html")
    }
}

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

#[async_trait]
impl Middleware for CompressMiddleware {
    async fn handle_request(
        &self,
        _req: &mut http::request::Parts,
        _ctx: &RequestContext,
    ) -> Result<Option<Response<Vec<u8>>>> {
        // Compression is applied on the response side, pass through on request
        Ok(None)
    }

    async fn handle_response(&self, resp: &mut http::response::Parts) -> Result<()> {
        // Mark that compression should be applied by adding a header
        // The actual compression happens in the proxy layer when building
        // the response body. Here we just set the Content-Encoding header
        // if the response is eligible.
        //
        // Note: In a real implementation, the proxy layer would check this
        // header and compress the body before sending it to the client.
        if !resp.headers.contains_key("content-encoding") {
            resp.headers
                .insert("x-gateway-compress", "eligible".parse().unwrap());
        }
        Ok(())
    }

    fn name(&self) -> &str {
        "compress"
    }
}

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

    // --- Encoding tests ---

    #[test]
    fn test_encoding_header_values() {
        assert_eq!(Encoding::Brotli.header_value(), "br");
        assert_eq!(Encoding::Gzip.header_value(), "gzip");
        assert_eq!(Encoding::Deflate.header_value(), "deflate");
        assert_eq!(Encoding::Identity.header_value(), "identity");
    }

    #[test]
    fn test_encoding_display() {
        assert_eq!(Encoding::Brotli.to_string(), "br");
        assert_eq!(Encoding::Gzip.to_string(), "gzip");
        assert_eq!(Encoding::Deflate.to_string(), "deflate");
    }

    // --- Negotiate encoding tests ---

    #[test]
    fn test_negotiate_gzip() {
        assert_eq!(
            CompressMiddleware::negotiate_encoding("gzip, deflate"),
            Encoding::Gzip
        );
    }

    #[test]
    fn test_negotiate_deflate() {
        assert_eq!(
            CompressMiddleware::negotiate_encoding("deflate"),
            Encoding::Deflate
        );
    }

    #[test]
    fn test_negotiate_brotli() {
        assert_eq!(
            CompressMiddleware::negotiate_encoding("br"),
            Encoding::Brotli
        );
    }

    #[test]
    fn test_negotiate_brotli_preferred_over_gzip() {
        assert_eq!(
            CompressMiddleware::negotiate_encoding("gzip, br, deflate"),
            Encoding::Brotli
        );
    }

    #[test]
    fn test_negotiate_identity() {
        assert_eq!(
            CompressMiddleware::negotiate_encoding("zstd"),
            Encoding::Identity
        );
    }

    #[test]
    fn test_negotiate_case_insensitive() {
        assert_eq!(
            CompressMiddleware::negotiate_encoding("GZIP"),
            Encoding::Gzip
        );
    }

    #[test]
    fn test_negotiate_gzip_preferred() {
        assert_eq!(
            CompressMiddleware::negotiate_encoding("deflate, gzip"),
            Encoding::Gzip
        );
    }

    #[test]
    fn test_negotiate_empty() {
        assert_eq!(
            CompressMiddleware::negotiate_encoding(""),
            Encoding::Identity
        );
    }

    // --- Compression tests ---

    #[test]
    fn test_gzip_compress_decompress() {
        let data = b"Hello, World! This is test data for compression.";
        let compressed = CompressMiddleware::compress(data, Encoding::Gzip, 6).unwrap();
        assert!(compressed.len() < data.len() || data.len() < 50);
        // Verify it's valid gzip (starts with gzip magic bytes)
        assert_eq!(compressed[0], 0x1f);
        assert_eq!(compressed[1], 0x8b);
    }

    #[test]
    fn test_deflate_compress() {
        let data = b"Hello, World! This is test data for compression that should be long enough.";
        let compressed = CompressMiddleware::compress(data, Encoding::Deflate, 6).unwrap();
        assert!(!compressed.is_empty());
    }

    #[test]
    fn test_identity_no_compression() {
        let data = b"Hello, World!";
        let result = CompressMiddleware::compress(data, Encoding::Identity, 6).unwrap();
        assert_eq!(result, data);
    }

    #[test]
    fn test_compress_empty_data() {
        let compressed = CompressMiddleware::compress(b"", Encoding::Gzip, 6).unwrap();
        assert!(!compressed.is_empty()); // Gzip has header even for empty data
    }

    #[test]
    fn test_compress_large_data() {
        let data = vec![b'A'; 10000];
        let compressed = CompressMiddleware::compress(&data, Encoding::Gzip, 6).unwrap();
        // Highly repetitive data should compress well
        assert!(compressed.len() < data.len() / 2);
    }

    #[test]
    fn test_compression_levels() {
        let data = vec![b'X'; 5000];
        let fast = CompressMiddleware::compress(&data, Encoding::Gzip, 1).unwrap();
        let best = CompressMiddleware::compress(&data, Encoding::Gzip, 9).unwrap();
        // Both should work, best should be ≤ fast
        assert!(best.len() <= fast.len());
    }

    // --- Brotli compression tests ---

    #[test]
    fn test_brotli_compress() {
        let data = b"Hello, World! This is test data for brotli compression testing.";
        let compressed = CompressMiddleware::compress(data, Encoding::Brotli, 6).unwrap();
        assert!(!compressed.is_empty());
    }

    #[test]
    fn test_brotli_compress_large_data() {
        let data = vec![b'A'; 10000];
        let compressed = CompressMiddleware::compress(&data, Encoding::Brotli, 6).unwrap();
        // Highly repetitive data should compress well
        assert!(compressed.len() < data.len() / 2);
    }

    #[test]
    fn test_brotli_compress_empty() {
        let compressed = CompressMiddleware::compress(b"", Encoding::Brotli, 6).unwrap();
        // Brotli produces output even for empty data
        assert!(!compressed.is_empty());
    }

    #[test]
    fn test_brotli_quality_clamped() {
        // Quality > 11 should be clamped to 11
        let data = b"test data for quality clamping";
        let result = CompressMiddleware::compress(data, Encoding::Brotli, 20);
        assert!(result.is_ok());
    }

    #[test]
    fn test_brotli_vs_gzip_size() {
        // For text data, brotli should generally compress better than gzip
        let data = "The quick brown fox jumps over the lazy dog. ".repeat(100);
        let br = CompressMiddleware::compress(data.as_bytes(), Encoding::Brotli, 6).unwrap();
        let gz = CompressMiddleware::compress(data.as_bytes(), Encoding::Gzip, 6).unwrap();
        // Brotli should be at least as good as gzip for text
        assert!(br.len() <= gz.len());
    }

    // --- Compressible content type tests ---

    #[test]
    fn test_is_compressible_text() {
        assert!(CompressMiddleware::is_compressible("text/html"));
        assert!(CompressMiddleware::is_compressible("text/plain"));
        assert!(CompressMiddleware::is_compressible("text/css"));
    }

    #[test]
    fn test_is_compressible_json() {
        assert!(CompressMiddleware::is_compressible("application/json"));
    }

    #[test]
    fn test_is_compressible_xml() {
        assert!(CompressMiddleware::is_compressible("application/xml"));
        assert!(CompressMiddleware::is_compressible("text/xml"));
    }

    #[test]
    fn test_is_compressible_javascript() {
        assert!(CompressMiddleware::is_compressible(
            "application/javascript"
        ));
    }

    #[test]
    fn test_is_compressible_svg() {
        assert!(CompressMiddleware::is_compressible("image/svg+xml"));
    }

    #[test]
    fn test_not_compressible_binary() {
        assert!(!CompressMiddleware::is_compressible("image/png"));
        assert!(!CompressMiddleware::is_compressible("image/jpeg"));
        assert!(!CompressMiddleware::is_compressible(
            "application/octet-stream"
        ));
    }

    #[test]
    fn test_is_compressible_case_insensitive() {
        assert!(CompressMiddleware::is_compressible("Application/JSON"));
    }

    // --- Config tests ---

    #[test]
    fn test_default_config() {
        let config = CompressConfig::default();
        assert_eq!(config.min_size, 1024);
        assert_eq!(config.level, 6);
    }

    #[test]
    fn test_custom_config() {
        let mw = CompressMiddleware::with_config(CompressConfig {
            min_size: 512,
            level: 9,
        });
        assert_eq!(mw.config().min_size, 512);
        assert_eq!(mw.config().level, 9);
    }

    // --- Middleware interface ---

    #[test]
    fn test_middleware_name() {
        let mw = CompressMiddleware::new();
        assert_eq!(mw.name(), "compress");
    }

    #[test]
    fn test_default_impl() {
        let mw = CompressMiddleware::default();
        assert_eq!(mw.config().min_size, 1024);
    }

    #[tokio::test]
    async fn test_request_passthrough() {
        let mw = CompressMiddleware::new();
        let (mut parts, _) = http::Request::builder()
            .uri("/test")
            .header("Accept-Encoding", "gzip")
            .body(())
            .unwrap()
            .into_parts();
        let ctx = RequestContext {
            client_ip: "127.0.0.1".to_string(),
            entrypoint: "web".to_string(),
            router: "test".to_string(),
        };
        let result = mw.handle_request(&mut parts, &ctx).await.unwrap();
        assert!(result.is_none()); // Always passes through
    }

    #[tokio::test]
    async fn test_response_marks_eligible() {
        let mw = CompressMiddleware::new();
        let (mut parts, _) = http::Response::builder()
            .status(200)
            .body(())
            .unwrap()
            .into_parts();
        mw.handle_response(&mut parts).await.unwrap();
        assert_eq!(parts.headers.get("x-gateway-compress").unwrap(), "eligible");
    }

    #[tokio::test]
    async fn test_response_already_encoded_skipped() {
        let mw = CompressMiddleware::new();
        let (mut parts, _) = http::Response::builder()
            .status(200)
            .header("content-encoding", "gzip")
            .body(())
            .unwrap()
            .into_parts();
        mw.handle_response(&mut parts).await.unwrap();
        assert!(parts.headers.get("x-gateway-compress").is_none());
    }
}