rustysquid 1.2.0

High-performance HTTP caching proxy optimized for embedded systems and routers
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
use bytes::Bytes;
use lru::LruCache;
use std::num::NonZeroUsize;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::sync::Mutex;

pub mod memory;
pub mod connection_pool;

/// Maximum number of cache entries
pub const CACHE_SIZE: usize = 10000;

/// Maximum size of a single response that can be cached (10MB)
pub const MAX_RESPONSE_SIZE: usize = 10 * 1024 * 1024;

/// Maximum total size of all cached entries (50MB)
pub const MAX_CACHE_BYTES: usize = 50 * 1024 * 1024;

/// Maximum size of a single cache entry (5MB)
pub const MAX_ENTRY_SIZE: usize = 5 * 1024 * 1024;

/// Default cache TTL in seconds (1 hour)
pub const CACHE_TTL: u64 = 3600;

/// Maximum number of concurrent connections
pub const MAX_CONNECTIONS: usize = 100;

/// Maximum size of request headers (64KB)
pub const MAX_REQUEST_SIZE: usize = 64 * 1024;

/// A cached HTTP response
///
/// # Examples
///
/// ```
/// use rustysquid::CachedResponse;
/// use bytes::Bytes;
///
/// let response = CachedResponse {
///     status_line: "HTTP/1.1 200 OK".to_string(),
///     headers: vec!["Content-Type: text/html".to_string()],
///     body: Bytes::from("<html>Hello</html>"),
///     expires: 1234567890,
/// };
/// assert_eq!(response.status_line, "HTTP/1.1 200 OK");
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CachedResponse {
    pub status_line: String,
    pub headers: Vec<String>,
    pub body: Bytes,
    pub expires: u64,
}

/// Thread-safe LRU cache for HTTP responses
#[derive(Clone)]
pub struct ProxyCache {
    cache: Arc<Mutex<LruCache<u64, Arc<CachedResponse>>>>,
    total_size: Arc<AtomicUsize>,
}

impl ProxyCache {
    /// Creates a new `ProxyCache` with the default cache size.
    ///
    /// # Examples
    ///
    /// ```
    /// use rustysquid::ProxyCache;
    ///
    /// let cache = ProxyCache::new();
    /// assert_eq!(cache.total_size(), 0);
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if `CACHE_SIZE` is 0, which should never happen in normal operation.
    pub fn new() -> Self {
        Self {
            cache: Arc::new(Mutex::new(LruCache::new(
                NonZeroUsize::new(CACHE_SIZE).expect("CACHE_SIZE must be non-zero"),
            ))),
            total_size: Arc::new(AtomicUsize::new(0)),
        }
    }

    /// Check if the cache is empty
    ///
    /// # Examples
    ///
    /// ```
    /// # tokio_test::block_on(async {
    /// use rustysquid::ProxyCache;
    ///
    /// let cache = ProxyCache::new();
    /// assert!(cache.is_empty().await);
    /// # })
    /// ```
    pub async fn is_empty(&self) -> bool {
        let cache = self.cache.lock().await;
        cache.is_empty()
    }

    /// Get a cached response by key, returns None if not found or expired
    pub async fn get(&self, key: u64) -> Option<Arc<CachedResponse>> {
        let mut cache = self.cache.lock().await;
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        if let Some(entry) = cache.get(&key) {
            if entry.expires > now {
                return Some(Arc::clone(entry));
            }
            // Remove expired entry and update size
            if let Some(expired) = cache.pop(&key) {
                let size = Self::calculate_entry_size(&expired);
                self.total_size.fetch_sub(size, Ordering::Relaxed);
            }
        }
        None
    }

