1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-12-01 02:44:37 +01:00

add ws error tracing

This commit is contained in:
Nikolay Kim 2018-02-27 15:38:57 -08:00
parent 1f063e4136
commit 4f99cd1580

View File

@ -36,13 +36,17 @@ pub enum WsClientError {
#[fail(display="Invalid url")] #[fail(display="Invalid url")]
InvalidUrl, InvalidUrl,
#[fail(display="Invalid response status")] #[fail(display="Invalid response status")]
InvalidResponseStatus, InvalidResponseStatus(StatusCode),
#[fail(display="Invalid upgrade header")] #[fail(display="Invalid upgrade header")]
InvalidUpgradeHeader, InvalidUpgradeHeader,
#[fail(display="Invalid connection header")] #[fail(display="Invalid connection header")]
InvalidConnectionHeader, InvalidConnectionHeader(HeaderValue),
#[fail(display="Missing CONNECTION header")]
MissingConnectionHeader,
#[fail(display="Missing SEC-WEBSOCKET-ACCEPT header")]
MissingWebSocketAcceptHeader,
#[fail(display="Invalid challenge response")] #[fail(display="Invalid challenge response")]
InvalidChallengeResponse, InvalidChallengeResponse(String, HeaderValue),
#[fail(display="Http parsing error")] #[fail(display="Http parsing error")]
Http(HttpError), Http(HttpError),
#[fail(display="Url parsing error")] #[fail(display="Url parsing error")]
@ -292,7 +296,7 @@ impl Future for WsClientHandshake {
// verify response // verify response
if resp.status() != StatusCode::SWITCHING_PROTOCOLS { if resp.status() != StatusCode::SWITCHING_PROTOCOLS {
return Err(WsClientError::InvalidResponseStatus) return Err(WsClientError::InvalidResponseStatus(resp.status()))
} }
// Check for "UPGRADE" to websocket header // Check for "UPGRADE" to websocket header
let has_hdr = if let Some(hdr) = resp.headers().get(header::UPGRADE) { let has_hdr = if let Some(hdr) = resp.headers().get(header::UPGRADE) {
@ -305,19 +309,26 @@ impl Future for WsClientHandshake {
false false
}; };
if !has_hdr { if !has_hdr {
trace!("Invalid upgrade header");
return Err(WsClientError::InvalidUpgradeHeader) return Err(WsClientError::InvalidUpgradeHeader)
} }
// Check for "CONNECTION" header // Check for "CONNECTION" header
let has_hdr = if let Some(conn) = resp.headers().get(header::CONNECTION) { if let Some(conn) = resp.headers().get(header::CONNECTION) {
if let Ok(s) = conn.to_str() { if let Ok(s) = conn.to_str() {
s.to_lowercase().contains("upgrade") if !s.to_lowercase().contains("upgrade") {
} else { false } trace!("Invalid connection header: {}", s);
} else { false }; return Err(WsClientError::InvalidConnectionHeader(conn.clone()))
if !has_hdr { }
return Err(WsClientError::InvalidConnectionHeader) } else {
trace!("Invalid connection header: {:?}", conn);
return Err(WsClientError::InvalidConnectionHeader(conn.clone()))
}
} else {
trace!("Missing connection header");
return Err(WsClientError::MissingConnectionHeader)
} }
let match_key = if let Some(key) = resp.headers().get( if let Some(key) = resp.headers().get(
HeaderName::try_from("SEC-WEBSOCKET-ACCEPT").unwrap()) HeaderName::try_from("SEC-WEBSOCKET-ACCEPT").unwrap())
{ {
// field is constructed by concatenating /key/ // field is constructed by concatenating /key/
@ -326,13 +337,17 @@ impl Future for WsClientHandshake {
let mut sha1 = Sha1::new(); let mut sha1 = Sha1::new();
sha1.update(self.key.as_ref()); sha1.update(self.key.as_ref());
sha1.update(WS_GUID); sha1.update(WS_GUID);
key.as_bytes() == base64::encode(&sha1.digest().bytes()).as_bytes() let encoded = base64::encode(&sha1.digest().bytes());
} else { if key.as_bytes() != encoded.as_bytes() {
false trace!(
}; "Invalid challenge response: expected: {} received: {:?}",
if !match_key { encoded, key);
return Err(WsClientError::InvalidChallengeResponse) return Err(WsClientError::InvalidChallengeResponse(encoded, key.clone()));
} }
} else {
trace!("Missing SEC-WEBSOCKET-ACCEPT header");
return Err(WsClientError::MissingWebSocketAcceptHeader)
};
let inner = WsInner { let inner = WsInner {
tx: self.tx.take().unwrap(), tx: self.tx.take().unwrap(),