fune/third_party/rust/ws/examples/router.rs
Bastien Orivel 8a3f3e785c Bug 1550903 - Part 2: Revendor dependencies. r=emilio,kats,froydnj
It was unhappy about the new LICENSE (fuchsia-cprng) but it's the same
as the other fuschia crates. Since I don't think this is used at build
time but has the same license as the other fuschia crates, I put it in
the RUNTIME_LICENSE_PACKAGE_WHITELIST list. I also removed sha1 from
that list as it's not used anymore

Differential Revision: https://phabricator.services.mozilla.com/D30746

--HG--
rename : third_party/rust/httparse/Cargo.toml => third_party/rust/autocfg/Cargo.toml
rename : third_party/rust/rand/LICENSE-APACHE => third_party/rust/autocfg/LICENSE-APACHE
rename : third_party/rust/rand/LICENSE-MIT => third_party/rust/autocfg/LICENSE-MIT
rename : third_party/rust/httparse/Cargo.toml => third_party/rust/cloudabi/Cargo.toml
rename : third_party/rust/httparse/Cargo.toml => third_party/rust/fuchsia-cprng/Cargo.toml
rename : third_party/rust/sha1/LICENSE => third_party/rust/fuchsia-cprng/LICENSE
rename : third_party/rust/rand/LICENSE-APACHE => third_party/rust/mio-extras/LICENSE-APACHE
rename : third_party/rust/rand/LICENSE-MIT => third_party/rust/mio-extras/LICENSE-MIT
rename : third_party/rust/rand/.cargo-checksum.json => third_party/rust/rand-0.4.3/.cargo-checksum.json
rename : third_party/rust/rand/CHANGELOG.md => third_party/rust/rand-0.4.3/CHANGELOG.md
rename : third_party/rust/rand/Cargo.toml => third_party/rust/rand-0.4.3/Cargo.toml
rename : third_party/rust/rand/LICENSE-APACHE => third_party/rust/rand-0.4.3/LICENSE-APACHE
rename : third_party/rust/rand/LICENSE-MIT => third_party/rust/rand-0.4.3/LICENSE-MIT
rename : third_party/rust/rand/README.md => third_party/rust/rand-0.4.3/README.md
rename : third_party/rust/rand/appveyor.yml => third_party/rust/rand-0.4.3/appveyor.yml
rename : third_party/rust/rand/benches/bench.rs => third_party/rust/rand-0.4.3/benches/bench.rs
rename : third_party/rust/rand/benches/distributions/exponential.rs => third_party/rust/rand-0.4.3/benches/distributions/exponential.rs
rename : third_party/rust/rand/benches/distributions/gamma.rs => third_party/rust/rand-0.4.3/benches/distributions/gamma.rs
rename : third_party/rust/rand/benches/distributions/mod.rs => third_party/rust/rand-0.4.3/benches/distributions/mod.rs
rename : third_party/rust/rand/benches/distributions/normal.rs => third_party/rust/rand-0.4.3/benches/distributions/normal.rs
rename : third_party/rust/rand/benches/generators.rs => third_party/rust/rand-0.4.3/benches/generators.rs
rename : third_party/rust/rand/benches/misc.rs => third_party/rust/rand-0.4.3/benches/misc.rs
rename : third_party/rust/rand/src/distributions/exponential.rs => third_party/rust/rand-0.4.3/src/distributions/exponential.rs
rename : third_party/rust/rand/src/distributions/gamma.rs => third_party/rust/rand-0.4.3/src/distributions/gamma.rs
rename : third_party/rust/rand/src/distributions/mod.rs => third_party/rust/rand-0.4.3/src/distributions/mod.rs
rename : third_party/rust/rand/src/distributions/normal.rs => third_party/rust/rand-0.4.3/src/distributions/normal.rs
rename : third_party/rust/rand/src/distributions/range.rs => third_party/rust/rand-0.4.3/src/distributions/range.rs
rename : third_party/rust/rand/src/distributions/ziggurat_tables.rs => third_party/rust/rand-0.4.3/src/distributions/ziggurat_tables.rs
rename : third_party/rust/rand/src/jitter.rs => third_party/rust/rand-0.4.3/src/jitter.rs
rename : third_party/rust/rand/src/lib.rs => third_party/rust/rand-0.4.3/src/lib.rs
rename : third_party/rust/rand/src/os.rs => third_party/rust/rand-0.4.3/src/os.rs
rename : third_party/rust/rand/src/prng/chacha.rs => third_party/rust/rand-0.4.3/src/prng/chacha.rs
rename : third_party/rust/rand/src/prng/isaac.rs => third_party/rust/rand-0.4.3/src/prng/isaac.rs
rename : third_party/rust/rand/src/prng/isaac64.rs => third_party/rust/rand-0.4.3/src/prng/isaac64.rs
rename : third_party/rust/rand/src/prng/mod.rs => third_party/rust/rand-0.4.3/src/prng/mod.rs
rename : third_party/rust/rand/src/prng/xorshift.rs => third_party/rust/rand-0.4.3/src/prng/xorshift.rs
rename : third_party/rust/rand/src/rand_impls.rs => third_party/rust/rand-0.4.3/src/rand_impls.rs
rename : third_party/rust/rand/src/read.rs => third_party/rust/rand-0.4.3/src/read.rs
rename : third_party/rust/rand/src/reseeding.rs => third_party/rust/rand-0.4.3/src/reseeding.rs
rename : third_party/rust/rand/src/seq.rs => third_party/rust/rand-0.4.3/src/seq.rs
rename : third_party/rust/rand/utils/ziggurat_tables.py => third_party/rust/rand-0.4.3/utils/ziggurat_tables.py
rename : third_party/rust/rand/LICENSE-APACHE => third_party/rust/rand_chacha/LICENSE-APACHE
rename : third_party/rust/rand/LICENSE-MIT => third_party/rust/rand_chacha/LICENSE-MIT
rename : third_party/rust/rand/LICENSE-APACHE => third_party/rust/rand_core-0.3.1/LICENSE-APACHE
rename : third_party/rust/rand/LICENSE-MIT => third_party/rust/rand_core-0.3.1/LICENSE-MIT
rename : third_party/rust/rand/LICENSE-APACHE => third_party/rust/rand_core/LICENSE-APACHE
rename : third_party/rust/rand/LICENSE-MIT => third_party/rust/rand_core/LICENSE-MIT
rename : third_party/rust/httparse/Cargo.toml => third_party/rust/rand_hc/Cargo.toml
rename : third_party/rust/rand/LICENSE-APACHE => third_party/rust/rand_hc/LICENSE-APACHE
rename : third_party/rust/rand/LICENSE-MIT => third_party/rust/rand_hc/LICENSE-MIT
rename : third_party/rust/rand/LICENSE-APACHE => third_party/rust/rand_isaac/LICENSE-APACHE
rename : third_party/rust/rand/LICENSE-MIT => third_party/rust/rand_isaac/LICENSE-MIT
rename : third_party/rust/rand/LICENSE-APACHE => third_party/rust/rand_jitter/LICENSE-APACHE
rename : third_party/rust/rand/LICENSE-MIT => third_party/rust/rand_jitter/LICENSE-MIT
rename : third_party/rust/rand/src/jitter.rs => third_party/rust/rand_jitter/src/lib.rs
rename : third_party/rust/rand/LICENSE-APACHE => third_party/rust/rand_os/LICENSE-APACHE
rename : third_party/rust/rand/LICENSE-MIT => third_party/rust/rand_os/LICENSE-MIT
rename : third_party/rust/rand/LICENSE-APACHE => third_party/rust/rand_pcg/LICENSE-APACHE
rename : third_party/rust/rand/LICENSE-MIT => third_party/rust/rand_pcg/LICENSE-MIT
rename : third_party/rust/rand/LICENSE-APACHE => third_party/rust/rand_xorshift/LICENSE-APACHE
rename : third_party/rust/rand/LICENSE-MIT => third_party/rust/rand_xorshift/LICENSE-MIT
rename : third_party/rust/httparse/Cargo.toml => third_party/rust/rdrand/Cargo.toml
rename : third_party/rust/rand/LICENSE-APACHE => third_party/rust/sha-1/LICENSE-APACHE
rename : third_party/rust/rand/LICENSE-MIT => third_party/rust/sha-1/LICENSE-MIT
extra : moz-landing-system : lando
2019-05-18 13:39:31 +00:00

