2017-10-08 07:41:02 +02:00
|
|
|
//! `WebSocket` support for Actix
|
|
|
|
//!
|
2018-04-14 01:02:01 +02:00
|
|
|
//! To setup a `WebSocket`, first do web socket handshake then on success
|
|
|
|
//! convert `Payload` into a `WsStream` stream and then use `WsWriter` to
|
|
|
|
//! communicate with the peer.
|
2017-10-08 07:41:02 +02:00
|
|
|
//!
|
|
|
|
//! ## Example
|
|
|
|
//!
|
|
|
|
//! ```rust
|
2017-12-27 05:52:21 +01:00
|
|
|
//! # extern crate actix;
|
|
|
|
//! # extern crate actix_web;
|
2018-01-10 19:12:34 +01:00
|
|
|
//! # use actix::*;
|
|
|
|
//! # use actix_web::*;
|
2018-03-31 18:18:25 +02:00
|
|
|
//! use actix_web::{ws, HttpRequest, HttpResponse};
|
2017-10-08 07:41:02 +02:00
|
|
|
//!
|
2017-11-29 23:12:27 +01:00
|
|
|
//! // do websocket handshake and start actor
|
2018-01-01 02:26:32 +01:00
|
|
|
//! fn ws_index(req: HttpRequest) -> Result<HttpResponse> {
|
2017-12-27 05:52:21 +01:00
|
|
|
//! ws::start(req, Ws)
|
2017-11-29 19:31:24 +01:00
|
|
|
//! }
|
|
|
|
//!
|
2017-12-27 05:52:21 +01:00
|
|
|
//! struct Ws;
|
2017-10-08 07:41:02 +02:00
|
|
|
//!
|
2017-12-27 05:52:21 +01:00
|
|
|
//! impl Actor for Ws {
|
2018-01-10 19:12:34 +01:00
|
|
|
//! type Context = ws::WebsocketContext<Self>;
|
2017-10-08 07:41:02 +02:00
|
|
|
//! }
|
|
|
|
//!
|
2018-02-27 19:09:24 +01:00
|
|
|
//! // Handler for ws::Message messages
|
2018-03-27 08:29:53 +02:00
|
|
|
//! impl StreamHandler<ws::Message, ws::ProtocolError> for Ws {
|
2018-01-05 22:30:21 +01:00
|
|
|
//!
|
2018-01-10 19:12:34 +01:00
|
|
|
//! fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {
|
2017-10-08 07:41:02 +02:00
|
|
|
//! match msg {
|
2018-01-10 19:12:34 +01:00
|
|
|
//! ws::Message::Ping(msg) => ctx.pong(&msg),
|
2018-02-10 07:26:48 +01:00
|
|
|
//! ws::Message::Text(text) => ctx.text(text),
|
2018-01-10 19:12:34 +01:00
|
|
|
//! ws::Message::Binary(bin) => ctx.binary(bin),
|
2017-10-08 07:41:02 +02:00
|
|
|
//! _ => (),
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! }
|
2017-12-27 05:52:21 +01:00
|
|
|
//! #
|
|
|
|
//! # fn main() {
|
2018-03-31 09:16:55 +02:00
|
|
|
//! # App::new()
|
2017-12-27 05:52:21 +01:00
|
|
|
//! # .resource("/ws/", |r| r.f(ws_index)) // <- register websocket route
|
|
|
|
//! # .finish();
|
|
|
|
//! # }
|
2017-10-08 07:41:02 +02:00
|
|
|
//! ```
|
2018-02-26 22:58:23 +01:00
|
|
|
use bytes::Bytes;
|
2017-10-08 07:41:02 +02:00
|
|
|
use futures::{Async, Poll, Stream};
|
2018-04-14 01:02:01 +02:00
|
|
|
use http::{header, Method, StatusCode};
|
2017-10-08 06:48:00 +02:00
|
|
|
|
2018-02-27 19:09:24 +01:00
|
|
|
use actix::{Actor, AsyncContext, StreamHandler};
|
2017-10-08 06:48:00 +02:00
|
|
|
|
2018-01-10 19:12:34 +01:00
|
|
|
use body::Binary;
|
2018-02-27 19:09:24 +01:00
|
|
|
use error::{Error, PayloadError, ResponseError};
|
2018-02-28 00:03:28 +01:00
|
|
|
use httpmessage::HttpMessage;
|
2017-10-15 07:52:38 +02:00
|
|
|
use httprequest::HttpRequest;
|
2018-01-01 02:26:32 +01:00
|
|
|
use httpresponse::{ConnectionType, HttpResponse, HttpResponseBuilder};
|
2018-04-14 01:02:01 +02:00
|
|
|
use payload::PayloadHelper;
|
2017-10-08 06:48:00 +02:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
mod client;
|
2018-01-10 20:13:29 +01:00
|
|
|
mod context;
|
2018-04-14 01:02:01 +02:00
|
|
|
mod frame;
|
2018-01-21 01:47:34 +01:00
|
|
|
mod mask;
|
2018-04-14 01:02:01 +02:00
|
|
|
mod proto;
|
2018-01-10 20:13:29 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
pub use self::client::{Client, ClientError, ClientHandshake, ClientReader, ClientWriter};
|
|
|
|
pub use self::context::WebsocketContext;
|
2018-03-09 22:03:15 +01:00
|
|
|
pub use self::frame::Frame;
|
2018-04-21 22:50:27 +02:00
|
|
|
pub use self::proto::{CloseCode, CloseReason, OpCode};
|
2018-03-02 20:29:55 +01:00
|
|
|
|
2018-03-29 19:44:26 +02:00
|
|
|
/// Websocket protocol errors
|
2018-02-27 19:09:24 +01:00
|
|
|
#[derive(Fail, Debug)]
|
2018-03-02 20:29:55 +01:00
|
|
|
pub enum ProtocolError {
|
2018-02-27 19:09:24 +01:00
|
|
|
/// Received an unmasked frame from client
|
2018-04-14 01:02:01 +02:00
|
|
|
#[fail(display = "Received an unmasked frame from client")]
|
2018-02-27 19:09:24 +01:00
|
|
|
UnmaskedFrame,
|
|
|
|
/// Received a masked frame from server
|
2018-04-14 01:02:01 +02:00
|
|
|
#[fail(display = "Received a masked frame from server")]
|
2018-02-27 19:09:24 +01:00
|
|
|
MaskedFrame,
|
|
|
|
/// Encountered invalid opcode
|
2018-04-14 01:02:01 +02:00
|
|
|
#[fail(display = "Invalid opcode: {}", _0)]
|
2018-02-27 19:09:24 +01:00
|
|
|
InvalidOpcode(u8),
|
|
|
|
/// Invalid control frame length
|
2018-04-14 01:02:01 +02:00
|
|
|
#[fail(display = "Invalid control frame length: {}", _0)]
|
2018-02-27 19:09:24 +01:00
|
|
|
InvalidLength(usize),
|
|
|
|
/// Bad web socket op code
|
2018-04-14 01:02:01 +02:00
|
|
|
#[fail(display = "Bad web socket op code")]
|
2018-02-27 19:09:24 +01:00
|
|
|
BadOpCode,
|
|
|
|
/// A payload reached size limit.
|
2018-04-14 01:02:01 +02:00
|
|
|
#[fail(display = "A payload reached size limit.")]
|
2018-02-27 19:09:24 +01:00
|
|
|
Overflow,
|
2018-03-24 07:35:52 +01:00
|
|
|
/// Continuation is not supported
|
2018-04-14 01:02:01 +02:00
|
|
|
#[fail(display = "Continuation is not supported.")]
|
2018-02-27 19:09:24 +01:00
|
|
|
NoContinuation,
|
|
|
|
/// Bad utf-8 encoding
|
2018-04-14 01:02:01 +02:00
|
|
|
#[fail(display = "Bad utf-8 encoding.")]
|
2018-02-27 19:09:24 +01:00
|
|
|
BadEncoding,
|
|
|
|
/// Payload error
|
2018-04-14 01:02:01 +02:00
|
|
|
#[fail(display = "Payload error: {}", _0)]
|
2018-02-27 19:09:24 +01:00
|
|
|
Payload(#[cause] PayloadError),
|
|
|
|
}
|
|
|
|
|
2018-03-02 20:29:55 +01:00
|
|
|
impl ResponseError for ProtocolError {}
|
2018-02-27 19:09:24 +01:00
|
|
|
|
2018-03-02 20:29:55 +01:00
|
|
|
impl From<PayloadError> for ProtocolError {
|
|
|
|
fn from(err: PayloadError) -> ProtocolError {
|
|
|
|
ProtocolError::Payload(err)
|
2018-02-27 19:09:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Websocket handshake errors
|
|
|
|
#[derive(Fail, PartialEq, Debug)]
|
2018-03-02 20:29:55 +01:00
|
|
|
pub enum HandshakeError {
|
2018-02-27 19:09:24 +01:00
|
|
|
/// Only get method is allowed
|
2018-04-14 01:02:01 +02:00
|
|
|
#[fail(display = "Method not allowed")]
|
2018-02-27 19:09:24 +01:00
|
|
|
GetMethodRequired,
|
|
|
|
/// Upgrade header if not set to websocket
|
2018-04-14 01:02:01 +02:00
|
|
|
#[fail(display = "Websocket upgrade is expected")]
|
2018-02-27 19:09:24 +01:00
|
|
|
NoWebsocketUpgrade,
|
|
|
|
/// Connection header is not set to upgrade
|
2018-04-14 01:02:01 +02:00
|
|
|
#[fail(display = "Connection upgrade is expected")]
|
2018-02-27 19:09:24 +01:00
|
|
|
NoConnectionUpgrade,
|
|
|
|
/// Websocket version header is not set
|
2018-04-14 01:02:01 +02:00
|
|
|
#[fail(display = "Websocket version header is required")]
|
2018-02-27 19:09:24 +01:00
|
|
|
NoVersionHeader,
|
|
|
|
/// Unsupported websocket version
|
2018-04-14 01:02:01 +02:00
|
|
|
#[fail(display = "Unsupported version")]
|
2018-02-27 19:09:24 +01:00
|
|
|
UnsupportedVersion,
|
|
|
|
/// Websocket key is not set or wrong
|
2018-04-14 01:02:01 +02:00
|
|
|
#[fail(display = "Unknown websocket key")]
|
2018-02-27 19:09:24 +01:00
|
|
|
BadWebsocketKey,
|
|
|
|
}
|
|
|
|
|
2018-03-02 20:29:55 +01:00
|
|
|
impl ResponseError for HandshakeError {
|
2018-02-27 19:09:24 +01:00
|
|
|
fn error_response(&self) -> HttpResponse {
|
|
|
|
match *self {
|
2018-04-29 18:09:08 +02:00
|
|
|
HandshakeError::GetMethodRequired => HttpResponse::MethodNotAllowed()
|
|
|
|
.header(header::ALLOW, "GET")
|
|
|
|
.finish(),
|
2018-03-31 08:07:33 +02:00
|
|
|
HandshakeError::NoWebsocketUpgrade => HttpResponse::BadRequest()
|
2018-04-14 01:02:01 +02:00
|
|
|
.reason("No WebSocket UPGRADE header found")
|
|
|
|
.finish(),
|
2018-04-29 18:09:08 +02:00
|
|
|
HandshakeError::NoConnectionUpgrade => HttpResponse::BadRequest()
|
|
|
|
.reason("No CONNECTION upgrade")
|
|
|
|
.finish(),
|
2018-03-31 08:07:33 +02:00
|
|
|
HandshakeError::NoVersionHeader => HttpResponse::BadRequest()
|
2018-04-14 01:02:01 +02:00
|
|
|
.reason("Websocket version header is required")
|
|
|
|
.finish(),
|
2018-04-29 18:09:08 +02:00
|
|
|
HandshakeError::UnsupportedVersion => HttpResponse::BadRequest()
|
|
|
|
.reason("Unsupported version")
|
|
|
|
.finish(),
|
|
|
|
HandshakeError::BadWebsocketKey => HttpResponse::BadRequest()
|
|
|
|
.reason("Handshake error")
|
|
|
|
.finish(),
|
2018-02-27 19:09:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 08:59:57 +02:00
|
|
|
/// `WebSocket` Message
|
2018-02-12 21:17:30 +01:00
|
|
|
#[derive(Debug, PartialEq, Message)]
|
2017-10-08 06:48:00 +02:00
|
|
|
pub enum Message {
|
|
|
|
Text(String),
|
2018-01-10 19:12:34 +01:00
|
|
|
Binary(Binary),
|
2017-10-08 06:48:00 +02:00
|
|
|
Ping(String),
|
|
|
|
Pong(String),
|
2018-04-21 22:50:27 +02:00
|
|
|
Close(Option<CloseReason>),
|
2017-10-08 06:48:00 +02:00
|
|
|
}
|
|
|
|
|
2017-11-29 23:12:27 +01:00
|
|
|
/// Do websocket handshake and start actor
|
2018-02-25 09:21:45 +01:00
|
|
|
pub fn start<A, S>(req: HttpRequest<S>, actor: A) -> Result<HttpResponse, Error>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
A: Actor<Context = WebsocketContext<A, S>> + StreamHandler<Message, ProtocolError>,
|
|
|
|
S: 'static,
|
2017-11-29 19:31:24 +01:00
|
|
|
{
|
2018-01-01 02:26:32 +01:00
|
|
|
let mut resp = handshake(&req)?;
|
2018-02-27 00:26:27 +01:00
|
|
|
let stream = WsStream::new(req.clone());
|
2018-01-01 02:26:32 +01:00
|
|
|
|
2018-01-10 19:12:34 +01:00
|
|
|
let mut ctx = WebsocketContext::new(req, actor);
|
2018-02-27 19:09:24 +01:00
|
|
|
ctx.add_stream(stream);
|
2018-01-01 02:26:32 +01:00
|
|
|
|
2018-03-31 08:07:33 +02:00
|
|
|
Ok(resp.body(ctx))
|
2017-11-29 19:31:24 +01:00
|
|
|
}
|
|
|
|
|
2017-10-08 07:41:02 +02:00
|
|
|
/// Prepare `WebSocket` handshake response.
|
2017-10-08 06:48:00 +02:00
|
|
|
///
|
2017-10-08 08:59:57 +02:00
|
|
|
/// This function returns handshake `HttpResponse`, ready to send to peer.
|
2017-10-08 07:41:02 +02:00
|
|
|
/// It does not perform any IO.
|
2017-10-08 06:48:00 +02:00
|
|
|
///
|
2017-10-08 07:41:02 +02:00
|
|
|
// /// `protocols` is a sequence of known protocols. On successful handshake,
|
|
|
|
// /// the returned response headers contain the first protocol in this list
|
|
|
|
// /// which the server also knows.
|
2018-04-14 01:02:01 +02:00
|
|
|
pub fn handshake<S>(
|
2018-04-29 07:55:47 +02:00
|
|
|
req: &HttpRequest<S>,
|
2018-04-14 01:02:01 +02:00
|
|
|
) -> Result<HttpResponseBuilder, HandshakeError> {
|
2017-10-08 06:48:00 +02:00
|
|
|
// WebSocket accepts only GET
|
|
|
|
if *req.method() != Method::GET {
|
2018-04-14 01:02:01 +02:00
|
|
|
return Err(HandshakeError::GetMethodRequired);
|
2017-10-08 06:48:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for "UPGRADE" to websocket header
|
2017-10-10 08:07:32 +02:00
|
|
|
let has_hdr = if let Some(hdr) = req.headers().get(header::UPGRADE) {
|
|
|
|
if let Ok(s) = hdr.to_str() {
|
|
|
|
s.to_lowercase().contains("websocket")
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2017-10-08 06:48:00 +02:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
if !has_hdr {
|
2018-04-14 01:02:01 +02:00
|
|
|
return Err(HandshakeError::NoWebsocketUpgrade);
|
2017-10-08 06:48:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upgrade connection
|
2017-10-14 09:11:12 +02:00
|
|
|
if !req.upgrade() {
|
2018-04-14 01:02:01 +02:00
|
|
|
return Err(HandshakeError::NoConnectionUpgrade);
|
2017-10-08 06:48:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// check supported version
|
2018-04-29 18:09:08 +02:00
|
|
|
if !req.headers()
|
|
|
|
.contains_key(header::SEC_WEBSOCKET_VERSION)
|
|
|
|
{
|
2018-04-14 01:02:01 +02:00
|
|
|
return Err(HandshakeError::NoVersionHeader);
|
2017-10-08 06:48:00 +02:00
|
|
|
}
|
|
|
|
let supported_ver = {
|
2018-02-28 23:16:55 +01:00
|
|
|
if let Some(hdr) = req.headers().get(header::SEC_WEBSOCKET_VERSION) {
|
2017-10-10 08:07:32 +02:00
|
|
|
hdr == "13" || hdr == "8" || hdr == "7"
|
|
|
|
} else {
|
|
|
|
false
|
2017-10-08 06:48:00 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if !supported_ver {
|
2018-04-14 01:02:01 +02:00
|
|
|
return Err(HandshakeError::UnsupportedVersion);
|
2017-10-08 06:48:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// check client handshake for validity
|
2018-02-28 23:16:55 +01:00
|
|
|
if !req.headers().contains_key(header::SEC_WEBSOCKET_KEY) {
|
2018-04-14 01:02:01 +02:00
|
|
|
return Err(HandshakeError::BadWebsocketKey);
|
2017-10-10 08:07:32 +02:00
|
|
|
}
|
|
|
|
let key = {
|
2018-02-28 23:16:55 +01:00
|
|
|
let key = req.headers().get(header::SEC_WEBSOCKET_KEY).unwrap();
|
2018-03-09 22:03:15 +01:00
|
|
|
proto::hash_key(key.as_ref())
|
2017-10-08 06:48:00 +02:00
|
|
|
};
|
|
|
|
|
2017-11-27 07:31:29 +01:00
|
|
|
Ok(HttpResponse::build(StatusCode::SWITCHING_PROTOCOLS)
|
2018-04-14 01:02:01 +02:00
|
|
|
.connection_type(ConnectionType::Upgrade)
|
|
|
|
.header(header::UPGRADE, "websocket")
|
|
|
|
.header(header::TRANSFER_ENCODING, "chunked")
|
|
|
|
.header(header::SEC_WEBSOCKET_ACCEPT, key.as_str())
|
|
|
|
.take())
|
2017-10-08 06:48:00 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 07:41:02 +02:00
|
|
|
/// Maps `Payload` stream into stream of `ws::Message` items
|
2018-02-26 22:58:23 +01:00
|
|
|
pub struct WsStream<S> {
|
|
|
|
rx: PayloadHelper<S>,
|
2017-10-14 01:33:23 +02:00
|
|
|
closed: bool,
|
2018-02-27 19:09:24 +01:00
|
|
|
max_size: usize,
|
2017-10-08 06:48:00 +02:00
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
impl<S> WsStream<S>
|
|
|
|
where
|
|
|
|
S: Stream<Item = Bytes, Error = PayloadError>,
|
|
|
|
{
|
2018-02-27 19:09:24 +01:00
|
|
|
/// Create new websocket frames stream
|
2018-02-26 22:58:23 +01:00
|
|
|
pub fn new(stream: S) -> WsStream<S> {
|
2018-04-14 01:02:01 +02:00
|
|
|
WsStream {
|
|
|
|
rx: PayloadHelper::new(stream),
|
|
|
|
closed: false,
|
|
|
|
max_size: 65_536,
|
2018-02-27 19:09:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set max frame size
|
|
|
|
///
|
|
|
|
/// By default max size is set to 64kb
|
|
|
|
pub fn max_size(mut self, size: usize) -> Self {
|
|
|
|
self.max_size = size;
|
|
|
|
self
|
2017-10-08 06:48:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
impl<S> Stream for WsStream<S>
|
|
|
|
where
|
|
|
|
S: Stream<Item = Bytes, Error = PayloadError>,
|
|
|
|
{
|
2017-10-08 06:48:00 +02:00
|
|
|
type Item = Message;
|
2018-03-02 20:29:55 +01:00
|
|
|
type Error = ProtocolError;
|
2017-10-08 06:48:00 +02:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
2018-02-26 22:58:23 +01:00
|
|
|
if self.closed {
|
2018-04-14 01:02:01 +02:00
|
|
|
return Ok(Async::Ready(None));
|
2017-10-09 05:16:48 +02:00
|
|
|
}
|
2017-10-08 06:48:00 +02:00
|
|
|
|
2018-02-27 19:09:24 +01:00
|
|
|
match Frame::parse(&mut self.rx, true, self.max_size) {
|
2018-02-26 22:58:23 +01:00
|
|
|
Ok(Async::Ready(Some(frame))) => {
|
2018-02-27 19:09:24 +01:00
|
|
|
let (finished, opcode, payload) = frame.unpack();
|
|
|
|
|
|
|
|
// continuation is not supported
|
|
|
|
if !finished {
|
|
|
|
self.closed = true;
|
2018-04-14 01:02:01 +02:00
|
|
|
return Err(ProtocolError::NoContinuation);
|
2018-02-27 19:09:24 +01:00
|
|
|
}
|
2017-10-08 06:48:00 +02:00
|
|
|
|
2018-02-26 22:58:23 +01:00
|
|
|
match opcode {
|
2018-03-09 02:19:50 +01:00
|
|
|
OpCode::Continue => Err(ProtocolError::NoContinuation),
|
2018-02-27 19:09:24 +01:00
|
|
|
OpCode::Bad => {
|
|
|
|
self.closed = true;
|
2018-03-02 20:29:55 +01:00
|
|
|
Err(ProtocolError::BadOpCode)
|
2018-02-27 19:09:24 +01:00
|
|
|
}
|
2018-02-26 22:58:23 +01:00
|
|
|
OpCode::Close => {
|
|
|
|
self.closed = true;
|
2018-04-21 22:50:27 +02:00
|
|
|
let close_reason = Frame::parse_close_payload(&payload);
|
|
|
|
Ok(Async::Ready(Some(Message::Close(close_reason))))
|
2018-04-14 01:02:01 +02:00
|
|
|
}
|
|
|
|
OpCode::Ping => Ok(Async::Ready(Some(Message::Ping(
|
|
|
|
String::from_utf8_lossy(payload.as_ref()).into(),
|
|
|
|
)))),
|
|
|
|
OpCode::Pong => Ok(Async::Ready(Some(Message::Pong(
|
|
|
|
String::from_utf8_lossy(payload.as_ref()).into(),
|
|
|
|
)))),
|
|
|
|
OpCode::Binary => Ok(Async::Ready(Some(Message::Binary(payload)))),
|
2018-02-26 22:58:23 +01:00
|
|
|
OpCode::Text => {
|
|
|
|
let tmp = Vec::from(payload.as_ref());
|
|
|
|
match String::from_utf8(tmp) {
|
2018-04-14 01:02:01 +02:00
|
|
|
Ok(s) => Ok(Async::Ready(Some(Message::Text(s)))),
|
2018-03-20 19:23:35 +01:00
|
|
|
Err(_) => {
|
2018-02-27 19:09:24 +01:00
|
|
|
self.closed = true;
|
2018-03-02 20:29:55 +01:00
|
|
|
Err(ProtocolError::BadEncoding)
|
2018-02-27 19:09:24 +01:00
|
|
|
}
|
2017-10-14 01:33:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-08 06:48:00 +02:00
|
|
|
}
|
2018-02-26 22:58:23 +01:00
|
|
|
Ok(Async::Ready(None)) => Ok(Async::Ready(None)),
|
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
2018-02-27 19:09:24 +01:00
|
|
|
Err(e) => {
|
2018-02-26 22:58:23 +01:00
|
|
|
self.closed = true;
|
2018-02-27 19:09:24 +01:00
|
|
|
Err(e)
|
2018-02-26 22:58:23 +01:00
|
|
|
}
|
2017-10-08 06:48:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 02:33:24 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-11-16 07:06:28 +01:00
|
|
|
use super::*;
|
2018-04-14 01:02:01 +02:00
|
|
|
use http::{header, HeaderMap, Method, Uri, Version};
|
2017-12-01 04:01:25 +01:00
|
|
|
use std::str::FromStr;
|
2017-10-23 02:33:24 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_handshake() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let req = HttpRequest::new(
|
|
|
|
Method::POST,
|
|
|
|
Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11,
|
|
|
|
HeaderMap::new(),
|
|
|
|
None,
|
|
|
|
);
|
2018-04-29 18:09:08 +02:00
|
|
|
assert_eq!(
|
|
|
|
HandshakeError::GetMethodRequired,
|
|
|
|
handshake(&req).err().unwrap()
|
|
|
|
);
|
2018-04-14 01:02:01 +02:00
|
|
|
|
|
|
|
let req = HttpRequest::new(
|
|
|
|
Method::GET,
|
|
|
|
Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11,
|
|
|
|
HeaderMap::new(),
|
|
|
|
None,
|
|
|
|
);
|
2018-04-29 18:09:08 +02:00
|
|
|
assert_eq!(
|
|
|
|
HandshakeError::NoWebsocketUpgrade,
|
|
|
|
handshake(&req).err().unwrap()
|
|
|
|
);
|
2017-10-23 02:33:24 +02:00
|
|
|
|
|
|
|
let mut headers = HeaderMap::new();
|
2018-04-29 18:09:08 +02:00
|
|
|
headers.insert(
|
|
|
|
header::UPGRADE,
|
|
|
|
header::HeaderValue::from_static("test"),
|
|
|
|
);
|
2018-04-14 01:02:01 +02:00
|
|
|
let req = HttpRequest::new(
|
|
|
|
Method::GET,
|
|
|
|
Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11,
|
|
|
|
headers,
|
|
|
|
None,
|
|
|
|
);
|
2018-04-29 18:09:08 +02:00
|
|
|
assert_eq!(
|
|
|
|
HandshakeError::NoWebsocketUpgrade,
|
|
|
|
handshake(&req).err().unwrap()
|
|
|
|
);
|
2017-10-23 02:33:24 +02:00
|
|
|
|
|
|
|
let mut headers = HeaderMap::new();
|
2018-04-29 18:09:08 +02:00
|
|
|
headers.insert(
|
|
|
|
header::UPGRADE,
|
|
|
|
header::HeaderValue::from_static("websocket"),
|
|
|
|
);
|
2018-04-14 01:02:01 +02:00
|
|
|
let req = HttpRequest::new(
|
|
|
|
Method::GET,
|
|
|
|
Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11,
|
|
|
|
headers,
|
|
|
|
None,
|
|
|
|
);
|
2018-04-29 18:09:08 +02:00
|
|
|
assert_eq!(
|
|
|
|
HandshakeError::NoConnectionUpgrade,
|
|
|
|
handshake(&req).err().unwrap()
|
|
|
|
);
|
2017-10-23 02:33:24 +02:00
|
|
|
|
|
|
|
let mut headers = HeaderMap::new();
|
2018-04-29 18:09:08 +02:00
|
|
|
headers.insert(
|
|
|
|
header::UPGRADE,
|
|
|
|
header::HeaderValue::from_static("websocket"),
|
|
|
|
);
|
|
|
|
headers.insert(
|
|
|
|
header::CONNECTION,
|
|
|
|
header::HeaderValue::from_static("upgrade"),
|
|
|
|
);
|
2018-04-14 01:02:01 +02:00
|
|
|
let req = HttpRequest::new(
|
|
|
|
Method::GET,
|
|
|
|
Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11,
|
|
|
|
headers,
|
|
|
|
None,
|
|
|
|
);
|
2018-04-29 18:09:08 +02:00
|
|
|
assert_eq!(
|
|
|
|
HandshakeError::NoVersionHeader,
|
|
|
|
handshake(&req).err().unwrap()
|
|
|
|
);
|
2017-10-23 02:33:24 +02:00
|
|
|
|
|
|
|
let mut headers = HeaderMap::new();
|
2018-04-29 18:09:08 +02:00
|
|
|
headers.insert(
|
|
|
|
header::UPGRADE,
|
|
|
|
header::HeaderValue::from_static("websocket"),
|
|
|
|
);
|
|
|
|
headers.insert(
|
|
|
|
header::CONNECTION,
|
|
|
|
header::HeaderValue::from_static("upgrade"),
|
|
|
|
);
|
2018-04-14 01:02:01 +02:00
|
|
|
headers.insert(
|
|
|
|
header::SEC_WEBSOCKET_VERSION,
|
|
|
|
header::HeaderValue::from_static("5"),
|
|
|
|
);
|
|
|
|
let req = HttpRequest::new(
|
|
|
|
Method::GET,
|
|
|
|
Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11,
|
|
|
|
headers,
|
|
|
|
None,
|
|
|
|
);
|
2018-04-29 18:09:08 +02:00
|
|
|
assert_eq!(
|
|
|
|
HandshakeError::UnsupportedVersion,
|
|
|
|
handshake(&req).err().unwrap()
|
|
|
|
);
|
2017-10-23 02:33:24 +02:00
|
|
|
|
|
|
|
let mut headers = HeaderMap::new();
|
2018-04-29 18:09:08 +02:00
|
|
|
headers.insert(
|
|
|
|
header::UPGRADE,
|
|
|
|
header::HeaderValue::from_static("websocket"),
|
|
|
|
);
|
|
|
|
headers.insert(
|
|
|
|
header::CONNECTION,
|
|
|
|
header::HeaderValue::from_static("upgrade"),
|
|
|
|
);
|
2018-04-14 01:02:01 +02:00
|
|
|
headers.insert(
|
|
|
|
header::SEC_WEBSOCKET_VERSION,
|
|
|
|
header::HeaderValue::from_static("13"),
|
|
|
|
);
|
|
|
|
let req = HttpRequest::new(
|
|
|
|
Method::GET,
|
|
|
|
Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11,
|
|
|
|
headers,
|
|
|
|
None,
|
|
|
|
);
|
2018-04-29 18:09:08 +02:00
|
|
|
assert_eq!(
|
|
|
|
HandshakeError::BadWebsocketKey,
|
|
|
|
handshake(&req).err().unwrap()
|
|
|
|
);
|
2017-10-23 02:33:24 +02:00
|
|
|
|
|
|
|
let mut headers = HeaderMap::new();
|
2018-04-29 18:09:08 +02:00
|
|
|
headers.insert(
|
|
|
|
header::UPGRADE,
|
|
|
|
header::HeaderValue::from_static("websocket"),
|
|
|
|
);
|
|
|
|
headers.insert(
|
|
|
|
header::CONNECTION,
|
|
|
|
header::HeaderValue::from_static("upgrade"),
|
|
|
|
);
|
2018-04-14 01:02:01 +02:00
|
|
|
headers.insert(
|
|
|
|
header::SEC_WEBSOCKET_VERSION,
|
|
|
|
header::HeaderValue::from_static("13"),
|
|
|
|
);
|
2018-04-29 18:09:08 +02:00
|
|
|
headers.insert(
|
|
|
|
header::SEC_WEBSOCKET_KEY,
|
|
|
|
header::HeaderValue::from_static("13"),
|
|
|
|
);
|
2018-04-14 01:02:01 +02:00
|
|
|
let req = HttpRequest::new(
|
|
|
|
Method::GET,
|
|
|
|
Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11,
|
|
|
|
headers,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
StatusCode::SWITCHING_PROTOCOLS,
|
|
|
|
handshake(&req).unwrap().finish().status()
|
|
|
|
);
|
2017-10-23 02:33:24 +02:00
|
|
|
}
|
2018-02-27 19:09:24 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_wserror_http_response() {
|
2018-03-02 20:29:55 +01:00
|
|
|
let resp: HttpResponse = HandshakeError::GetMethodRequired.error_response();
|
2018-02-27 19:09:24 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
2018-03-02 20:29:55 +01:00
|
|
|
let resp: HttpResponse = HandshakeError::NoWebsocketUpgrade.error_response();
|
2018-02-27 19:09:24 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
2018-03-02 20:29:55 +01:00
|
|
|
let resp: HttpResponse = HandshakeError::NoConnectionUpgrade.error_response();
|
2018-02-27 19:09:24 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
2018-03-02 20:29:55 +01:00
|
|
|
let resp: HttpResponse = HandshakeError::NoVersionHeader.error_response();
|
2018-02-27 19:09:24 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
2018-03-02 20:29:55 +01:00
|
|
|
let resp: HttpResponse = HandshakeError::UnsupportedVersion.error_response();
|
2018-02-27 19:09:24 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
2018-03-02 20:29:55 +01:00
|
|
|
let resp: HttpResponse = HandshakeError::BadWebsocketKey.error_response();
|
2018-02-27 19:09:24 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
}
|
2017-10-23 02:33:24 +02:00
|
|
|
}
|