MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / collect_chunked_body

Function collect_chunked_body

crates/openshell-supervisor-network/src/l7/rest.rs:996–1098  ·  view source on GitHub ↗
(
    client: &mut C,
    already_read: &[u8],
    generation_guard: Option<&PolicyGenerationGuard>,
)

Source from the content-addressed store, hash-verified

994}
995
996async fn collect_chunked_body<C: AsyncRead + Unpin>(
997 client: &mut C,
998 already_read: &[u8],
999 generation_guard: Option<&PolicyGenerationGuard>,
1000) -> Result<Vec<u8>> {
1001 let mut read_buf = [0u8; RELAY_BUF_SIZE];
1002 let mut parse_buf = Vec::from(already_read);
1003 let mut pos = 0usize;
1004
1005 loop {
1006 if parse_buf.len() > MAX_REWRITE_BODY_BYTES {
1007 return Err(miette!(
1008 "request body credential rewrite buffers at most {MAX_REWRITE_BODY_BYTES} bytes"
1009 ));
1010 }
1011
1012 let size_line_end = loop {
1013 if let Some(end) = find_crlf(&parse_buf, pos) {
1014 break end;
1015 }
1016 let n = client.read(&mut read_buf).await.into_diagnostic()?;
1017 if n == 0 {
1018 return Err(miette!("Chunked body ended before chunk-size line"));
1019 }
1020 if let Some(guard) = generation_guard {
1021 guard.ensure_current()?;
1022 }
1023 parse_buf.extend_from_slice(&read_buf[..n]);
1024 if parse_buf.len() > MAX_REWRITE_BODY_BYTES {
1025 return Err(miette!(
1026 "request body credential rewrite buffers at most {MAX_REWRITE_BODY_BYTES} bytes"
1027 ));
1028 }
1029 };
1030
1031 let size_line = std::str::from_utf8(&parse_buf[pos..size_line_end])
1032 .into_diagnostic()
1033 .map_err(|_| miette!("Invalid UTF-8 in chunk-size line"))?;
1034 let size_token = size_line
1035 .split(';')
1036 .next()
1037 .map(str::trim)
1038 .unwrap_or_default();
1039 let chunk_size = usize::from_str_radix(size_token, 16)
1040 .into_diagnostic()
1041 .map_err(|_| miette!("Invalid chunk size token: {size_token:?}"))?;
1042 pos = size_line_end + 2;
1043
1044 if chunk_size == 0 {
1045 loop {
1046 let trailer_end = loop {
1047 if let Some(end) = find_crlf(&parse_buf, pos) {
1048 break end;
1049 }
1050 let n = client.read(&mut read_buf).await.into_diagnostic()?;
1051 if n == 0 {
1052 return Err(miette!("Chunked body ended before trailer terminator"));
1053 }

Callers 1

Calls 6

lenMethod · 0.80
ensure_currentMethod · 0.80
find_crlfFunction · 0.70
readMethod · 0.45
nextMethod · 0.45
is_emptyMethod · 0.45

Tested by

no test coverage detected