feat: error responses return body upon non 101 status code

modified `client::Response` type to contain `String` instead of `()` to achieve this.
pull/298/head
Josiah Bull 2 years ago
parent b473e19f7e
commit 4a7beaa897
No known key found for this signature in database
GPG Key ID: 10FCCC1BF551A8A8
  1. 11
      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<Option<String>>;
/// Client handshake role.
#[derive(Debug)]
@ -83,7 +83,7 @@ impl<S: Read + Write> HandshakeRole for ClientHandshake<S> {
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<Response> {
pub fn verify_response(&self, mut response: Response, tail: &[u8]) -> Result<Response> {
// 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<httparse::Response<'h, 'b>> 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

Loading…
Cancel
Save