ruvel 0.16.0

The laravel alternative framework with rust performance
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
//! Growable per-connection HTTP/1.x framing driver (`Conn`)
//! and zero-copy borrowed `Request<'a>` view.

use std::io::{self, Read};

use tokio::io::{AsyncRead, AsyncReadExt};

use crate::constants::{
    MAX_REQUEST_BODY_SIZE, REQUEST_BODY_SIZE, REQUEST_BUFFER_SIZE, REQUEST_HEADER_SIZE,
};
use crate::http::Status as HttpStatus;
use crate::http::parser::Error;

use super::dechunk::{Dechunker, MAX_CHUNK_LINE, MAX_TRAILER};
use super::head::{Body, Head, Method, find_head_end};

const TIER_1: usize = 16 * 1024; // head buffer tier between `initial` and `REQUEST_HEADER_SIZE`
const RETAIN_MAX: usize = 64 * 1024; // buffers bigger than this are shrunk when idle
const CHUNK_SLACK: usize = MAX_CHUNK_LINE + MAX_TRAILER + 64;
const SHRINK_THRESHOLD: usize = if REQUEST_BUFFER_SIZE > RETAIN_MAX {
    REQUEST_BUFFER_SIZE
} else {
    RETAIN_MAX
};

#[derive(Clone, Copy, Debug)]
enum Phase {
    Head,
    /// `need` = `head_len + body_len`, counted from `pos`.
    Body {
        head_len: usize,
        need: usize,
    },
    Chunked {
        head_len: usize,
        dec: Dechunker,
    },
}

/// One complete request in the connection buffer. Pass it to [`Conn::request`] / [`Conn::consume`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Frame {
    pub head_len: usize,
    pub body_len: usize,
}

/// Per-connection growable buffer and resumable HTTP/1.x request state machine.
pub struct Conn {
    // always fully initialized; reads fill buf[filled..]
    buf: Vec<u8>,

    /// first unconsumed byte
    pos: usize,

    /// end of valid bytes
    filled: usize,

    // bytes of buf[pos..] already searched for the head terminator
    scanned: usize,

    phase: Phase,
    pending_100_continue: bool,
    pub head: Head,
}

impl Default for Conn {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl Conn {
    pub fn new() -> Conn {
        Conn {
            buf: vec![0u8; REQUEST_BUFFER_SIZE], // calloc: pages are not touched until used
            pos: 0,
            filled: 0,
            scanned: 0,
            phase: Phase::Head,
            pending_100_continue: false,
            head: Head::new(),
        }
    }

    #[inline]
    pub fn buffer_capacity(&self) -> usize {
        self.buf.len()
    }

    /// Returns `true` if there are unconsumed bytes buffered in this connection.
    #[inline]
    pub fn has_buffered_bytes(&self) -> bool {
        self.filled > self.pos
    }

    /// Atomically returns and clears whether an interim `100 Continue` response should be emitted
    /// before reading the request body.
    #[inline]
    pub fn take_expect_100_continue(&mut self) -> bool {
        let pending = self.pending_100_continue;
        self.pending_100_continue = false;
        pending
    }

    /// Attempts to advance and return one complete request strictly from already-buffered bytes
    /// without performing any socket I/O.
    ///
    /// Returns:
    /// - `Ok(Some(frame))` when a complete request is buffered and ready for [`Conn::request`].
    /// - `Ok(None)` when more bytes must be read from the peer via [`Conn::prepare_read`] and
    ///   [`Conn::fill`] / [`Conn::fill_async`].
    /// - `Err(status)` immediately on malformed syntax, unsupported protocol/encoding, or limit violation.
    pub fn try_next_request(&mut self) -> Result<Option<Frame>, HttpStatus> {
        loop {
            match self.phase {
                Phase::Head => {
                    if self.try_head()? {
                        continue;
                    }
                    return Ok(None);
                }
                Phase::Body { head_len, need } => {
                    let unread = self.filled - self.pos;
                    if unread >= need {
                        return Ok(Some(Frame {
                            head_len,
                            body_len: need - head_len,
                        }));
                    }
                    return Ok(None);
                }
                Phase::Chunked { head_len, mut dec } => {
                    let b0 = self.pos + head_len;
                    let (done, new_len) = dec.advance(&mut self.buf[b0..self.filled])?;
                    self.filled = b0 + new_len;
                    self.phase = Phase::Chunked { head_len, dec };
                    if done {
                        return Ok(Some(Frame {
                            head_len,
                            body_len: dec.w,
                        }));
                    }
                    return Ok(None);
                }
            }
        }
    }

