From 4a7beaa897f4468cabf4ba8e6f1ed4ebaf4bc6bb Mon Sep 17 00:00:00 2001 From: Josiah Bull Date: Sat, 27 Aug 2022 18:50:48 +1200 Subject: [PATCH] feat: error responses return body upon non 101 status code modified `client::Response` type to contain `String` instead of `()` to achieve this. --- src/handshake/client.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/handshake/client.rs b/src/handshake/client.rs index 0700fa2..32a3c40 100644 --- a/src/handshake/client.rs +++ b/src/handshake/client.rs @@ -26,7 +26,7 @@ use crate::{ pub type Request = HttpRequest<()>; /// Client response type. -pub type Response = HttpResponse<()>; +pub type Response = HttpResponse>; /// Client handshake role. #[derive(Debug)] @@ -83,7 +83,7 @@ impl HandshakeRole for ClientHandshake { ProcessingResult::Continue(HandshakeMachine::start_read(stream)) } StageResult::DoneReading { stream, result, tail } => { - let result = self.verify_data.verify_response(result)?; + let result = self.verify_data.verify_response(result, &tail)?; debug!("Client handshake done."); let websocket = WebSocket::from_partially_read(stream, tail, Role::Client, self.config); @@ -178,11 +178,12 @@ struct VerifyData { } impl VerifyData { - pub fn verify_response(&self, response: Response) -> Result { + pub fn verify_response(&self, mut response: Response, tail: &[u8]) -> Result { // 1. If the status code received from the server is not 101, the // client handles the response per HTTP [RFC2616] procedures. (RFC 6455) if response.status() != StatusCode::SWITCHING_PROTOCOLS { - return Err(Error::Http(response.map(|_| None))); + *response.body_mut() = Some(String::from_utf8_lossy(tail).to_string()); + return Err(Error::Http(response)); } let headers = response.headers(); @@ -255,7 +256,7 @@ impl<'h, 'b: 'h> FromHttparse> for Response { let headers = HeaderMap::from_httparse(raw.headers)?; - let mut response = Response::new(()); + let mut response = Response::new(None); *response.status_mut() = StatusCode::from_u16(raw.code.expect("Bug: no HTTP status code"))?; *response.headers_mut() = headers; // TODO: httparse only supports HTTP 0.9/1.0/1.1 but not HTTP 2.0