use super::*;
pub(crate) async fn fill_chunked_body<R>(
reader: &mut R,
body: &mut RequestBody,
config: &RequestConfig,
) -> Result<(), RequestError>
where
R: AsyncBufReadExt + Unpin,
{
body.clear();
let mut line: Vec<u8> = Vec::new();
let mut extension_budget: usize = MAX_CHUNK_EXTENSION_BYTES;
let max_body_size: usize = config.get_max_body_size();
loop {
read_chunk_line(reader, &mut line, MAX_CHUNK_LINE_BYTES).await?;
let size_end: usize = line
.iter()
.position(|byte: &u8| !byte.is_ascii_hexdigit())
.unwrap_or(line.len());
let (size, extensions): (&[u8], &[u8]) = line.split_at(size_end);
extension_budget = extension_budget
.checked_sub(extensions.len())
.ok_or(CHUNK_METADATA_TOO_LARGE)?;
let size: &str = from_utf8(size).map_err(|_| INVALID_CHUNK)?;
let size: usize = usize::from_str_radix(size, 16).map_err(|_| INVALID_CHUNK)?;
validate_chunk_extensions(extensions)?;
if size == 0 {
return discard_chunk_trailers(reader, &mut line, config).await;
}
if size > max_body_size.saturating_sub(body.len()) {
return Err(RequestError::ContentLengthTooLarge(
HttpStatus::PayloadTooLarge,
));
}
read_chunk_data(reader, body, size).await?;
}
}
async fn read_chunk_line<R>(
reader: &mut R,
line: &mut Vec<u8>,
max_len: usize,
) -> Result<(), RequestError>
where
R: AsyncBufReadExt + Unpin,
{
line.clear();
while line.len() < max_len {
let available: &[u8] = reader.fill_buf().await?;
if available.is_empty() {
return Err(INVALID_CHUNK);
}
let scan_len: usize = available.len().min(max_len - line.len());
let newline: Option<usize> = available[..scan_len]
.iter()
.position(|byte: &u8| *byte == b'\n');
let consumed: usize = newline.map_or(scan_len, |index: usize| index + 1);
line.extend_from_slice(&available[..consumed]);
reader.consume(consumed);
if newline.is_some() {
if !line.ends_with(HTTP_BR_BYTES) {
return Err(INVALID_CHUNK);
}
line.truncate(line.len() - HTTP_BR_BYTES.len());
return Ok(());
}
}
Err(CHUNK_METADATA_TOO_LARGE)
}
async fn read_chunk_data<R>(
reader: &mut R,
body: &mut RequestBody,
mut remaining: usize,
) -> Result<(), RequestError>
where
R: AsyncBufReadExt + Unpin,
{
while remaining != 0 {
let available: &[u8] = reader.fill_buf().await?;
if available.is_empty() {
return Err(INVALID_CHUNK);
}
let consumed: usize = available.len().min(remaining);
body.try_reserve(consumed)
.map_err(|_| RequestError::ContentLengthTooLarge(HttpStatus::PayloadTooLarge))?;
body.extend_from_slice(&available[..consumed]);
reader.consume(consumed);
remaining -= consumed;
}
let mut delimiter: [u8; HTTP_BR_BYTES.len()] = [0; HTTP_BR_BYTES.len()];
reader.read_exact(&mut delimiter).await?;
if delimiter != HTTP_BR_BYTES {
return Err(INVALID_CHUNK);
}
Ok(())
}
fn validate_chunk_extensions(mut input: &[u8]) -> Result<(), RequestError> {
while !input.is_empty() {
let name: &[u8] = trim_chunk_ows(input)
.strip_prefix(b";")
.ok_or(INVALID_CHUNK)?;
input = skip_chunk_token(trim_chunk_ows(name))?;
if let Some(value) = trim_chunk_ows(input).strip_prefix(b"=") {
let value: &[u8] = trim_chunk_ows(value);
input = match value.strip_prefix(b"\"") {
Some(quoted) => skip_chunk_quoted_string(quoted)?,
None => skip_chunk_token(value)?,
};
}
}
Ok(())
}
fn skip_chunk_quoted_string(input: &[u8]) -> Result<&[u8], RequestError> {
let mut escaped: bool = false;
for (index, &byte) in input.iter().enumerate() {
if !is_chunk_field_value_byte(byte) {
return Err(INVALID_CHUNK);
}
if escaped {
escaped = false;
} else if byte == b'\\' {
escaped = true;
} else if byte == b'"' {
return Ok(&input[index + 1..]);
}
}
Err(INVALID_CHUNK)
}
async fn discard_chunk_trailers<R>(
reader: &mut R,
line: &mut Vec<u8>,
config: &RequestConfig,
) -> Result<(), RequestError>
where
R: AsyncBufReadExt + Unpin,
{
let mut budget: usize = MAX_CHUNK_TRAILER_BYTES;
let mut count: usize = 0;
loop {
read_chunk_line(reader, line, budget.min(MAX_CHUNK_LINE_BYTES)).await?;
if line.is_empty() {
return Ok(());
}
budget -= line.len() + HTTP_BR_BYTES.len();
count += 1;
Request::check_http_header_count(count, config.get_max_header_count())?;
validate_chunk_trailer(line, config)?;
}
}
fn validate_chunk_trailer(line: &[u8], config: &RequestConfig) -> Result<(), RequestError> {
let colon: usize = line
.iter()
.position(|byte: &u8| *byte == b':')
.ok_or(INVALID_CHUNK)?;
let key: &[u8] = &line[..colon];
let value: &[u8] = &line[colon + 1..];
if key.is_empty()
|| !key.iter().copied().all(is_chunk_token_byte)
|| !value.iter().copied().all(is_chunk_field_value_byte)
|| key.eq_ignore_ascii_case(CONTENT_LENGTH.as_bytes())
|| key.eq_ignore_ascii_case(HTTP_HEADER_TRANSFER_ENCODING.as_bytes())
|| key.eq_ignore_ascii_case(HOST.as_bytes())
{
return Err(INVALID_CHUNK);
}
if key.len() > config.get_max_header_key_size() {
return Err(RequestError::HeaderKeyTooLong(
HttpStatus::RequestHeaderFieldsTooLarge,
));
}
let value: &[u8] = trim_chunk_ows(value);
let end: usize = value
.iter()
.rposition(|byte: &u8| !is_chunk_ows(*byte))
.map_or(0, |index: usize| index + 1);
if end > config.get_max_header_value_size() {
return Err(RequestError::HeaderValueTooLong(
HttpStatus::RequestHeaderFieldsTooLarge,
));
}
Ok(())
}
fn skip_chunk_token(input: &[u8]) -> Result<&[u8], RequestError> {
let end: usize = input
.iter()
.position(|byte: &u8| !is_chunk_token_byte(*byte))
.unwrap_or(input.len());
if end == 0 {
return Err(INVALID_CHUNK);
}
Ok(&input[end..])
}
fn trim_chunk_ows(input: &[u8]) -> &[u8] {
let start: usize = input
.iter()
.position(|byte: &u8| !is_chunk_ows(*byte))
.unwrap_or(input.len());
&input[start..]
}
fn is_chunk_ows(byte: u8) -> bool {
byte == b' ' || byte == HTTP_HEADER_TAB
}
fn is_chunk_token_byte(byte: u8) -> bool {
byte.is_ascii_alphanumeric() || HTTP_HEADER_TOKEN_PUNCTUATION.contains(&byte)
}
fn is_chunk_field_value_byte(byte: u8) -> bool {
(byte >= HTTP_HEADER_CONTROL_LIMIT || byte == HTTP_HEADER_TAB) && byte != HTTP_HEADER_DELETE
}