irondrop 2.7.2

Drop files, not dependencies - a well tested fully featured & battle-ready server in a single Rust binary with support for indexing through 10M files.
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
// SPDX-License-Identifier: MIT

use crate::cli::Cli;
use crate::config::Config;
use crate::error::AppError;
use crate::handlers::register_internal_routes;
use crate::middleware::AuthMiddleware;
use crate::router::Router;
use glob::Pattern;
use log::{debug, info, trace, warn};
use std::collections::HashMap;
use std::net::{IpAddr, SocketAddr};
use std::sync::{Arc, Mutex, mpsc};
use std::time::{Duration, Instant};

use rustls::ServerConfig;
use std::io::BufReader;
use tokio::net::TcpStream as TokioTcpStream;

#[cfg(target_os = "linux")]
use std::fs;
#[cfg(any(target_os = "macos", target_os = "windows"))]
use std::mem;

/// Rate limiter for basic DoS protection
#[derive(Clone)]
pub struct RateLimiter {
    connections: Arc<Mutex<HashMap<IpAddr, ConnectionInfo>>>,
    max_requests_per_minute: u32,
    max_concurrent_per_ip: u32,
    max_connections_per_ip: u32,
}

#[derive(Debug)]
struct ConnectionInfo {
    request_count: u32,
    last_reset: Instant,
    active_connections: u32,
    last_activity: Instant,
    total_connections: u32,
}

impl RateLimiter {
    pub fn new(max_requests_per_minute: u32, max_concurrent_per_ip: u32) -> Self {
        Self {
            connections: Arc::new(Mutex::new(HashMap::new())),
            max_requests_per_minute,
            max_concurrent_per_ip,
            max_connections_per_ip: 1000, // Limit stored connections per IP
        }
    }

    pub fn check_rate_limit(&self, ip: IpAddr) -> bool {
        trace!("Checking rate limit for IP: {}", ip);
        let mut connections = self.connections.lock().unwrap();
        let now = Instant::now();

        // Hard limit on the number of entries to prevent unbounded memory growth
        const MAX_RATE_LIMITER_ENTRIES: usize = 100_000;

        // Check if we need to evict entries before inserting a new one
        if !connections.contains_key(&ip) && connections.len() >= MAX_RATE_LIMITER_ENTRIES {
            // Smart eviction: find the least recently used entry (oldest last_activity)
            if let Some((&lru_ip, _)) = connections
                .iter()
                .min_by_key(|(_, info)| info.last_activity)
            {
                let lru_ip_copy = lru_ip;
                connections.remove(&lru_ip_copy);
                debug!(
                    "Evicted LRU entry for IP {} to make space for new IP {}",
                    lru_ip_copy, ip
                );
            }
        }

        let conn_info = connections.entry(ip).or_insert(ConnectionInfo {
            request_count: 0,
            last_reset: now,
            active_connections: 0,
            last_activity: now,
            total_connections: 0,
        });

        trace!(
            "IP {} current state: requests={}, active_conns={}, total_conns={}",
            ip, conn_info.request_count, conn_info.active_connections, conn_info.total_connections
        );

        // Reset counter if more than a minute has passed
        if now.duration_since(conn_info.last_reset) >= Duration::from_secs(60) {
            trace!("Resetting request counter for IP {} (minute elapsed)", ip);
            conn_info.request_count = 0;
            conn_info.last_reset = now;
        }

        // Check concurrent connections
        if conn_info.active_connections >= self.max_concurrent_per_ip {
            warn!("Rate limit exceeded for {ip}: too many concurrent connections");
            return false;
        }

        // Check request rate
        if conn_info.request_count >= self.max_requests_per_minute {
            warn!("Rate limit exceeded for {ip}: too many requests per minute");
            return false;
        }

        conn_info.request_count += 1;
        conn_info.active_connections += 1;
        conn_info.last_activity = now;
        conn_info.total_connections += 1;

        trace!(
            "Rate limit check passed for IP {}: new counts - requests={}, active_conns={}",
            ip, conn_info.request_count, conn_info.active_connections
        );

        // Check if this IP has too many stored connections
        if conn_info.total_connections > self.max_connections_per_ip {
            warn!("IP {ip} has exceeded max stored connections limit");
        }

        true
    }

    pub fn release_connection(&self, ip: IpAddr) {
        trace!("Releasing connection for IP: {}", ip);
        if let Ok(mut connections) = self.connections.lock() {
            if let Some(conn_info) = connections.get_mut(&ip) {
                let old_count = conn_info.active_connections;
                conn_info.active_connections = conn_info.active_connections.saturating_sub(1);
                conn_info.last_activity = Instant::now();
                trace!(
                    "IP {} active connections: {} -> {}",
                    ip, old_count, conn_info.active_connections
                );
            } else {
                trace!("No connection info found for IP {} during release", ip);
            }
        }
    }

    pub fn cleanup_old_entries(&self) {
        trace!("Starting manual cleanup of old rate limiter entries");
        let mut connections = self.connections.lock().unwrap();
        let now = Instant::now();
        let initial_count = connections.len();

        trace!("Rate limiter has {} entries before cleanup", initial_count);

        // Reduced retention time from 5 minutes to 2 minutes
        connections
            .retain(|_, info| now.duration_since(info.last_activity) < Duration::from_secs(120));

        let cleaned_count = initial_count - connections.len();
        if cleaned_count > 0 {
            debug!("Cleaned up {} old rate limiter entries", cleaned_count);
            // Reduce underlying capacity if we removed many entries
            if connections.capacity() > connections.len() * 2 {
                connections.shrink_to_fit();
            }
        } else {
            trace!("No old entries to clean up");
        }
    }

