irondrop 2.7.0

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
// SPDX-License-Identifier: MIT

//! Handles HTTP request parsing, routing, and response generation.

use crate::error::AppError;
use crate::fs::FileDetails;
use crate::response::create_error_response;
use crate::router::Router;
use log::{debug, error, info, trace, warn};
use rustls;
use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;
use std::net::TcpStream;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

/// Maximum size for request body (10GB) to prevent memory exhaustion attacks
const MAX_REQUEST_BODY_SIZE: usize = 10 * 1024 * 1024 * 1024;

/// Maximum size for request headers (8KB) to prevent header buffer overflow
const MAX_HEADERS_SIZE: usize = 8 * 1024;

/// Threshold for streaming request bodies to disk (64MB)
/// This ensures total memory usage stays well below 128MB
pub const STREAM_TO_DISK_THRESHOLD: usize = 64 * 1024 * 1024;

/// Abstraction over plain TCP and TLS-encrypted streams.
/// Allows the HTTP handling code to work transparently with both.
pub enum ClientStream {
    Plain(TcpStream),
    Tls(Box<rustls::StreamOwned<rustls::ServerConnection, TcpStream>>),
}

impl ClientStream {
    /// Get the peer address of the underlying TCP connection
    pub fn peer_addr(&self) -> std::io::Result<std::net::SocketAddr> {
        match self {
            ClientStream::Plain(s) => s.peer_addr(),
            ClientStream::Tls(s) => s.sock.peer_addr(),
        }
    }

    /// Set the read timeout on the underlying TCP connection
    pub fn set_read_timeout(&self, dur: Option<Duration>) -> std::io::Result<()> {
        match self {
            ClientStream::Plain(s) => s.set_read_timeout(dur),
            ClientStream::Tls(s) => s.sock.set_read_timeout(dur),
        }
    }
}

impl std::io::Read for ClientStream {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        match self {
            ClientStream::Plain(s) => s.read(buf),
            ClientStream::Tls(s) => s.read(buf),
        }
    }
}

impl std::io::Write for ClientStream {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        match self {
            ClientStream::Plain(s) => s.write(buf),
            ClientStream::Tls(s) => s.write(buf),
        }
    }

    fn flush(&mut self) -> std::io::Result<()> {
        match self {
            ClientStream::Plain(s) => s.flush(),
            ClientStream::Tls(s) => s.flush(),
        }
    }
}

/// Represents a parsed incoming HTTP request.
#[derive(Debug)]
pub struct Request {
    pub method: String,
    pub path: String,
    pub headers: HashMap<String, String>,
    pub body: Option<RequestBody>,
}

/// Request body can be either in memory or streamed to disk for large uploads
#[derive(Debug)]
pub enum RequestBody {
    /// Small bodies stored in memory
    Memory(Vec<u8>),
    /// Large bodies streamed to temporary file
    File { path: PathBuf, size: u64 },
}

impl RequestBody {
    /// Get the size of the request body in bytes
    pub fn len(&self) -> usize {
        let size = match self {
            RequestBody::Memory(data) => data.len(),
            RequestBody::File { size, .. } => *size as usize,
        };
        trace!("RequestBody size: {} bytes", size);
        size
    }

