|
|
|
@ -26,7 +26,7 @@ use crate::{ |
|
|
|
|
pub type Request = HttpRequest<()>; |
|
|
|
|
|
|
|
|
|
/// Client response type.
|
|
|
|
|
pub type Response = HttpResponse<Option<String>>; |
|
|
|
|
pub type Response = HttpResponse<Vec<u8>>; |
|
|
|
|
|
|
|
|
|
/// Client handshake role.
|
|
|
|
|
#[derive(Debug)] |
|
|
|
@ -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, &tail)?; |
|
|
|
|
let result = match self.verify_data.verify_response(result) { |
|
|
|
|
Ok(r) => r, |
|
|
|
|
Err(Error::Http(mut e)) => { |
|
|
|
|
*e.body_mut() = 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); |
|
|
|
@ -178,11 +186,10 @@ struct VerifyData { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl VerifyData { |
|
|
|
|
pub fn verify_response(&self, mut response: Response, tail: &[u8]) -> Result<Response> { |
|
|
|
|
pub fn verify_response(&self, response: Response) -> 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 { |
|
|
|
|
*response.body_mut() = Some(String::from_utf8_lossy(tail).to_string()); |
|
|
|
|
return Err(Error::Http(response)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -256,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(None); |
|
|
|
|
let mut response = Response::new(vec![]); |
|
|
|
|
*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
|
|
|
|
|