    /// Perform aggressive cleanup when memory pressure is detected
    pub fn cleanup_on_memory_pressure(&self) {
        debug!("Starting aggressive cleanup due to memory pressure");
        let mut connections = self.connections.lock().unwrap();
        let now = Instant::now();
        let initial_count = connections.len();

        trace!(
            "Memory pressure cleanup: checking {} entries",
            initial_count
        );

        // More aggressive cleanup - remove entries older than 30 seconds
        connections.retain(|_, info| {
            info.active_connections > 0
                || now.duration_since(info.last_activity) < Duration::from_secs(30)
        });

        let cleaned_count = initial_count - connections.len();
        if cleaned_count > 0 {
            warn!(
                "Memory pressure cleanup removed {} rate limiter entries",
                cleaned_count
            );
            if connections.capacity() > connections.len() * 2 {
                connections.shrink_to_fit();
            }
        } else {
            trace!("Memory pressure cleanup: no entries removed");
        }
    }

    /// Get rate limiter memory statistics
    pub fn get_memory_stats(&self) -> (usize, usize) {
        if let Ok(connections) = self.connections.lock() {
            let entry_count = connections.len();
            let estimated_memory = entry_count * std::mem::size_of::<(IpAddr, ConnectionInfo)>();
            trace!(
                "Rate limiter stats: {} entries, ~{} bytes",
                entry_count, estimated_memory
            );
            (entry_count, estimated_memory)
        } else {
            trace!("Failed to acquire rate limiter lock for stats");
            (0, 0)
        }
    }
}

/// Comprehensive server statistics and monitoring
///
/// Tracks both HTTP request statistics and file upload metrics with thread-safe
/// concurrent access using Arc<Mutex<T>> for all counters.
///
/// # Request Statistics
/// - Total requests processed (successful and failed)
/// - Bytes served via downloads
/// - Server uptime tracking
///
/// # Upload Statistics
/// - Upload request counts and success rates
/// - File upload counts and total bytes uploaded
/// - Processing time metrics and concurrent upload tracking
/// - Largest upload size tracking for capacity planning
///
/// All statistics are automatically reported every 5 minutes in the background
/// and provide comprehensive insights into server usage and performance.
#[derive(Default, Clone)]
pub struct ServerStats {
    // Request statistics
    pub total_requests: Arc<Mutex<u64>>,
    pub successful_requests: Arc<Mutex<u64>>,
    pub error_requests: Arc<Mutex<u64>>,
    pub bytes_served: Arc<Mutex<u64>>,
    pub start_time: Arc<Mutex<Option<Instant>>>,

    // Upload statistics
    pub total_uploads: Arc<Mutex<u64>>,
    pub successful_uploads: Arc<Mutex<u64>>,
    pub failed_uploads: Arc<Mutex<u64>>,
    pub files_uploaded: Arc<Mutex<u64>>,
    pub upload_bytes: Arc<Mutex<u64>>,
    pub largest_upload: Arc<Mutex<u64>>,
    pub concurrent_uploads: Arc<Mutex<u64>>,
    pub upload_processing_times: Arc<Mutex<Vec<u64>>>,

    // Memory statistics
    pub process_memory_bytes: Arc<Mutex<Option<u64>>>,
    pub peak_memory_bytes: Arc<Mutex<Option<u64>>>,
    pub last_memory_check: Arc<Mutex<Option<Instant>>>,
    pub memory_available: Arc<Mutex<bool>>,
}

impl ServerStats {
    pub fn new() -> Self {
        Self {
            // Request statistics
            total_requests: Arc::new(Mutex::new(0)),
            successful_requests: Arc::new(Mutex::new(0)),
            error_requests: Arc::new(Mutex::new(0)),
            bytes_served: Arc::new(Mutex::new(0)),
            start_time: Arc::new(Mutex::new(Some(Instant::now()))),

            // Upload statistics
            total_uploads: Arc::new(Mutex::new(0)),
            successful_uploads: Arc::new(Mutex::new(0)),
            failed_uploads: Arc::new(Mutex::new(0)),
            files_uploaded: Arc::new(Mutex::new(0)),
            upload_bytes: Arc::new(Mutex::new(0)),
            largest_upload: Arc::new(Mutex::new(0)),
            concurrent_uploads: Arc::new(Mutex::new(0)),
            upload_processing_times: Arc::new(Mutex::new(Vec::new())),

            // Memory statistics
            process_memory_bytes: Arc::new(Mutex::new(None)),
            peak_memory_bytes: Arc::new(Mutex::new(None)),
            last_memory_check: Arc::new(Mutex::new(None)),
            memory_available: Arc::new(Mutex::new(true)), // Assume available until proven otherwise
        }
    }

    pub fn record_request(&self, success: bool, bytes: u64) {
        trace!("Recording request: success={}, bytes={}", success, bytes);

        if let Ok(mut total) = self.total_requests.lock() {
            *total += 1;
            trace!("Total requests now: {}", *total);
        }

        if success {
            if let Ok(mut successful) = self.successful_requests.lock() {
                *successful += 1;
                trace!("Successful requests now: {}", *successful);
            }
        } else if let Ok(mut errors) = self.error_requests.lock() {
            *errors += 1;
            trace!("Error requests now: {}", *errors);
        }

        if let Ok(mut total_bytes) = self.bytes_served.lock() {
            *total_bytes += bytes;
            trace!("Total bytes served now: {}", *total_bytes);
        }
    }

    pub fn get_stats(&self) -> (u64, u64, u64, u64, Duration) {
        let total = *self
            .total_requests
            .lock()
            .unwrap_or_else(|_| panic!("Stats lock poisoned"));
        let successful = *self
            .successful_requests
            .lock()
            .unwrap_or_else(|_| panic!("Stats lock poisoned"));
        let errors = *self
            .error_requests
            .lock()
            .unwrap_or_else(|_| panic!("Stats lock poisoned"));
        let bytes = *self
            .bytes_served
            .lock()
            .unwrap_or_else(|_| panic!("Stats lock poisoned"));
        let uptime = self
            .start_time
            .lock()
            .unwrap_or_else(|_| panic!("Stats lock poisoned"))
            .map(|start| start.elapsed())
            .unwrap_or_default();

        (total, successful, errors, bytes, uptime)
    }