    /// Store a response in the cache, returns false if rejected (too large, memory pressure, etc)
    pub async fn put(&self, key: u64, response: CachedResponse) -> bool {
        // Check memory pressure
        if !memory::has_sufficient_memory() {
            return false;
        }

        let entry_size = Self::calculate_entry_size(&response);

        // Reject entries that are too large
        if entry_size > MAX_ENTRY_SIZE {
            return false;
        }

        let mut cache = self.cache.lock().await;

        // Check if we need to evict entries to make room
        let mut current_size = self.total_size.load(Ordering::Relaxed);
        while current_size + entry_size > MAX_CACHE_BYTES && !cache.is_empty() {
            // Evict LRU entry
            if let Some((_, evicted)) = cache.pop_lru() {
                let evicted_size = Self::calculate_entry_size(&evicted);
                self.total_size.fetch_sub(evicted_size, Ordering::Relaxed);
                current_size = self.total_size.load(Ordering::Relaxed);
            } else {
                break;
            }
        }

        // Remove old entry if it exists
        if let Some(old) = cache.get(&key) {
            let old_size = Self::calculate_entry_size(old);
            self.total_size.fetch_sub(old_size, Ordering::Relaxed);
        }

        // Add new entry wrapped in Arc
        cache.put(key, Arc::new(response));
        self.total_size.fetch_add(entry_size, Ordering::Relaxed);
        true
    }

    pub async fn clear(&self) {
        let mut cache = self.cache.lock().await;
        cache.clear();
        self.total_size.store(0, Ordering::Relaxed);
    }

    /// Get the number of entries in the cache
    ///
    /// # Examples
    ///
    /// ```
    /// # tokio_test::block_on(async {
    /// use rustysquid::{ProxyCache, CachedResponse};
    /// use bytes::Bytes;
    ///
    /// let cache = ProxyCache::new();
    /// assert_eq!(cache.len().await, 0);
    ///
    /// let response = CachedResponse {
    ///     status_line: "HTTP/1.1 200 OK".to_string(),
    ///     headers: vec![],
    ///     body: Bytes::from("test"),
    ///     expires: u64::MAX,
    /// };
    /// cache.put(12345, response).await;
    /// assert_eq!(cache.len().await, 1);
    /// # })
    /// ```
    pub async fn len(&self) -> usize {
        let cache = self.cache.lock().await;
        cache.len()
    }

    /// Get the total size of all cached entries in bytes
    ///
    /// # Examples
    ///
    /// ```
    /// use rustysquid::ProxyCache;
    ///
    /// let cache = ProxyCache::new();
    /// assert_eq!(cache.total_size(), 0);
    /// ```
    pub fn total_size(&self) -> usize {
        self.total_size.load(Ordering::Relaxed)
    }

    fn calculate_entry_size(entry: &CachedResponse) -> usize {
        entry.status_line.len()
            + entry
                .headers
                .iter()
                .map(std::string::String::len)
                .sum::<usize>()
            + entry.body.len()
            + std::mem::size_of::<u64>() // expires field
            + std::mem::size_of::<Arc<CachedResponse>>() // Arc overhead
    }
}

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

/// Parse an HTTP request, returns (method, path, headers) or None if invalid
///
/// # Examples
///
/// ```
/// use rustysquid::parse_request;
///
/// let request = b"GET /index.html HTTP/1.1\r\nHost: example.com\r\n\r\n";
/// let result = parse_request(request);
/// assert!(result.is_some());
/// let (method, path, headers) = result.unwrap();
/// assert_eq!(method, "GET");
/// assert_eq!(path, "/index.html");
/// assert_eq!(headers[0], "Host: example.com");
/// ```
pub fn parse_request(data: &[u8]) -> Option<(String, String, Vec<String>)> {
    let mut headers = [httparse::EMPTY_HEADER; 64];
    let mut req = httparse::Request::new(&mut headers);

    match req.parse(data) {
        Ok(httparse::Status::Complete(_)) => {
            let method = req.method?.to_string();
            let path = req.path?.to_string();
            let headers: Vec<String> = req
                .headers
                .iter()
                .map(|h| format!("{}: {}", h.name, String::from_utf8_lossy(h.value)))
                .collect();
            Some((method, path, headers))
        }
        _ => None,
    }
}

/// Extract host and port from HTTP headers
///
/// # Examples
///
/// ```
/// use rustysquid::extract_host;
///
/// let headers = vec!["Host: example.com:8080".to_string()];
/// assert_eq!(extract_host(&headers), Some(("example.com".to_string(), 8080)));
///
/// let headers = vec!["Host: example.com".to_string()];
/// assert_eq!(extract_host(&headers), Some(("example.com".to_string(), 80)));
///
/// let headers = vec!["Content-Type: text/html".to_string()];
/// assert_eq!(extract_host(&headers), None);
/// ```
pub fn extract_host(headers: &[String]) -> Option<(String, u16)> {
    for header in headers {
        if header.to_lowercase().starts_with("host:") {
            let host_value = header[5..].trim();
            if let Some(colon_pos) = host_value.rfind(':') {
                let host = host_value[..colon_pos].to_string();
                let port = host_value[colon_pos + 1..].parse::<u16>().unwrap_or(80);
                return Some((host, port));
            }
            return Some((host_value.to_string(), 80));
        }
    }
    None
}

