1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-24 22:37:35 +02:00

add client websockets support

This commit is contained in:
Nikolay Kim
2019-03-27 18:53:19 -07:00
parent e254fe4f9c
commit c59937784e
19 changed files with 709 additions and 536 deletions

View File

@ -1,8 +1,12 @@
use std::io;
use actix_codec::{AsyncRead, AsyncWrite, Framed};
use actix_http::body::Body;
use actix_http::client::{ConnectError, Connection, SendRequestError};
use actix_http::{http, RequestHead};
use actix_http::h1::ClientCodec;
use actix_http::{http, RequestHead, ResponseHead};
use actix_service::Service;
use futures::Future;
use futures::{Future, Poll};
use crate::response::ClientResponse;
@ -14,13 +18,26 @@ pub(crate) trait Connect {
head: RequestHead,
body: Body,
) -> Box<Future<Item = ClientResponse, Error = SendRequestError>>;
/// Send request, returns Response and Framed
fn open_tunnel(
&mut self,
head: RequestHead,
) -> Box<
Future<
Item = (ResponseHead, Framed<BoxedSocket, ClientCodec>),
Error = SendRequestError,
>,
>;
}
impl<T> Connect for ConnectorWrapper<T>
where
T: Service<Request = http::Uri, Error = ConnectError>,
T::Response: Connection,
<T::Response as Connection>::Io: 'static,
<T::Response as Connection>::Future: 'static,
<T::Response as Connection>::TunnelFuture: 'static,
T::Future: 'static,
{
fn send_request(
@ -38,4 +55,77 @@ where
.map(|(head, payload)| ClientResponse::new(head, payload)),
)
}
fn open_tunnel(
&mut self,
head: RequestHead,
) -> Box<
Future<
Item = (ResponseHead, Framed<BoxedSocket, ClientCodec>),
Error = SendRequestError,
>,
> {
Box::new(
self.0
// connect to the host
.call(head.uri.clone())
.from_err()
// send request
.and_then(move |connection| connection.open_tunnel(head))
.map(|(head, framed)| {
let framed = framed.map_io(|io| BoxedSocket(Box::new(Socket(io))));
(head, framed)
}),
)
}
}
trait AsyncSocket {
fn as_read(&self) -> &AsyncRead;
fn as_read_mut(&mut self) -> &mut AsyncRead;
fn as_write(&mut self) -> &mut AsyncWrite;
}
struct Socket<T: AsyncRead + AsyncWrite>(T);
impl<T: AsyncRead + AsyncWrite> AsyncSocket for Socket<T> {
fn as_read(&self) -> &AsyncRead {
&self.0
}
fn as_read_mut(&mut self) -> &mut AsyncRead {
&mut self.0
}
fn as_write(&mut self) -> &mut AsyncWrite {
&mut self.0
}
}
pub struct BoxedSocket(Box<dyn AsyncSocket>);
impl io::Read for BoxedSocket {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.as_read_mut().read(buf)
}
}
impl AsyncRead for BoxedSocket {
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
self.0.as_read().prepare_uninitialized_buffer(buf)
}
}
impl io::Write for BoxedSocket {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.as_write().write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.0.as_write().flush()
}
}
impl AsyncWrite for BoxedSocket {
fn shutdown(&mut self) -> Poll<(), io::Error> {
self.0.as_write().shutdown()
}
}

48
awc/src/error.rs Normal file
View File

@ -0,0 +1,48 @@
//! Http client errors
pub use actix_http::client::{ConnectError, InvalidUrl, SendRequestError};
pub use actix_http::error::PayloadError;
pub use actix_http::ws::ProtocolError as WsProtocolError;
use actix_http::http::{header::HeaderValue, Error as HttpError, StatusCode};
use derive_more::{Display, From};
/// Websocket client error
#[derive(Debug, Display, From)]
pub enum WsClientError {
/// Invalid response status
#[display(fmt = "Invalid response status")]
InvalidResponseStatus(StatusCode),
/// Invalid upgrade header
#[display(fmt = "Invalid upgrade header")]
InvalidUpgradeHeader,
/// Invalid connection header
#[display(fmt = "Invalid connection header")]
InvalidConnectionHeader(HeaderValue),
/// Missing CONNECTION header
#[display(fmt = "Missing CONNECTION header")]
MissingConnectionHeader,
/// Missing SEC-WEBSOCKET-ACCEPT header
#[display(fmt = "Missing SEC-WEBSOCKET-ACCEPT header")]
MissingWebSocketAcceptHeader,
/// Invalid challenge response
#[display(fmt = "Invalid challenge response")]
InvalidChallengeResponse(String, HeaderValue),
/// Protocol error
#[display(fmt = "{}", _0)]
Protocol(WsProtocolError),
/// Send request error
#[display(fmt = "{}", _0)]
SendRequest(SendRequestError),
}
impl From<InvalidUrl> for WsClientError {
fn from(err: InvalidUrl) -> Self {
WsClientError::SendRequest(err.into())
}
}
impl From<HttpError> for WsClientError {
fn from(err: HttpError) -> Self {
WsClientError::SendRequest(err.into())
}
}