    /// Record an upload request and track statistics
    pub fn record_upload_request(
        &self,
        success: bool,
        files_count: u64,
        upload_bytes: u64,
        processing_time_ms: u64,
        largest_file: u64,
    ) {
        trace!(
            "Recording upload: success={}, files={}, bytes={}, time={}ms, largest={}",
            success, files_count, upload_bytes, processing_time_ms, largest_file
        );

        // Increment total uploads
        if let Ok(mut total) = self.total_uploads.lock() {
            *total += 1;
            trace!("Total uploads now: {}", *total);
        }

        // Track success/failure
        if success {
            if let Ok(mut successful) = self.successful_uploads.lock() {
                *successful += 1;
            }
        } else if let Ok(mut failed) = self.failed_uploads.lock() {
            *failed += 1;
        }

        // Only record additional metrics for successful uploads
        if success {
            // Record number of files uploaded
            if let Ok(mut files) = self.files_uploaded.lock() {
                *files += files_count;
            }

            // Record total bytes uploaded
            if let Ok(mut bytes) = self.upload_bytes.lock() {
                *bytes += upload_bytes;
            }

            // Update largest upload if this is bigger
            if let Ok(mut largest) = self.largest_upload.lock()
                && largest_file > *largest
            {
                *largest = largest_file;
            }

            // Record processing time (keep last 100 entries for average calculation)
            if let Ok(mut times) = self.upload_processing_times.lock() {
                times.push(processing_time_ms);
                // Use more efficient removal and shrink capacity to prevent unbounded growth
                let len = times.len();
                if len > 100 {
                    times.drain(0..len - 100);
                    // Shrink capacity if it's grown too large (prevent memory leak)
                    if times.capacity() > 200 {
                        times.shrink_to(100);
                    }
                }
            }
        }
    }

    /// Track concurrent upload start
    pub fn start_upload(&self) {
        if let Ok(mut concurrent) = self.concurrent_uploads.lock() {
            *concurrent += 1;
        }
    }

    /// Track concurrent upload completion
    pub fn finish_upload(&self) {
        if let Ok(mut concurrent) = self.concurrent_uploads.lock() {
            *concurrent = concurrent.saturating_sub(1);
        }
    }

    /// Get upload statistics
    pub fn get_upload_stats(&self) -> UploadStats {
        let total_uploads = *self
            .total_uploads
            .lock()
            .unwrap_or_else(|_| panic!("Stats lock poisoned"));
        let successful_uploads = *self
            .successful_uploads
            .lock()
            .unwrap_or_else(|_| panic!("Stats lock poisoned"));
        let failed_uploads = *self
            .failed_uploads
            .lock()
            .unwrap_or_else(|_| panic!("Stats lock poisoned"));
        let files_uploaded = *self
            .files_uploaded
            .lock()
            .unwrap_or_else(|_| panic!("Stats lock poisoned"));
        let upload_bytes = *self
            .upload_bytes
            .lock()
            .unwrap_or_else(|_| panic!("Stats lock poisoned"));
        let largest_upload = *self
            .largest_upload
            .lock()
            .unwrap_or_else(|_| panic!("Stats lock poisoned"));
        let concurrent_uploads = *self
            .concurrent_uploads
            .lock()
            .unwrap_or_else(|_| panic!("Stats lock poisoned"));

        let processing_times = self
            .upload_processing_times
            .lock()
            .unwrap_or_else(|_| panic!("Stats lock poisoned"));
        let average_processing_time = if processing_times.is_empty() {
            0.0
        } else {
            processing_times.iter().sum::<u64>() as f64 / processing_times.len() as f64
        };

        UploadStats {
            total_uploads,
            successful_uploads,
            failed_uploads,
            files_uploaded,
            upload_bytes,
            average_upload_size: if files_uploaded > 0 {
                upload_bytes / files_uploaded
            } else {
                0
            },
            largest_upload,
            concurrent_uploads,
            average_processing_time,
            success_rate: if total_uploads > 0 {
                (successful_uploads as f64 / total_uploads as f64) * 100.0
            } else {
                0.0
            },
        }
    }

    /// Check if memory pressure is detected (memory usage > 150MB)
    /// This triggers aggressive cleanup in rate limiter and other components
    pub fn check_memory_pressure(&self, rate_limiter: Option<&RateLimiter>) -> bool {
        trace!("Checking memory pressure");
        let (current_memory, _, available) = self.get_memory_usage();

        if available {
            if let Some(memory) = current_memory {
                // Trigger cleanup if memory exceeds 150MB (baseline is ~30MB)
                let memory_mb = memory / (1024 * 1024);
                trace!("Current memory usage: {}MB", memory_mb);

                if memory_mb > 150 {
                    warn!("Memory pressure detected: {}MB usage", memory_mb);

                    // Trigger rate limiter cleanup if provided
                    if let Some(limiter) = rate_limiter {
                        debug!("Triggering rate limiter cleanup due to memory pressure");
                        limiter.cleanup_on_memory_pressure();
                    }

                    // Clear search cache if available
                    debug!("Clearing search cache due to memory pressure");
                    crate::search::clear_cache();

                    return true;
                }
                trace!("Memory usage within normal limits: {}MB", memory_mb);
            } else {
                trace!("Memory usage unavailable but tracking is enabled");
            }
        } else {
            trace!("Memory tracking not available");
        }
        false
    }

