Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamed read for binary HTTP #74

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge main
  • Loading branch information
martinthomson committed Oct 29, 2024
commit 00cfe4f018b52c52f8ae5b00273bda4af12ef8d8
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -3,3 +3,4 @@
*~
*.swp
/.vscode/
/mutants.out*/
3 changes: 0 additions & 3 deletions bhttp/src/err.rs
Original file line number Diff line number Diff line change
@@ -4,9 +4,6 @@ pub enum Error {
ConnectUnsupported,
#[error("a field contained invalid Unicode: {0}")]
CharacterEncoding(#[from] std::string::FromUtf8Error),
#[error("a chunk of data of {0} bytes is too large")]
#[cfg(feature = "stream")]
ChunkTooLarge(u64),
#[error("read a response when expecting a request")]
ExpectedRequest,
#[error("read a request when expecting a response")]
68 changes: 66 additions & 2 deletions bhttp/src/rw.rs
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ use crate::{
pub(crate) fn write_uint<const N: usize>(v: impl Into<u64>, w: &mut impl io::Write) -> Res<()> {
let v = v.into().to_be_bytes();
assert!((1..=std::mem::size_of::<u64>()).contains(&N));
w.write_all(&v[8 - N..])?;
w.write_all(&v[std::mem::size_of::<u64>() - N..])?;
Ok(())
}

@@ -20,7 +20,7 @@ pub fn write_varint(v: impl Into<u64>, w: &mut impl io::Write) -> Res<()> {
() if v < (1 << 14) => write_uint::<2>(v | (1 << 14), w),
() if v < (1 << 30) => write_uint::<4>(v | (2 << 30), w),
() if v < (1 << 62) => write_uint::<8>(v | (3 << 62), w),
() => panic!("Varint value too large"),
() => panic!("varint value too large"),
}
}

@@ -92,3 +92,67 @@ where
Ok(None)
}
}

#[cfg(test)]
mod test {
use std::io::Cursor;

use super::{read_varint, write_varint};
use crate::{rw::read_vec, Error};

#[test]
fn basics() {
for i in [
0_u64,
1,
17,
63,
64,
100,
0x3fff,
0x4000,
0x1_0002,
0x3fff_ffff,
0x4000_0000,
0x3456_dead_beef,
0x3fff_ffff_ffff_ffff,
] {
let mut buf = Vec::new();
write_varint(i, &mut buf).unwrap();
let sz_bytes = (64 - i.leading_zeros() + 2 + 7) / 8; // +2 size bits, +7 to round up
assert_eq!(
buf.len(),
usize::try_from(sz_bytes.next_power_of_two()).unwrap()
);

let o = read_varint(&mut Cursor::new(buf.clone())).unwrap();
assert_eq!(Some(i), o);

for cut in 1..buf.len() {
let e = read_varint(&mut Cursor::new(buf[..cut].to_vec())).unwrap_err();
assert!(matches!(e, Error::Truncated));
}
}
}

#[test]
fn read_nothing() {
let o = read_varint(&mut Cursor::new(Vec::new())).unwrap();
assert!(o.is_none());
}

#[test]
#[should_panic(expected = "varint value too large")]
fn too_big() {
_ = write_varint(0x4000_0000_0000_0000_u64, &mut Vec::new());
}

#[test]
fn too_big_vec() {
let mut buf = Vec::new();
write_varint(10_u64, &mut buf).unwrap();
buf.resize(10, 0); // Not enough extra for the promised length.
let e = read_vec(&mut Cursor::new(buf.clone())).unwrap_err();
assert!(matches!(e, Error::Truncated));
}
}