diff --git a/examples/callback-error.rs b/examples/callback-error.rs index ee51136..cf78a2e 100644 --- a/examples/callback-error.rs +++ b/examples/callback-error.rs @@ -13,7 +13,7 @@ fn main() { let callback = |_req: &Request, _resp| { let resp = Response::builder() .status(StatusCode::FORBIDDEN) - .body("Access denied".into()) + .body(Some("Access denied".into())) .unwrap(); Err(resp) }; diff --git a/src/error.rs b/src/error.rs index 9d6576e..c830024 100644 --- a/src/error.rs +++ b/src/error.rs @@ -65,7 +65,7 @@ pub enum Error { /// HTTP error. #[error("HTTP error: {}", .0.status())] #[cfg(feature = "handshake")] - Http(Response>), + Http(Response>>), /// HTTP format error. #[error("HTTP format error: {0}")] #[cfg(feature = "handshake")] diff --git a/src/handshake/client.rs b/src/handshake/client.rs index 87305f4..aaeabb7 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)] @@ -86,7 +86,7 @@ impl HandshakeRole for ClientHandshake { let result = match self.verify_data.verify_response(result) { Ok(r) => r, Err(Error::Http(mut e)) => { - *e.body_mut() = tail; + *e.body_mut() = Some(tail); return Err(Error::Http(e)) }, Err(e) => return Err(e), @@ -263,7 +263,7 @@ impl<'h, 'b: 'h> FromHttparse> for Response { let headers = HeaderMap::from_httparse(raw.headers)?; - let mut response = Response::new(vec![]); + 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 diff --git a/src/handshake/server.rs b/src/handshake/server.rs index 7e3a57f..4cf1c5c 100644 --- a/src/handshake/server.rs +++ b/src/handshake/server.rs @@ -30,7 +30,7 @@ pub type Request = HttpRequest<()>; pub type Response = HttpResponse<()>; /// Server error response type. -pub type ErrorResponse = HttpResponse>; +pub type ErrorResponse = HttpResponse>; fn create_parts(request: &HttpRequest) -> Result { if request.method() != http::Method::GET { @@ -265,7 +265,9 @@ impl HandshakeRole for ServerHandshake { let mut output = vec![]; write_response(&mut output, resp)?; - output.extend_from_slice(resp.body()); + if let Some(body) = resp.body() { + output.extend_from_slice(body.as_bytes()); + } ProcessingResult::Continue(HandshakeMachine::start_write(stream, output)) } @@ -275,7 +277,10 @@ impl HandshakeRole for ServerHandshake { StageResult::DoneWriting(stream) => { if let Some(err) = self.error_response.take() { debug!("Server handshake failed."); - return Err(Error::Http(err)); + + let (parts, body) = err.into_parts(); + let body = body.map(|b| b.as_bytes().to_vec()); + return Err(Error::Http(http::Response::from_parts(parts, body))); } else { debug!("Server handshake done."); let websocket = WebSocket::from_raw_socket(stream, Role::Server, self.config);