    /// Get current process memory usage in bytes
    ///
    /// This function implements cross-platform memory reading with caching
    /// to avoid frequent expensive syscalls. Memory is cached for 5 seconds.
    /// Returns (current_memory, peak_memory, available) where memory values
    /// are None if memory tracking is unavailable.
    pub fn get_memory_usage(&self) -> (Option<u64>, Option<u64>, bool) {
        let now = Instant::now();

        // Check if we need to refresh the memory reading (cache for 5 seconds)
        let should_refresh = {
            let last_check = self.last_memory_check.lock().unwrap();
            match *last_check {
                Some(last) => {
                    let elapsed = now.duration_since(last);
                    let should_refresh = elapsed >= Duration::from_secs(5);
                    trace!(
                        "Memory cache check: elapsed={}s, should_refresh={}",
                        elapsed.as_secs(),
                        should_refresh
                    );
                    should_refresh
                }
                None => {
                    trace!("First memory check, refreshing");
                    true
                }
            }
        };

        if should_refresh {
            trace!("Refreshing memory statistics");
            let current_memory_opt = get_process_memory_bytes();

            // Update availability status
            let is_available = current_memory_opt.is_some();
            if let Ok(mut available) = self.memory_available.lock() {
                if !*available && is_available {
                    info!("Memory tracking is now available");
                } else if *available && !is_available {
                    info!("Memory tracking is no longer available");
                }
                *available = is_available;
            }

            // Update current memory
            if let Ok(mut mem) = self.process_memory_bytes.lock() {
                *mem = current_memory_opt;
            }

            // Update peak memory if this is higher
            if let (Some(current_memory), Ok(mut peak)) =
                (current_memory_opt, self.peak_memory_bytes.lock())
            {
                match *peak {
                    Some(peak_val) => {
                        if current_memory > peak_val {
                            *peak = Some(current_memory);
                        }
                    }
                    None => {
                        *peak = Some(current_memory);
                    }
                }
            }

            // Update last check time
            if let Ok(mut last_check) = self.last_memory_check.lock() {
                *last_check = Some(now);
            }
        }

        // Return current and peak memory with availability
        let current = *self
            .process_memory_bytes
            .lock()
            .unwrap_or_else(|_| panic!("Stats lock poisoned"));
        let peak = *self
            .peak_memory_bytes
            .lock()
            .unwrap_or_else(|_| panic!("Stats lock poisoned"));
        let available = *self
            .memory_available
            .lock()
            .unwrap_or_else(|_| panic!("Stats lock poisoned"));
        (current, peak, available)
    }

    /// Force refresh memory statistics (bypasses cache)
    pub fn refresh_memory_stats(&self) {
        let current_memory_opt = get_process_memory_bytes();

        // Update availability status
        let is_available = current_memory_opt.is_some();
        if let Ok(mut available) = self.memory_available.lock() {
            *available = is_available;
        }

        if let Ok(mut mem) = self.process_memory_bytes.lock() {
            *mem = current_memory_opt;
        }

        if let (Some(current_memory), Ok(mut peak)) =
            (current_memory_opt, self.peak_memory_bytes.lock())
        {
            match *peak {
                Some(peak_val) => {
                    if current_memory > peak_val {
                        *peak = Some(current_memory);
                    }
                }
                None => {
                    *peak = Some(current_memory);
                }
            }
        }

        if let Ok(mut last_check) = self.last_memory_check.lock() {
            *last_check = Some(Instant::now());
        }
    }
}