    /// Check if the request body is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// Represents an outgoing HTTP response.
pub struct Response {
    pub status_code: u16,
    pub status_text: String,
    pub headers: HashMap<String, String>,
    pub body: ResponseBody,
}

pub enum ResponseBody {
    Text(String),
    StaticText(&'static str),
    Binary(Vec<u8>),
    StaticBinary(&'static [u8]),
    Stream(FileDetails),
}

impl Request {
    /// Validates if the given method is a valid HTTP method
    fn is_valid_http_method(method: &str) -> bool {
        matches!(
            method,
            "GET"
                | "POST"
                | "PUT"
                | "DELETE"
                | "HEAD"
                | "OPTIONS"
                | "PATCH"
                | "TRACE"
                | "CONNECT"
                | "PROPFIND"
                | "MKCOL"
                | "COPY"
                | "MOVE"
                | "PROPPATCH"
                | "LOCK"
                | "UNLOCK"
        )
    }

    /// Enhanced HTTP request parser with better performance and compliance
    pub fn from_stream(stream: &mut ClientStream) -> Result<Self, AppError> {
        trace!("Starting HTTP request parsing from stream");
        // Set a reasonable timeout for reading requests
        stream.set_read_timeout(Some(std::time::Duration::from_secs(30)))?;

        // Read the entire HTTP headers in chunks for better performance
        let (headers_data, remaining_bytes) = Self::read_headers_with_remaining(stream)?;
        debug!(
            "Received headers ({} bytes), remaining buffer: {} bytes",
            headers_data.len(),
            remaining_bytes.len()
        );

        // Parse the headers
        let mut lines = headers_data.lines();

        // Parse request line
        let request_line = lines.next().ok_or(AppError::BadRequest)?;
        trace!("Request line: {}", request_line);
        let parts: Vec<&str> = request_line.split_whitespace().collect();

        if parts.len() != 3 {
            debug!("Invalid request line format: {}", request_line);
            return Err(AppError::BadRequest);
        }

        let method = parts[0].to_string();
        let raw_path = parts[1];
        let version = parts[2];

        // Validate HTTP method
        if !Self::is_valid_http_method(&method) {
            debug!("Invalid HTTP method: {}", method);
            return Err(AppError::BadRequest);
        }

        // Validate path doesn't contain null bytes or other invalid characters
        if raw_path.contains('\0') || raw_path.is_empty() {
            debug!("Invalid path: contains null byte or is empty");
            return Err(AppError::BadRequest);
        }

        let path = Self::decode_url(raw_path)?;

        debug!("Parsed request: {} {}", method, path);
        trace!("Raw path before decoding: {}", raw_path);
        trace!("HTTP version: {}", version);

        // Validate HTTP version
        if !version.starts_with("HTTP/1.") {
            return Err(AppError::BadRequest);
        }

        // Parse headers
        let mut headers = HashMap::new();
        for line in lines {
            let line = line.trim();
            if line.is_empty() {
                break;
            }

            if let Some((key, value)) = line.split_once(':') {
                let key = key.trim().to_lowercase();
                let value = value.trim().to_string();
                trace!("Header: {} = {}", key, value);

                // Handle multiple header values (comma-separated)
                if let Some(existing) = headers.get(&key) {
                    headers.insert(key, format!("{existing}, {value}"));
                } else {
                    headers.insert(key, value);
                }
            }
        }

        // Read request body if present
        let body = Self::read_request_body(stream, &headers, remaining_bytes)?;

        if let Some(ref body) = body {
            debug!("Request body parsed: {} bytes", body.len());
            match body {
                RequestBody::Memory(_) => trace!("Body stored in memory"),
                RequestBody::File { path, size } => {
                    trace!("Body streamed to file: {} ({} bytes)", path.display(), size);
                }
            }
        } else {
            trace!("No request body");
        }

        debug!(
            "Parsed request: {} {} (headers: {}, body_size: {})",
            method,
            path,
            headers.len(),
            body.as_ref().map(|b| b.len()).unwrap_or(0)
        );

        Ok(Request {
            method,
            path,
            headers,
            body,
        })
    }

    /// Read HTTP headers efficiently in chunks and return remaining bytes from body
    fn read_headers_with_remaining(
        stream: &mut ClientStream,
    ) -> Result<(String, Vec<u8>), AppError> {
        let mut buffer = vec![0; MAX_HEADERS_SIZE];
        let mut total_read = 0;

        loop {
            match stream.read(&mut buffer[total_read..]) {
                Ok(0) => {
                    if total_read == 0 {
                        return Err(AppError::BadRequest);
                    }
                    break;
                }
                Ok(bytes_read) => {
                    total_read += bytes_read;

                    // Look for the end of headers (\r\n\r\n or \n\n) in raw bytes
                    let double_crlf = b"\r\n\r\n";
                    let double_lf = b"\n\n";

                    if let Some(pos) = buffer[0..total_read]
                        .windows(4)
                        .position(|window| window == double_crlf)
                    {
                        let headers_end = pos;
                        let body_start = pos + 4;

                        // Only convert headers portion to UTF-8
                        match std::str::from_utf8(&buffer[0..headers_end]) {
                            Ok(headers_data) => {
                                let remaining_bytes = buffer[body_start..total_read].to_vec();
                                return Ok((headers_data.to_string(), remaining_bytes));
                            }
                            Err(_) => {
                                return Err(AppError::BadRequest);
                            }
                        }
                    } else if let Some(pos) = buffer[0..total_read]
                        .windows(2)
                        .position(|window| window == double_lf)
                    {
                        let headers_end = pos;
                        let body_start = pos + 2;

                        // Only convert headers portion to UTF-8
                        match std::str::from_utf8(&buffer[0..headers_end]) {
                            Ok(headers_data) => {
                                let remaining_bytes = buffer[body_start..total_read].to_vec();
                                return Ok((headers_data.to_string(), remaining_bytes));
                            }
                            Err(_) => {
                                return Err(AppError::BadRequest);
                            }
                        }
                    }

                    // Prevent header buffer overflow attacks
                    if total_read >= buffer.len() {
                        return Err(AppError::BadRequest);
                    }
                }
                Err(e) => return Err(AppError::Io(e)),
            }
        }

        // No body separator found, return all as headers with empty remaining bytes
        match std::str::from_utf8(&buffer[0..total_read]) {
            Ok(data) => Ok((data.to_string(), Vec::new())),
            Err(_) => Err(AppError::BadRequest),
        }
    }

    /// Read request body based on Content-Length header with security validations
    /// Large bodies are streamed to disk to prevent memory exhaustion
    fn read_request_body(
        stream: &mut ClientStream,
        headers: &HashMap<String, String>,
        remaining_bytes: Vec<u8>,
    ) -> Result<Option<RequestBody>, AppError> {
        let has_content_length = headers.contains_key("content-length");
        let has_chunked_transfer = Self::has_chunked_transfer_encoding(headers);

        // RFC 9112: Transfer-Encoding and Content-Length must not be sent together.
        if has_content_length && has_chunked_transfer {
            return Err(AppError::BadRequest);
        }

        // Chunked request bodies are decoded before any method-specific handling.
        if has_chunked_transfer {
            let body = Self::read_chunked_body(stream, remaining_bytes)?;
            return Ok(Some(body));
        }

        // Check if we have a Content-Length header
        let content_length = match headers.get("content-length") {
            Some(length_str) => match length_str.parse::<usize>() {
                Ok(length) => length,
                Err(_) => return Err(AppError::BadRequest),
            },
            None => {
                // No body expected
                return Ok(None);
            }
        };

        // Validate content length against security limits
        if content_length == 0 {
            return Ok(Some(RequestBody::Memory(Vec::new())));
        }

        if content_length > MAX_REQUEST_BODY_SIZE {
            return Err(AppError::PayloadTooLarge(MAX_REQUEST_BODY_SIZE as u64));
        }

        // Decide whether to use memory or disk based on size
        if content_length <= STREAM_TO_DISK_THRESHOLD {
            // Small body - use memory
            Self::read_body_to_memory(stream, content_length, remaining_bytes)
                .map(|body| Some(RequestBody::Memory(body)))
        } else {
            // Large body - stream to disk
            Self::read_body_to_disk(stream, content_length, remaining_bytes)
                .map(|(path, size)| Some(RequestBody::File { path, size }))
        }
    }

    fn has_chunked_transfer_encoding(headers: &HashMap<String, String>) -> bool {
        headers
            .get("transfer-encoding")
            .map(|encoding| {
                encoding
                    .split(',')
                    .map(|token| token.trim())
                    .any(|token| token.eq_ignore_ascii_case("chunked"))
            })
            .unwrap_or(false)
    }

    fn read_chunked_body(
        stream: &mut ClientStream,
        mut pending: Vec<u8>,
    ) -> Result<RequestBody, AppError> {
        const CHUNK_LINE_LIMIT: usize = 8 * 1024;

        let mut total_size: usize = 0;
        let mut memory_body: Vec<u8> = Vec::new();
        let mut file_sink: Option<(PathBuf, File)> = None;

        loop {
            let line = Self::read_crlf_line(stream, &mut pending, CHUNK_LINE_LIMIT)?;
            let line_str = std::str::from_utf8(&line).map_err(|_| AppError::BadRequest)?;
            let size_token = line_str
                .split(';')
                .next()
                .ok_or(AppError::BadRequest)?
                .trim();
            if size_token.is_empty() {
                return Err(AppError::BadRequest);
            }

            let chunk_size =
                usize::from_str_radix(size_token, 16).map_err(|_| AppError::BadRequest)?;
            if chunk_size == 0 {
                Self::consume_chunked_trailers(stream, &mut pending)?;
                break;
            }

            let next_total = total_size
                .checked_add(chunk_size)
                .ok_or(AppError::PayloadTooLarge(MAX_REQUEST_BODY_SIZE as u64))?;
            if next_total > MAX_REQUEST_BODY_SIZE {
                return Err(AppError::PayloadTooLarge(MAX_REQUEST_BODY_SIZE as u64));
            }

            let chunk_data = Self::read_exact_from_buffer(stream, &mut pending, chunk_size)?;
            Self::consume_expected_crlf(stream, &mut pending)?;

            if file_sink.is_none() && next_total <= STREAM_TO_DISK_THRESHOLD {
                memory_body.extend_from_slice(&chunk_data);
            } else {
                if file_sink.is_none() {
                    let (temp_path, mut temp_file) = Self::create_temp_body_file()?;
                    if !memory_body.is_empty() {
                        temp_file.write_all(&memory_body).map_err(|e| {
                            let _ = std::fs::remove_file(&temp_path);
                            AppError::from(e)
                        })?;
                        memory_body.clear();
                    }
                    file_sink = Some((temp_path, temp_file));
                }
                if let Some((temp_path, temp_file)) = file_sink.as_mut() {
                    temp_file.write_all(&chunk_data).map_err(|e| {
                        let _ = std::fs::remove_file(temp_path);
                        AppError::from(e)
                    })?;
                }
            }

            total_size = next_total;
        }

        if let Some((temp_path, temp_file)) = file_sink.as_mut() {
            temp_file.sync_all().map_err(|e| {
                let _ = std::fs::remove_file(temp_path);
                AppError::from(e)
            })?;
        }

        if let Some((temp_path, _)) = file_sink {
            Ok(RequestBody::File {
                path: temp_path,
                size: total_size as u64,
            })
        } else {
            Ok(RequestBody::Memory(memory_body))
        }
    }

    fn create_temp_body_file() -> Result<(PathBuf, File), AppError> {
        let temp_filename = format!(
            "irondrop_request_{}_{:x}.tmp",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos()
        );
        let temp_path = std::env::temp_dir().join(temp_filename);
        let temp_file = File::create(&temp_path).map_err(|e| {
            error!("Failed to create temporary file {temp_path:?}: {e}");
            AppError::from(e)
        })?;
        Ok((temp_path, temp_file))
    }

    fn read_crlf_line(
        stream: &mut ClientStream,
        pending: &mut Vec<u8>,
        max_line_len: usize,
    ) -> Result<Vec<u8>, AppError> {
        loop {
            if let Some(pos) = pending.windows(2).position(|w| w == b"\r\n") {
                let line = pending[..pos].to_vec();
                pending.drain(0..pos + 2);
                return Ok(line);
            }

            if pending.len() > max_line_len + 2 {
                return Err(AppError::BadRequest);
            }

            let mut buffer = [0u8; 8192];
            match stream.read(&mut buffer) {
                Ok(0) => return Err(AppError::BadRequest),
                Ok(n) => pending.extend_from_slice(&buffer[..n]),
                Err(e) => return Err(AppError::Io(e)),
            }
        }
    }

    fn read_exact_from_buffer(
        stream: &mut ClientStream,
        pending: &mut Vec<u8>,
        count: usize,
    ) -> Result<Vec<u8>, AppError> {
        while pending.len() < count {
            let mut buffer = [0u8; 8192];
            match stream.read(&mut buffer) {
                Ok(0) => return Err(AppError::BadRequest),
                Ok(n) => pending.extend_from_slice(&buffer[..n]),
                Err(e) => return Err(AppError::Io(e)),
            }
        }
        Ok(pending.drain(0..count).collect())
    }

    fn consume_expected_crlf(
        stream: &mut ClientStream,
        pending: &mut Vec<u8>,
    ) -> Result<(), AppError> {
        let crlf = Self::read_exact_from_buffer(stream, pending, 2)?;
        if crlf != b"\r\n" {
            return Err(AppError::BadRequest);
        }
        Ok(())
    }

    fn consume_chunked_trailers(
        stream: &mut ClientStream,
        pending: &mut Vec<u8>,
    ) -> Result<(), AppError> {
        let mut total_trailer_size = 0usize;
        loop {
            let line = Self::read_crlf_line(stream, pending, MAX_HEADERS_SIZE)?;
            total_trailer_size += line.len() + 2;
            if total_trailer_size > MAX_HEADERS_SIZE {
                return Err(AppError::BadRequest);
            }

            // Empty line marks end of trailers.
            if line.is_empty() {
                break;
            }
        }
        Ok(())
    }

    /// Read small request body into memory
    fn read_body_to_memory(
        stream: &mut ClientStream,
        content_length: usize,
        remaining_bytes: Vec<u8>,
    ) -> Result<Vec<u8>, AppError> {
        let mut body = Vec::with_capacity(content_length);

        // Use any remaining bytes from header parsing
        let bytes_from_headers = remaining_bytes.len().min(content_length);
        body.extend_from_slice(&remaining_bytes[..bytes_from_headers]);

        // Calculate how many more bytes we need to read
        let bytes_needed = content_length - bytes_from_headers;

        if bytes_needed > 0 {
            // Read the remaining body in chunks
            let mut bytes_read = 0;
            let chunk_size = 8192; // 8KB chunks
            let mut buffer = vec![0; chunk_size];

            while bytes_read < bytes_needed {
                let to_read = (bytes_needed - bytes_read).min(chunk_size);

                match stream.read(&mut buffer[..to_read]) {
                    Ok(0) => {
                        return Err(AppError::BadRequest);
                    }
                    Ok(n) => {
                        body.extend_from_slice(&buffer[..n]);
                        bytes_read += n;
                    }
                    Err(e) => {
                        if e.kind() == std::io::ErrorKind::TimedOut {
                            warn!("Request body read timeout");
                        }
                        return Err(AppError::Io(e));
                    }
                }
            }
        }

        // Verify we read exactly the expected amount
        if body.len() != content_length {
            return Err(AppError::BadRequest);
        }

        debug!(
            "Successfully read request body to memory: {} bytes",
            body.len()
        );
        Ok(body)
    }

    /// Read large request body directly to disk to prevent memory exhaustion
    fn read_body_to_disk(
        stream: &mut ClientStream,
        content_length: usize,
        remaining_bytes: Vec<u8>,
    ) -> Result<(PathBuf, u64), AppError> {
        // Create temporary file for the request body
        let temp_filename = format!(
            "irondrop_request_{}_{:x}.tmp",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos()
        );

        // Use system temp directory
        let temp_dir = std::env::temp_dir();
        let temp_path = temp_dir.join(&temp_filename);

        let mut temp_file = File::create(&temp_path).map_err(|e| {
            error!("Failed to create temporary file {temp_path:?}: {e}");
            AppError::from(e)
        })?;

        let mut total_written = 0;

        // Write any remaining bytes from header parsing
        if !remaining_bytes.is_empty() {
            let bytes_to_write = remaining_bytes.len().min(content_length);
            temp_file
                .write_all(&remaining_bytes[..bytes_to_write])
                .map_err(|e| {
                    let _ = std::fs::remove_file(&temp_path);
                    AppError::from(e)
                })?;
            total_written += bytes_to_write;
        }

        // Stream remaining bytes directly to disk
        let bytes_needed = content_length - total_written;
        if bytes_needed > 0 {
            let mut bytes_read = 0;
            let chunk_size = 64 * 1024; // 64KB chunks for better disk I/O
            let mut buffer = vec![0; chunk_size];

            while bytes_read < bytes_needed {
                let to_read = (bytes_needed - bytes_read).min(chunk_size);

                match stream.read(&mut buffer[..to_read]) {
                    Ok(0) => {
                        let _ = std::fs::remove_file(&temp_path);
                        return Err(AppError::BadRequest);
                    }
                    Ok(n) => {
                        temp_file.write_all(&buffer[..n]).map_err(|e| {
                            let _ = std::fs::remove_file(&temp_path);
                            AppError::from(e)
                        })?;
                        bytes_read += n;
                        total_written += n;
                    }
                    Err(e) => {
                        let _ = std::fs::remove_file(&temp_path);
                        if e.kind() == std::io::ErrorKind::TimedOut {
                            warn!("Request body read timeout");
                        }
                        return Err(AppError::Io(e));
                    }
                }
            }
        }

        // Ensure all data is written to disk
        temp_file.sync_all().map_err(|e| {
            let _ = std::fs::remove_file(&temp_path);
            AppError::from(e)
        })?;

        // Verify we read exactly the expected amount
        if total_written != content_length {
            let _ = std::fs::remove_file(&temp_path);
            return Err(AppError::BadRequest);
        }

        debug!(
            "Successfully streamed request body to disk: {} bytes at {temp_path:?}",
            total_written
        );
        Ok((temp_path, total_written as u64))
    }

    /// Simple URL decoding for percent-encoded paths
    fn decode_url(path: &str) -> Result<String, AppError> {
        let mut decoded = String::with_capacity(path.len());
        let mut chars = path.chars().peekable();

        while let Some(ch) = chars.next() {
            if ch == '%' {
                // Try to decode percent-encoded character
                let hex1 = chars.next().ok_or(AppError::BadRequest)?;
                let hex2 = chars.next().ok_or(AppError::BadRequest)?;

                if let Ok(byte_val) = u8::from_str_radix(&format!("{hex1}{hex2}"), 16) {
                    if let Some(decoded_char) = char::from_u32(byte_val as u32) {
                        decoded.push(decoded_char);
                    } else {
                        // Invalid character, keep as-is
                        decoded.push(ch);
                        decoded.push(hex1);
                        decoded.push(hex2);
                    }
                } else {
                    // Invalid hex, keep as-is
                    decoded.push(ch);
                    decoded.push(hex1);
                    decoded.push(hex2);
                }
            } else {
                decoded.push(ch);
            }
        }

        Ok(decoded)
    }

    /// Clean up any temporary files associated with this request
    pub fn cleanup(&self) {
        if let Some(RequestBody::File { path, .. }) = &self.body {
            if let Err(e) = std::fs::remove_file(path) {
                warn!("Failed to clean up temporary file {path:?}: {e}");
            } else {
                debug!("Cleaned up temporary file: {path:?}");
            }
        }
    }
}

/// Top-level function to handle a client connection.
#[allow(clippy::too_many_arguments)]
pub fn handle_client(
    mut stream: ClientStream,
    base_dir: &Arc<PathBuf>,
    allowed_extensions: &Arc<Vec<glob::Pattern>>,
    username: &Arc<Option<String>>,
    password: &Arc<Option<String>>,
    chunk_size: usize,
    cli_config: Option<&crate::cli::Cli>,
    stats: Option<&crate::server::ServerStats>,
    router: &Arc<Router>,
) {
    let log_prefix = format!("[{}]", stream.peer_addr().unwrap());
    debug!("{} Handling client connection", log_prefix);
    trace!(
        "{} Client connection established, starting request processing",
        log_prefix
    );

    let request = match Request::from_stream(&mut stream) {
        Ok(req) => {
            debug!(
                "{} Successfully parsed request: {} {}",
                log_prefix, req.method, req.path
            );
            trace!(
                "{} Request headers count: {}",
                log_prefix,
                req.headers.len()
            );
            req
        }
        Err(e) => {
            warn!("{log_prefix} Failed to parse request: {e}");
            debug!("{} Sending error response for parse failure", log_prefix);
            send_error_response(&mut stream, e, &log_prefix);
            return;
        }
    };

    let start_time = std::time::Instant::now();
    let response_result = route_request(
        &request,
        base_dir,
        allowed_extensions,
        username,
        password,
        chunk_size,
        cli_config,
        stats,
        router,
    );
    let processing_time = start_time.elapsed();
    debug!("{} Request processed in {:?}", log_prefix, processing_time);

    match response_result {
        Ok(response) => {
            info!(
                "{} {} {} -> {}",
                log_prefix, request.method, request.path, response.status_code
            );
            trace!("{} Response status: {}", log_prefix, response.status_code);
            match send_response(&mut stream, response, &log_prefix) {
                Ok(body_bytes) => {
                    trace!(
                        "{} Response sent successfully, {} bytes",
                        log_prefix, body_bytes
                    );
                    if let Some(stats) = stats {
                        stats.record_request(true, body_bytes);
                    }
                }
                Err(e) => {
                    error!("{log_prefix} Failed to send response: {e}");
                    if let Some(stats) = stats {
                        stats.record_request(false, 0);
                    }
                }
            }
        }
        Err(e) => {
            warn!("{log_prefix} Error processing request: {e}");
            debug!(
                "{} Sending error response for processing failure",
                log_prefix
            );
            send_error_response(&mut stream, e, &log_prefix);
            if let Some(stats) = stats {
                stats.record_request(false, 0);
            }
        }
    }

    // Clean up any temporary files created during request processing
    request.cleanup();
}

// Static asset, favicon, upload, and health handlers moved to handlers.rs

/// Determines the correct response based on the request.
#[allow(clippy::too_many_arguments)]
fn route_request(
    request: &Request,
    base_dir: &Arc<PathBuf>,
    allowed_extensions: &Arc<Vec<glob::Pattern>>,
    _username: &Arc<Option<String>>,
    _password: &Arc<Option<String>>,
    chunk_size: usize,
    cli_config: Option<&crate::cli::Cli>,
    _stats: Option<&crate::server::ServerStats>,
    router: &Arc<Router>,
) -> Result<Response, AppError> {
    trace!("Routing {} {} through router", request.method, request.path);
    // Authentication is now handled by middleware in the router
    // First check if router handles the request (internal routes)
    if let Some(router_response) = router.route(request) {
        debug!(
            "Route found in router for {} {}",
            request.method, request.path
        );
        trace!("Router handler execution starting");
        return router_response;
    }

    // All non-internal paths (not starting with /_irondrop/) are treated as file / directory lookup
    if request.path.starts_with("/_irondrop/") {
        debug!("Internal path {} not found in router", request.path);
        return Err(AppError::NotFound);
    }

    debug!("Handling file request for path: {}", request.path);
    trace!("Using file handler for non-internal path");
    // Handle file and directory serving via dedicated handler
    crate::handlers::handle_file_request(
        request,
        base_dir,
        allowed_extensions,
        chunk_size,
        cli_config,
    )
}

/// Sends a fully formed `Response` to the client with enhanced headers.
fn send_response(
    stream: &mut ClientStream,
    response: Response,
    log_prefix: &str,
) -> Result<u64, std::io::Error> {
    info!(
        "{} {} {}",
        log_prefix, response.status_code, response.status_text
    );
    debug!(
        "{} Preparing response headers ({} custom headers)",
        log_prefix,
        response.headers.len()
    );

    let mut response_str = format!(
        "HTTP/1.1 {} {}
",
        response.status_code, response.status_text
    );

    // Add standard server headers first
    response_str.push_str(&format!(
        "Server: irondrop/{}
",
        crate::VERSION
    ));
    response_str.push_str(
        "Connection: close
",
    );

    // Add response-specific headers
    for (key, value) in &response.headers {
        trace!("{} Response header: {}: {}", log_prefix, key, value);
        response_str.push_str(&format!(
            "{key}: {value}
"
        ));
    }

    // Add Content-Length header ONLY if it is not already present in response.headers
    let has_content_length = response
        .headers
        .keys()
        .any(|k| k.to_lowercase() == "content-length");
    if !has_content_length {
        let length = match &response.body {
            ResponseBody::Text(text) => text.len(),
            ResponseBody::StaticText(text) => text.len(),
            ResponseBody::Binary(bytes) => bytes.len(),
            ResponseBody::StaticBinary(bytes) => bytes.len(),
            ResponseBody::Stream(file_details) => file_details.size as usize,
        };
        response_str.push_str(&format!(
            "Content-Length: {length}
"
        ));
    }

    response_str.push_str("\r\n");

    debug!(
        "{} Sending response headers ({} bytes)",
        log_prefix,
        response_str.len()
    );
    stream.write_all(response_str.as_bytes())?;

    // Send body and count only body bytes (exclude headers for stats)
    let mut body_sent: u64 = 0;
    debug!("{} Starting body transmission", log_prefix);
    match response.body {
        ResponseBody::Text(text) => {
            let bytes = text.as_bytes();
            trace!("{} Sending {} bytes of text data", log_prefix, bytes.len());
            stream.write_all(bytes)?;
            body_sent += bytes.len() as u64;
        }
        ResponseBody::StaticText(text) => {
            let bytes = text.as_bytes();
            trace!(
                "{} Sending {} bytes of static text",
                log_prefix,
                bytes.len()
            );
            stream.write_all(bytes)?;
            body_sent += bytes.len() as u64;
        }
        ResponseBody::Binary(bytes) => {
            trace!(
                "{} Sending {} bytes of binary data",
                log_prefix,
                bytes.len()
            );
            stream.write_all(&bytes)?;
            body_sent += bytes.len() as u64;
        }
        ResponseBody::StaticBinary(bytes) => {
            trace!(
                "{} Sending {} bytes of static binary data",
                log_prefix,
                bytes.len()
            );
            stream.write_all(bytes)?;
            body_sent += bytes.len() as u64;
        }
        ResponseBody::Stream(mut file_details) => {
            trace!(
                "{} Streaming file: {} bytes, chunk size: {}",
                log_prefix, file_details.size, file_details.chunk_size
            );
            let mut buffer = vec![0; file_details.chunk_size];
            let mut chunks_sent = 0;
            loop {
                let bytes_read = file_details.file.read(&mut buffer)?;
                if bytes_read == 0 {
                    break;
                }
                stream.write_all(&buffer[..bytes_read])?;
                body_sent += bytes_read as u64;
                chunks_sent += 1;
                if chunks_sent % 100 == 0 {
                    trace!(
                        "{} Streamed {} chunks ({} bytes so far)",
                        log_prefix, chunks_sent, body_sent
                    );
                }
            }
            debug!(
                "{} File streaming completed: {} chunks, {} bytes total",
                log_prefix, chunks_sent, body_sent
            );
        }
    }

    stream.flush()?;
    Ok(body_sent)
}

/// Sends a pre-canned error response using the new response system.
fn send_error_response(stream: &mut ClientStream, error: AppError, log_prefix: &str) {
    let (status_code, status_text) = match error {
        AppError::NotFound => (404, "Not Found"),
        AppError::Forbidden => (403, "Forbidden"),
        AppError::BadRequest => (400, "Bad Request"),
        AppError::Unauthorized => (401, "Unauthorized"),
        AppError::MethodNotAllowed => (405, "Method Not Allowed"),
        // Upload-specific error mappings
        AppError::PayloadTooLarge(_) => (413, "Payload Too Large"),
        AppError::InvalidFilename(_) => (400, "Bad Request"),
        AppError::UploadDiskFull(_) => (507, "Insufficient Storage"),
        AppError::UnsupportedMediaType(_) => (415, "Unsupported Media Type"),
        AppError::UploadDisabled => (403, "Forbidden"),
        _ => (500, "Internal Server Error"),
    };

    info!("{log_prefix} {status_code} {status_text}");

    let response = create_error_response(status_code, status_text);
    if let Err(e) = response.send(stream, log_prefix) {
        error!("{log_prefix} Failed to send error response: {e}");
    }
}