-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconn.rs
More file actions
1383 lines (1267 loc) · 52.9 KB
/
Copy pathconn.rs
File metadata and controls
1383 lines (1267 loc) · 52.9 KB
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
//! The sans-I/O HTTP/1.x connection engine.
//!
//! [`H1Conn`] owns no socket. You [`feed`](H1Conn::feed) it the plaintext bytes
//! that arrived on the connection, [`poll_request`](H1Conn::poll_request) until
//! it yields a complete [`Request`], hand it back a [`Response`] with
//! [`respond`](H1Conn::respond), and drain the serialized reply with
//! [`take_out`](H1Conn::take_out). A runtime driver supplies the actual I/O (and,
//! for HTTPS, a TLS layer sits between the socket and this engine).
use std::fs::File;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use super::response::BodyKind;
use super::response::STREAM_CHUNK;
use super::{Headers, Method, Request, Response, StatusCode, Version};
use crate::error::{Error, Result};
use crate::proto::read_at_exact;
/// Tunable limits applied while parsing requests, to bound memory use and
/// reject abusive peers.
#[derive(Debug, Clone, Copy)]
pub struct Limits {
/// Maximum size of the request line + header block, in bytes.
pub max_header_bytes: usize,
/// Maximum size of a (buffered) request body, in bytes.
pub max_body_bytes: usize,
}
impl Default for Limits {
fn default() -> Limits {
Limits {
max_header_bytes: 64 * 1024,
max_body_bytes: 16 * 1024 * 1024,
}
}
}
/// Per-request facts the engine must remember between [`H1Conn::poll_request`]
/// and [`H1Conn::respond`].
#[derive(Debug, Clone, Copy)]
struct Pending {
version: Version,
keep_alive: bool,
is_head: bool,
}
/// A response body being streamed from a file across multiple drains.
#[derive(Debug)]
struct FileBody {
file: Arc<File>,
/// Absolute offset of the next byte to read.
offset: u64,
/// Bytes still to send.
remaining: u64,
}
/// A sans-I/O HTTP/1.x server connection.
#[derive(Debug)]
pub struct H1Conn {
inbuf: Vec<u8>,
outbuf: Vec<u8>,
/// A file body still being streamed out. While set, [`take_out`](H1Conn::take_out)
/// keeps yielding the next `STREAM_CHUNK`-sized slice until EOF.
body_stream: Option<FileBody>,
limits: Limits,
/// The request currently awaiting a response, if any. While set,
/// `poll_request` yields nothing (one in-flight request at a time).
pending: Option<Pending>,
/// How far into `inbuf` we have already scanned for the header-terminating
/// `\r\n\r\n`. Lets a request fed one byte at a time resume the scan instead
/// of re-scanning the whole buffer every poll (otherwise O(n²)). Indexes
/// into `inbuf`, so it MUST be reset to 0 whenever consumed bytes are
/// drained from `inbuf`.
head_scanned: usize,
/// In-progress chunked-body decode state, persisted across polls so a slowly
/// arriving body is decoded incrementally rather than from offset 0 each
/// time (otherwise O(n²)). Present only while a chunked body is mid-receive.
chunk: Option<ChunkDecoder>,
/// Set once a response with `Connection: close` (or a fatal error) has been
/// serialized; the driver should close after draining `outbuf`.
closed: bool,
/// Whether a `100 Continue` interim response has already been emitted for
/// the request currently being received.
interim_sent: bool,
/// Optional `Server` header value advertised on responses.
server_name: Option<String>,
}
impl Default for H1Conn {
fn default() -> H1Conn {
H1Conn::new(Limits::default())
}
}
impl H1Conn {
/// Create a new connection engine with the given limits.
pub fn new(limits: Limits) -> H1Conn {
H1Conn {
inbuf: Vec::new(),
outbuf: Vec::new(),
body_stream: None,
limits,
pending: None,
head_scanned: 0,
chunk: None,
closed: false,
interim_sent: false,
server_name: Some(concat!("httpsd/", env!("CARGO_PKG_VERSION")).to_owned()),
}
}
/// Set the `Server` header value (or `None` to omit it).
pub fn set_server_name(&mut self, name: Option<String>) {
self.server_name = name;
}
/// Push freshly received plaintext bytes into the engine.
pub fn feed(&mut self, data: &[u8]) {
self.inbuf.extend_from_slice(data);
}
/// Drain and return the next batch of serialized response bytes.
///
/// The caller is expected to write the returned bytes to the transport in
/// full, then keep calling while [`has_output`](Self::has_output) is true so
/// a file body is streamed to completion. Returns queued buffer bytes first;
/// once those are drained it reads and returns the next `STREAM_CHUNK` of
/// any in-progress file body. Returns an empty vec when there is nothing
/// pending. A mid-stream read error (or a file that shrank) drops the stream
/// and marks the connection for close rather than panicking.
pub fn take_out(&mut self) -> Vec<u8> {
if !self.outbuf.is_empty() {
return std::mem::take(&mut self.outbuf);
}
let Some(fb) = self.body_stream.as_mut() else {
return Vec::new();
};
let want = (fb.remaining as usize).min(STREAM_CHUNK);
let mut buf = vec![0u8; want];
match read_at_exact(&fb.file, fb.offset, &mut buf) {
Ok(n) => {
buf.truncate(n);
fb.offset += n as u64;
fb.remaining -= n as u64;
if n < want {
// The file came up short of the promised Content-Length: the
// response is now corrupt, so close the connection.
self.body_stream = None;
self.closed = true;
} else if fb.remaining == 0 {
self.body_stream = None;
}
buf
}
Err(_) => {
self.body_stream = None;
self.closed = true;
Vec::new()
}
}
}
/// Whether there is output still to be written — queued bytes or an
/// in-progress file body that has not reached EOF.
pub fn has_output(&self) -> bool {
!self.outbuf.is_empty() || self.body_stream.is_some()
}
/// Whether the connection should be closed once `outbuf` has been written.
pub fn wants_close(&self) -> bool {
self.closed
}
/// Whether a request has been delivered and is awaiting [`respond`](Self::respond).
pub fn awaiting_response(&self) -> bool {
self.pending.is_some()
}
/// Try to parse the next complete request from the buffered input.
///
/// Returns `Ok(Some(req))` when a full request (headers + body) is
/// available, `Ok(None)` when more bytes are needed, and `Err(..)` on a
/// protocol violation — in which case an error response has already been
/// queued via [`take_out`](Self::take_out) and the connection is marked for
/// close.
pub fn poll_request(&mut self) -> Result<Option<Request>> {
if self.closed || self.pending.is_some() {
return Ok(None);
}
// Locate the end of the header block, resuming the scan where the
// previous poll left off so a request fed one byte at a time is not
// re-scanned from the start (which would be O(n²)). Back up by 3 bytes
// so a `\r\n\r\n` straddling the previous boundary is still found.
let search_from = self.head_scanned.saturating_sub(3);
let head_end = match find_subslice(&self.inbuf[search_from..], b"\r\n\r\n") {
Some(rel) => {
let end = search_from + rel;
// Resume future scans right at the terminator: while we wait for
// the body, re-finding it then costs O(1) instead of re-scanning
// the (possibly large) buffered body for the header terminator.
self.head_scanned = end + 3;
end
}
None => {
self.head_scanned = self.inbuf.len();
if self.inbuf.len() > self.limits.max_header_bytes {
return Err(self.fail(StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE, "headers"));
}
return Ok(None);
}
};
let header_block_len = head_end; // bytes before the terminator
if header_block_len > self.limits.max_header_bytes {
return Err(self.fail(StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE, "headers"));
}
let body_start = head_end + 4;
// Parse request line + headers from the header block.
let head = &self.inbuf[..header_block_len];
let (method, target, version, headers) = match parse_head(head) {
Ok(parts) => parts,
Err(e) => {
// Map parse errors to the right status, then fail the connection.
let status = match &e {
Error::BadRequest(_) => StatusCode::BAD_REQUEST,
_ => StatusCode::BAD_REQUEST,
};
return Err(self.fail(status, "request line/headers"));
}
};
if version.is_none() {
return Err(self.fail(StatusCode::HTTP_VERSION_NOT_SUPPORTED, "version"));
}
let version = version.unwrap();
// Determine body framing.
let framing = match body_framing(&headers) {
Ok(f) => f,
Err(()) => return Err(self.fail(StatusCode::BAD_REQUEST, "body framing")),
};
// Resolve the body (may need more bytes).
let body: Vec<u8>;
let consumed_total: usize;
match framing {
BodyFraming::None => {
body = Vec::new();
consumed_total = body_start;
}
BodyFraming::Length(len) => {
if len > self.limits.max_body_bytes {
return Err(self.fail(StatusCode::PAYLOAD_TOO_LARGE, "body"));
}
// Compute the body end with checked arithmetic: with a
// pathologically large `max_body_bytes`, `body_start + len` could
// otherwise wrap (release) or panic (debug), and the slice below
// could then panic. Mirror the chunked path's checked_add.
let Some(body_end) = body_start.checked_add(len) else {
return Err(self.fail(StatusCode::PAYLOAD_TOO_LARGE, "body"));
};
if self.inbuf.len() < body_end {
self.maybe_send_continue(&headers);
return Ok(None);
}
body = self.inbuf[body_start..body_end].to_vec();
consumed_total = body_end;
}
BodyFraming::Chunked => {
// Resume the incremental decoder where the previous poll stopped
// (or start a fresh one). `body_start` is stable for the lifetime
// of this request — `inbuf` is not drained until it completes —
// so the decoder's offsets remain valid across polls.
let mut dec = self.chunk.take().unwrap_or_default();
match dec.advance(&self.inbuf[body_start..], self.limits.max_body_bytes) {
Ok(Some(used)) => {
body = std::mem::take(&mut dec.out);
consumed_total = body_start + used;
}
Ok(None) => {
self.chunk = Some(dec);
self.maybe_send_continue(&headers);
return Ok(None);
}
Err(status) => return Err(self.fail(status, "chunked body")),
}
}
}
// Commit: drop the consumed bytes and remember per-request state. The
// header scan cursor indexes into `inbuf`, so reset it now that those
// bytes are gone; the chunked decoder (if any) was consumed above.
self.inbuf.drain(..consumed_total);
self.head_scanned = 0;
self.chunk = None;
self.interim_sent = false;
let keep_alive = negotiate_keep_alive(version, &headers);
let is_head = method.is_head();
self.pending = Some(Pending {
version,
keep_alive,
is_head,
});
Ok(Some(Request::new(method, target, version, headers, body)))
}
/// Serialize `resp` for the request most recently returned by
/// [`poll_request`](Self::poll_request).
///
/// Panics if there is no request awaiting a response.
pub fn respond(&mut self, resp: Response) {
let meta = self
.pending
.take()
.expect("respond() called with no request in flight");
self.serialize(meta, resp);
}
// ---- internals ----
/// Queue a self-contained error response and mark the connection closed.
fn fail(&mut self, status: StatusCode, what: &'static str) -> Error {
let meta = Pending {
version: Version::Http11,
keep_alive: false,
is_head: false,
};
let resp = Response::status(status);
self.pending = None;
self.head_scanned = 0;
self.chunk = None;
self.serialize(meta, resp);
self.closed = true;
match status.code() {
413 | 431 => Error::TooLarge(what),
_ => Error::BadRequest(what),
}
}
fn maybe_send_continue(&mut self, headers: &Headers) {
if !self.interim_sent && headers.contains_token("expect", "100-continue") {
self.outbuf
.extend_from_slice(b"HTTP/1.1 100 Continue\r\n\r\n");
self.interim_sent = true;
}
}
fn serialize(&mut self, meta: Pending, resp: Response) {
let (status, mut headers, body) = resp.into_parts();
let bodyless = status.is_bodyless();
let omit_body = bodyless || meta.is_head;
// Strip hop-by-hop / framing headers a handler may have supplied so the
// engine's own framing (Content-Length / Connection) stays authoritative
// and no rogue Transfer-Encoding can desync the connection.
for h in HOP_BY_HOP_HEADERS {
headers.remove(h);
}
// Framing headers.
if !bodyless {
// Authoritative Content-Length (handlers should not set their own).
headers.set("Content-Length", body.len().to_string());
} else {
headers.remove("Content-Length");
}
let keep_alive = meta.keep_alive && !self.closed;
headers.set(
"Connection",
if keep_alive { "keep-alive" } else { "close" },
);
if let Some(server) = &self.server_name {
headers.set_if_absent("Server", server.clone());
}
headers.set_if_absent("Date", http_date(now_secs()));
// Status line.
let line = format!(
"{} {} {}\r\n",
meta.version.as_str(),
status.code(),
status.reason()
);
self.outbuf.extend_from_slice(line.as_bytes());
for (name, value) in headers.iter() {
// Never emit a header whose name is not a valid token or whose value
// carries CR/LF/NUL: that would allow response splitting / header
// injection from handler-controlled data.
if !is_token(name)
|| value
.bytes()
.any(|b| b == b'\r' || b == b'\n' || b == b'\0')
{
continue;
}
self.outbuf.extend_from_slice(name.as_bytes());
self.outbuf.extend_from_slice(b": ");
self.outbuf.extend_from_slice(value.as_bytes());
self.outbuf.extend_from_slice(b"\r\n");
}
self.outbuf.extend_from_slice(b"\r\n");
if !omit_body {
match body.into_kind() {
BodyKind::Bytes(bytes) => self.outbuf.extend_from_slice(&bytes),
// Stream a file body across subsequent `take_out` calls rather
// than buffering it. HEAD/bodyless responses fall in the
// `omit_body` arm above, so no read happens for them.
BodyKind::File { file, offset, len } if len > 0 => {
self.body_stream = Some(FileBody {
file,
offset,
remaining: len,
});
}
BodyKind::File { .. } => {}
}
}
if !keep_alive {
self.closed = true;
}
}
}
/// How a request body is delimited.
enum BodyFraming {
None,
Length(usize),
Chunked,
}
/// Decide body framing from headers, rejecting the smuggling-prone combination
/// of both `Transfer-Encoding` and `Content-Length`.
fn body_framing(headers: &Headers) -> std::result::Result<BodyFraming, ()> {
let has_te = headers.contains("transfer-encoding");
let has_cl = headers.contains("content-length");
if has_te && has_cl {
return Err(());
}
if has_te {
// RFC 9112 §6.1: if `Transfer-Encoding` is present, `chunked` must be the
// final coding, or the message length is undeterminable. Reject anything
// else (an unknown final coding, a non-final `chunked`, or `chunked`
// spread across multiple TE fields) rather than guessing the framing.
return match final_coding_is_chunked(headers) {
true => Ok(BodyFraming::Chunked),
false => Err(()),
};
}
// Multiple Content-Length values must agree.
let mut len: Option<usize> = None;
for v in headers.get_all("content-length") {
// Require a non-empty, all-ASCII-digit value: `parse()` would otherwise
// accept a leading `+` and assorted Unicode whitespace/digits.
let v = v.trim();
if v.is_empty() || !v.bytes().all(|b| b.is_ascii_digit()) {
return Err(());
}
let parsed: usize = v.parse().map_err(|_| ())?;
match len {
Some(prev) if prev != parsed => return Err(()),
_ => len = Some(parsed),
}
}
match len {
Some(0) | None => Ok(BodyFraming::None),
Some(n) => Ok(BodyFraming::Length(n)),
}
}
/// Whether the `Transfer-Encoding` header(s) name `chunked` as the FINAL coding
/// and nowhere else, per RFC 9112 §6.1.
///
/// The codings across every `Transfer-Encoding` field are concatenated in order
/// (multiple fields are equivalent to one comma-joined field). `chunked` is
/// accepted only when it is exactly the last coding; if it also appears as a
/// non-final coding (which would mean the body is chunked more than once, i.e.
/// framing is ambiguous), or appears in more than one field, we reject.
fn final_coding_is_chunked(headers: &Headers) -> bool {
let mut codings: Vec<&str> = Vec::new();
for v in headers.get_all("transfer-encoding") {
for tok in v.split(',') {
let tok = tok.trim();
if !tok.is_empty() {
codings.push(tok);
}
}
}
let chunked_count = codings
.iter()
.filter(|t| t.eq_ignore_ascii_case("chunked"))
.count();
// `chunked` must appear exactly once, and it must be the final coding.
chunked_count == 1
&& codings
.last()
.is_some_and(|t| t.eq_ignore_ascii_case("chunked"))
}
/// Negotiate connection persistence per RFC 9112 §9.3.
fn negotiate_keep_alive(version: Version, headers: &Headers) -> bool {
if headers.contains_token("connection", "close") {
return false;
}
if headers.contains_token("connection", "keep-alive") {
return true;
}
version.default_keep_alive()
}
/// Parse the request line and header fields from the header block (the bytes
/// before the terminating CRLFCRLF).
fn parse_head(head: &[u8]) -> Result<(Method, String, Option<Version>, Headers)> {
// Reject any bare CR or bare LF in the header block: every CR must be
// immediately followed by LF and every LF immediately preceded by CR.
// A stray `\n`/`\r` inside a field would otherwise enable request
// smuggling / response splitting downstream (RFC 9112 §2.2).
for (i, &b) in head.iter().enumerate() {
if b == b'\r' {
if head.get(i + 1) != Some(&b'\n') {
return Err(Error::BadRequest("bare CR in header block"));
}
} else if b == b'\n' && (i == 0 || head[i - 1] != b'\r') {
return Err(Error::BadRequest("bare LF in header block"));
}
}
let text = std::str::from_utf8(head).map_err(|_| Error::BadRequest("non-UTF-8 header"))?;
let mut lines = text.split("\r\n");
let request_line = lines.next().ok_or(Error::BadRequest("empty request"))?;
let mut parts = request_line.split(' ');
let method = parts.next().ok_or(Error::BadRequest("no method"))?;
let target = parts.next().ok_or(Error::BadRequest("no target"))?;
let version_tok = parts.next().ok_or(Error::BadRequest("no version"))?;
if parts.next().is_some() {
return Err(Error::BadRequest("trailing request-line tokens"));
}
if method.is_empty() || target.is_empty() {
return Err(Error::BadRequest("empty request-line token"));
}
// Reject control characters in the request target. CR/LF/SP are already
// excluded (they delimit the request line / header block), but NUL, TAB,
// DEL and other C0 controls must not slip through into the target, where
// they could enable smuggling or corrupt downstream routing.
if target.bytes().any(|b| b < 0x20 || b == 0x7f) {
return Err(Error::BadRequest("control character in request target"));
}
let method = Method::parse(method);
let version = Version::parse(version_tok);
let mut headers = Headers::new();
for line in lines {
if line.is_empty() {
continue;
}
// Reject obsolete line folding (leading whitespace continuation).
if line.starts_with(' ') || line.starts_with('\t') {
return Err(Error::BadRequest("obsolete header folding"));
}
if headers.len() >= MAX_HEADER_FIELDS {
return Err(Error::BadRequest("too many header fields"));
}
let (name, value) = line
.split_once(':')
.ok_or(Error::BadRequest("header without colon"))?;
// The field name must be a valid RFC 9110 token, validated on the
// UNtrimmed name: whitespace between the name and the colon is illegal.
if !is_token(name) {
return Err(Error::BadRequest("invalid header name"));
}
let value = value.trim();
if !is_valid_field_value(value) {
return Err(Error::BadRequest("invalid header value"));
}
headers.append(name, value);
}
Ok((method, target.to_owned(), version, headers))
}
/// Incremental decoder for a chunked request body.
///
/// Unlike a one-shot decoder, this persists its progress (`pos`, the decoded
/// `out` accumulator, and the in-chunk `state`) across calls to [`advance`].
/// Each call resumes where the previous one stopped and only processes
/// newly-arrived bytes, so a body fed one byte at a time costs O(n) total work
/// rather than O(n²) from re-decoding the whole region every poll.
///
/// [`advance`]: ChunkDecoder::advance
#[derive(Debug, Default)]
struct ChunkDecoder {
/// Bytes of the body region already consumed (parse cursor).
pos: usize,
/// Decoded body accumulated so far.
out: Vec<u8>,
/// What part of a chunk we are currently in.
state: ChunkState,
}
/// The position within the chunked grammar that [`ChunkDecoder`] is resuming at.
#[derive(Debug, Default)]
enum ChunkState {
/// Awaiting (the rest of) a chunk-size line.
#[default]
Size,
/// In a chunk's data: `remaining` data bytes still to copy.
Data { remaining: usize },
/// Awaiting the CRLF that terminates a chunk's data.
DataCrlf,
/// In the trailer section that follows the terminating zero-length chunk;
/// `start` is the body offset where the trailer section began (used to bound
/// it against [`MAX_TRAILER_BYTES`]).
Trailer { start: usize },
}
impl ChunkDecoder {
/// Advance the decode over `data` (the full body region as buffered so far).
///
/// Returns `Ok(Some(consumed))` once the terminating zero-length chunk and
/// its trailer section have arrived (`consumed` is the number of body-region
/// bytes the body occupies, and [`out`](Self::out) holds the decoded body),
/// `Ok(None)` if more bytes are needed, or `Err(status)` on a malformed or
/// oversized body. Every security check matches the one-shot decoder; only
/// the incrementality differs.
fn advance(
&mut self,
data: &[u8],
max_body: usize,
) -> std::result::Result<Option<usize>, StatusCode> {
loop {
match self.state {
ChunkState::Size => {
// Chunk size line. Bound how far we'll scan for its CRLF so a
// peer that never terminates the line (or pads it with
// megabytes of chunk extensions) cannot make us buffer
// without bound.
let eol = match find_subslice(&data[self.pos..], b"\r\n") {
Some(eol) => eol,
None => {
if data.len() - self.pos > MAX_CHUNK_LINE_BYTES {
return Err(StatusCode::BAD_REQUEST);
}
return Ok(None);
}
};
if eol > MAX_CHUNK_LINE_BYTES {
return Err(StatusCode::BAD_REQUEST);
}
let size_line = &data[self.pos..self.pos + eol];
// Strip any chunk extensions after ';'.
let hex = match size_line.iter().position(|&b| b == b';') {
Some(i) => &size_line[..i],
None => size_line,
};
let hex = std::str::from_utf8(hex).map_err(|_| StatusCode::BAD_REQUEST)?;
let hex = hex.trim();
// Require a non-empty run of hex digits: this both rejects
// garbage and surfaces a too-large value as a parse error
// rather than wrapping.
if hex.is_empty() || !hex.bytes().all(|b| b.is_ascii_hexdigit()) {
return Err(StatusCode::BAD_REQUEST);
}
// A value that does not fit in usize (e.g. `fffffffffffffff0`
// on a 32-bit target, or 17+ hex digits anywhere) parses to
// Err here rather than overflowing.
let size =
usize::from_str_radix(hex, 16).map_err(|_| StatusCode::BAD_REQUEST)?;
// Cap a single chunk immediately: this both enforces the body
// limit and keeps every subsequent arithmetic operation far
// from overflow.
if size > max_body {
return Err(StatusCode::PAYLOAD_TOO_LARGE);
}
let after_size = self.pos + eol + 2;
if size == 0 {
// Last chunk: consume the whole trailer section
// `*(field CRLF) CRLF` so trailer bytes are not left to be
// misread as the next request.
self.pos = after_size;
self.state = ChunkState::Trailer { start: after_size };
continue;
}
// Enforce the cumulative body limit with checked arithmetic.
match self.out.len().checked_add(size) {
Some(total) if total <= max_body => {}
_ => return Err(StatusCode::PAYLOAD_TOO_LARGE),
}
self.pos = after_size;
self.state = ChunkState::Data { remaining: size };
}
ChunkState::Data { remaining } => {
// Copy only the newly-arrived slice of this chunk's data; the
// already-copied prefix is never revisited.
let avail = data.len() - self.pos;
let take = remaining.min(avail);
self.out.extend_from_slice(&data[self.pos..self.pos + take]);
self.pos += take;
let left = remaining - take;
if left > 0 {
self.state = ChunkState::Data { remaining: left };
return Ok(None);
}
self.state = ChunkState::DataCrlf;
}
ChunkState::DataCrlf => {
// Each chunk's data is followed by a literal CRLF.
if data.len() < self.pos + 2 {
return Ok(None);
}
if &data[self.pos..self.pos + 2] != b"\r\n" {
return Err(StatusCode::BAD_REQUEST);
}
self.pos += 2;
self.state = ChunkState::Size;
}
ChunkState::Trailer { start } => {
// Consume the trailer section `*(field CRLF) CRLF`, resuming at
// `self.pos`. Bounded against MAX_TRAILER_BYTES so a peer
// cannot stream trailers forever.
loop {
if self.pos.saturating_sub(start) > MAX_TRAILER_BYTES {
return Err(StatusCode::BAD_REQUEST);
}
match find_subslice(&data[self.pos..], b"\r\n") {
None => {
// Incomplete; bound how much we'll buffer waiting.
if data.len() - start > MAX_TRAILER_BYTES {
return Err(StatusCode::BAD_REQUEST);
}
return Ok(None);
}
// An empty line terminates the trailer section.
Some(0) => return Ok(Some(self.pos + 2)),
// A trailer field line; skip it (trailers are not surfaced).
Some(eol) => self.pos += eol + 2,
}
}
}
}
}
}
}
/// Maximum number of header fields accepted in a single request.
const MAX_HEADER_FIELDS: usize = 100;
/// Maximum length of a single chunk-size line (size + extensions), in bytes.
const MAX_CHUNK_LINE_BYTES: usize = 16 * 1024;
/// Maximum size of the trailer section after the terminating zero-length chunk.
const MAX_TRAILER_BYTES: usize = 8 * 1024;
/// Hop-by-hop / framing headers that must never be forwarded from a handler's
/// response: the engine owns connection framing, so these are stripped before
/// serialization (RFC 9110 §7.6.1, plus the de-facto `proxy-connection`).
const HOP_BY_HOP_HEADERS: [&str; 7] = [
"transfer-encoding",
"connection",
"keep-alive",
"upgrade",
"te",
"trailer",
"proxy-connection",
];
/// Whether `b` is an RFC 9110 `tchar` (a valid character in a `token`).
fn is_tchar(b: u8) -> bool {
b.is_ascii_alphanumeric()
|| matches!(
b,
b'!' | b'#'
| b'$'
| b'%'
| b'&'
| b'\''
| b'*'
| b'+'
| b'-'
| b'.'
| b'^'
| b'_'
| b'`'
| b'|'
| b'~'
)
}
/// Whether `s` is a non-empty RFC 9110 `token` (used for field names).
fn is_token(s: &str) -> bool {
!s.is_empty() && s.bytes().all(is_tchar)
}
/// Whether `s` is a valid field value: no control characters (0x00-0x1F) other
/// than HTAB, and no DEL (0x7F). obs-text (0x80-0xFF) is permitted.
fn is_valid_field_value(s: &str) -> bool {
s.bytes().all(|b| (b >= 0x20 && b != 0x7f) || b == b'\t')
}
/// Find the first occurrence of `needle` in `haystack`.
///
/// Scans for the needle's first byte and only then compares the full needle, so
/// for the short `\r\n` / `\r\n\r\n` needles used here it is a single linear
/// pass rather than the O(n·m) `windows().position()` it replaces.
fn find_subslice(haystack: &[u8], needle: &[u8]) -> Option<usize> {
let nlen = needle.len();
if nlen == 0 || haystack.len() < nlen {
return None;
}
let first = needle[0];
let last_start = haystack.len() - nlen;
let mut i = 0;
while i <= last_start {
// Jump straight to the next occurrence of the needle's first byte.
let off = haystack[i..=last_start].iter().position(|&b| b == first)?;
let cand = i + off;
if haystack[cand..cand + nlen] == *needle {
return Some(cand);
}
i = cand + 1;
}
None
}
/// Current Unix time in whole seconds (0 if the clock is before the epoch).
fn now_secs() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0)
}
/// Format a Unix timestamp as an RFC 9110 IMF-fixdate, e.g.
/// `Sun, 06 Nov 1994 08:49:37 GMT`.
pub(crate) fn http_date(secs: u64) -> String {
const WDAY: [&str; 7] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const MON: [&str; 12] = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
];
let days = (secs / 86_400) as i64;
let tod = secs % 86_400;
let (h, mi, s) = (tod / 3600, (tod % 3600) / 60, tod % 60);
// Howard Hinnant's civil-from-days algorithm.
let z = days + 719_468;
let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
let doe = z - era * 146_097; // [0, 146096]
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; // [0, 399]
let mut year = yoe + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365]
let mp = (5 * doy + 2) / 153; // [0, 11]
let day = doy - (153 * mp + 2) / 5 + 1; // [1, 31]
let month = if mp < 10 { mp + 3 } else { mp - 9 }; // [1, 12]
if month <= 2 {
year += 1;
}
let wday = ((days % 7 + 7) % 7 + 4) % 7; // 1970-01-01 was a Thursday
format!(
"{}, {:02} {} {:04} {:02}:{:02}:{:02} GMT",
WDAY[wday as usize],
day,
MON[(month - 1) as usize],
year,
h,
mi,
s,
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::proto::Body;
/// Write `data` to a fresh temp file and return it opened read-only.
fn temp_file(data: &[u8]) -> Arc<File> {
use std::io::Write;
let path = std::env::temp_dir().join(format!(
"httpsd-h1-stream-{}-{}",
std::process::id(),
COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
));
let mut f = File::create(&path).unwrap();
f.write_all(data).unwrap();
f.sync_all().unwrap();
let opened = File::open(&path).unwrap();
let _ = std::fs::remove_file(&path); // unlinked; fd keeps it alive
Arc::new(opened)
}
static COUNTER: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
/// Drain a connection's output fully (headers + every streamed body chunk),
/// asserting it returns a result for each chunk while `has_output` is true.
fn drain_all(conn: &mut H1Conn) -> Vec<u8> {
let mut out = Vec::new();
while conn.has_output() {
let chunk = conn.take_out();
if chunk.is_empty() {
break;
}
out.extend_from_slice(&chunk);
}
out
}
#[test]
fn streams_multichunk_file_with_correct_length() {
// Larger than STREAM_CHUNK so it must be served across several drains.
let n = 5 * STREAM_CHUNK + 123;
let data: Vec<u8> = (0..n).map(|i| (i % 251) as u8).collect();
let file = temp_file(&data);
let mut c = H1Conn::default();
let _ = drive(&mut c, b"GET / HTTP/1.1\r\nConnection: close\r\n\r\n").unwrap();
c.respond(Response::new(StatusCode::OK).body(Body::file(file, 0, n as u64)));
let out = drain_all(&mut c);
let split = find_subslice(&out, b"\r\n\r\n").unwrap() + 4;
let head = String::from_utf8(out[..split].to_vec()).unwrap();
assert!(
head.contains(&format!("Content-Length: {n}\r\n")),
"head: {head}"
);
assert_eq!(&out[split..], &data[..], "streamed body must be byte-exact");
}
#[test]
fn streams_file_range_span_only() {
let data: Vec<u8> = (0..(2 * STREAM_CHUNK)).map(|i| (i % 256) as u8).collect();
let file = temp_file(&data);
let (start, len) = (1000u64, (STREAM_CHUNK + 7) as u64);
let mut c = H1Conn::default();
let _ = drive(&mut c, b"GET / HTTP/1.1\r\nConnection: close\r\n\r\n").unwrap();
c.respond(Response::new(StatusCode::PARTIAL_CONTENT).body(Body::file(file, start, len)));
let out = drain_all(&mut c);
let split = find_subslice(&out, b"\r\n\r\n").unwrap() + 4;
assert_eq!(
&out[split..],
&data[start as usize..(start + len) as usize],
"range body must be exactly the requested span"
);
}
#[test]
fn head_file_sends_length_but_no_body() {
let data = vec![7u8; 3 * STREAM_CHUNK];
let file = temp_file(&data);
let mut c = H1Conn::default();
let _ = drive(&mut c, b"HEAD / HTTP/1.1\r\nConnection: close\r\n\r\n").unwrap();
c.respond(Response::new(StatusCode::OK).body(Body::file(file, 0, data.len() as u64)));
let out = drain_all(&mut c);
let text = String::from_utf8(out).unwrap();
assert!(
text.contains(&format!("Content-Length: {}\r\n", data.len())),
"head: {text}"
);
assert!(text.ends_with("\r\n\r\n"), "HEAD must send no body: {text}");
}
fn drive(conn: &mut H1Conn, input: &[u8]) -> Option<Request> {
conn.feed(input);
conn.poll_request().unwrap()
}
#[test]
fn parses_simple_get() {
let mut c = H1Conn::default();
let req = drive(&mut c, b"GET /hello?x=1 HTTP/1.1\r\nHost: a\r\n\r\n").unwrap();
assert_eq!(req.method(), &Method::Get);
assert_eq!(req.path(), "/hello");
assert_eq!(req.query(), Some("x=1"));
assert_eq!(req.host(), Some("a"));
assert!(req.body().is_empty());
}
#[test]
fn waits_for_full_body() {
let mut c = H1Conn::default();
c.feed(b"POST / HTTP/1.1\r\nContent-Length: 5\r\n\r\nab");
assert!(c.poll_request().unwrap().is_none());