diff --git a/clients/native/examples/go-examples/websocket/filesend.go b/clients/native/examples/go-examples/websocket/filesend.go index 5fe4859c8e..e143ef7546 100644 --- a/clients/native/examples/go-examples/websocket/filesend.go +++ b/clients/native/examples/go-examples/websocket/filesend.go @@ -56,14 +56,6 @@ func main() { if err = conn.WriteMessage(websocket.BinaryMessage, payload); err != nil { panic(err) } - sendConfirmationJSON := make(map[string]interface{}) - err = conn.ReadJSON(&sendConfirmationJSON) - if err != nil { - panic(err) - } - if sendConfirmationJSON["type"].(string) != "send" { - panic("invalid send confirmation") - } fmt.Printf("waiting to receive a message from the mix network...\n") _, receivedMessage, err := conn.ReadMessage() diff --git a/clients/native/examples/go-examples/websocket/textsend.go b/clients/native/examples/go-examples/websocket/textsend.go index 2f46490016..0396891d75 100644 --- a/clients/native/examples/go-examples/websocket/textsend.go +++ b/clients/native/examples/go-examples/websocket/textsend.go @@ -49,15 +49,6 @@ func main() { panic(err) } - sendConfirmationJSON := make(map[string]interface{}) - err = conn.ReadJSON(&sendConfirmationJSON) - if err != nil { - panic(err) - } - if sendConfirmationJSON["type"].(string) != "send" { - panic("invalid send confirmation") - } - fmt.Printf("waiting to receive a message from the mix network...\n") _, receivedMessage, err := conn.ReadMessage() if err != nil { diff --git a/clients/native/examples/python-examples/websocket/filesend.py b/clients/native/examples/python-examples/websocket/filesend.py index 7eba9a52a4..561be863c8 100644 --- a/clients/native/examples/python-examples/websocket/filesend.py +++ b/clients/native/examples/python-examples/websocket/filesend.py @@ -26,8 +26,6 @@ async def send_file(): print("sending content of 'dummy_file' over the mix network...") await websocket.send(bin_payload) - msg_send_confirmation = json.loads(await websocket.recv()) - assert msg_send_confirmation["type"], "send" print("waiting to receive the 'dummy_file' from the mix network...") received_data = await websocket.recv() diff --git a/clients/native/examples/python-examples/websocket/textsend.py b/clients/native/examples/python-examples/websocket/textsend.py index b92a1e1838..ea9cf770a5 100644 --- a/clients/native/examples/python-examples/websocket/textsend.py +++ b/clients/native/examples/python-examples/websocket/textsend.py @@ -24,8 +24,6 @@ async def send_text(): print("sending '{}' over the mix network...".format(message)) await websocket.send(text_send) - msg_send_confirmation = json.loads(await websocket.recv()) - assert msg_send_confirmation["type"], "send" print("waiting to receive a message from the mix network...") received_message = await websocket.recv() diff --git a/clients/native/src/websocket/handler.rs b/clients/native/src/websocket/handler.rs index 9696c52fdd..346d85c824 100644 --- a/clients/native/src/websocket/handler.rs +++ b/clients/native/src/websocket/handler.rs @@ -91,14 +91,18 @@ impl Handler { } } - fn handle_text_send(&mut self, msg: String, full_recipient_address: String) -> ServerResponse { + fn handle_text_send( + &mut self, + msg: String, + full_recipient_address: String, + ) -> Option { let message_bytes = msg.into_bytes(); let recipient = match Recipient::try_from_string(full_recipient_address) { Ok(address) => address, Err(e) => { trace!("failed to parse received Recipient: {:?}", e); - return ServerResponse::new_error("malformed recipient address"); + return Some(ServerResponse::new_error("malformed recipient address")); } }; @@ -107,8 +111,7 @@ impl Handler { self.msg_input.unbounded_send(input_msg).unwrap(); self.received_response_type = ReceivedResponseType::Text; - - ServerResponse::Send + None } async fn handle_text_get_clients(&mut self) -> ServerResponse { @@ -129,52 +132,58 @@ impl Handler { } } - async fn handle_text_message(&mut self, msg: String) -> Message { + async fn handle_text_message(&mut self, msg: String) -> Option { debug!("Handling text message request"); trace!("Content: {:?}", msg.clone()); match ClientRequest::try_from(msg) { - Err(e) => ServerResponse::Error { - message: format!("received invalid request. err: {:?}", e), - } - .into(), - Ok(req) => match req { - ClientRequest::Send { message, recipient } => { - self.handle_text_send(message, recipient) + Err(e) => Some( + ServerResponse::Error { + message: format!("received invalid request. err: {:?}", e), } - ClientRequest::GetClients => self.handle_text_get_clients().await, - ClientRequest::SelfAddress => self.handle_text_self_address(), - } - .into(), + .into(), + ), + Ok(req) => match req { + ClientRequest::Send { message, recipient } => self + .handle_text_send(message, recipient) + .map(|resp| resp.into()), + ClientRequest::GetClients => Some(self.handle_text_get_clients().await.into()), + ClientRequest::SelfAddress => Some(self.handle_text_self_address().into()), + }, } } - async fn handle_binary_send(&mut self, recipient: Recipient, data: Vec) -> ServerResponse { + async fn handle_binary_send( + &mut self, + recipient: Recipient, + data: Vec, + ) -> Option { // the ack control is now responsible for chunking, etc. let input_msg = InputMessage::new(recipient, data); self.msg_input.unbounded_send(input_msg).unwrap(); self.received_response_type = ReceivedResponseType::Binary; - ServerResponse::Send + + None } // if it's binary we assume it's a sphinx packet formatted the same way as we'd have sent // it to the gateway - async fn handle_binary_message(&mut self, msg: Vec) -> Message { + async fn handle_binary_message(&mut self, msg: Vec) -> Option { debug!("Handling binary message request"); self.received_response_type = ReceivedResponseType::Binary; // make sure it is correctly formatted let binary_request = BinaryClientRequest::try_from_bytes(&msg); if binary_request.is_none() { - return ServerResponse::new_error("invalid binary request").into(); + return Some(ServerResponse::new_error("invalid binary request").into()); } match binary_request.unwrap() { BinaryClientRequest::Send { recipient, data } => { self.handle_binary_send(recipient, data).await } } - .into() + .map(|resp| resp.into()) } async fn handle_request(&mut self, raw_request: Message) -> Option { @@ -182,10 +191,8 @@ impl Handler { // them and let's test that claim. If that's not the case, just copy code from // old version of this file. match raw_request { - Message::Text(text_message) => Some(self.handle_text_message(text_message).await), - Message::Binary(binary_message) => { - Some(self.handle_binary_message(binary_message).await) - } + Message::Text(text_message) => self.handle_text_message(text_message).await, + Message::Binary(binary_message) => self.handle_binary_message(binary_message).await, _ => None, } }