/// Cross-platform process memory reading
///
/// Returns current process memory usage in bytes, or None if unavailable.
/// Prioritizes Linux /proc/self/status, with fallbacks for other platforms.
/// Returns None when memory tracking is restricted (e.g., containers, CI environments).
fn get_process_memory_bytes() -> Option<u64> {
    #[cfg(target_os = "linux")]
    {
        match fs::read_to_string("/proc/self/status") {
            Ok(status) => {
                for line in status.lines() {
                    if line.starts_with("VmRSS:")
                        && let Some(kb_str) = line.split_whitespace().nth(1)
                        && let Ok(kb) = kb_str.parse::<u64>()
                    {
                        return Some(kb * 1024); // Convert KB to bytes
                    }
                }
                // Parsing succeeded but VmRSS not found - unusual but possible
                debug!("VmRSS not found in /proc/self/status");
                None
            }
            Err(e) => {
                // Log different error types with appropriate levels
                match e.kind() {
                    std::io::ErrorKind::NotFound => {
                        debug!("Memory tracking unavailable: /proc/self/status not found");
                    }
                    std::io::ErrorKind::PermissionDenied => {
                        debug!("Memory tracking unavailable: /proc/self/status access denied");
                    }
                    _ => {
                        warn!("Memory tracking unavailable: failed to read /proc/self/status: {e}");
                    }
                }
                None
            }
        }
    }

    #[cfg(target_os = "macos")]
    {
        use std::ffi::c_void;

        // time_value_t (seconds + microseconds) used in mach_task_basic_info
        #[repr(C)]
        #[allow(non_camel_case_types)]
        struct time_value_t {
            seconds: i32,
            microseconds: i32,
        }

        // Layout aligned with <mach/task_info.h> (basic flavor)
        #[repr(C)]
        #[allow(non_camel_case_types)]
        struct mach_task_basic_info {
            virtual_size: u64,
            resident_size: u64,
            resident_size_max: u64,
            user_time: time_value_t,
            system_time: time_value_t,
            policy: i32,
            suspend_count: i32,
        }

        // TASK_VM_INFO fallback struct (subset; order matters)
        #[repr(C)]
        #[allow(non_camel_case_types)]
        struct task_vm_info {
            virtual_size: u64,
            region_count: i32,
            page_size: i32,
            resident_size: u64,
            resident_size_peak: u64,
            device: u64,
            device_peak: u64,
            internal: u64,
            internal_peak: u64,
            external: u64,
            external_peak: u64,
            reusable: u64,
            reusable_peak: u64,
            purgeable_volatile_pmap: u64,
            purgeable_volatile_resident: u64,
            purgeable_volatile_virtual: u64,
            compressed: u64,
            compressed_peak: u64,
            compressed_lifetime: u64,
            phys_footprint: u64,
            min_address: u64,
            max_address: u64,
        }

        unsafe extern "C" {
            fn mach_task_self() -> u32;
            fn task_info(
                target_task: u32,
                flavor: u32,
                task_info_out: *mut c_void,
                task_info_outCnt: *mut u32,
            ) -> i32;
        }

        const MACH_TASK_BASIC_INFO: u32 = 20; // flavor constant
        const TASK_VM_INFO: u32 = 22; // fallback flavor
        // natural_t == u32; express counts in u32 units
        const MACH_TASK_BASIC_INFO_COUNT: u32 =
            (std::mem::size_of::<mach_task_basic_info>() / std::mem::size_of::<u32>()) as u32;
        const TASK_VM_INFO_COUNT: u32 =
            (std::mem::size_of::<task_vm_info>() / std::mem::size_of::<u32>()) as u32;

        unsafe {
            let mut basic: mach_task_basic_info = mem::zeroed();
            let mut count = MACH_TASK_BASIC_INFO_COUNT;
            let r_basic = task_info(
                mach_task_self(),
                MACH_TASK_BASIC_INFO,
                &mut basic as *mut _ as *mut c_void,
                &mut count,
            );
            if r_basic == 0 && basic.resident_size > 0 {
                return Some(basic.resident_size as u64);
            }
            debug!("mach_task_basic_info failed (code {r_basic}), trying TASK_VM_INFO");
            let mut vm: task_vm_info = mem::zeroed();
            let mut vm_count = TASK_VM_INFO_COUNT;
            let r_vm = task_info(
                mach_task_self(),
                TASK_VM_INFO,
                &mut vm as *mut _ as *mut c_void,
                &mut vm_count,
            );
            if r_vm == 0 && vm.resident_size > 0 {
                return Some(vm.resident_size as u64);
            }
            debug!("TASK_VM_INFO failed (code {r_vm}) on macOS");
        }
        debug!("Memory tracking unavailable: macOS APIs failed");
        None
    }

    #[cfg(target_os = "windows")]
    {
        use std::ffi::c_void;

        #[repr(C)]
        #[allow(non_snake_case)]
        struct PROCESS_MEMORY_COUNTERS {
            cb: u32,
            PageFaultCount: u32,
            PeakWorkingSetSize: usize,
            WorkingSetSize: usize,
            QuotaPeakPagedPoolUsage: usize,
            QuotaPagedPoolUsage: usize,
            QuotaPeakNonPagedPoolUsage: usize,
            QuotaNonPagedPoolUsage: usize,
            PagefileUsage: usize,
            PeakPagefileUsage: usize,
        }

        unsafe extern "system" {
            fn GetCurrentProcess() -> *mut c_void;
        }

        #[link(name = "psapi")]
        unsafe extern "system" {
            fn GetProcessMemoryInfo(
                hProcess: *mut c_void,
                ppsmemCounters: *mut PROCESS_MEMORY_COUNTERS,
                cb: u32,
            ) -> i32;
        }

        unsafe {
            let mut pmc: PROCESS_MEMORY_COUNTERS = mem::zeroed();
            pmc.cb = mem::size_of::<PROCESS_MEMORY_COUNTERS>() as u32;

            let result = GetProcessMemoryInfo(GetCurrentProcess(), &mut pmc, pmc.cb);

            if result != 0 {
                return Some(pmc.WorkingSetSize as u64);
            }
        }
        debug!("Memory tracking unavailable: failed to get memory info on Windows");
        None
    }

    #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
    {
        debug!("Memory tracking not supported on this platform");
        None
    }
}

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

    #[test]
    fn test_upload_statistics_tracking() {
        let stats = ServerStats::new();

        // Verify initial state
        let initial_stats = stats.get_upload_stats();
        assert_eq!(initial_stats.total_uploads, 0);
        assert_eq!(initial_stats.successful_uploads, 0);
        assert_eq!(initial_stats.failed_uploads, 0);
        assert_eq!(initial_stats.files_uploaded, 0);
        assert_eq!(initial_stats.upload_bytes, 0);
        assert_eq!(initial_stats.success_rate, 0.0);

        // Test concurrent upload tracking
        stats.start_upload();
        stats.start_upload();
        let concurrent_stats = stats.get_upload_stats();
        assert_eq!(concurrent_stats.concurrent_uploads, 2);

        // Test successful upload recording
        stats.record_upload_request(true, 3, 1024, 150, 512);
        stats.finish_upload();

        stats.record_upload_request(true, 2, 2048, 200, 1024);
        stats.finish_upload();

        let success_stats = stats.get_upload_stats();
        assert_eq!(success_stats.total_uploads, 2);
        assert_eq!(success_stats.successful_uploads, 2);
        assert_eq!(success_stats.failed_uploads, 0);
        assert_eq!(success_stats.files_uploaded, 5);
        assert_eq!(success_stats.upload_bytes, 3072);
        assert_eq!(success_stats.largest_upload, 1024);
        assert_eq!(success_stats.success_rate, 100.0);
        assert_eq!(success_stats.average_upload_size, 3072 / 5);
        assert_eq!(success_stats.average_processing_time, 175.0); // (150 + 200) / 2

        // Test failed upload recording
        stats.record_upload_request(false, 0, 0, 0, 0);

        let final_stats = stats.get_upload_stats();
        assert_eq!(final_stats.total_uploads, 3);
        assert_eq!(final_stats.successful_uploads, 2);
        assert_eq!(final_stats.failed_uploads, 1);
        assert!((final_stats.success_rate - (200.0 / 3.0)).abs() < 0.01);
    }

    #[test]
    fn test_memory_tracking() {
        let stats = ServerStats::new();

        // Test initial memory state
        let (current, peak, available) = stats.get_memory_usage();

        // Test behavior based on availability
        if available {
            // When memory tracking is available, we should have Some values
            assert!(current.is_some());
            assert!(peak.is_some());
            assert!(current.unwrap() <= peak.unwrap());

            // Test forced refresh
            stats.refresh_memory_stats();
            let (current2, peak2, available2) = stats.get_memory_usage();

            assert!(available2);
            assert!(current2.is_some());
            assert!(peak2.is_some());

            // Peak should never decrease
            assert!(peak2.unwrap() >= peak.unwrap());
            // Current might change but should be reasonable
            assert!(current2.unwrap() <= peak2.unwrap());
        } else {
            // When memory tracking is unavailable, values should be None
            assert!(current.is_none());
            assert!(peak.is_none());

            // Test forced refresh
            stats.refresh_memory_stats();
            let (current2, peak2, available2) = stats.get_memory_usage();

            // Should remain unavailable
            assert!(!available2);
            assert!(current2.is_none());
            assert!(peak2.is_none());
        }
    }

    #[test]
    fn test_memory_caching() {
        let stats = ServerStats::new();

        // First call should set the cache
        let (current1, peak1, available1) = stats.get_memory_usage();

        // Immediate second call should use cache (values should be identical)
        let (current2, peak2, available2) = stats.get_memory_usage();
        assert_eq!(current1, current2);
        assert_eq!(peak1, peak2);
        assert_eq!(available1, available2);

        // Verify cache timestamp was set
        let last_check = stats.last_memory_check.lock().unwrap();
        assert!(last_check.is_some());
    }

    #[test]
    fn test_memory_unavailable_scenario() {
        let stats = ServerStats::new();

        // First check the actual system state
        let (initial_current, initial_peak, initial_available) = stats.get_memory_usage();

        if initial_available {
            // System has memory tracking available, so let's manually simulate unavailable state
            // Set memory as unavailable for testing
            if let Ok(mut available) = stats.memory_available.lock() {
                *available = false;
            }
            if let Ok(mut mem) = stats.process_memory_bytes.lock() {
                *mem = None;
            }
            if let Ok(mut peak) = stats.peak_memory_bytes.lock() {
                *peak = None;
            }

            // Now the get_memory_usage should return the cached unavailable state
            // without refreshing (since we didn't change the timestamp)
            let (current, peak, available) = (
                *stats.process_memory_bytes.lock().unwrap(),
                *stats.peak_memory_bytes.lock().unwrap(),
                *stats.memory_available.lock().unwrap(),
            );

            // Should indicate unavailable memory based on what we set
            assert!(!available);
            assert!(current.is_none());
            assert!(peak.is_none());
        } else {
            // System doesn't have memory tracking, verify the behavior
            assert!(!initial_available);
            assert!(initial_current.is_none());
            assert!(initial_peak.is_none());

            // Test refresh maintains unavailable state
            stats.refresh_memory_stats();
            let (current2, peak2, available2) = stats.get_memory_usage();

            // Should remain unavailable if system doesn't support it
            assert!(!available2);
            assert!(current2.is_none());
            assert!(peak2.is_none());
        }
    }
}

