mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-26 02:19:22 +02:00
clippy warnings; fmt
This commit is contained in:
@ -121,7 +121,7 @@ impl Client {
|
||||
|
||||
/// Create new websocket connection with custom `ClientConnector`
|
||||
pub fn with_connector<S: AsRef<str>>(
|
||||
uri: S, conn: Addr<Unsync, ClientConnector>
|
||||
uri: S, conn: Addr<Unsync, ClientConnector>,
|
||||
) -> Client {
|
||||
let mut cl = Client {
|
||||
request: ClientRequest::build(),
|
||||
@ -142,9 +142,8 @@ impl Client {
|
||||
U: IntoIterator<Item = V> + 'static,
|
||||
V: AsRef<str>,
|
||||
{
|
||||
let mut protos = protos
|
||||
.into_iter()
|
||||
.fold(String::new(), |acc, s| acc + s.as_ref() + ",");
|
||||
let mut protos =
|
||||
protos.into_iter().fold(String::new(), |acc, s| acc + s.as_ref() + ",");
|
||||
protos.pop();
|
||||
self.protocols = Some(protos);
|
||||
self
|
||||
@ -218,8 +217,7 @@ impl Client {
|
||||
self.request.upgrade();
|
||||
self.request.set_header(header::UPGRADE, "websocket");
|
||||
self.request.set_header(header::CONNECTION, "upgrade");
|
||||
self.request
|
||||
.set_header(header::SEC_WEBSOCKET_VERSION, "13");
|
||||
self.request.set_header(header::SEC_WEBSOCKET_VERSION, "13");
|
||||
self.request.with_connector(self.conn.clone());
|
||||
|
||||
if let Some(protocols) = self.protocols.take() {
|
||||
@ -394,10 +392,7 @@ impl Future for ClientHandshake {
|
||||
encoded,
|
||||
key
|
||||
);
|
||||
return Err(ClientError::InvalidChallengeResponse(
|
||||
encoded,
|
||||
key.clone(),
|
||||
));
|
||||
return Err(ClientError::InvalidChallengeResponse(encoded, key.clone()));
|
||||
}
|
||||
} else {
|
||||
trace!("Missing SEC-WEBSOCKET-ACCEPT header");
|
||||
@ -416,7 +411,9 @@ impl Future for ClientHandshake {
|
||||
inner: Rc::clone(&inner),
|
||||
max_size: self.max_size,
|
||||
},
|
||||
ClientWriter { inner },
|
||||
ClientWriter {
|
||||
inner,
|
||||
},
|
||||
)))
|
||||
}
|
||||
}
|
||||
@ -536,23 +533,13 @@ impl ClientWriter {
|
||||
/// Send ping frame
|
||||
#[inline]
|
||||
pub fn ping(&mut self, message: &str) {
|
||||
self.write(Frame::message(
|
||||
Vec::from(message),
|
||||
OpCode::Ping,
|
||||
true,
|
||||
true,
|
||||
));
|
||||
self.write(Frame::message(Vec::from(message), OpCode::Ping, true, true));
|
||||
}
|
||||
|
||||
/// Send pong frame
|
||||
#[inline]
|
||||
pub fn pong(&mut self, message: &str) {
|
||||
self.write(Frame::message(
|
||||
Vec::from(message),
|
||||
OpCode::Pong,
|
||||
true,
|
||||
true,
|
||||
));
|
||||
self.write(Frame::message(Vec::from(message), OpCode::Pong, true, true));
|
||||
}
|
||||
|
||||
/// Send close frame
|
||||
|
@ -156,23 +156,13 @@ where
|
||||
/// Send ping frame
|
||||
#[inline]
|
||||
pub fn ping(&mut self, message: &str) {
|
||||
self.write(Frame::message(
|
||||
Vec::from(message),
|
||||
OpCode::Ping,
|
||||
true,
|
||||
false,
|
||||
));
|
||||
self.write(Frame::message(Vec::from(message), OpCode::Ping, true, false));
|
||||
}
|
||||
|
||||
/// Send pong frame
|
||||
#[inline]
|
||||
pub fn pong(&mut self, message: &str) {
|
||||
self.write(Frame::message(
|
||||
Vec::from(message),
|
||||
OpCode::Pong,
|
||||
true,
|
||||
false,
|
||||
));
|
||||
self.write(Frame::message(Vec::from(message), OpCode::Pong, true, false));
|
||||
}
|
||||
|
||||
/// Send close frame
|
||||
|
@ -8,9 +8,9 @@ use body::Binary;
|
||||
use error::PayloadError;
|
||||
use payload::PayloadHelper;
|
||||
|
||||
use ws::ProtocolError;
|
||||
use ws::mask::apply_mask;
|
||||
use ws::proto::{CloseCode, CloseReason, OpCode};
|
||||
use ws::ProtocolError;
|
||||
|
||||
/// A struct representing a `WebSocket` frame.
|
||||
#[derive(Debug)]
|
||||
@ -36,7 +36,7 @@ impl Frame {
|
||||
NetworkEndian::write_u16(&mut code_bytes, reason.code.into());
|
||||
|
||||
let mut payload = Vec::from(&code_bytes[..]);
|
||||
if let Some(description) = reason.description{
|
||||
if let Some(description) = reason.description {
|
||||
payload.extend(description.as_bytes());
|
||||
}
|
||||
payload
|
||||
@ -48,7 +48,7 @@ impl Frame {
|
||||
|
||||
#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
|
||||
fn read_copy_md<S>(
|
||||
pl: &mut PayloadHelper<S>, server: bool, max_size: usize
|
||||
pl: &mut PayloadHelper<S>, server: bool, max_size: usize,
|
||||
) -> Poll<Option<(usize, bool, OpCode, usize, Option<u32>)>, ProtocolError>
|
||||
where
|
||||
S: Stream<Item = Bytes, Error = PayloadError>,
|
||||
@ -122,17 +122,11 @@ impl Frame {
|
||||
None
|
||||
};
|
||||
|
||||
Ok(Async::Ready(Some((
|
||||
idx,
|
||||
finished,
|
||||
opcode,
|
||||
length,
|
||||
mask,
|
||||
))))
|
||||
Ok(Async::Ready(Some((idx, finished, opcode, length, mask))))
|
||||
}
|
||||
|
||||
fn read_chunk_md(
|
||||
chunk: &[u8], server: bool, max_size: usize
|
||||
chunk: &[u8], server: bool, max_size: usize,
|
||||
) -> Poll<(usize, bool, OpCode, usize, Option<u32>), ProtocolError> {
|
||||
let chunk_len = chunk.len();
|
||||
|
||||
@ -203,7 +197,7 @@ impl Frame {
|
||||
|
||||
/// Parse the input stream into a frame.
|
||||
pub fn parse<S>(
|
||||
pl: &mut PayloadHelper<S>, server: bool, max_size: usize
|
||||
pl: &mut PayloadHelper<S>, server: bool, max_size: usize,
|
||||
) -> Poll<Option<Frame>, ProtocolError>
|
||||
where
|
||||
S: Stream<Item = Bytes, Error = PayloadError>,
|
||||
@ -288,7 +282,10 @@ impl Frame {
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Some(CloseReason { code, description })
|
||||
Some(CloseReason {
|
||||
code,
|
||||
description,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@ -296,7 +293,7 @@ impl Frame {
|
||||
|
||||
/// Generate binary representation
|
||||
pub fn message<B: Into<Binary>>(
|
||||
data: B, code: OpCode, finished: bool, genmask: bool
|
||||
data: B, code: OpCode, finished: bool, genmask: bool,
|
||||
) -> Binary {
|
||||
let payload = data.into();
|
||||
let one: u8 = if finished {
|
||||
|
@ -28,7 +28,11 @@ fn apply_mask_fast32(buf: &mut [u8], mask_u32: u32) {
|
||||
// Possible first unaligned block.
|
||||
let head = min(len, (8 - (ptr as usize & 0x7)) & 0x3);
|
||||
let mask_u32 = if head > 0 {
|
||||
let n = if head > 4 { head - 4 } else { head };
|
||||
let n = if head > 4 {
|
||||
head - 4
|
||||
} else {
|
||||
head
|
||||
};
|
||||
|
||||
let mask_u32 = if n > 0 {
|
||||
unsafe {
|
||||
|
121
src/ws/mod.rs
121
src/ws/mod.rs
@ -133,24 +133,24 @@ pub enum HandshakeError {
|
||||
impl ResponseError for HandshakeError {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
match *self {
|
||||
HandshakeError::GetMethodRequired => HttpResponse::MethodNotAllowed()
|
||||
.header(header::ALLOW, "GET")
|
||||
.finish(),
|
||||
HandshakeError::GetMethodRequired => {
|
||||
HttpResponse::MethodNotAllowed().header(header::ALLOW, "GET").finish()
|
||||
}
|
||||
HandshakeError::NoWebsocketUpgrade => HttpResponse::BadRequest()
|
||||
.reason("No WebSocket UPGRADE header found")
|
||||
.finish(),
|
||||
HandshakeError::NoConnectionUpgrade => HttpResponse::BadRequest()
|
||||
.reason("No CONNECTION upgrade")
|
||||
.finish(),
|
||||
HandshakeError::NoConnectionUpgrade => {
|
||||
HttpResponse::BadRequest().reason("No CONNECTION upgrade").finish()
|
||||
}
|
||||
HandshakeError::NoVersionHeader => HttpResponse::BadRequest()
|
||||
.reason("Websocket version header is required")
|
||||
.finish(),
|
||||
HandshakeError::UnsupportedVersion => HttpResponse::BadRequest()
|
||||
.reason("Unsupported version")
|
||||
.finish(),
|
||||
HandshakeError::BadWebsocketKey => HttpResponse::BadRequest()
|
||||
.reason("Handshake error")
|
||||
.finish(),
|
||||
HandshakeError::UnsupportedVersion => {
|
||||
HttpResponse::BadRequest().reason("Unsupported version").finish()
|
||||
}
|
||||
HandshakeError::BadWebsocketKey => {
|
||||
HttpResponse::BadRequest().reason("Handshake error").finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -189,7 +189,7 @@ where
|
||||
// /// the returned response headers contain the first protocol in this list
|
||||
// /// which the server also knows.
|
||||
pub fn handshake<S>(
|
||||
req: &HttpRequest<S>
|
||||
req: &HttpRequest<S>,
|
||||
) -> Result<HttpResponseBuilder, HandshakeError> {
|
||||
// WebSocket accepts only GET
|
||||
if *req.method() != Method::GET {
|
||||
@ -216,9 +216,7 @@ pub fn handshake<S>(
|
||||
}
|
||||
|
||||
// check supported version
|
||||
if !req.headers()
|
||||
.contains_key(header::SEC_WEBSOCKET_VERSION)
|
||||
{
|
||||
if !req.headers().contains_key(header::SEC_WEBSOCKET_VERSION) {
|
||||
return Err(HandshakeError::NoVersionHeader);
|
||||
}
|
||||
let supported_ver = {
|
||||
@ -355,10 +353,7 @@ mod tests {
|
||||
HeaderMap::new(),
|
||||
None,
|
||||
);
|
||||
assert_eq!(
|
||||
HandshakeError::GetMethodRequired,
|
||||
handshake(&req).err().unwrap()
|
||||
);
|
||||
assert_eq!(HandshakeError::GetMethodRequired, handshake(&req).err().unwrap());
|
||||
|
||||
let req = HttpRequest::new(
|
||||
Method::GET,
|
||||
@ -367,16 +362,10 @@ mod tests {
|
||||
HeaderMap::new(),
|
||||
None,
|
||||
);
|
||||
assert_eq!(
|
||||
HandshakeError::NoWebsocketUpgrade,
|
||||
handshake(&req).err().unwrap()
|
||||
);
|
||||
assert_eq!(HandshakeError::NoWebsocketUpgrade, handshake(&req).err().unwrap());
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
header::UPGRADE,
|
||||
header::HeaderValue::from_static("test"),
|
||||
);
|
||||
headers.insert(header::UPGRADE, header::HeaderValue::from_static("test"));
|
||||
let req = HttpRequest::new(
|
||||
Method::GET,
|
||||
Uri::from_str("/").unwrap(),
|
||||
@ -384,16 +373,10 @@ mod tests {
|
||||
headers,
|
||||
None,
|
||||
);
|
||||
assert_eq!(
|
||||
HandshakeError::NoWebsocketUpgrade,
|
||||
handshake(&req).err().unwrap()
|
||||
);
|
||||
assert_eq!(HandshakeError::NoWebsocketUpgrade, handshake(&req).err().unwrap());
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
header::UPGRADE,
|
||||
header::HeaderValue::from_static("websocket"),
|
||||
);
|
||||
headers.insert(header::UPGRADE, header::HeaderValue::from_static("websocket"));
|
||||
let req = HttpRequest::new(
|
||||
Method::GET,
|
||||
Uri::from_str("/").unwrap(),
|
||||
@ -401,20 +384,11 @@ mod tests {
|
||||
headers,
|
||||
None,
|
||||
);
|
||||
assert_eq!(
|
||||
HandshakeError::NoConnectionUpgrade,
|
||||
handshake(&req).err().unwrap()
|
||||
);
|
||||
assert_eq!(HandshakeError::NoConnectionUpgrade, handshake(&req).err().unwrap());
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
header::UPGRADE,
|
||||
header::HeaderValue::from_static("websocket"),
|
||||
);
|
||||
headers.insert(
|
||||
header::CONNECTION,
|
||||
header::HeaderValue::from_static("upgrade"),
|
||||
);
|
||||
headers.insert(header::UPGRADE, header::HeaderValue::from_static("websocket"));
|
||||
headers.insert(header::CONNECTION, header::HeaderValue::from_static("upgrade"));
|
||||
let req = HttpRequest::new(
|
||||
Method::GET,
|
||||
Uri::from_str("/").unwrap(),
|
||||
@ -422,20 +396,11 @@ mod tests {
|
||||
headers,
|
||||
None,
|
||||
);
|
||||
assert_eq!(
|
||||
HandshakeError::NoVersionHeader,
|
||||
handshake(&req).err().unwrap()
|
||||
);
|
||||
assert_eq!(HandshakeError::NoVersionHeader, handshake(&req).err().unwrap());
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
header::UPGRADE,
|
||||
header::HeaderValue::from_static("websocket"),
|
||||
);
|
||||
headers.insert(
|
||||
header::CONNECTION,
|
||||
header::HeaderValue::from_static("upgrade"),
|
||||
);
|
||||
headers.insert(header::UPGRADE, header::HeaderValue::from_static("websocket"));
|
||||
headers.insert(header::CONNECTION, header::HeaderValue::from_static("upgrade"));
|
||||
headers.insert(
|
||||
header::SEC_WEBSOCKET_VERSION,
|
||||
header::HeaderValue::from_static("5"),
|
||||
@ -447,20 +412,11 @@ mod tests {
|
||||
headers,
|
||||
None,
|
||||
);
|
||||
assert_eq!(
|
||||
HandshakeError::UnsupportedVersion,
|
||||
handshake(&req).err().unwrap()
|
||||
);
|
||||
assert_eq!(HandshakeError::UnsupportedVersion, handshake(&req).err().unwrap());
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
header::UPGRADE,
|
||||
header::HeaderValue::from_static("websocket"),
|
||||
);
|
||||
headers.insert(
|
||||
header::CONNECTION,
|
||||
header::HeaderValue::from_static("upgrade"),
|
||||
);
|
||||
headers.insert(header::UPGRADE, header::HeaderValue::from_static("websocket"));
|
||||
headers.insert(header::CONNECTION, header::HeaderValue::from_static("upgrade"));
|
||||
headers.insert(
|
||||
header::SEC_WEBSOCKET_VERSION,
|
||||
header::HeaderValue::from_static("13"),
|
||||
@ -472,28 +428,17 @@ mod tests {
|
||||
headers,
|
||||
None,
|
||||
);
|
||||
assert_eq!(
|
||||
HandshakeError::BadWebsocketKey,
|
||||
handshake(&req).err().unwrap()
|
||||
);
|
||||
assert_eq!(HandshakeError::BadWebsocketKey, handshake(&req).err().unwrap());
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
header::UPGRADE,
|
||||
header::HeaderValue::from_static("websocket"),
|
||||
);
|
||||
headers.insert(
|
||||
header::CONNECTION,
|
||||
header::HeaderValue::from_static("upgrade"),
|
||||
);
|
||||
headers.insert(header::UPGRADE, header::HeaderValue::from_static("websocket"));
|
||||
headers.insert(header::CONNECTION, header::HeaderValue::from_static("upgrade"));
|
||||
headers.insert(
|
||||
header::SEC_WEBSOCKET_VERSION,
|
||||
header::HeaderValue::from_static("13"),
|
||||
);
|
||||
headers.insert(
|
||||
header::SEC_WEBSOCKET_KEY,
|
||||
header::HeaderValue::from_static("13"),
|
||||
);
|
||||
headers
|
||||
.insert(header::SEC_WEBSOCKET_KEY, header::HeaderValue::from_static("13"));
|
||||
let req = HttpRequest::new(
|
||||
Method::GET,
|
||||
Uri::from_str("/").unwrap(),
|
||||
|
@ -194,11 +194,11 @@ impl From<CloseCode> for CloseReason {
|
||||
}
|
||||
}
|
||||
|
||||
impl <T: Into<String>> From<(CloseCode, T)> for CloseReason {
|
||||
impl<T: Into<String>> From<(CloseCode, T)> for CloseReason {
|
||||
fn from(info: (CloseCode, T)) -> Self {
|
||||
CloseReason{
|
||||
CloseReason {
|
||||
code: info.0,
|
||||
description: Some(info.1.into())
|
||||
description: Some(info.1.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user