Debian Codegraph @

examples/autobahn_client.rs

raw symbols SCIP
1 // Copyright (c) 2019 Parity Technologies (UK) Ltd.
2 //
3 // Licensed under the Apache License, Version 2.0
4 // <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5 // license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. All files in the project carrying such notice may not be copied,
7 // modified, or distributed except according to those terms.
8
9 // Example to be used with the autobahn test suite, a fully automated test
10 // suite to verify client and server implementations of websocket
11 // implementation.
12 //
13 // Once started, the tests can be executed with: wstest -m fuzzingserver
14 //
15 // See https://github.com/crossbario/autobahn-testsuite for details.
16
17 use futuresrust-analyzer cargo futures 0.3.34 crate/::iorust-analyzer cargo futures-util 0.3.34 io/::{BufReaderrust-analyzer cargo futures-util 0.3.34 io/buf_reader/BufReader#, BufWriterrust-analyzer cargo futures-util 0.3.34 io/buf_writer/BufWriter#};
18 use sokettorust-analyzer cargo soketto 0.8.1 crate/::{connectionrust-analyzer cargo soketto 0.8.1 connection/, handshakerust-analyzer cargo soketto 0.8.1 handshake/, BoxedErrorrust-analyzer cargo soketto 0.8.1 BoxedError#};
19 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#;
20 use tokiorust-analyzer cargo tokio 1.53.1 crate/::netrust-analyzer cargo tokio 1.53.1 net/::TcpStreamrust-analyzer cargo tokio 1.53.1 net/tcp/stream/TcpStream#;
21 use tokio_utilrust-analyzer cargo tokio-util 0.7.19 crate/::compatrust-analyzer cargo tokio-util 0.7.19 compat/::{Compatrust-analyzer cargo tokio-util 0.7.19 compat/Compat#, TokioAsyncReadCompatExtrust-analyzer cargo tokio-util 0.7.19 compat/TokioAsyncReadCompatExt#};
22
23 const SOKETTO_VERSIONrust-analyzer cargo soketto 0.8.1 SOKETTO_VERSION.: &str = envrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/env!!("CARGO_PKG_VERSION");
24
25 #[tokiorust-analyzer cargo tokio 1.53.1 crate/::mainrust-analyzer cargo tokio-macros 2.7.2 main:]
26 async fn mainrust-analyzer cargo soketto 0.8.1 main().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), BoxedErrorrust-analyzer cargo soketto 0.8.1 BoxedError#> {
27 let n = num_of_casesrust-analyzer cargo soketto 0.8.1 num_of_cases().().rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
28 for i in 1..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#n {
29 if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) = run_caserust-analyzer cargo soketto 0.8.1 run_case().(i).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll(). {
30 logrust-analyzer cargo log 0.4.34 crate/::errorrust-analyzer cargo log 0.4.34 macros/error!!("case {}: {:?}", i, e)
31 }
32 }
33 update_reportrust-analyzer cargo soketto 0.8.1 update_report().().rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
34 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
35 }
36
37 async fn num_of_casesrust-analyzer cargo soketto 0.8.1 num_of_cases().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, BoxedErrorrust-analyzer cargo soketto 0.8.1 BoxedError#> {
38 let socket = TcpStreamrust-analyzer cargo tokio 1.53.1 net/tcp/stream/TcpStream#::connectrust-analyzer cargo tokio 1.53.1 net/tcp/stream/impl#[TcpStream]connect().("127.0.0.1:9001").rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
39 let mut client = new_clientrust-analyzer cargo soketto 0.8.1 new_client().(socket, "/getCaseCount");
40 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(client.handshakerust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]handshake().().rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., handshakerust-analyzer cargo soketto 0.8.1 handshake/::ServerResponserust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#::Acceptedrust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#Accepted# { .. }));
41 let (_, mut receiver) = client.into_builderrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]into_builder().().finishrust-analyzer cargo soketto 0.8.1 connection/impl#[`Builder<T>`]finish().();
42 let mut data = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
43 let kind = receiver.receive_datarust-analyzer cargo soketto 0.8.1 connection/impl#[`Receiver<T>`]receive_data().(&mut data).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
44 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(kind.is_textrust-analyzer cargo soketto 0.8.1 data/impl#[Data]is_text().());
45 let num = usize::from_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize][FromStr]from_str().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(&data)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
46 logrust-analyzer cargo log 0.4.34 crate/::inforust-analyzer cargo log 0.4.34 macros/info!!("{} cases to run", num);
47 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(num)
48 }
49
50 async fn run_caserust-analyzer cargo soketto 0.8.1 run_case().(n: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), BoxedErrorrust-analyzer cargo soketto 0.8.1 BoxedError#> {
51 logrust-analyzer cargo log 0.4.34 crate/::inforust-analyzer cargo log 0.4.34 macros/info!!("running case {}", n);
52 let resource = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("/runCase?case={}&agent=soketto-{}", n, SOKETTO_VERSIONrust-analyzer cargo soketto 0.8.1 SOKETTO_VERSION.);
53 let socket = TcpStreamrust-analyzer cargo tokio 1.53.1 net/tcp/stream/TcpStream#::connectrust-analyzer cargo tokio 1.53.1 net/tcp/stream/impl#[TcpStream]connect().("127.0.0.1:9001").rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
54 let mut client = new_clientrust-analyzer cargo soketto 0.8.1 new_client().(socket, &resource);
55 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(client.handshakerust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]handshake().().rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., handshakerust-analyzer cargo soketto 0.8.1 handshake/::ServerResponserust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#::Acceptedrust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#Accepted# { .. }));
56 let (mut sender, mut receiver) = client.into_builderrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]into_builder().().finishrust-analyzer cargo soketto 0.8.1 connection/impl#[`Builder<T>`]finish().();
57 let mut message = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
58 loop {
59 message.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]clear().();
60 match receiver.receive_datarust-analyzer cargo soketto 0.8.1 connection/impl#[`Receiver<T>`]receive_data().(&mut message).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll(). {
61 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(sokettorust-analyzer cargo soketto 0.8.1 crate/::Datarust-analyzer cargo soketto 0.8.1 data/Data#::Binaryrust-analyzer cargo soketto 0.8.1 data/Data#Binary#(n)) => {
62 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(n, message.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().());
63 sender.send_binary_mutrust-analyzer cargo soketto 0.8.1 connection/impl#[`Sender<T>`]send_binary_mut().(&mut message).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
64 sender.flushrust-analyzer cargo soketto 0.8.1 connection/impl#[`Sender<T>`]flush().().rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
65 }
66 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(sokettorust-analyzer cargo soketto 0.8.1 crate/::Datarust-analyzer cargo soketto 0.8.1 data/Data#::Textrust-analyzer cargo soketto 0.8.1 data/Data#Text#(n)) => {
67 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(n, message.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().());
68 sender.send_textrust-analyzer cargo soketto 0.8.1 connection/impl#[`Sender<T>`]send_text().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(&message)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
69 sender.flushrust-analyzer cargo soketto 0.8.1 connection/impl#[`Sender<T>`]flush().().rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
70 }
71 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(connectionrust-analyzer cargo soketto 0.8.1 connection/::Errorrust-analyzer cargo soketto 0.8.1 connection/Error#::Closedrust-analyzer cargo soketto 0.8.1 connection/Error#Closed#) => return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()),
72 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()),
73 }
74 }
75 }
76
77 async fn update_reportrust-analyzer cargo soketto 0.8.1 update_report().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), BoxedErrorrust-analyzer cargo soketto 0.8.1 BoxedError#> {
78 logrust-analyzer cargo log 0.4.34 crate/::inforust-analyzer cargo log 0.4.34 macros/info!!("requesting report generation");
79 let resource = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("/updateReports?agent=soketto-{}", SOKETTO_VERSIONrust-analyzer cargo soketto 0.8.1 SOKETTO_VERSION.);
80 let socket = TcpStreamrust-analyzer cargo tokio 1.53.1 net/tcp/stream/TcpStream#::connectrust-analyzer cargo tokio 1.53.1 net/tcp/stream/impl#[TcpStream]connect().("127.0.0.1:9001").rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
81 let mut client = new_clientrust-analyzer cargo soketto 0.8.1 new_client().(socket, &resource);
82 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(client.handshakerust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]handshake().().rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., handshakerust-analyzer cargo soketto 0.8.1 handshake/::ServerResponserust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#::Acceptedrust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#Accepted# { .. }));
83 client.into_builderrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]into_builder().().finishrust-analyzer cargo soketto 0.8.1 connection/impl#[`Builder<T>`]finish().().0.closerust-analyzer cargo soketto 0.8.1 connection/impl#[`Sender<T>`]close().().rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
84 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
85 }
86
87 #[cfg(not(feature = "deflate"))]
88 fn new_client(socket: TcpStreamrust-analyzer cargo tokio 1.53.1 net/tcp/stream/TcpStream#, path: &str) -> handshakerust-analyzer cargo soketto 0.8.1 handshake/::Clientrust-analyzer cargo soketto 0.8.1 handshake/client/Client#<'_, BufReaderrust-analyzer cargo futures-util 0.3.34 io/buf_reader/BufReader#<BufWriterrust-analyzer cargo futures-util 0.3.34 io/buf_writer/BufWriter#<Compatrust-analyzer cargo tokio-util 0.7.19 compat/Compat#<TcpStreamrust-analyzer cargo tokio 1.53.1 net/tcp/stream/TcpStream#>>>> {
89 handshakerust-analyzer cargo soketto 0.8.1 handshake/::Clientrust-analyzer cargo soketto 0.8.1 handshake/client/Client#::new(BufReaderrust-analyzer cargo futures-util 0.3.34 io/buf_reader/BufReader#::new(BufWriterrust-analyzer cargo futures-util 0.3.34 io/buf_writer/BufWriter#::new(socket.compat())), "127.0.0.1:9001", path)
90 }
91
92 #[cfg(feature = "deflate")]
93 fn new_clientrust-analyzer cargo soketto 0.8.1 new_client().(socket: TcpStreamrust-analyzer cargo tokio 1.53.1 net/tcp/stream/TcpStream#, path: &str) -> handshakerust-analyzer cargo soketto 0.8.1 handshake/::Clientrust-analyzer cargo soketto 0.8.1 handshake/client/Client#<'_, BufReaderrust-analyzer cargo futures-util 0.3.34 io/buf_reader/BufReader#<BufWriterrust-analyzer cargo futures-util 0.3.34 io/buf_writer/BufWriter#<Compatrust-analyzer cargo tokio-util 0.7.19 compat/Compat#<TcpStreamrust-analyzer cargo tokio 1.53.1 net/tcp/stream/TcpStream#>>>> {
94 let socket = BufReaderrust-analyzer cargo futures-util 0.3.34 io/buf_reader/BufReader#::with_capacityrust-analyzer cargo futures-util 0.3.34 io/buf_reader/impl#[`BufReader<R>`]with_capacity().(8 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().1024, BufWriterrust-analyzer cargo futures-util 0.3.34 io/buf_writer/BufWriter#::with_capacityrust-analyzer cargo futures-util 0.3.34 io/buf_writer/impl#[`BufWriter<W>`]with_capacity().(64 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().1024, socket.compatrust-analyzer cargo tokio-util 0.7.19 compat/TokioAsyncReadCompatExt#compat().()));
95 let mut client = handshakerust-analyzer cargo soketto 0.8.1 handshake/::Clientrust-analyzer cargo soketto 0.8.1 handshake/client/Client#::newrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]new().(socket, "127.0.0.1:9001", path);
96 let deflate = sokettorust-analyzer cargo soketto 0.8.1 crate/::extensionrust-analyzer cargo soketto 0.8.1 extension/::deflaterust-analyzer cargo soketto 0.8.1 extension/deflate/::Deflaterust-analyzer cargo soketto 0.8.1 extension/deflate/Deflate#::newrust-analyzer cargo soketto 0.8.1 extension/deflate/impl#[Deflate]new().(sokettorust-analyzer cargo soketto 0.8.1 crate/::Moderust-analyzer cargo soketto 0.8.1 connection/Mode#::Clientrust-analyzer cargo soketto 0.8.1 connection/Mode#Client#);
97 client.add_extensionrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]add_extension().(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(deflate));
98 client
99 }