2018-01-28 07:03:03 +01:00
|
|
|
//! Http client request
|
2018-01-30 08:01:20 +01:00
|
|
|
#![allow(unused_imports, dead_code)]
|
2018-01-31 00:13:33 +01:00
|
|
|
use std::{fmt, io, str};
|
2018-01-28 07:03:03 +01:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::time::Duration;
|
|
|
|
use std::cell::UnsafeCell;
|
|
|
|
|
|
|
|
use base64;
|
|
|
|
use rand;
|
2018-01-29 20:39:26 +01:00
|
|
|
use cookie::Cookie;
|
2018-02-26 22:58:23 +01:00
|
|
|
use bytes::{Bytes, BytesMut};
|
2018-01-29 20:39:26 +01:00
|
|
|
use http::{HttpTryFrom, StatusCode, Error as HttpError};
|
2018-01-28 07:03:03 +01:00
|
|
|
use http::header::{self, HeaderName, HeaderValue};
|
|
|
|
use sha1::Sha1;
|
|
|
|
use futures::{Async, Future, Poll, Stream};
|
2018-01-30 08:01:20 +01:00
|
|
|
use futures::future::{Either, err as FutErr};
|
2018-02-26 22:58:23 +01:00
|
|
|
use futures::unsync::mpsc::{unbounded, UnboundedSender};
|
2018-01-28 07:03:03 +01:00
|
|
|
use tokio_core::net::TcpStream;
|
2018-02-26 22:58:23 +01:00
|
|
|
use byteorder::{ByteOrder, NetworkEndian};
|
2018-01-28 07:03:03 +01:00
|
|
|
|
2018-01-30 08:01:20 +01:00
|
|
|
use actix::prelude::*;
|
|
|
|
|
2018-02-26 22:58:23 +01:00
|
|
|
use body::{Body, Binary};
|
|
|
|
use error::{WsError, UrlParseError};
|
|
|
|
use payload::PayloadHelper;
|
2018-01-28 07:03:03 +01:00
|
|
|
use server::shared::SharedBytes;
|
|
|
|
|
|
|
|
use server::{utils, IoStream};
|
2018-02-26 22:58:23 +01:00
|
|
|
use client::{ClientRequest, ClientRequestBuilder, ClientResponse,
|
2018-01-29 23:44:25 +01:00
|
|
|
HttpResponseParser, HttpResponseParserError, HttpClientWriter};
|
2018-02-26 22:58:23 +01:00
|
|
|
use client::{Connect, Connection, ClientConnector, ClientConnectorError,
|
|
|
|
SendRequest, SendRequestError};
|
2018-01-28 07:03:03 +01:00
|
|
|
|
|
|
|
use super::Message;
|
2018-02-10 09:05:20 +01:00
|
|
|
use super::frame::Frame;
|
2018-01-28 07:03:03 +01:00
|
|
|
use super::proto::{CloseCode, OpCode};
|
|
|
|
|
2018-01-30 00:45:37 +01:00
|
|
|
|
2018-01-31 18:28:53 +01:00
|
|
|
/// Websocket client error
|
2018-01-28 07:03:03 +01:00
|
|
|
#[derive(Fail, Debug)]
|
|
|
|
pub enum WsClientError {
|
|
|
|
#[fail(display="Invalid url")]
|
|
|
|
InvalidUrl,
|
|
|
|
#[fail(display="Invalid response status")]
|
|
|
|
InvalidResponseStatus,
|
|
|
|
#[fail(display="Invalid upgrade header")]
|
|
|
|
InvalidUpgradeHeader,
|
|
|
|
#[fail(display="Invalid connection header")]
|
|
|
|
InvalidConnectionHeader,
|
|
|
|
#[fail(display="Invalid challenge response")]
|
|
|
|
InvalidChallengeResponse,
|
|
|
|
#[fail(display="Http parsing error")]
|
|
|
|
Http(HttpError),
|
|
|
|
#[fail(display="Url parsing error")]
|
|
|
|
Url(UrlParseError),
|
|
|
|
#[fail(display="Response parsing error")]
|
|
|
|
ResponseParseError(HttpResponseParserError),
|
|
|
|
#[fail(display="{}", _0)]
|
2018-02-26 22:58:23 +01:00
|
|
|
SendRequest(SendRequestError),
|
|
|
|
#[fail(display="{}", _0)]
|
|
|
|
Protocol(#[cause] WsError),
|
2018-01-28 07:03:03 +01:00
|
|
|
#[fail(display="{}", _0)]
|
|
|
|
Io(io::Error),
|
|
|
|
#[fail(display="Disconnected")]
|
|
|
|
Disconnected,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<HttpError> for WsClientError {
|
|
|
|
fn from(err: HttpError) -> WsClientError {
|
|
|
|
WsClientError::Http(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<UrlParseError> for WsClientError {
|
|
|
|
fn from(err: UrlParseError) -> WsClientError {
|
|
|
|
WsClientError::Url(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-26 22:58:23 +01:00
|
|
|
impl From<SendRequestError> for WsClientError {
|
|
|
|
fn from(err: SendRequestError) -> WsClientError {
|
|
|
|
WsClientError::SendRequest(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<WsError> for WsClientError {
|
|
|
|
fn from(err: WsError) -> WsClientError {
|
|
|
|
WsClientError::Protocol(err)
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for WsClientError {
|
|
|
|
fn from(err: io::Error) -> WsClientError {
|
|
|
|
WsClientError::Io(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<HttpResponseParserError> for WsClientError {
|
|
|
|
fn from(err: HttpResponseParserError) -> WsClientError {
|
|
|
|
WsClientError::ResponseParseError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 02:20:28 +01:00
|
|
|
/// `WebSocket` client
|
2018-01-31 00:26:58 +01:00
|
|
|
///
|
2018-02-10 02:20:28 +01:00
|
|
|
/// Example of `WebSocket` client usage is available in
|
2018-01-31 00:26:58 +01:00
|
|
|
/// [websocket example](
|
|
|
|
/// https://github.com/actix/actix-web/blob/master/examples/websocket/src/client.rs#L24)
|
2018-01-28 07:03:03 +01:00
|
|
|
pub struct WsClient {
|
2018-01-29 20:39:26 +01:00
|
|
|
request: ClientRequestBuilder,
|
2018-01-28 07:03:03 +01:00
|
|
|
err: Option<WsClientError>,
|
|
|
|
http_err: Option<HttpError>,
|
|
|
|
origin: Option<HeaderValue>,
|
|
|
|
protocols: Option<String>,
|
2018-02-13 01:08:04 +01:00
|
|
|
conn: Addr<Unsync, ClientConnector>,
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WsClient {
|
|
|
|
|
2018-01-30 20:17:17 +01:00
|
|
|
/// Create new websocket connection
|
2018-01-29 20:39:26 +01:00
|
|
|
pub fn new<S: AsRef<str>>(uri: S) -> WsClient {
|
2018-01-30 20:17:17 +01:00
|
|
|
WsClient::with_connector(uri, ClientConnector::from_registry())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create new websocket connection with custom `ClientConnector`
|
2018-02-13 01:08:04 +01:00
|
|
|
pub fn with_connector<S: AsRef<str>>(uri: S, conn: Addr<Unsync, ClientConnector>) -> WsClient {
|
2018-01-28 07:03:03 +01:00
|
|
|
let mut cl = WsClient {
|
2018-01-29 20:39:26 +01:00
|
|
|
request: ClientRequest::build(),
|
2018-01-28 07:03:03 +01:00
|
|
|
err: None,
|
|
|
|
http_err: None,
|
|
|
|
origin: None,
|
2018-01-30 20:17:17 +01:00
|
|
|
protocols: None,
|
2018-02-26 23:33:56 +01:00
|
|
|
conn,
|
2018-01-30 20:17:17 +01:00
|
|
|
};
|
2018-01-29 20:39:26 +01:00
|
|
|
cl.request.uri(uri.as_ref());
|
2018-01-28 07:03:03 +01:00
|
|
|
cl
|
|
|
|
}
|
|
|
|
|
2018-01-30 20:17:17 +01:00
|
|
|
/// Set supported websocket protocols
|
2018-02-24 05:36:50 +01:00
|
|
|
pub fn protocols<U, V>(mut self, protos: U) -> Self
|
2018-01-28 07:03:03 +01:00
|
|
|
where U: IntoIterator<Item=V> + 'static,
|
|
|
|
V: AsRef<str>
|
|
|
|
{
|
|
|
|
let mut protos = protos.into_iter()
|
|
|
|
.fold(String::new(), |acc, s| {acc + s.as_ref() + ","});
|
|
|
|
protos.pop();
|
|
|
|
self.protocols = Some(protos);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-01-30 20:17:17 +01:00
|
|
|
/// Set cookie for handshake request
|
2018-02-24 06:14:21 +01:00
|
|
|
pub fn cookie(mut self, cookie: Cookie) -> Self {
|
2018-01-29 20:39:26 +01:00
|
|
|
self.request.cookie(cookie);
|
2018-01-28 07:03:03 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set request Origin
|
2018-02-24 05:36:50 +01:00
|
|
|
pub fn origin<V>(mut self, origin: V) -> Self
|
2018-01-28 07:03:03 +01:00
|
|
|
where HeaderValue: HttpTryFrom<V>
|
|
|
|
{
|
|
|
|
match HeaderValue::try_from(origin) {
|
|
|
|
Ok(value) => self.origin = Some(value),
|
|
|
|
Err(e) => self.http_err = Some(e.into()),
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-01-30 20:17:17 +01:00
|
|
|
/// Set request header
|
2018-02-24 05:36:50 +01:00
|
|
|
pub fn header<K, V>(mut self, key: K, value: V) -> Self
|
2018-01-29 20:39:26 +01:00
|
|
|
where HeaderName: HttpTryFrom<K>, HeaderValue: HttpTryFrom<V>
|
2018-01-28 07:03:03 +01:00
|
|
|
{
|
2018-01-29 20:39:26 +01:00
|
|
|
self.request.header(key, value);
|
2018-01-28 07:03:03 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-01-30 20:17:17 +01:00
|
|
|
/// Connect to websocket server and do ws handshake
|
2018-02-24 06:14:21 +01:00
|
|
|
pub fn connect(&mut self) -> WsHandshake {
|
2018-01-28 07:03:03 +01:00
|
|
|
if let Some(e) = self.err.take() {
|
2018-02-24 06:14:21 +01:00
|
|
|
WsHandshake::new(None, Some(e), &self.conn)
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
2018-02-24 06:14:21 +01:00
|
|
|
else if let Some(e) = self.http_err.take() {
|
|
|
|
WsHandshake::new(None, Some(e.into()), &self.conn)
|
|
|
|
} else {
|
|
|
|
// origin
|
|
|
|
if let Some(origin) = self.origin.take() {
|
|
|
|
self.request.set_header(header::ORIGIN, origin);
|
|
|
|
}
|
2018-01-28 07:03:03 +01:00
|
|
|
|
2018-02-24 06:14:21 +01:00
|
|
|
self.request.upgrade();
|
|
|
|
self.request.set_header(header::UPGRADE, "websocket");
|
|
|
|
self.request.set_header(header::CONNECTION, "upgrade");
|
|
|
|
self.request.set_header("SEC-WEBSOCKET-VERSION", "13");
|
2018-01-28 07:03:03 +01:00
|
|
|
|
2018-02-24 06:14:21 +01:00
|
|
|
if let Some(protocols) = self.protocols.take() {
|
|
|
|
self.request.set_header("SEC-WEBSOCKET-PROTOCOL", protocols.as_str());
|
|
|
|
}
|
|
|
|
let request = match self.request.finish() {
|
|
|
|
Ok(req) => req,
|
|
|
|
Err(err) => return WsHandshake::new(None, Some(err.into()), &self.conn),
|
|
|
|
};
|
2018-01-29 20:39:26 +01:00
|
|
|
|
2018-02-24 06:14:21 +01:00
|
|
|
if request.uri().host().is_none() {
|
|
|
|
return WsHandshake::new(None, Some(WsClientError::InvalidUrl), &self.conn)
|
|
|
|
}
|
|
|
|
if let Some(scheme) = request.uri().scheme_part() {
|
|
|
|
if scheme != "http" && scheme != "https" && scheme != "ws" && scheme != "wss" {
|
|
|
|
return WsHandshake::new(
|
|
|
|
None, Some(WsClientError::InvalidUrl), &self.conn)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return WsHandshake::new(None, Some(WsClientError::InvalidUrl), &self.conn)
|
2018-01-29 20:39:26 +01:00
|
|
|
}
|
2018-01-28 07:03:03 +01:00
|
|
|
|
2018-02-24 06:14:21 +01:00
|
|
|
// start handshake
|
|
|
|
WsHandshake::new(Some(request), None, &self.conn)
|
|
|
|
}
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 08:01:20 +01:00
|
|
|
struct WsInner {
|
2018-02-26 22:58:23 +01:00
|
|
|
tx: UnboundedSender<Bytes>,
|
|
|
|
rx: PayloadHelper<ClientResponse>,
|
2018-01-28 07:03:03 +01:00
|
|
|
closed: bool,
|
|
|
|
}
|
|
|
|
|
2018-02-24 06:14:21 +01:00
|
|
|
pub struct WsHandshake {
|
2018-01-30 08:01:20 +01:00
|
|
|
inner: Option<WsInner>,
|
2018-02-26 22:58:23 +01:00
|
|
|
request: Option<SendRequest>,
|
|
|
|
tx: Option<UnboundedSender<Bytes>>,
|
2018-01-28 07:03:03 +01:00
|
|
|
key: String,
|
2018-02-24 06:14:21 +01:00
|
|
|
error: Option<WsClientError>,
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 08:01:20 +01:00
|
|
|
impl WsHandshake {
|
2018-02-24 06:14:21 +01:00
|
|
|
fn new(request: Option<ClientRequest>,
|
|
|
|
err: Option<WsClientError>,
|
|
|
|
conn: &Addr<Unsync, ClientConnector>) -> WsHandshake
|
|
|
|
{
|
2018-01-28 07:03:03 +01:00
|
|
|
// Generate a random key for the `Sec-WebSocket-Key` header.
|
|
|
|
// a base64-encoded (see Section 4 of [RFC4648]) value that,
|
|
|
|
// when decoded, is 16 bytes in length (RFC 6455)
|
|
|
|
let sec_key: [u8; 16] = rand::random();
|
|
|
|
let key = base64::encode(&sec_key);
|
|
|
|
|
2018-02-24 06:14:21 +01:00
|
|
|
if let Some(mut request) = request {
|
|
|
|
request.headers_mut().insert(
|
|
|
|
HeaderName::try_from("SEC-WEBSOCKET-KEY").unwrap(),
|
|
|
|
HeaderValue::try_from(key.as_str()).unwrap());
|
|
|
|
|
2018-02-26 22:58:23 +01:00
|
|
|
let (tx, rx) = unbounded();
|
|
|
|
request.set_body(Body::Streaming(
|
|
|
|
Box::new(rx.map_err(|_| io::Error::new(
|
|
|
|
io::ErrorKind::Other, "disconnected").into()))));
|
|
|
|
|
2018-02-24 06:14:21 +01:00
|
|
|
WsHandshake {
|
2018-02-26 23:33:56 +01:00
|
|
|
key,
|
2018-02-24 06:14:21 +01:00
|
|
|
inner: None,
|
2018-02-26 22:58:23 +01:00
|
|
|
request: Some(request.with_connector(conn.clone())),
|
|
|
|
tx: Some(tx),
|
2018-02-24 06:14:21 +01:00
|
|
|
error: err,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
WsHandshake {
|
2018-02-26 23:33:56 +01:00
|
|
|
key,
|
2018-02-24 06:14:21 +01:00
|
|
|
inner: None,
|
|
|
|
request: None,
|
2018-02-26 22:58:23 +01:00
|
|
|
tx: None,
|
2018-02-24 06:14:21 +01:00
|
|
|
error: err,
|
|
|
|
}
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 08:01:20 +01:00
|
|
|
impl Future for WsHandshake {
|
|
|
|
type Item = (WsClientReader, WsClientWriter);
|
2018-01-28 07:03:03 +01:00
|
|
|
type Error = WsClientError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2018-02-24 06:14:21 +01:00
|
|
|
if let Some(err) = self.error.take() {
|
|
|
|
return Err(err)
|
|
|
|
}
|
|
|
|
|
2018-02-26 22:58:23 +01:00
|
|
|
let resp = match self.request.as_mut().unwrap().poll()? {
|
|
|
|
Async::Ready(response) => {
|
|
|
|
self.request.take();
|
|
|
|
response
|
|
|
|
},
|
|
|
|
Async::NotReady => return Ok(Async::NotReady)
|
|
|
|
};
|
2018-01-28 07:03:03 +01:00
|
|
|
|
2018-02-26 22:58:23 +01:00
|
|
|
// verify response
|
|
|
|
if resp.status() != StatusCode::SWITCHING_PROTOCOLS {
|
|
|
|
return Err(WsClientError::InvalidResponseStatus)
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
2018-02-26 22:58:23 +01:00
|
|
|
// Check for "UPGRADE" to websocket header
|
|
|
|
let has_hdr = if let Some(hdr) = resp.headers().get(header::UPGRADE) {
|
|
|
|
if let Ok(s) = hdr.to_str() {
|
|
|
|
s.to_lowercase().contains("websocket")
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
if !has_hdr {
|
|
|
|
return Err(WsClientError::InvalidUpgradeHeader)
|
|
|
|
}
|
|
|
|
// Check for "CONNECTION" header
|
|
|
|
let has_hdr = if let Some(conn) = resp.headers().get(header::CONNECTION) {
|
|
|
|
if let Ok(s) = conn.to_str() {
|
|
|
|
s.to_lowercase().contains("upgrade")
|
|
|
|
} else { false }
|
|
|
|
} else { false };
|
|
|
|
if !has_hdr {
|
|
|
|
return Err(WsClientError::InvalidConnectionHeader)
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
|
2018-02-26 22:58:23 +01:00
|
|
|
let match_key = if let Some(key) = resp.headers().get(
|
|
|
|
HeaderName::try_from("SEC-WEBSOCKET-ACCEPT").unwrap())
|
|
|
|
{
|
|
|
|
// field is constructed by concatenating /key/
|
|
|
|
// with the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" (RFC 6455)
|
|
|
|
const WS_GUID: &[u8] = b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
|
|
|
let mut sha1 = Sha1::new();
|
|
|
|
sha1.update(self.key.as_ref());
|
|
|
|
sha1.update(WS_GUID);
|
|
|
|
key.as_bytes() == base64::encode(&sha1.digest().bytes()).as_bytes()
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
if !match_key {
|
|
|
|
return Err(WsClientError::InvalidChallengeResponse)
|
|
|
|
}
|
2018-01-28 07:03:03 +01:00
|
|
|
|
2018-02-26 22:58:23 +01:00
|
|
|
let inner = WsInner {
|
|
|
|
tx: self.tx.take().unwrap(),
|
|
|
|
rx: PayloadHelper::new(resp),
|
|
|
|
closed: false,
|
|
|
|
};
|
2018-01-28 07:03:03 +01:00
|
|
|
|
2018-02-26 22:58:23 +01:00
|
|
|
let inner = Rc::new(UnsafeCell::new(inner));
|
|
|
|
Ok(Async::Ready(
|
2018-02-26 23:33:56 +01:00
|
|
|
(WsClientReader{inner: Rc::clone(&inner)}, WsClientWriter{inner})))
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-30 08:01:20 +01:00
|
|
|
pub struct WsClientReader {
|
2018-02-10 02:20:28 +01:00
|
|
|
inner: Rc<UnsafeCell<WsInner>>
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 00:13:33 +01:00
|
|
|
impl fmt::Debug for WsClientReader {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "WsClientReader()")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 08:01:20 +01:00
|
|
|
impl WsClientReader {
|
2018-01-28 07:03:03 +01:00
|
|
|
#[inline]
|
2018-02-10 02:20:28 +01:00
|
|
|
fn as_mut(&mut self) -> &mut WsInner {
|
2018-01-28 07:03:03 +01:00
|
|
|
unsafe{ &mut *self.inner.get() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 08:01:20 +01:00
|
|
|
impl Stream for WsClientReader {
|
2018-01-28 07:03:03 +01:00
|
|
|
type Item = Message;
|
|
|
|
type Error = WsClientError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
|
|
|
let inner = self.as_mut();
|
2018-02-26 22:58:23 +01:00
|
|
|
if inner.closed {
|
|
|
|
return Ok(Async::Ready(None))
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// read
|
2018-02-26 22:58:23 +01:00
|
|
|
match Frame::parse(&mut inner.rx, false) {
|
|
|
|
Ok(Async::Ready(Some(frame))) => {
|
2018-01-28 07:03:03 +01:00
|
|
|
// trace!("WsFrame {}", frame);
|
|
|
|
let (_finished, opcode, payload) = frame.unpack();
|
|
|
|
|
|
|
|
match opcode {
|
|
|
|
OpCode::Continue => unimplemented!(),
|
|
|
|
OpCode::Bad =>
|
|
|
|
Ok(Async::Ready(Some(Message::Error))),
|
|
|
|
OpCode::Close => {
|
2018-02-10 02:20:28 +01:00
|
|
|
inner.closed = true;
|
2018-02-26 22:58:23 +01:00
|
|
|
let code = NetworkEndian::read_uint(payload.as_ref(), 2) as u16;
|
|
|
|
Ok(Async::Ready(
|
|
|
|
Some(Message::Close(CloseCode::from(code)))))
|
2018-01-28 07:03:03 +01: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)))),
|
|
|
|
OpCode::Text => {
|
|
|
|
let tmp = Vec::from(payload.as_ref());
|
|
|
|
match String::from_utf8(tmp) {
|
|
|
|
Ok(s) =>
|
|
|
|
Ok(Async::Ready(Some(Message::Text(s)))),
|
|
|
|
Err(_) =>
|
|
|
|
Ok(Async::Ready(Some(Message::Error))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-26 22:58:23 +01:00
|
|
|
Ok(Async::Ready(None)) => Ok(Async::Ready(None)),
|
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
2018-01-28 07:03:03 +01:00
|
|
|
Err(err) => {
|
2018-02-10 02:20:28 +01:00
|
|
|
inner.closed = true;
|
2018-01-28 07:03:03 +01:00
|
|
|
Err(err.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 08:01:20 +01:00
|
|
|
pub struct WsClientWriter {
|
2018-02-10 02:20:28 +01:00
|
|
|
inner: Rc<UnsafeCell<WsInner>>
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 08:01:20 +01:00
|
|
|
impl WsClientWriter {
|
2018-01-28 07:03:03 +01:00
|
|
|
#[inline]
|
2018-02-10 02:20:28 +01:00
|
|
|
fn as_mut(&mut self) -> &mut WsInner {
|
2018-01-28 07:03:03 +01:00
|
|
|
unsafe{ &mut *self.inner.get() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 08:01:20 +01:00
|
|
|
impl WsClientWriter {
|
2018-01-28 07:03:03 +01:00
|
|
|
|
|
|
|
/// Write payload
|
|
|
|
#[inline]
|
2018-02-26 22:58:23 +01:00
|
|
|
fn write(&mut self, mut data: Binary) {
|
2018-02-10 02:20:28 +01:00
|
|
|
if !self.as_mut().closed {
|
2018-02-26 22:58:23 +01:00
|
|
|
let _ = self.as_mut().tx.unbounded_send(data.take());
|
2018-01-28 07:03:03 +01:00
|
|
|
} else {
|
|
|
|
warn!("Trying to write to disconnected response");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send text frame
|
2018-02-10 05:43:14 +01:00
|
|
|
#[inline]
|
2018-02-10 09:05:20 +01:00
|
|
|
pub fn text<T: Into<String>>(&mut self, text: T) {
|
2018-02-20 07:48:27 +01:00
|
|
|
self.write(Frame::message(text.into(), OpCode::Text, true, true));
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send binary frame
|
2018-02-10 05:43:14 +01:00
|
|
|
#[inline]
|
2018-01-28 07:03:03 +01:00
|
|
|
pub fn binary<B: Into<Binary>>(&mut self, data: B) {
|
2018-02-20 07:48:27 +01:00
|
|
|
self.write(Frame::message(data, OpCode::Binary, true, true));
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send ping frame
|
2018-02-10 05:43:14 +01:00
|
|
|
#[inline]
|
2018-01-28 07:03:03 +01:00
|
|
|
pub fn ping(&mut self, message: &str) {
|
2018-02-20 07:48:27 +01:00
|
|
|
self.write(Frame::message(Vec::from(message), OpCode::Ping, true, true));
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send pong frame
|
2018-02-10 05:43:14 +01:00
|
|
|
#[inline]
|
2018-01-28 07:03:03 +01:00
|
|
|
pub fn pong(&mut self, message: &str) {
|
2018-02-20 07:48:27 +01:00
|
|
|
self.write(Frame::message(Vec::from(message), OpCode::Pong, true, true));
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send close frame
|
2018-02-10 05:43:14 +01:00
|
|
|
#[inline]
|
2018-01-28 07:03:03 +01:00
|
|
|
pub fn close(&mut self, code: CloseCode, reason: &str) {
|
2018-02-20 07:48:27 +01:00
|
|
|
self.write(Frame::close(code, reason, true));
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
}
|