Skip to main content

turnloop_http/
http1.rs

1//! Incremental RFC 9112 framing. The host retains unconsumed input and owns writes.
2//! `Body` borrows input; head/trailer allocations are the returned representation.
3use crate::{Error, Result};
4use std::io::Write;
5
6#[derive(Debug, Clone, Copy)]
7pub struct Limits {
8    pub headers: usize,
9    pub head_bytes: usize,
10    pub line_bytes: usize,
11}
12impl Default for Limits {
13    fn default() -> Self {
14        Self {
15            headers: 128,
16            head_bytes: 32768,
17            line_bytes: 8192,
18        }
19    }
20}
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct Header {
23    pub name: String,
24    pub value: Vec<u8>,
25}
26impl Header {
27    pub fn new(name: &str, value: impl AsRef<[u8]>) -> Self {
28        Self {
29            name: name.to_ascii_lowercase(),
30            value: value.as_ref().to_vec(),
31        }
32    }
33}
34#[derive(Debug, Clone)]
35pub struct Head {
36    pub method: String,
37    pub target: String,
38    pub status: u16,
39    pub version: u8,
40    pub headers: Vec<Header>,
41    pub keep_alive: bool,
42}
43impl Head {
44    pub fn get(&self, name: &str) -> Option<&[u8]> {
45        self.headers
46            .iter()
47            .find(|h| h.name.eq_ignore_ascii_case(name))
48            .map(|h| h.value.as_slice())
49    }
50    pub fn token(&self, name: &str, token: &str) -> bool {
51        self.headers
52            .iter()
53            .filter(|h| h.name.eq_ignore_ascii_case(name))
54            .any(|h| tokens(&h.value).any(|t| t.eq_ignore_ascii_case(token.as_bytes())))
55    }
56}
57#[derive(Debug, Clone, Copy, PartialEq, Eq)]
58pub enum Mode {
59    Request,
60    Response,
61}
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
63enum State {
64    Head,
65    Fixed(u64),
66    ChunkSize,
67    Chunk(u64),
68    ChunkEnd,
69    Trailers,
70    Eof,
71    End,
72    Done,
73    Upgrade,
74    Failed,
75}
76#[derive(Debug)]
77pub enum Event<'a> {
78    Head(Head),
79    Informational(Head),
80    Body(&'a [u8]),
81    Trailers(Vec<Header>),
82    End,
83    Upgrade,
84}
85#[derive(Debug)]
86pub struct Step<'a> {
87    pub consumed: usize,
88    pub event: Option<Event<'a>>,
89}
90#[derive(Debug)]
91pub struct Decoder {
92    mode: Mode,
93    limits: Limits,
94    state: State,
95    head_request: bool,
96    connect_request: bool,
97    keep_alive: bool,
98}
99fn invalid(message: &'static str) -> Error {
100    Error::new("HPE_INVALID_HEADER_TOKEN", message)
101}
102fn tokens(v: &[u8]) -> impl Iterator<Item = &[u8]> {
103    v.split(|b| *b == b',').map(trim)
104}
105fn trim(v: &[u8]) -> &[u8] {
106    v.trim_ascii()
107}
108fn boundary(input: &[u8]) -> Option<usize> {
109    input
110        .windows(4)
111        .position(|b| b == b"\r\n\r\n")
112        .map(|n| n + 4)
113}
114fn lines_valid(input: &[u8], max: usize) -> Result<()> {
115    let mut start = 0;
116    for (i, b) in input.iter().enumerate() {
117        if *b == b'\n' {
118            if i == 0 || input[i - 1] != b'\r' {
119                return Err(invalid("bare LF"));
120            }
121            if i - start > max {
122                return Err(Error::new("HPE_HEADER_OVERFLOW", "line limit"));
123            }
124            start = i + 1;
125        } else if *b == b'\r' && i + 1 < input.len() && input[i + 1] != b'\n' {
126            return Err(invalid("bare CR"));
127        }
128    }
129    if input.len() - start > max {
130        return Err(Error::new("HPE_HEADER_OVERFLOW", "line limit"));
131    }
132    Ok(())
133}
134fn headers(raw: &[httparse::Header<'_>], limits: Limits) -> Result<Vec<Header>> {
135    if raw.len() > limits.headers {
136        return Err(Error::new("HPE_HEADER_OVERFLOW", "header count limit"));
137    }
138    Ok(raw.iter().map(|h| Header::new(h.name, h.value)).collect())
139}
140fn lengths(head: &Head) -> Result<(Option<u64>, bool)> {
141    let mut cl = None;
142    let mut te = false;
143    for h in &head.headers {
144        if h.name.eq_ignore_ascii_case("content-length") {
145            // Deliberately reject even identical duplicates: no downstream ambiguity.
146            if cl.is_some() || h.value.is_empty() || !h.value.iter().all(u8::is_ascii_digit) {
147                return Err(Error::new(
148                    "HPE_UNEXPECTED_CONTENT_LENGTH",
149                    "invalid or duplicate content-length",
150                ));
151            }
152            cl = Some(
153                h.value
154                    .iter()
155                    .try_fold(0u64, |n, b| {
156                        n.checked_mul(10)?.checked_add((b - b'0') as u64)
157                    })
158                    .ok_or(invalid("content-length overflow"))?,
159            );
160        }
161        if h.name.eq_ignore_ascii_case("transfer-encoding") {
162            if te || !h.value.eq_ignore_ascii_case(b"chunked") {
163                return Err(Error::new(
164                    "HPE_INVALID_TRANSFER_ENCODING",
165                    "unsupported or duplicate transfer coding",
166                ));
167            }
168            te = true;
169        }
170    }
171    if cl.is_some() && te {
172        return Err(Error::new(
173            "HPE_UNEXPECTED_CONTENT_LENGTH",
174            "transfer-encoding with content-length",
175        ));
176    }
177    if te && head.version == 0 {
178        return Err(invalid("transfer-encoding on HTTP/1.0"));
179    }
180    Ok((cl, te))
181}
182impl Decoder {
183    pub fn new(mode: Mode, limits: Limits) -> Self {
184        Self {
185            mode,
186            limits,
187            state: State::Head,
188            head_request: false,
189            connect_request: false,
190            keep_alive: false,
191        }
192    }
193    /// Must precede response parsing. Pipelining is intentionally not provided.
194    pub fn response_to(&mut self, method: &str) {
195        self.head_request = method == "HEAD";
196        self.connect_request = method == "CONNECT";
197    }
198    pub fn reusable(&self) -> bool {
199        self.state == State::Done && self.keep_alive
200    }
201    pub fn reset(&mut self) -> Result<()> {
202        if !self.reusable() {
203            return Err(invalid("connection is not reusable"));
204        }
205        self.state = State::Head;
206        Ok(())
207    }
208    pub fn receive<'a>(&mut self, input: &'a [u8]) -> Result<Step<'a>> {
209        let result = self.receive_inner(input);
210        if result.is_err() {
211            self.state = State::Failed;
212        }
213        result
214    }
215    fn receive_inner<'a>(&mut self, input: &'a [u8]) -> Result<Step<'a>> {
216        let mut step = Step {
217            consumed: 0,
218            event: None,
219        };
220        match self.state {
221            State::Head => {
222                let end = boundary(input);
223                let n = end.unwrap_or(input.len());
224                if n > self.limits.head_bytes {
225                    return Err(Error::new("HPE_HEADER_OVERFLOW", "head size limit"));
226                }
227                lines_valid(&input[..n], self.limits.line_bytes)?;
228                let Some(end) = end else { return Ok(step) };
229                let mut slots = [httparse::EMPTY_HEADER; 256];
230                let mut head = if self.mode == Mode::Response {
231                    let mut parsed = httparse::Response::new(&mut slots);
232                    parsed
233                        .parse(&input[..end])
234                        .map_err(|_| invalid("invalid response head"))?;
235                    Head {
236                        method: String::new(),
237                        target: String::new(),
238                        status: parsed.code.ok_or(invalid("missing status"))?,
239                        version: parsed.version.unwrap_or(1),
240                        headers: headers(parsed.headers, self.limits)?,
241                        keep_alive: false,
242                    }
243                } else {
244                    let mut parsed = httparse::Request::new(&mut slots);
245                    parsed
246                        .parse(&input[..end])
247                        .map_err(|_| invalid("invalid request head"))?;
248                    Head {
249                        method: parsed.method.ok_or(invalid("missing method"))?.into(),
250                        target: parsed.path.ok_or(invalid("missing target"))?.into(),
251                        status: 0,
252                        version: parsed.version.unwrap_or(1),
253                        headers: headers(parsed.headers, self.limits)?,
254                        keep_alive: false,
255                    }
256                };
257                if self.mode == Mode::Request
258                    && head.version == 1
259                    && (head.headers.iter().filter(|h| h.name == "host").count() != 1
260                        || head.get("host").is_none_or(|v| v.is_empty()))
261                {
262                    return Err(invalid("HTTP/1.1 requires exactly one Host"));
263                }
264                // RFC 9112 6.3: successful CONNECT starts a tunnel regardless of
265                // message framing fields in its response.
266                let (cl, te) = if self.mode == Mode::Response
267                    && self.connect_request
268                    && (200..300).contains(&head.status)
269                {
270                    (None, false)
271                } else {
272                    lengths(&head)?
273                };
274                head.keep_alive = !head.token("connection", "close")
275                    && (head.version == 1 || head.token("connection", "keep-alive"));
276                self.keep_alive = head.keep_alive;
277                step.consumed = end;
278                if self.mode == Mode::Response
279                    && (100..200).contains(&head.status)
280                    && head.status != 101
281                {
282                    if cl.is_some() || te {
283                        return Err(invalid("informational response has body framing"));
284                    }
285                    step.event = Some(Event::Informational(head));
286                    return Ok(step);
287                }
288                self.state = if self.mode == Mode::Response
289                    && (head.status == 101
290                        || (self.connect_request && (200..300).contains(&head.status)))
291                {
292                    self.keep_alive = false;
293                    State::Upgrade
294                } else if self.mode == Mode::Response
295                    && (self.head_request || head.status == 204 || head.status == 304)
296                {
297                    State::End
298                } else if te {
299                    State::ChunkSize
300                } else if let Some(n) = cl {
301                    if n == 0 { State::End } else { State::Fixed(n) }
302                } else if self.mode == Mode::Request {
303                    State::End
304                } else {
305                    self.keep_alive = false;
306                    State::Eof
307                };
308                if self.mode == Mode::Response && head.status == 204 && (cl.is_some() || te) {
309                    return Err(invalid("204 has body framing"));
310                }
311                step.event = Some(Event::Head(head));
312            }
313            State::Fixed(left) | State::Chunk(left) => {
314                let n = input.len().min(usize::try_from(left).unwrap_or(usize::MAX));
315                if n > 0 {
316                    step.consumed = n;
317                    step.event = Some(Event::Body(&input[..n]));
318                    self.state = match self.state {
319                        State::Fixed(_) => {
320                            if left == n as u64 {
321                                State::End
322                            } else {
323                                State::Fixed(left - n as u64)
324                            }
325                        }
326                        _ => {
327                            if left == n as u64 {
328                                State::ChunkEnd
329                            } else {
330                                State::Chunk(left - n as u64)
331                            }
332                        }
333                    };
334                }
335            }
336            State::ChunkSize => {
337                let end = input.windows(2).position(|w| w == b"\r\n");
338                let n = end.unwrap_or(input.len());
339                if n > self.limits.line_bytes {
340                    return Err(invalid("chunk line limit"));
341                }
342                let Some(n) = end else { return Ok(step) };
343                let digits = input[..n].split(|b| *b == b';').next().unwrap();
344                if digits.is_empty() || !digits.iter().all(u8::is_ascii_hexdigit) {
345                    return Err(invalid("invalid chunk size"));
346                }
347                if input[..n].iter().any(|b| *b < 32 || *b == 127) {
348                    return Err(invalid("invalid chunk extension"));
349                }
350                let size = digits
351                    .iter()
352                    .try_fold(0u64, |v, b| {
353                        v.checked_mul(16)?
354                            .checked_add((*b as char).to_digit(16)? as u64)
355                    })
356                    .ok_or(invalid("chunk overflow"))?;
357                self.state = if size == 0 {
358                    State::Trailers
359                } else {
360                    State::Chunk(size)
361                };
362                step.consumed = n + 2;
363            }
364            State::ChunkEnd => {
365                if input.len() >= 2 {
366                    if &input[..2] != b"\r\n" {
367                        return Err(invalid("missing chunk CRLF"));
368                    }
369                    step.consumed = 2;
370                    self.state = State::ChunkSize;
371                }
372            }
373            State::Trailers => {
374                if input.starts_with(b"\r\n") {
375                    step.consumed = 2;
376                    self.state = State::End;
377                    return Ok(step);
378                }
379                let end = boundary(input);
380                let n = end.unwrap_or(input.len());
381                if n > self.limits.head_bytes {
382                    return Err(invalid("trailer size limit"));
383                }
384                lines_valid(&input[..n], self.limits.line_bytes)?;
385                let Some(n) = end else { return Ok(step) };
386                let mut slots = [httparse::EMPTY_HEADER; 256];
387                let httparse::Status::Complete((_, raw)) =
388                    httparse::parse_headers(&input[..n], &mut slots)
389                        .map_err(|_| invalid("invalid trailers"))?
390                else {
391                    return Err(invalid("incomplete trailers"));
392                };
393                let trailers = headers(raw, self.limits)?;
394                if trailers.iter().any(|h| {
395                    matches!(
396                        h.name.as_str(),
397                        "content-length"
398                            | "transfer-encoding"
399                            | "host"
400                            | "connection"
401                            | "trailer"
402                            | "upgrade"
403                    )
404                }) {
405                    return Err(invalid("forbidden trailer"));
406                }
407                step.consumed = n;
408                step.event = Some(Event::Trailers(trailers));
409                self.state = State::End;
410            }
411            State::Eof => {
412                if !input.is_empty() {
413                    step.consumed = input.len();
414                    step.event = Some(Event::Body(input));
415                }
416            }
417            State::End => {
418                self.state = State::Done;
419                step.event = Some(Event::End);
420            }
421            State::Upgrade => {
422                self.state = State::Done;
423                step.event = Some(Event::Upgrade);
424            }
425            State::Done => {}
426            State::Failed => return Err(invalid("failed connection")),
427        }
428        Ok(step)
429    }
430    pub fn eof(&mut self) -> Result<()> {
431        if self.state == State::Eof {
432            self.state = State::End;
433            Ok(())
434        } else if matches!(self.state, State::End | State::Done) {
435            Ok(())
436        } else {
437            self.state = State::Failed;
438            Err(Error::new("UND_ERR_SOCKET", "unexpected EOF"))
439        }
440    }
441}
442#[derive(Debug, Clone, Copy)]
443pub enum BodyLength {
444    Empty,
445    Known(u64),
446    Chunked,
447}
448/// Caller-owned wire buffer, reused across commands. No body buffering.
449pub struct Encoder {
450    left: BodyLength,
451    finished: bool,
452}
453impl Encoder {
454    pub fn start(head: &Head, body: BodyLength, out: &mut Vec<u8>) -> Result<Self> {
455        // Validate everything before mutating the output.
456        for h in &head.headers {
457            validate_field(h)?;
458        }
459        let (cl, te) = lengths(head)?;
460        let expect_cl = match body {
461            BodyLength::Known(n) => Some(n),
462            BodyLength::Empty => Some(0),
463            BodyLength::Chunked => None,
464        };
465        if (cl.is_some() && cl != expect_cl)
466            || (te && !matches!(body, BodyLength::Chunked))
467            || (cl.is_some() && matches!(body, BodyLength::Chunked))
468        {
469            return Err(invalid("body length conflicts with headers"));
470        }
471        if head.status == 0 {
472            if !valid_token(head.method.as_bytes()) {
473                return Err(invalid("invalid method"));
474            }
475            if head.target.is_empty() || head.target.bytes().any(|b| b <= 32 || b == 127) {
476                return Err(invalid("invalid request target"));
477            }
478            write!(out, "{} {} HTTP/1.1\r\n", head.method, head.target).unwrap();
479        } else {
480            let status =
481                http::StatusCode::from_u16(head.status).map_err(|_| invalid("invalid status"))?;
482            write!(
483                out,
484                "HTTP/1.1 {} {}\r\n",
485                head.status,
486                status.canonical_reason().unwrap_or("")
487            )
488            .unwrap();
489        }
490        for h in &head.headers {
491            out.extend_from_slice(h.name.as_bytes());
492            out.extend_from_slice(b": ");
493            out.extend_from_slice(&h.value);
494            out.extend_from_slice(b"\r\n");
495        }
496        if cl.is_none() && !te {
497            match body {
498                BodyLength::Known(n) => write!(out, "content-length: {n}\r\n").unwrap(),
499                BodyLength::Chunked => out.extend_from_slice(b"transfer-encoding: chunked\r\n"),
500                BodyLength::Empty => {}
501            }
502        }
503        out.extend_from_slice(b"\r\n");
504        Ok(Self {
505            left: body,
506            finished: false,
507        })
508    }
509    pub fn body(&mut self, bytes: &[u8], out: &mut Vec<u8>) -> Result<()> {
510        if self.finished {
511            return Err(invalid("body already finished"));
512        }
513        match self.left {
514            BodyLength::Empty => {
515                if !bytes.is_empty() {
516                    return Err(invalid("unexpected body"));
517                }
518            }
519            BodyLength::Known(n) => {
520                if bytes.len() as u64 > n {
521                    return Err(invalid("body exceeds content-length"));
522                }
523                out.extend_from_slice(bytes);
524                self.left = BodyLength::Known(n - bytes.len() as u64);
525            }
526            BodyLength::Chunked => {
527                if !bytes.is_empty() {
528                    write!(out, "{:x}\r\n", bytes.len()).unwrap();
529                    out.extend_from_slice(bytes);
530                    out.extend_from_slice(b"\r\n");
531                }
532            }
533        }
534        Ok(())
535    }
536    pub fn finish(&mut self, trailers: &[Header], out: &mut Vec<u8>) -> Result<()> {
537        if self.finished || matches!(self.left, BodyLength::Known(1..)) {
538            return Err(invalid("incomplete or already finished body"));
539        }
540        if !trailers.is_empty() && !matches!(self.left, BodyLength::Chunked) {
541            return Err(invalid("trailers require chunked body"));
542        }
543        for h in trailers {
544            if matches!(
545                h.name.as_str(),
546                "content-length"
547                    | "transfer-encoding"
548                    | "host"
549                    | "connection"
550                    | "trailer"
551                    | "upgrade"
552            ) {
553                return Err(invalid("forbidden trailer"));
554            }
555            validate_field(h)?;
556        }
557        if matches!(self.left, BodyLength::Chunked) {
558            out.extend_from_slice(b"0\r\n");
559            for h in trailers {
560                out.extend_from_slice(h.name.as_bytes());
561                out.extend_from_slice(b": ");
562                out.extend_from_slice(&h.value);
563                out.extend_from_slice(b"\r\n");
564            }
565            out.extend_from_slice(b"\r\n");
566        }
567        self.finished = true;
568        Ok(())
569    }
570}
571
572/// RFC token grammar, shared with HPACK field validation without temporary types.
573pub(crate) fn valid_token(value: &[u8]) -> bool {
574    !value.is_empty()
575        && value
576            .iter()
577            .all(|b| b.is_ascii_alphanumeric() || b"!#$%&'*+-.^_`|~".contains(b))
578}
579fn validate_field(h: &Header) -> Result<()> {
580    if !valid_token(h.name.as_bytes())
581        || h.value
582            .iter()
583            .any(|b| (*b < 32 && *b != b'\t') || *b == 127)
584    {
585        return Err(invalid("invalid header field"));
586    }
587    Ok(())
588}