| @@ -17,14 +17,14 @@ |
| 17 | 17 | //! and `Transfer-Encoding` (400) and any coding other than `chunked` |
| 18 | 18 | //! (501), and closes the connection after every rejected request. |
| 19 | 19 | |
| 20 | +use tilrt_prims::SMALL_INT; |
| 21 | + |
| 20 | 22 | use crate::abi::{Ctx, Value, int}; |
| 21 | 23 | use crate::int::saturating; |
| 22 | 24 | use crate::values::{bytes_of, cell, list, string}; |
| 23 | 25 | |
| 24 | 26 | /// Headers a request may have; a request with more is invalid. |
| 25 | 27 | const MAX_HEADERS: usize = 64; |
| 26 | | −/// Lengths become `Int`s, which must stay small (63 bits). |
| 27 | | −const MAX_LENGTH: u64 = 1 << 62; |
| 28 | 28 | |
| 29 | 29 | // Constructor tags of `Head` and `Chunk` (`std/http.til`). |
| 30 | 30 | const HEAD_PARTIAL: i64 = 0; |
| @@ -93,14 +93,15 @@ pub(crate) fn parse_head(buffer: &[u8], at: usize) -> Head<'_> { |
| 93 | 93 | }) |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | | −/// A `Content-Length` value: decimal digits only (no sign), below 2^62, |
| 96 | +/// A `Content-Length` value: decimal digits only (no sign), a small `Int`, |
| 97 | 97 | /// once surrounding whitespace is trimmed. |
| 98 | 98 | fn decimal_length(value: &[u8]) -> Option<i64> { |
| 99 | 99 | let value = std::str::from_utf8(value).unwrap_or("").trim(); |
| 100 | 100 | if value.is_empty() || !value.bytes().all(|b| b.is_ascii_digit()) { |
| 101 | 101 | return None; |
| 102 | 102 | } |
| 103 | | − value.parse::<i64>().ok().filter(|n| *n < MAX_LENGTH as i64) |
| 103 | + // An offset or a length is a small `Int`. |
| 104 | + value.parse::<i64>().ok().filter(|n| SMALL_INT.contains(n)) |
| 104 | 105 | } |
| 105 | 106 | |
| 106 | 107 | /// One chunk of a chunked body, parsed from an offset of a buffer. |
| @@ -132,13 +133,9 @@ pub(crate) fn parse_chunk(buffer: &[u8], at: usize) -> Chunk { |
| 132 | 133 | }; |
| 133 | 134 | let start = at + line; |
| 134 | 135 | if size > 0 { |
| 135 | | − return if size < MAX_LENGTH { |
| 136 | | − Chunk::Data { |
| 137 | | − start, |
| 138 | | − size: size as i64, |
| 139 | | − } |
| 140 | | − } else { |
| 141 | | − Chunk::Invalid("chunk too large".into()) |
| 136 | + return match i64::try_from(size) { |
| 137 | + Ok(size) if SMALL_INT.contains(&size) => Chunk::Data { start, size }, |
| 138 | + _ => Chunk::Invalid("chunk too large".into()), |
| 142 | 139 | }; |
| 143 | 140 | } |
| 144 | 141 | let mut trailers = [httparse::EMPTY_HEADER; MAX_HEADERS]; |