View File

@ -23,8 +23,6 @@
use std::cell::RefCell;
use std::rc::Rc;
pub use actix_http::client::{ConnectError, InvalidUrl, SendRequestError};
pub use actix_http::error::PayloadError;
pub use actix_http::http;
use actix_http::client::Connector;
@ -33,13 +31,16 @@ use actix_http::RequestHead;
mod builder;
mod connect;
pub mod error;
mod request;
mod response;
pub mod test;
mod ws;
pub use self::builder::ClientBuilder;
pub use self::request::ClientRequest;
pub use self::response::ClientResponse;
pub use self::ws::WebsocketsRequest;
use self::connect::{Connect, ConnectorWrapper};
@ -165,4 +166,11 @@ impl Client {
{
ClientRequest::new(Method::OPTIONS, url, self.connector.clone())
}
pub fn ws<U>(&self, url: U) -> WebsocketsRequest
where
Uri: HttpTryFrom<U>,
{
WebsocketsRequest::new(url, self.connector.clone())
}
}

View File

@ -12,7 +12,6 @@ use serde::Serialize;
use serde_json;
use actix_http::body::{Body, BodyStream};
use actix_http::client::{InvalidUrl, SendRequestError};
use actix_http::encoding::Decoder;
use actix_http::http::header::{self, ContentEncoding, Header, IntoHeaderValue};
use actix_http::http::{
@ -21,8 +20,9 @@ use actix_http::http::{
};
use actix_http::{Error, Payload, RequestHead};
use crate::connect::Connect;
use crate::error::{InvalidUrl, PayloadError, SendRequestError};
use crate::response::ClientResponse;
use crate::{Connect, PayloadError};
#[cfg(any(feature = "brotli", feature = "flate2-zlib", feature = "flate2-rust"))]
const HTTPS_ENCODING: &str = "br, gzip, deflate";

398
awc/src/ws.rs Normal file
View File

@ -0,0 +1,398 @@
//! Websockets client
use std::cell::RefCell;
use std::io::Write;
use std::rc::Rc;
use std::{fmt, str};
use actix_codec::Framed;
use actix_http::{ws, Payload, RequestHead};
use bytes::{BufMut, BytesMut};
#[cfg(feature = "cookies")]
use cookie::{Cookie, CookieJar};
use futures::future::{err, Either, Future};
use crate::connect::{BoxedSocket, Connect};
use crate::error::{InvalidUrl, WsClientError};
use crate::http::header::{
self, HeaderName, HeaderValue, IntoHeaderValue, AUTHORIZATION,
};
use crate::http::{
ConnectionType, Error as HttpError, HttpTryFrom, Method, StatusCode, Uri, Version,
};
use crate::response::ClientResponse;
/// `WebSocket` connection
pub struct WebsocketsRequest {
head: RequestHead,
err: Option<HttpError>,
origin: Option<HeaderValue>,
protocols: Option<String>,
max_size: usize,
server_mode: bool,
default_headers: bool,
#[cfg(feature = "cookies")]
cookies: Option<CookieJar>,
connector: Rc<RefCell<dyn Connect>>,
}
impl WebsocketsRequest {
/// Create new websocket connection
pub(crate) fn new<U>(uri: U, connector: Rc<RefCell<dyn Connect>>) -> Self
where
Uri: HttpTryFrom<U>,
{
let mut err = None;
let mut head = RequestHead::default();
head.method = Method::GET;
head.version = Version::HTTP_11;
match Uri::try_from(uri) {
Ok(uri) => head.uri = uri,
Err(e) => err = Some(e.into()),
}
WebsocketsRequest {
head,
err,
connector,
origin: None,
protocols: None,
max_size: 65_536,
server_mode: false,
#[cfg(feature = "cookies")]
cookies: None,
default_headers: true,
}
}
/// Set supported websocket protocols
pub fn protocols<U, V>(mut self, protos: U) -> Self
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
}
#[cfg(feature = "cookies")]
/// Set a cookie
pub fn cookie<'c>(mut self, cookie: Cookie<'c>) -> Self {
if self.cookies.is_none() {
let mut jar = CookieJar::new();
jar.add(cookie.into_owned());
self.cookies = Some(jar)
} else {
self.cookies.as_mut().unwrap().add(cookie.into_owned());
}
self
}
/// Set request Origin
pub fn origin<V>(mut self, origin: V) -> Self
where
HeaderValue: HttpTryFrom<V>,
{
match HeaderValue::try_from(origin) {
Ok(value) => self.origin = Some(value),
Err(e) => self.err = Some(e.into()),
}
self
}
/// Set max frame size
///
/// By default max size is set to 64kb
pub fn max_frame_size(mut self, size: usize) -> Self {
self.max_size = size;
self
}
/// Disable payload masking. By default ws client masks frame payload.
pub fn server_mode(mut self) -> Self {
self.server_mode = true;
self
}
/// Do not add default request headers.
/// By default `Date` and `User-Agent` headers are set.
pub fn no_default_headers(mut self) -> Self {
self.default_headers = false;
self
}
/// Append a header.
///
/// Header gets appended to existing header.
/// To override header use `set_header()` method.
pub fn header<K, V>(mut self, key: K, value: V) -> Self
where
HeaderName: HttpTryFrom<K>,
V: IntoHeaderValue,
{
match HeaderName::try_from(key) {
Ok(key) => match value.try_into() {
Ok(value) => {
self.head.headers.append(key, value);
}
Err(e) => self.err = Some(e.into()),
},
Err(e) => self.err = Some(e.into()),
}
self
}
/// Insert a header, replaces existing header.
pub fn set_header<K, V>(mut self, key: K, value: V) -> Self
where
HeaderName: HttpTryFrom<K>,
V: IntoHeaderValue,
{
match HeaderName::try_from(key) {
Ok(key) => match value.try_into() {
Ok(value) => {
self.head.headers.insert(key, value);
}
Err(e) => self.err = Some(e.into()),
},
Err(e) => self.err = Some(e.into()),
}
self
}
/// Insert a header only if it is not yet set.
pub fn set_header_if_none<K, V>(mut self, key: K, value: V) -> Self
where
HeaderName: HttpTryFrom<K>,
V: IntoHeaderValue,
{
match HeaderName::try_from(key) {
Ok(key) => {
if !self.head.headers.contains_key(&key) {
match value.try_into() {
Ok(value) => {
self.head.headers.insert(key, value);
}
Err(e) => self.err = Some(e.into()),
}
}
}
Err(e) => self.err = Some(e.into()),
}
self
}
/// Set HTTP basic authorization header
pub fn basic_auth<U, P>(self, username: U, password: Option<P>) -> Self
where
U: fmt::Display,
P: fmt::Display,
{
let auth = match password {
Some(password) => format!("{}:{}", username, password),
None => format!("{}", username),
};
self.header(AUTHORIZATION, format!("Basic {}", base64::encode(&auth)))
}
/// Set HTTP bearer authentication header
pub fn bearer_auth<T>(self, token: T) -> Self
where
T: fmt::Display,
{
self.header(AUTHORIZATION, format!("Bearer {}", token))
}
/// Complete request construction and connect.
pub fn connect(
mut self,
) -> impl Future<
Item = (ClientResponse, Framed<BoxedSocket, ws::Codec>),
Error = WsClientError,
> {
if let Some(e) = self.err.take() {
return Either::A(err(e.into()));
}
// validate uri
let uri = &self.head.uri;
if uri.host().is_none() {
return Either::A(err(InvalidUrl::MissingHost.into()));
} else if uri.scheme_part().is_none() {
return Either::A(err(InvalidUrl::MissingScheme.into()));
} else if let Some(scheme) = uri.scheme_part() {
match scheme.as_str() {
"http" | "ws" | "https" | "wss" => (),
_ => return Either::A(err(InvalidUrl::UnknownScheme.into())),
}
} else {
return Either::A(err(InvalidUrl::UnknownScheme.into()));
}
// set default headers
let mut slf = if self.default_headers {
// set request host header
if let Some(host) = self.head.uri.host() {
if !self.head.headers.contains_key(header::HOST) {
let mut wrt = BytesMut::with_capacity(host.len() + 5).writer();
let _ = match self.head.uri.port_u16() {
None | Some(80) | Some(443) => write!(wrt, "{}", host),
Some(port) => write!(wrt, "{}:{}", host, port),
};
match wrt.get_mut().take().freeze().try_into() {
Ok(value) => {
self.head.headers.insert(header::HOST, value);
}
Err(e) => return Either::A(err(HttpError::from(e).into())),
}
}
}
// user agent
self.set_header_if_none(
header::USER_AGENT,
concat!("awc/", env!("CARGO_PKG_VERSION")),
)
} else {
self
};
#[allow(unused_mut)]
let mut head = slf.head;
#[cfg(feature = "cookies")]
{
use percent_encoding::{percent_encode, USERINFO_ENCODE_SET};
use std::fmt::Write;
// set cookies
if let Some(ref mut jar) = slf.cookies {
let mut cookie = String::new();
for c in jar.delta() {
let name = percent_encode(c.name().as_bytes(), USERINFO_ENCODE_SET);
let value =
percent_encode(c.value().as_bytes(), USERINFO_ENCODE_SET);
let _ = write!(&mut cookie, "; {}={}", name, value);
}
head.headers.insert(
header::COOKIE,
HeaderValue::from_str(&cookie.as_str()[2..]).unwrap(),
);
}
}
// origin
if let Some(origin) = slf.origin.take() {
head.headers.insert(header::ORIGIN, origin);
}
head.set_connection_type(ConnectionType::Upgrade);
head.headers
.insert(header::UPGRADE, HeaderValue::from_static("websocket"));
head.headers.insert(
header::SEC_WEBSOCKET_VERSION,
HeaderValue::from_static("13"),
);
if let Some(protocols) = slf.protocols.take() {
head.headers.insert(
header::SEC_WEBSOCKET_PROTOCOL,
HeaderValue::try_from(protocols.as_str()).unwrap(),
);
}
// 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);
head.headers.insert(
header::SEC_WEBSOCKET_KEY,
HeaderValue::try_from(key.as_str()).unwrap(),
);
let max_size = slf.max_size;
let server_mode = slf.server_mode;
let fut = slf
.connector
.borrow_mut()
.open_tunnel(head)
.from_err()
.and_then(move |(head, framed)| {
// verify response
if head.status != StatusCode::SWITCHING_PROTOCOLS {
return Err(WsClientError::InvalidResponseStatus(head.status));
}
// Check for "UPGRADE" to websocket header
let has_hdr = if let Some(hdr) = head.headers.get(header::UPGRADE) {
if let Ok(s) = hdr.to_str() {
s.to_ascii_lowercase().contains("websocket")
} else {
false
}
} else {
false
};
if !has_hdr {
log::trace!("Invalid upgrade header");
return Err(WsClientError::InvalidUpgradeHeader);
}
// Check for "CONNECTION" header
if let Some(conn) = head.headers.get(header::CONNECTION) {
if let Ok(s) = conn.to_str() {
if !s.to_ascii_lowercase().contains("upgrade") {
log::trace!("Invalid connection header: {}", s);
return Err(WsClientError::InvalidConnectionHeader(
conn.clone(),
));
}
} else {
log::trace!("Invalid connection header: {:?}", conn);
return Err(WsClientError::InvalidConnectionHeader(conn.clone()));
}
} else {
log::trace!("Missing connection header");
return Err(WsClientError::MissingConnectionHeader);
}
if let Some(hdr_key) = head.headers.get(header::SEC_WEBSOCKET_ACCEPT) {
let encoded = ws::hash_key(key.as_ref());
if hdr_key.as_bytes() != encoded.as_bytes() {
log::trace!(
"Invalid challenge response: expected: {} received: {:?}",
encoded,
key
);
return Err(WsClientError::InvalidChallengeResponse(
encoded,
hdr_key.clone(),
));
}
} else {
log::trace!("Missing SEC-WEBSOCKET-ACCEPT header");
return Err(WsClientError::MissingWebSocketAcceptHeader);
};
// response and ws framed
Ok((
ClientResponse::new(head, Payload::None),
framed.map_codec(|_| {
if server_mode {
ws::Codec::new().max_size(max_size)
} else {
ws::Codec::new().max_size(max_size).client_mode()
}
}),
))
});
Either::B(fut)
}
}