Skip to content

Commit

Permalink
lint: cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
canewsin committed Jan 22, 2024
1 parent 17cf9bc commit 8ab23fe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
18 changes: 15 additions & 3 deletions src/plugins/websocket/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,21 @@ impl Handler<RegisterWSClient> for WebsocketController {
#[serde(untagged)]
#[serde(rename_all = "camelCase")]
pub enum ServerEvent {
Event { cmd: String, id: usize, params: EventType },
Notification { cmd: String, id: usize, params: Value },
Confirm { cmd: String, id: usize, params: Value },
Event {
cmd: String,
id: usize,
params: EventType,
},
Notification {
cmd: String,
id: usize,
params: Value,
},
Confirm {
cmd: String,
id: usize,
params: Value,
},
}

#[allow(clippy::enum_variant_names)]
Expand Down
36 changes: 22 additions & 14 deletions src/plugins/websocket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ pub async fn serve_websocket(
trace!("Serving websocket");
#[cfg(not(debug_assertions))]
{
let origin = req
.headers()
.get(header_name!("origin"))
.and_then(|h| h.to_str().ok())
.unwrap_or("");
let host = req
.headers()
.get(header_name!("host"))
.and_then(|h| h.to_str().ok())
.unwrap_or("");
let origin = req
.headers()
.get(header_name!("origin"))
.and_then(|h| h.to_str().ok())
.unwrap_or("");
let host = req
.headers()
.get(header_name!("host"))
.and_then(|h| h.to_str().ok())
.unwrap_or("");
let origin_host = origin.split("://").collect::<Vec<&str>>()[1];
if origin_host != host {
//TODO!: and origin_host not in allowed_ws_origins
Expand Down Expand Up @@ -157,7 +157,7 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for ZeruWebsocket {
return;
}
};
if let Err(err) = self.handle_response(ctx, &cmd_res) {
if let Err(err) = self.handle_response(ctx, &cmd_res) {
error!("Error handling response: {:?}", err);
}
return;
Expand Down Expand Up @@ -367,12 +367,19 @@ impl ZeruWebsocket {
Ok(res)
}

fn handle_response(&mut self, ctx: &mut ws::WebsocketContext<ZeruWebsocket>, response: &CommandResponse) -> Result<(), Error> {
fn handle_response(
&mut self,
ctx: &mut ws::WebsocketContext<ZeruWebsocket>,
response: &CommandResponse,
) -> Result<(), Error> {
trace!("Handling response: {:?}", response);
let id = response.to as usize;
let callback = self.waiting_callbacks.remove(&id);
if let Some(callback) = callback {
let data = self.callback_data.remove(&id).unwrap_or(response.result.clone());
let data = self
.callback_data
.remove(&id)
.unwrap_or(response.result.clone());
let command = Command {
cmd: CommandType::Response,
params: data,
Expand All @@ -396,7 +403,8 @@ impl ZeruWebsocket {
) -> Result<(), Error> {
trace!(
"Handling command: {:?} with params: {:?}",
command.cmd, command.params
command.cmd,
command.params
);
let response = if let CommandType::UiServer(cmd) = &command.cmd {
match cmd {
Expand Down

0 comments on commit 8ab23fe

Please sign in to comment.