141 lines
4.2 KiB
Rust

extern crate env_logger;
/// WebSocket server using trait objects to route
/// to an infinitely extensible number of handlers
extern crate ws;
// A WebSocket handler that routes connections to different boxed handlers by resource
struct Router {
sender: ws::Sender,
inner: Box<ws::Handler>,
}
impl ws::Handler for Router {
fn on_request(&mut self, req: &ws::Request) -> ws::Result<(ws::Response)> {
// Clone the sender so that we can move it into the child handler
let out = self.sender.clone();
match req.resource() {
"/echo" => self.inner = Box::new(Echo { ws: out }),
// Route to a data handler
"/data/one" => {
self.inner = Box::new(Data {
ws: out,
data: vec!["one", "two", "three", "four", "five"],
})
}
// Route to another data handler
"/data/two" => {
self.inner = Box::new(Data {
ws: out,
data: vec!["いち", "", "さん", "", ""],
})
}
// Use a closure as the child handler
"/closure" => {
self.inner = Box::new(move |msg: ws::Message| {
println!("Got a message on a closure handler: {}", msg);
out.close_with_reason(ws::CloseCode::Error, "Not Implemented.")
})
}
// Use the default child handler, NotFound
_ => (),
}
// Delegate to the child handler
self.inner.on_request(req)
}
// Pass through any other methods that should be delegated to the child.
//
// You could probably use a macro for this if you have many different
// routers or were building some sort of routing framework.
fn on_shutdown(&mut self) {
self.inner.on_shutdown()
}
fn on_open(&mut self, shake: ws::Handshake) -> ws::Result<()> {
self.inner.on_open(shake)
}
fn on_message(&mut self, msg: ws::Message) -> ws::Result<()> {
self.inner.on_message(msg)
}
fn on_close(&mut self, code: ws::CloseCode, reason: &str) {
self.inner.on_close(code, reason)
}
fn on_error(&mut self, err: ws::Error) {
self.inner.on_error(err);
}
}
// This handler returns a 404 response to all handshake requests
struct NotFound;
impl ws::Handler for NotFound {
fn on_request(&mut self, req: &ws::Request) -> ws::Result<(ws::Response)> {
// This handler responds to all requests with a 404
let mut res = ws::Response::from_request(req)?;
res.set_status(404);
res.set_reason("Not Found");
Ok(res)
}
}
// This handler simply echoes all messages back to the client
struct Echo {
ws: ws::Sender,
}
impl ws::Handler for Echo {
fn on_message(&mut self, msg: ws::Message) -> ws::Result<()> {
println!("Echo handler received a message: {}", msg);
self.ws.send(msg)
}
}
// This handler sends some data to the client and then terminates the connection on the first
// message received, presumably confirming receipt of the data
struct Data {
ws: ws::Sender,
data: Vec<&'static str>,
}
impl ws::Handler for Data {
fn on_open(&mut self, _: ws::Handshake) -> ws::Result<()> {
for msg in &self.data {
self.ws.send(*msg)?
}
Ok(())
}
fn on_message(&mut self, msg: ws::Message) -> ws::Result<()> {
println!("Data handler received a message: {}", msg);
println!("Data handler going down.");
self.ws.close(ws::CloseCode::Normal)
}
}
fn main() {
env_logger::init();
// Listen on an address and call the closure for each connection
if let Err(error) = ws::listen("127.0.0.1:3012", |out| {
// Use our router as the handler to route the new connection
Router {
sender: out,
// Default to returning a 404 when the route doesn't match.
// You could default to any handler here.
inner: Box::new(NotFound),
}
}) {
// Inform the user of failure
println!("Failed to create WebSocket due to {:?}", error);
}
}