From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 0544263E04 for ; Fri, 17 Jul 2020 14:17:14 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id F00C51BD69 for ; Fri, 17 Jul 2020 14:17:13 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id A0C4B1BD4A for ; Fri, 17 Jul 2020 14:17:11 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 645DD430DC for ; Fri, 17 Jul 2020 14:17:11 +0200 (CEST) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Fri, 17 Jul 2020 14:17:08 +0200 Message-Id: <20200717121710.2297-2-d.csapak@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200717121710.2297-1-d.csapak@proxmox.com> References: <20200717121710.2297-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.008 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods NO_DNS_FOR_FROM 0.379 Envelope sender has no MX or A DNS records RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an SPF Record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [websocket.rs] Subject: [pbs-devel] [PATCH proxmox v2 2/4] proxmox/tools/websocket: improve error handling X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 17 Jul 2020 12:17:14 -0000 use io_err_other instead of io_format_err and change the Error type of create_frame from io::Error to WebSocketError it is not good to redefine the ErrorKinds of io::Error, since the caller probably is not aware of that Signed-off-by: Dominik Csapak --- proxmox/src/tools/websocket.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/proxmox/src/tools/websocket.rs b/proxmox/src/tools/websocket.rs index c6775f0..6d30061 100644 --- a/proxmox/src/tools/websocket.rs +++ b/proxmox/src/tools/websocket.rs @@ -155,7 +155,7 @@ fn mask_bytes(mask: Option<[u8; 4]>, data: &mut Box<[u8]>) { /// ``` /// # use proxmox::tools::websocket::*; /// # use std::io; -/// # fn main() -> io::Result<()> { +/// # fn main() -> Result<(), WebSocketError> { /// let data = vec![0,1,2,3,4]; /// let frame = create_frame(None, &data, OpCode::Text)?; /// assert_eq!(frame, vec![0b10000001, 5, 0, 1, 2, 3, 4]); @@ -168,7 +168,7 @@ fn mask_bytes(mask: Option<[u8; 4]>, data: &mut Box<[u8]>) { /// ``` /// # use proxmox::tools::websocket::*; /// # use std::io; -/// # fn main() -> io::Result<()> { +/// # fn main() -> Result<(), WebSocketError> { /// let data = vec![0,1,2,3,4]; /// let frame = create_frame(Some([0u8, 1u8, 2u8, 3u8]), &data, OpCode::Text)?; /// assert_eq!(frame, vec![0b10000001, 0b10000101, 0, 1, 2, 3, 0, 0, 0, 0, 4]); @@ -181,7 +181,7 @@ fn mask_bytes(mask: Option<[u8; 4]>, data: &mut Box<[u8]>) { /// ``` /// # use proxmox::tools::websocket::*; /// # use std::io; -/// # fn main() -> io::Result<()> { +/// # fn main() -> Result<(), WebSocketError> { /// let data = vec![0,1,2,3,4]; /// let frame = create_frame(None, &data, OpCode::Ping)?; /// assert_eq!(frame, vec![0b10001001, 0b00000101, 0, 1, 2, 3, 4]); @@ -193,13 +193,13 @@ pub fn create_frame( mask: Option<[u8; 4]>, data: &[u8], frametype: OpCode, -) -> io::Result> { +) -> Result, WebSocketError> { let first_byte = 0b10000000 | (frametype as u8); let len = data.len(); if (frametype as u8) & 0b00001000 > 0 && len > 125 { - return Err(io::Error::new( - ErrorKind::InvalidData, - "Control frames cannot have data longer than 125 bytes", + return Err(WebSocketError::new( + WebSocketErrorKind::Unexpected, + "Control frames cannot have data longer than 125 bytes", )); } @@ -265,7 +265,7 @@ impl WebSocketWriter { } pub async fn send_control_frame(&mut self, mask: Option<[u8; 4]>, opcode: OpCode, data: &[u8]) -> Result<(), Error> { - let frame = create_frame(mask, data, opcode)?; + let frame = create_frame(mask, data, opcode).map_err(Error::from)?; self.writer.write_all(&frame).await.map_err(Error::from) } } @@ -288,7 +288,7 @@ impl AsyncWrite for WebSocketWriter { let frame = match create_frame(this.mask, buf, frametype) { Ok(f) => f, Err(e) => { - return Poll::Ready(Err(e)); + return Poll::Ready(Err(io_err_other(e))); } }; this.frame = Some((frame, 0, buf.len())); @@ -533,12 +533,12 @@ impl AsyncRead for WebSocketReader ReaderState::NoData => { let mut reader = match this.reader.take() { Some(reader) => reader, - None => return Poll::Ready(Err(io_format_err!("no reader"))), + None => return Poll::Ready(Err(io_err_other("no reader"))), }; let mut buffer = match this.read_buffer.take() { Some(buffer) => buffer, - None => return Poll::Ready(Err(io_format_err!("no buffer"))), + None => return Poll::Ready(Err(io_err_other("no buffer"))), }; let future = async move { @@ -565,7 +565,7 @@ impl AsyncRead for WebSocketReader ReaderState::HaveData => { let mut read_buffer = match this.read_buffer.take() { Some(read_buffer) => read_buffer, - None => return Poll::Ready(Err(io_format_err!("no buffer"))), + None => return Poll::Ready(Err(io_err_other("no buffer"))), }; let mut header = match this.header.take() { -- 2.20.1