/// Upload statistics structure for reporting
#[derive(Debug, Clone)]
pub struct UploadStats {
    pub total_uploads: u64,
    pub successful_uploads: u64,
    pub failed_uploads: u64,
    pub files_uploaded: u64,
    pub upload_bytes: u64,
    pub average_upload_size: u64,
    pub largest_upload: u64,
    pub concurrent_uploads: u64,
    pub average_processing_time: f64,
    pub success_rate: f64,
}

/// Load TLS certificates from a PEM file
fn load_tls_certs(
    path: &std::path::Path,
) -> Result<Vec<rustls::pki_types::CertificateDer<'static>>, AppError> {
    let file = std::fs::File::open(path).map_err(|e| {
        AppError::InvalidConfiguration(format!(
            "Failed to open SSL certificate file {}: {}",
            path.display(),
            e
        ))
    })?;
    let mut reader = BufReader::new(file);
    let certs: Vec<rustls::pki_types::CertificateDer<'static>> = rustls_pemfile::certs(&mut reader)
        .collect::<Result<Vec<_>, _>>()
        .map_err(|e| {
            AppError::InvalidConfiguration(format!(
                "Failed to parse SSL certificate file {}: {}",
                path.display(),
                e
            ))
        })?;
    if certs.is_empty() {
        return Err(AppError::InvalidConfiguration(format!(
            "No certificates found in {}",
            path.display()
        )));
    }
    info!(
        "Loaded {} certificate(s) from {}",
        certs.len(),
        path.display()
    );
    Ok(certs)
}

/// Load TLS private key from a PEM file
fn load_tls_key(
    path: &std::path::Path,
) -> Result<rustls::pki_types::PrivateKeyDer<'static>, AppError> {
    let file = std::fs::File::open(path).map_err(|e| {
        AppError::InvalidConfiguration(format!(
            "Failed to open SSL key file {}: {}",
            path.display(),
            e
        ))
    })?;
    let mut reader = BufReader::new(file);
    let key = rustls_pemfile::private_key(&mut reader)
        .map_err(|e| {
            AppError::InvalidConfiguration(format!(
                "Failed to parse SSL key file {}: {}",
                path.display(),
                e
            ))
        })?
        .ok_or_else(|| {
            AppError::InvalidConfiguration(format!("No private key found in {}", path.display()))
        })?;
    info!("Loaded private key from {}", path.display());
    Ok(key)
}