/// Determine if a response should be cached based on method, path, and headers
///
/// # Examples
///
/// ```
/// use rustysquid::is_cacheable;
///
/// // GET requests are cacheable by default
/// assert!(is_cacheable("GET", "/index.html", &[]));
///
/// // POST requests are never cached
/// assert!(!is_cacheable("POST", "/api", &[]));
///
/// // Respect Cache-Control headers
/// let headers = vec!["Cache-Control: no-cache".to_string()];
/// assert!(!is_cacheable("GET", "/index.html", &headers));
///
/// // Private responses are not cached
/// let headers = vec!["Cache-Control: private".to_string()];
/// assert!(!is_cacheable("GET", "/user", &headers));
/// ```
pub fn is_cacheable(method: &str, path: &str, response_headers: &[String]) -> bool {
    if method != "GET" {
        return false;
    }

    // Check headers first - they override everything
    for header in response_headers {
        let header_lower = header.to_lowercase();
        if header_lower.starts_with("cache-control:") {
            if header_lower.contains("no-cache") 
                || header_lower.contains("no-store") 
                || header_lower.contains("private") {
                return false;
            }
            if header_lower.contains("max-age=") {
                return true;
            }
        }
    }

    // Check for static content extensions
    let cacheable_extensions = [
        ".jpg", ".jpeg", ".png", ".gif", ".ico", ".css", ".js", ".woff", ".woff2", ".ttf", ".svg",
        ".webp", ".mp4", ".webm", ".html", ".htm", ".xml", ".json", ".txt",
    ];

    let path_lower = path.to_lowercase();
    // Cache if it has a cacheable extension or is the root path
    path == "/" || cacheable_extensions
        .iter()
        .any(|ext| path_lower.ends_with(ext))
}

/// Calculate TTL from Cache-Control headers, defaults to `CACHE_TTL`
pub fn calculate_ttl(headers: &[String]) -> u64 {
    for header in headers {
        let header_lower = header.to_lowercase();
        if header_lower.starts_with("cache-control:") {
            if let Some(max_age_pos) = header_lower.find("max-age=") {
                let start = max_age_pos + 8;
                let value_str = &header_lower[start..];
                if let Some(end) = value_str.find(|c: char| !c.is_ascii_digit()) {
                    if let Ok(seconds) = value_str[..end].parse::<u64>() {
                        return seconds.min(86400);
                    }
                } else if let Ok(seconds) = value_str.parse::<u64>() {
                    return seconds.min(86400);
                }
            }
        }
    }
    CACHE_TTL
}

