Skip to content

Commit

Permalink
feat: allow message handlers to return None
Browse files Browse the repository at this point in the history
  • Loading branch information
omjadas committed Sep 1, 2021
1 parent 1294ed7 commit a7f33e4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
4 changes: 2 additions & 2 deletions examples/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ async fn main() {
shutdown_signal: shutdown_signal(),
request_handler,
response_handler,
incoming_message_handler: |msg| msg,
outgoing_message_handler: |msg| msg,
incoming_message_handler: |msg| Some(msg),
outgoing_message_handler: |msg| Some(msg),
upstream_proxy: None,
ca,
};
Expand Down
4 changes: 2 additions & 2 deletions examples/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ async fn main() {

let request_handler = |req| RequestOrResponse::Request(req);
let response_handler = |res| res;
let incoming_message_handler = |msg| msg;
let outgoing_message_handler = |msg| msg;
let incoming_message_handler = |msg| Some(msg);
let outgoing_message_handler = |msg| Some(msg);

let mut private_key_bytes: &[u8] = include_bytes!("ca/hudsucker.key");
let mut ca_cert_bytes: &[u8] = include_bytes!("ca/hudsucker.pem");
Expand Down
8 changes: 4 additions & 4 deletions examples/upstream_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ async fn main() {
shutdown_signal: shutdown_signal(),
request_handler,
response_handler,
incoming_message_handler: |msg| msg,
outgoing_message_handler: |msg| msg,
incoming_message_handler: |msg| Some(msg),
outgoing_message_handler: |msg| Some(msg),
upstream_proxy: None,
ca: ca.clone(),
};
Expand All @@ -54,8 +54,8 @@ async fn main() {
listen_addr: SocketAddr::from(([127, 0, 0, 1], 3000)),
request_handler: |req| RequestOrResponse::Request(req),
response_handler: |res| res,
incoming_message_handler: |msg| msg,
outgoing_message_handler: |msg| msg,
incoming_message_handler: |msg| Some(msg),
outgoing_message_handler: |msg| Some(msg),
shutdown_signal: shutdown_signal(),
upstream_proxy: Some(UpstreamProxy::new(
Intercept::All,
Expand Down
13 changes: 10 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,16 @@ impl<T> ResponseHandler for T where

/// Handler for websocket messages.
///
/// The handler will be called for each websocket message. It can return a modified message.
pub trait MessageHandler: FnMut(Message) -> Message + Send + Sync + Clone + 'static {}
impl<T> MessageHandler for T where T: FnMut(Message) -> Message + Send + Sync + Clone + 'static {}
/// The handler will be called for each websocket message. It can return an optional modified
/// message. If None is returned the message will not be forwarded.
pub trait MessageHandler:
FnMut(Message) -> Option<Message> + Send + Sync + Clone + 'static
{
}
impl<T> MessageHandler for T where
T: FnMut(Message) -> Option<Message> + Send + Sync + Clone + 'static
{
}

/// Configuration for the proxy server.
///
Expand Down
12 changes: 10 additions & 2 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ where
while let Some(message) = server_stream.next().await {
match message {
Ok(message) => {
let message = incoming_message_handler(message);
let message = match incoming_message_handler(message) {
Some(message) => message,
None => continue,
};

match client_sink.send(message).await {
Err(tungstenite::Error::ConnectionClosed) => (),
Err(e) => error!("websocket send error: {}", e),
Expand All @@ -177,7 +181,11 @@ where
while let Some(message) = client_stream.next().await {
match message {
Ok(message) => {
let message = outgoing_message_handler(message);
let message = match outgoing_message_handler(message) {
Some(message) => message,
None => continue,
};

match server_sink.send(message).await {
Err(tungstenite::Error::ConnectionClosed) => (),
Err(e) => error!("websocket send error: {}", e),
Expand Down

0 comments on commit a7f33e4

Please sign in to comment.