/// Build TLS server configuration from certificate and key paths
fn build_tls_config(
    cert_path: &std::path::Path,
    key_path: &std::path::Path,
) -> Result<Arc<ServerConfig>, AppError> {
    let certs = load_tls_certs(cert_path)?;
    let key = load_tls_key(key_path)?;

    let config = ServerConfig::builder()
        .with_no_client_auth()
        .with_single_cert(certs, key)
        .map_err(|e| {
            AppError::InvalidConfiguration(format!("Failed to build TLS configuration: {}", e))
        })?;

    info!("TLS configuration built successfully");
    Ok(Arc::new(config))
}

/// Run server with new configuration system
pub fn run_server_with_config(config: Config) -> Result<(), AppError> {
    // Convert Config back to Cli for compatibility with existing code
    // This is a transitional approach - eventually we could refactor to use Config throughout
    let cli = Cli {
        directory: config.directory,
        listen: Some(config.listen),
        port: Some(config.port),
        allowed_extensions: Some(config.allowed_extensions.join(",")),
        threads: Some(config.threads),
        chunk_size: Some(config.chunk_size),
        verbose: Some(config.verbose),
        detailed_logging: Some(config.detailed_logging),
        username: config.username,
        password: config.password,
        enable_upload: Some(config.enable_upload),
        max_upload_size: Some(config.max_upload_size / (1024 * 1024)), // Convert bytes back to MB
        enable_webdav: Some(config.enable_webdav),
        disable_rate_limit: Some(config.disable_rate_limit),
        config_file: None, // Not needed for server execution
        log_dir: config.log_dir,
        ssl_cert: config.ssl_cert,
        ssl_key: config.ssl_key,
        base_path: if config.base_path.is_empty() {
            None
        } else {
            Some(config.base_path)
        },
    };

    run_server(cli, None, None)
}

pub fn run_server(
    cli: Cli,
    shutdown_rx: Option<mpsc::Receiver<()>>,
    addr_tx: Option<mpsc::Sender<SocketAddr>>,
) -> Result<(), AppError> {
    let worker_threads = cli.threads.unwrap_or(8);
    let runtime = tokio::runtime::Builder::new_multi_thread()
        .enable_io()
        .enable_time()
        .worker_threads(worker_threads)
        .max_blocking_threads(worker_threads.saturating_mul(32).max(256))
        .build()
        .map_err(|e| AppError::InternalServerError(e.to_string()))?;

    runtime.block_on(run_server_async(cli, shutdown_rx, addr_tx))
}