/// Create a cache key from request parameters without allocation
pub fn create_cache_key(host: &str, port: u16, path: &str) -> u64 {
    use xxhash_rust::xxh64::Xxh64;
    
    let mut hasher = Xxh64::new(0);
    hasher.update(host.as_bytes());
    hasher.update(b":");
    hasher.update(&port.to_le_bytes());
    hasher.update(path.as_bytes());
    hasher.digest()
}

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

    #[test]
    fn test_extract_host() {
        let headers = vec![
            "Host: example.com".to_string(),
            "User-Agent: test".to_string(),
        ];
        assert_eq!(
            extract_host(&headers),
            Some(("example.com".to_string(), 80))
        );

        let headers_with_port = vec!["Host: example.com:8080".to_string()];
        assert_eq!(
            extract_host(&headers_with_port),
            Some(("example.com".to_string(), 8080))
        );
    }

    #[test]
    fn test_is_cacheable() {
        // Static content should be cacheable
        assert!(is_cacheable("GET", "/image.jpg", &[]));
        assert!(is_cacheable("GET", "/style.css", &[]));
        assert!(is_cacheable("GET", "/script.js", &[]));

        // POST requests should not be cacheable
        assert!(!is_cacheable("POST", "/image.jpg", &[]));

        // Respect no-cache headers
        let no_cache_headers = vec!["Cache-Control: no-cache".to_string()];
        assert!(!is_cacheable("GET", "/image.jpg", &no_cache_headers));

        // Respect max-age headers
        let max_age_headers = vec!["Cache-Control: max-age=3600".to_string()];
        assert!(is_cacheable("GET", "/api/data", &max_age_headers));
    }

    #[test]
    fn test_calculate_ttl() {
        let headers_with_max_age = vec!["Cache-Control: max-age=7200".to_string()];
        assert_eq!(calculate_ttl(&headers_with_max_age), 7200);

        let headers_with_large_max_age = vec!["Cache-Control: max-age=999999".to_string()];
        assert_eq!(calculate_ttl(&headers_with_large_max_age), 86400); // Capped at 24 hours

        let headers_without_cache = vec!["Content-Type: text/html".to_string()];
        assert_eq!(calculate_ttl(&headers_without_cache), CACHE_TTL);
    }

    #[test]
    fn test_cache_key_generation() {
        let key1 = create_cache_key("example.com", 80, "/index.html");
        let key2 = create_cache_key("example.com", 80, "/index.html");
        let key3 = create_cache_key("example.com", 80, "/other.html");

        assert_eq!(key1, key2); // Same input should produce same key
        assert_ne!(key1, key3); // Different input should produce different key
    }

    #[tokio::test]
    async fn test_proxy_cache_operations() {
        let cache = ProxyCache::new();

        // Test empty cache
        assert_eq!(cache.len().await, 0);

        // Test cache miss
        let key = create_cache_key("test.com", 80, "/test");
        assert!(cache.get(key).await.is_none());

        // Test cache put and get
        let response = CachedResponse {
            status_line: "HTTP/1.1 200 OK\r\n".to_string(),
            headers: vec!["Content-Type: text/html".to_string()],
            body: Bytes::from("test body"),
            expires: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs()
                + 3600,
        };

        cache.put(key, response.clone()).await;
        assert_eq!(cache.len().await, 1);

        let cached = cache.get(key).await;
        assert!(cached.is_some());
        let cached_arc = cached.unwrap();
        assert_eq!(*cached_arc, response);

        // Test cache clear
        cache.clear().await;
        assert_eq!(cache.len().await, 0);
    }

    #[tokio::test]
    async fn test_cache_size_limit() {
        let cache = ProxyCache::new();

        // Create a large response (1MB)
        let large_response = CachedResponse {
            status_line: "HTTP/1.1 200 OK\r\n".to_string(),
            headers: vec!["Content-Type: text/html".to_string()],
            body: Bytes::from(vec![0u8; 1024 * 1024]), // 1MB
            expires: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs()
                + 3600,
        };

        // Add entries until we exceed the limit
        for i in 0..60 {
            let key = create_cache_key("test.com", 80, &format!("/page{i}"));
            cache.put(key, large_response.clone()).await;
        }

        // Total size should not exceed MAX_CACHE_BYTES
        assert!(cache.total_size() <= MAX_CACHE_BYTES);

        // Cache should have evicted some entries
        assert!(cache.len().await < 60);
    }

    #[tokio::test]
    async fn test_entry_size_limit() {
        let cache = ProxyCache::new();

        // Create an oversized response (> MAX_ENTRY_SIZE)
        let oversized = CachedResponse {
            status_line: "HTTP/1.1 200 OK\r\n".to_string(),
            headers: vec![],
            body: Bytes::from(vec![0u8; MAX_ENTRY_SIZE + 1]),
            expires: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs()
                + 3600,
        };

        let key = create_cache_key("test.com", 80, "/large");
        let result = cache.put(key, oversized).await;

        // Should reject oversized entry
        assert!(!result);
        assert_eq!(cache.len().await, 0);
        assert_eq!(cache.total_size(), 0);
    }

    #[tokio::test]
    async fn test_cache_expiration() {
        let cache = ProxyCache::new();
        let key = create_cache_key("test.com", 80, "/expired");

        // Add expired entry
        let expired_response = CachedResponse {
            status_line: "HTTP/1.1 200 OK\r\n".to_string(),
            headers: vec![],
            body: Bytes::from("expired"),
            expires: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs()
                - 1, // Already expired
        };

        cache.put(key, expired_response).await;

        // Should not return expired entry
        assert!(cache.get(key).await.is_none());
    }
}