    /// Ensures spare room exists in `self.buf[self.filled..]` appropriate for the current phase
    /// before calling [`Conn::fill`] or [`Conn::fill_async`].
    pub fn prepare_read(&mut self) {
        match self.phase {
            Phase::Head => {
                if self.filled == self.buf.len() {
                    // Out of room. If a consumed prefix exists, compacting is enough;
                    // otherwise climb a tier (initial -> 16 KiB -> REQUEST_HEADER_SIZE).
                    let want = if self.pos > 0 {
                        self.buf.len()
                    } else {
                        next_tier(self.buf.len())
                    };
                    self.reserve(want);
                }
            }
            Phase::Body { head_len, need } => {
                let unread = self.filled - self.pos;
                let eager_body = head_len.saturating_add(REQUEST_BODY_SIZE);
                // Exact allocation for ordinary bodies up to `REQUEST_BODY_SIZE`. For larger ones
                // grow geometrically with the bytes that really arrived, so a lying Content-Length
                // cannot force megabytes to be reserved up front.
                let want = if need <= eager_body {
                    need
                } else {
                    need.min(unread.saturating_mul(2).max(eager_body).max(unread + 1))
                };
                self.reserve(want);
            }
            Phase::Chunked { head_len, .. } => {
                let unread = self.filled - self.pos;
                let eager_body = head_len.saturating_add(REQUEST_BODY_SIZE);
                let cap = head_len
                    .saturating_add(MAX_REQUEST_BODY_SIZE)
                    .saturating_add(CHUNK_SLACK);
                let want = unread
                    .saturating_mul(2)
                    .max(eager_body)
                    .min(cap)
                    .max(unread + 1);
                self.reserve(want);
            }
        }
    }

    /// Reads until one complete request is buffered from a synchronous [`Read`] stream.
    ///
    /// `Ok(None)` = peer closed cleanly between requests. Safe to call again after `WouldBlock`:
    /// all progress is preserved. The returned [`Frame`] stays valid until [`Conn::consume`].
    pub fn read_request<R: Read>(&mut self, r: &mut R) -> Result<Option<Frame>, Error> {
        loop {
            if let Some(frame) = self.try_next_request()? {
                return Ok(Some(frame));
            }
            self.prepare_read();
            if self.fill(r)? == 0 {
                return if matches!(self.phase, Phase::Head) && self.filled == self.pos {
                    Ok(None)
                } else {
                    Err(Error::Eof)
                };
            }
        }
    }

    /// Reads until one complete request is buffered from an asynchronous [`AsyncRead`] stream.
    ///
    /// `Ok(None)` = peer closed cleanly between requests.
    pub async fn read_request_async<R: AsyncRead + Unpin>(
        &mut self,
        r: &mut R,
    ) -> Result<Option<Frame>, Error> {
        loop {
            if let Some(frame) = self.try_next_request()? {
                return Ok(Some(frame));
            }
            self.prepare_read();
            if self.fill_async(r).await? == 0 {
                return if matches!(self.phase, Phase::Head) && self.filled == self.pos {
                    Ok(None)
                } else {
                    Err(Error::Eof)
                };
            }
        }
    }

    /// Reads bytes asynchronously into the connection's spare buffer space.
    #[inline]
    pub async fn fill_async<R: AsyncRead + Unpin>(&mut self, r: &mut R) -> io::Result<usize> {
        debug_assert!(self.filled < self.buf.len());
        loop {
            match r.read(&mut self.buf[self.filled..]).await {
                Ok(n) => {
                    self.filled += n;
                    return Ok(n);
                }
                Err(e) if e.kind() == io::ErrorKind::Interrupted => {}
                Err(e) => return Err(e),
            }
        }
    }

