Skip to content

Commit

Permalink
Merge pull request snapview#298 from riptide-org/ISSUE-199-READ-BODY-…
Browse files Browse the repository at this point in the history
…ON-FAILED-READ

Error response on client type returns request body upon non receiving non 101 status code from server
  • Loading branch information
agalakhov authored Nov 14, 2022
2 parents b473e19 + 1c657d4 commit 4382106
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub enum Error {
/// HTTP error.
#[error("HTTP error: {}", .0.status())]
#[cfg(feature = "handshake")]
Http(Response<Option<String>>),
Http(Response<Option<Vec<u8>>>),
/// HTTP format error.
#[error("HTTP format error: {0}")]
#[cfg(feature = "handshake")]
Expand Down
16 changes: 12 additions & 4 deletions src/handshake/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
pub type Request = HttpRequest<()>;

/// Client response type.
pub type Response = HttpResponse<()>;
pub type Response = HttpResponse<Option<Vec<u8>>>;

/// Client handshake role.
#[derive(Debug)]
Expand Down Expand Up @@ -83,7 +83,15 @@ 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 = match self.verify_data.verify_response(result) {
Ok(r) => r,
Err(Error::Http(mut e)) => {
*e.body_mut() = Some(tail);
return Err(Error::Http(e))
},
Err(e) => return Err(e),
};

debug!("Client handshake done.");
let websocket =
WebSocket::from_partially_read(stream, tail, Role::Client, self.config);
Expand Down Expand Up @@ -182,7 +190,7 @@ impl VerifyData {
// 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)));
return Err(Error::Http(response));
}

let headers = response.headers();
Expand Down Expand Up @@ -255,7 +263,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
Expand Down
5 changes: 4 additions & 1 deletion src/handshake/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,10 @@ impl<S: Read + Write, C: Callback> HandshakeRole for ServerHandshake<S, C> {
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);
Expand Down

0 comments on commit 4382106

Please sign in to comment.