Skip to content

Commit

Permalink
src/protocol.rs: Add /webtransport (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
jxs authored Dec 16, 2022
1 parent 3d247d7 commit 2a754e2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.18.0 [unreleased]

- Add `WebTransport` instance for `Multiaddr`. See [PR 70].

[PR 70]: https://github.com/multiformats/rust-multiaddr/pull/70

# 0.17.0

- Update to multihash `v0.17`. See [PR 63].
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ keywords = ["multiaddr", "ipfs"]
license = "MIT"
name = "multiaddr"
readme = "README.md"
version = "0.17.0"
version = "0.18.0"

[features]
default = ["url"]
Expand Down
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 @@ -345,6 +348,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 @@ -446,6 +450,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 @@ -512,6 +517,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 @@ -549,6 +555,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 2a754e2

Please sign in to comment.