    /// Parses or scans buffered bytes for the head terminator; on success switches phase.
    /// Returns whether the phase changed.
    ///
    /// On the first read (`scanned == 0`), executes a single-pass SIMD/scalar parse directly
    /// via [`Head::parse_prefix_status`]. Only if the head spans multiple reads or errors
    /// does it use incremental [`find_head_end`] scanning / error classification.
    fn try_head(&mut self) -> Result<bool, HttpStatus> {
        let data = &self.buf[self.pos..self.filled];
        if data.is_empty() {
            return Ok(false);
        }
        let scan_limit = data.len().min(REQUEST_HEADER_SIZE);

        let head_len = if self.scanned == 0 {
            match self.head.parse_prefix_status(&data[..scan_limit]) {
                Ok(Some(len)) => len,
                Ok(None) => {
                    if data.len() >= REQUEST_HEADER_SIZE {
                        return Err(HttpStatus::RequestHeaderFieldsTooLarge);
                    }
                    self.scanned = scan_limit;
                    return Ok(false);
                }
                Err(status) => {
                    return match find_head_end(&data[..scan_limit], 0) {
                        Err(()) => Err(HttpStatus::BadRequest),
                        Ok(None) => {
                            if data.len() >= REQUEST_HEADER_SIZE {
                                Err(HttpStatus::RequestHeaderFieldsTooLarge)
                            } else {
                                self.scanned = scan_limit;
                                Ok(false)
                            }
                        }
                        Ok(Some(len)) => {
                            if len > REQUEST_HEADER_SIZE {
                                Err(HttpStatus::RequestHeaderFieldsTooLarge)
                            } else {
                                Err(status)
                            }
                        }
                    };
                }
            }
        } else {
            match find_head_end(&data[..scan_limit], self.scanned) {
                Err(()) => return Err(HttpStatus::BadRequest),
                Ok(None) => {
                    if data.len() >= REQUEST_HEADER_SIZE {
                        return Err(HttpStatus::RequestHeaderFieldsTooLarge);
                    }
                    self.scanned = scan_limit;
                    return Ok(false);
                }
                Ok(Some(len)) => {
                    if len > REQUEST_HEADER_SIZE {
                        return Err(HttpStatus::RequestHeaderFieldsTooLarge);
                    }
                    self.head.parse(&data[..len])?;
                    len
                }
            }
        };

        let no_body_buffered_yet = head_len == data.len();
        let (phase, expects_body) = match self.head.body {
            Body::None => (
                Phase::Body {
                    head_len,
                    need: head_len,
                },
                false,
            ),
            Body::Length(n) => {
                // Rejected from the header alone, before any body byte is buffered.
                if n > MAX_REQUEST_BODY_SIZE {
                    return Err(HttpStatus::PayloadTooLarge);
                }
                let need = head_len.checked_add(n).ok_or(HttpStatus::PayloadTooLarge)?;
                (Phase::Body { head_len, need }, n > 0)
            }
            Body::Chunked => (
                Phase::Chunked {
                    head_len,
                    dec: Dechunker::new(),
                },
                true,
            ),
        };
        self.pending_100_continue =
            self.head.expect_100_continue && no_body_buffered_yet && expects_body;
        self.phase = phase;
        Ok(true)
    }

    /// Guarantees `buf.len() - pos >= want`, compacting and/or growing in one copy.
    fn reserve(&mut self, want: usize) {
        if self.buf.len() - self.pos >= want {
            return;
        }
        let unread = self.filled - self.pos;
        if want <= self.buf.len() {
            self.buf.copy_within(self.pos..self.filled, 0);
        } else {
            let mut nb = vec![0u8; want];
            nb[..unread].copy_from_slice(&self.buf[self.pos..self.filled]);
            self.buf = nb;
        }
        self.pos = 0;
        self.filled = unread;
    }

