1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2025-04-26 19:34:43 +00:00

Expose the max limit for payload sizes in Websocket Actors.

This commit is contained in:
opq 2019-06-21 12:26:30 -10:00
parent ad0e6f73b3
commit a21f64087d

View file

@ -35,6 +35,16 @@ where
Ok(res.streaming(WebsocketContext::create(actor, stream)))
}
/// Do websocket handshake and start ws actor with provided limit.
pub fn start_with_limit<A, T>(actor: A, req: &HttpRequest, stream: T, limit: usize) -> Result<HttpResponse, Error>
where
A: Actor<Context = WebsocketContext<A>> + StreamHandler<Message, ProtocolError>,
T: Stream<Item = Bytes, Error = PayloadError> + 'static,
{
let mut res = handshake(req)?;
Ok(res.streaming(WebsocketContext::create_with_limit(actor, stream, limit)))
}
/// Prepare `WebSocket` handshake response.
///
/// This function returns handshake `HttpResponse`, ready to send to peer.
@ -171,15 +181,25 @@ where
where
A: StreamHandler<Message, ProtocolError>,
S: Stream<Item = Bytes, Error = PayloadError> + 'static,
{
WebsocketContext::create_with_limit(actor, stream, 65_536)
}
#[inline]
/// Create a new Websocket context from a request, an actor, and a limit
pub fn create_with_limit<S>(actor: A, stream: S, limit: usize) -> impl Stream<Item = Bytes, Error = Error>
where
A: StreamHandler<Message, ProtocolError>,
S: Stream<Item = Bytes, Error = PayloadError> + 'static,
{
let mb = Mailbox::default();
let mut ctx = WebsocketContext {
inner: ContextParts::new(mb.sender_producer()),
messages: VecDeque::new(),
};
ctx.add_stream(WsStream::new(stream));
ctx.add_stream(WsStream::new(stream, limit));
WebsocketContextFut::new(ctx, actor, mb)
WebsocketContextFut::new(ctx, actor, mb, limit)
}
/// Create a new Websocket context
@ -191,17 +211,31 @@ where
F: FnOnce(&mut Self) -> A + 'static,
A: StreamHandler<Message, ProtocolError>,
S: Stream<Item = Bytes, Error = PayloadError> + 'static,
{
WebsocketContext::with_factory_codec(stream, f, 65_536)
}
/// Create a new Websocket context with a limit
pub fn with_factory_codec<S, F>(
stream: S,
f: F,
limit: usize,
) -> impl Stream<Item = Bytes, Error = Error>
where
F: FnOnce(&mut Self) -> A + 'static,
A: StreamHandler<Message, ProtocolError>,
S: Stream<Item = Bytes, Error = PayloadError> + 'static,
{
let mb = Mailbox::default();
let mut ctx = WebsocketContext {
inner: ContextParts::new(mb.sender_producer()),
messages: VecDeque::new(),
};
ctx.add_stream(WsStream::new(stream));
ctx.add_stream(WsStream::new(stream, limit));
let act = f(&mut ctx);
WebsocketContextFut::new(ctx, act, mb)
WebsocketContextFut::new(ctx, act, mb, limit)
}
}
@ -288,11 +322,11 @@ impl<A> WebsocketContextFut<A>
where
A: Actor<Context = WebsocketContext<A>>,
{
fn new(ctx: WebsocketContext<A>, act: A, mailbox: Mailbox<A>) -> Self {
fn new(ctx: WebsocketContext<A>, act: A, mailbox: Mailbox<A>, limit: usize) -> Self {
let fut = ContextFut::new(ctx, act, mailbox);
WebsocketContextFut {
fut,
encoder: Codec::new(),
encoder: Codec::new().max_size(limit),
buf: BytesMut::new(),
closed: false,
}
@ -353,10 +387,10 @@ impl<S> WsStream<S>
where
S: Stream<Item = Bytes, Error = PayloadError>,
{
fn new(stream: S) -> Self {
fn new(stream: S, limit: usize) -> Self {
Self {
stream,
decoder: Codec::new(),
decoder: Codec::new().max_size(limit),
buf: BytesMut::new(),
closed: false,
}