async fn run_server_async(
    cli: Cli,
    shutdown_rx: Option<mpsc::Receiver<()>>,
    addr_tx: Option<mpsc::Sender<SocketAddr>>,
) -> Result<(), AppError> {
    debug!(
        "Starting server with configuration: verbose={:?}, detailed_logging={:?}",
        cli.verbose, cli.detailed_logging
    );

    let base_dir = Arc::new(cli.directory.canonicalize()?);
    trace!("Base directory resolved to: {:?}", base_dir);

    if !base_dir.is_dir() {
        return Err(AppError::DirectoryNotFound(
            cli.directory.to_string_lossy().into_owned(),
        ));
    }

    crate::search::initialize_search(base_dir.as_ref().clone());

    let allowed_extensions = Arc::new(
        cli.allowed_extensions
            .as_ref()
            .unwrap_or(&"*".to_string())
            .split(',')
            .map(|ext| Pattern::new(ext.trim()))
            .collect::<Result<Vec<Pattern>, _>>()?,
    );

    let bind_address = format!(
        "{}:{}",
        cli.listen.as_ref().unwrap_or(&"127.0.0.1".to_string()),
        cli.port.unwrap_or(8080)
    );

    let listener = tokio::net::TcpListener::bind(&bind_address).await?;
    let local_addr = listener.local_addr()?;

    let tls_config: Option<Arc<ServerConfig>> =
        if let (Some(cert_path), Some(key_path)) = (&cli.ssl_cert, &cli.ssl_key) {
            Some(build_tls_config(cert_path, key_path)?)
        } else {
            None
        };
    let tls_acceptor = tls_config
        .as_ref()
        .map(|cfg| tokio_rustls::TlsAcceptor::from(cfg.clone()));
    let is_https = tls_acceptor.is_some();

    let webdav_enabled = cli.enable_webdav.unwrap_or(false);
    let disable_rate_limit_requested = cli.disable_rate_limit.unwrap_or(false);
    let rate_limit_disabled = webdav_enabled && disable_rate_limit_requested;
    if disable_rate_limit_requested && !webdav_enabled {
        warn!(
            "Ignoring --disable-rate-limit because WebDAV is disabled. Enable WebDAV for it to take effect."
        );
    }
    if rate_limit_disabled {
        info!("WebDAV rate limiting is disabled by configuration.");
    }
    let (rate_limit_per_minute, concurrent_per_ip) = if webdav_enabled {
        (3500, 128)
    } else {
        (120, 10)
    };
    let rate_limiter = Arc::new(RateLimiter::new(rate_limit_per_minute, concurrent_per_ip));
    let stats = Arc::new(ServerStats::new());

    if let Some(tx) = addr_tx
        && tx.send(local_addr).is_err()
    {
        return Err(AppError::InternalServerError(
            "Failed to send server address to test thread".to_string(),
        ));
    }

    let protocol = if is_https { "https" } else { "http" };
    info!(
        "🚀 Server listening on {}://{} for directory '{}' (allowed extensions: {:?})",
        protocol,
        local_addr,
        base_dir.display(),
        allowed_extensions
    );

    let username = Arc::new(cli.username.clone());
    let password = Arc::new(cli.password.clone());
    let chunk_size = cli.chunk_size.unwrap_or(1024);
    let cli_arc = Arc::new(cli);

    // Initialize the global base path for reverse proxy sub-path support
    crate::templates::init_base_path(cli_arc.base_path.clone().unwrap_or_default());

    let mut router = Router::new();
    if cli_arc.username.is_some() && cli_arc.password.is_some() {
        crate::templates::AUTH_ENABLED.store(true, std::sync::atomic::Ordering::SeqCst);
        router.add_middleware(Box::new(AuthMiddleware::new(
            cli_arc.username.clone(),
            cli_arc.password.clone(),
        )));
    }
    register_internal_routes(
        &mut router,
        Some(cli_arc.clone()),
        Some(stats.clone()),
        Some(base_dir.clone()),
    );
    let shared_router = Arc::new(router);

    tokio::spawn({
        let rate_limiter = rate_limiter.clone();
        async move {
            let mut interval = tokio::time::interval(Duration::from_secs(60));
            loop {
                interval.tick().await;
                rate_limiter.cleanup_old_entries();
            }
        }
    });

    tokio::spawn({
        let stats_reporter = stats.clone();
        let rate_limiter_monitor = rate_limiter.clone();
        async move {
            let mut interval = tokio::time::interval(Duration::from_secs(300));
            loop {
                interval.tick().await;
                let (total, successful, errors, bytes, uptime) = stats_reporter.get_stats();
                let upload_stats = stats_reporter.get_upload_stats();
                let (current_memory, peak_memory, memory_available) =
                    stats_reporter.get_memory_usage();
                let memory_pressure =
                    stats_reporter.check_memory_pressure(Some(&rate_limiter_monitor));

                info!(
                    "📊 Request Stats: {} total ({} successful, {} errors), {:.2} MB served, uptime: {}s",
                    total,
                    successful,
                    errors,
                    bytes as f64 / 1024.0 / 1024.0,
                    uptime.as_secs()
                );

                if memory_available {
                    let current_mb = current_memory.unwrap_or(0) as f64 / 1024.0 / 1024.0;
                    let peak_mb = peak_memory.unwrap_or(0) as f64 / 1024.0 / 1024.0;
                    let pressure_indicator = if memory_pressure {
                        " ⚠️ PRESSURE"
                    } else {
                        ""
                    };
                    info!(
                        "🧠 Memory Stats: {current_mb:.2} MB current, {peak_mb:.2} MB peak{pressure_indicator}"
                    );
                }

                if upload_stats.total_uploads > 0 {
                    info!(
                        "📤 Upload Stats: {} uploads ({:.1}% success), {} files, {:.2} MB uploaded, avg: {:.2} MB/file, {:.0}ms/upload, {} concurrent",
                        upload_stats.total_uploads,
                        upload_stats.success_rate,
                        upload_stats.files_uploaded,
                        upload_stats.upload_bytes as f64 / 1024.0 / 1024.0,
                        upload_stats.average_upload_size as f64 / 1024.0 / 1024.0,
                        upload_stats.average_processing_time,
                        upload_stats.concurrent_uploads
                    );
                }
            }
        }
    });

    let mut shutdown_task = shutdown_rx.map(|rx| {
        tokio::task::spawn_blocking(move || {
            let _ = rx.recv();
        })
    });

    loop {
        if let Some(ref mut shutdown_task) = shutdown_task {
            tokio::select! {
                _ = shutdown_task => {
                    break;
                }
                res = listener.accept() => {
                    let (stream, peer_addr) = res?;
                    handle_connection(
                        stream,
                        peer_addr,
                        base_dir.clone(),
                        allowed_extensions.clone(),
                        username.clone(),
                        password.clone(),
                        chunk_size,
                        rate_limiter.clone(),
                        rate_limit_disabled,
                        stats.clone(),
                        cli_arc.clone(),
                        shared_router.clone(),
                        tls_acceptor.clone(),
                    );
                }
            }
        } else {
            let (stream, peer_addr) = listener.accept().await?;
            handle_connection(
                stream,
                peer_addr,
                base_dir.clone(),
                allowed_extensions.clone(),
                username.clone(),
                password.clone(),
                chunk_size,
                rate_limiter.clone(),
                rate_limit_disabled,
                stats.clone(),
                cli_arc.clone(),
                shared_router.clone(),
                tls_acceptor.clone(),
            );
        }
    }

    info!("✅ Server shut down gracefully.");
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn handle_connection(
    stream: TokioTcpStream,
    peer_addr: SocketAddr,
    base_dir: Arc<std::path::PathBuf>,
    allowed_extensions: Arc<Vec<glob::Pattern>>,
    username: Arc<Option<String>>,
    password: Arc<Option<String>>,
    chunk_size: usize,
    rate_limiter: Arc<RateLimiter>,
    rate_limit_disabled: bool,
    stats: Arc<ServerStats>,
    cli_config: Arc<Cli>,
    router: Arc<Router>,
    tls_acceptor: Option<tokio_rustls::TlsAcceptor>,
) {
    let client_ip = peer_addr.ip();
    if !rate_limit_disabled && !rate_limiter.check_rate_limit(client_ip) {
        return;
    }

    tokio::spawn(async move {
        let result = if let Some(acceptor) = tls_acceptor {
            match acceptor.accept(stream).await {
                Ok(tls_stream) => {
                    crate::http::handle_client_async(
                        tls_stream,
                        peer_addr,
                        base_dir,
                        allowed_extensions,
                        username,
                        password,
                        chunk_size,
                        Some(cli_config),
                        Some(stats.clone()),
                        router,
                    )
                    .await;
                    Ok(())
                }
                Err(_) => Err(()),
            }
        } else {
            crate::http::handle_client_async(
                stream,
                peer_addr,
                base_dir,
                allowed_extensions,
                username,
                password,
                chunk_size,
                Some(cli_config),
                Some(stats.clone()),
                router,
            )
            .await;
            Ok(())
        };

        if !rate_limit_disabled {
            rate_limiter.release_connection(client_ip);
        }
        let _ = result;
    });
}