    /// Reads bytes synchronously into the connection's spare buffer space.
    #[inline]
    pub fn fill<R: Read>(&mut self, r: &mut R) -> io::Result<usize> {
        debug_assert!(self.filled < self.buf.len());
        loop {
            match r.read(&mut self.buf[self.filled..]) {
                Ok(n) => {
                    self.filled += n;
                    return Ok(n);
                }
                Err(e) if e.kind() == io::ErrorKind::Interrupted => {}
                Err(e) => return Err(e),
            }
        }
    }

    /// Borrowed view of the request described by `f`.
    #[inline]
    pub fn request(&self, f: Frame) -> Request<'_> {
        let h = self.pos + f.head_len;
        Request {
            head: &self.head,
            raw: &self.buf[self.pos..h],
            body: &self.buf[h..h + f.body_len],
        }
    }

    /// Drops the request. Pipelined bytes already read stay for the next call.
    pub fn consume(&mut self, f: Frame) {
        self.pos += f.head_len + f.body_len;
        self.scanned = 0;
        self.pending_100_continue = false;
        self.phase = Phase::Head;
        let unread = self.filled - self.pos;
        if unread == 0 {
            self.pos = 0;
            self.filled = 0;
        }
        // A one-off big upload must not pin its buffer for the life of the connection.
        if self.buf.len() > SHRINK_THRESHOLD && unread <= REQUEST_BUFFER_SIZE {
            let mut nb = vec![0u8; REQUEST_BUFFER_SIZE];
            nb[..unread].copy_from_slice(&self.buf[self.pos..self.filled]);
            self.buf = nb;
            self.pos = 0;
            self.filled = unread;
        }
    }
}

fn next_tier(len: usize) -> usize {
    let t = if len < TIER_1 {
        TIER_1.min(REQUEST_HEADER_SIZE)
    } else {
        REQUEST_HEADER_SIZE
    };
    t.max(len + 1)
}

/// Zero-copy borrowed HTTP/1.x request view backed by a [`Conn`] or raw slice buffer.
#[derive(Clone, Copy)]
pub struct Request<'a> {
    head: &'a Head,
    raw: &'a [u8],
    body: &'a [u8],
}

impl<'a> Request<'a> {
    #[inline]
    pub(crate) fn from_parts(head: &'a Head, raw: &'a [u8], body: &'a [u8]) -> Self {
        Self { head, raw, body }
    }

    #[inline]
    pub fn method(&self) -> Method {
        self.head.method
    }

    #[inline]
    pub fn raw_head(&self) -> &'a [u8] {
        self.raw
    }

    #[inline]
    pub fn minor(&self) -> u8 {
        self.head.minor
    }

    #[inline]
    pub fn target(&self) -> &'a [u8] {
        self.head.target.of(self.raw)
    }

    #[inline]
    pub fn path(&self) -> &'a [u8] {
        self.head.path.of(self.raw)
    }

    #[inline]
    pub fn query(&self) -> &'a [u8] {
        self.head.query.of(self.raw)
    }

    #[inline]
    pub fn body(&self) -> &'a [u8] {
        self.body
    }

    #[inline]
    pub fn keep_alive(&self) -> bool {
        self.head.keep_alive
    }

    #[inline]
    pub fn header_count(&self) -> usize {
        self.head.header_count()
    }

    #[inline]
    pub fn headers(&self) -> impl Iterator<Item = (&'a [u8], &'a [u8])> + 'a {
        let (head, raw) = (self.head, self.raw);
        head.header_spans()
            .iter()
            .map(move |&(n, v)| (n.of(raw), v.of(raw)))
    }

    /// Case-insensitive lookup; linear over at most `MAX_REQUEST_HEADERS` short names.
    #[inline]
    pub fn header(&self, name: &[u8]) -> Option<&'a [u8]> {
        self.headers()
            .find(|(n, _)| n.eq_ignore_ascii_case(name))
            .map(|(_, v)| v)
    }
}