Skip to content

Commit

Permalink
src/protocol.rs: Add /webtransport (multiformats#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
jxs authored and oblique committed May 16, 2023
1 parent 1b363b0 commit a54e205
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const UDP: u32 = 273;
const UDT: u32 = 301;
const UNIX: u32 = 400;
const UTP: u32 = 302;
const WEBTRANSPORT: u32 = 465;
const WS: u32 = 477;
const WS_WITH_PATH: u32 = 4770; // Note: not standard
const WSS: u32 = 478;
Expand Down Expand Up @@ -102,6 +103,7 @@ pub enum Protocol<'a> {
Udt,
Unix(Cow<'a, str>),
Utp,
WebTransport,
Ws(Cow<'a, str>),
Wss(Cow<'a, str>),
}
Expand Down Expand Up @@ -185,6 +187,7 @@ impl<'a> Protocol<'a> {
.map(|(a, p)| Protocol::Onion3((a, p).into())),
"quic" => Ok(Protocol::Quic),
"quic-v1" => Ok(Protocol::QuicV1),
"webtransport" => Ok(Protocol::WebTransport),
"ws" => Ok(Protocol::Ws(Cow::Borrowed("/"))),
"wss" => Ok(Protocol::Wss(Cow::Borrowed("/"))),
"x-parity-ws" => {
Expand Down Expand Up @@ -349,6 +352,7 @@ impl<'a> Protocol<'a> {
Ok((Protocol::Unix(Cow::Borrowed(str::from_utf8(data)?)), rest))
}
UTP => Ok((Protocol::Utp, input)),
WEBTRANSPORT => Ok((Protocol::WebTransport, input)),
WS => Ok((Protocol::Ws(Cow::Borrowed("/")), input)),
WS_WITH_PATH => {
let (n, input) = decode::usize(input)?;
Expand Down Expand Up @@ -450,6 +454,7 @@ impl<'a> Protocol<'a> {
Protocol::Udt => w.write_all(encode::u32(UDT, &mut buf))?,
Protocol::Http => w.write_all(encode::u32(HTTP, &mut buf))?,
Protocol::Https => w.write_all(encode::u32(HTTPS, &mut buf))?,
Protocol::WebTransport => w.write_all(encode::u32(WEBTRANSPORT, &mut buf))?,
Protocol::Ws(ref s) if s == "/" => w.write_all(encode::u32(WS, &mut buf))?,
Protocol::Ws(s) => {
w.write_all(encode::u32(WS_WITH_PATH, &mut buf))?;
Expand Down Expand Up @@ -516,6 +521,7 @@ impl<'a> Protocol<'a> {
Udt => Udt,
Unix(cow) => Unix(Cow::Owned(cow.into_owned())),
Utp => Utp,
WebTransport => WebTransport,
Ws(cow) => Ws(Cow::Owned(cow.into_owned())),
Wss(cow) => Wss(Cow::Owned(cow.into_owned())),
}
Expand Down Expand Up @@ -553,6 +559,7 @@ impl<'a> Protocol<'a> {
Udt => "udt",
Unix(_) => "unix",
Utp => "utp",
WebTransport => "webtransport",
Ws(ref s) if s == "/" => "ws",
Ws(_) => "x-parity-ws",
Wss(ref s) if s == "/" => "wss",
Expand Down
22 changes: 21 additions & 1 deletion tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct Proto(Protocol<'static>);
impl Arbitrary for Proto {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
use Protocol::*;
match u8::arbitrary(g) % 27 {
match u8::arbitrary(g) % 28 {
0 => Proto(Dccp(Arbitrary::arbitrary(g))),
1 => Proto(Dns(Cow::Owned(SubString::arbitrary(g).0))),
2 => Proto(Dns4(Cow::Owned(SubString::arbitrary(g).0))),
Expand Down Expand Up @@ -133,6 +133,7 @@ impl Arbitrary for Proto {
}
25 => Proto(Tls),
26 => Proto(QuicV1),
27 => Proto(WebTransport),
_ => panic!("outside range"),
}
}
Expand Down Expand Up @@ -359,6 +360,25 @@ fn construct_success() {
Certhash(Multihash::from_bytes(&decoded).unwrap()),
],
);

ma_valid(
"/ip4/127.0.0.1/udp/1234/quic/webtransport",
"047F000001910204D2CC03D103",
vec![Ip4(local), Udp(1234), Quic, WebTransport],
);

let (_base, decoded) =
multibase::decode("uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g").unwrap();
ma_valid(
"/ip4/127.0.0.1/udp/1234/webtransport/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g",
"047F000001910204D2D103D203221220C3AB8FF13720E8AD9047DD39466B3C8974E592C2FA383D4A3960714CAEF0C4F2",
vec![
Ip4(local),
Udp(1234),
WebTransport,
Certhash(Multihash::from_bytes(&decoded).unwrap()),
],
);
}

#[test]
Expand Down

0 comments on commit a54e205

Please sign in to comment.