1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-12 02:09:36 +00:00

Expose the max limit for payload sizes in Websocket Actors. #925 (#933)

* Expose the max limit for payload sizes in Websocket Actors.

* Revert to previous not-formatted code.

* Implement WebsocketContext::with_codec and make Codec Copy and Clone.

* Fix formatting.

* Fix formatting.
This commit is contained in:
anthonyjchriste 2019-06-27 18:49:03 -10:00 committed by Nikolay Kim
parent 44bb79cd07
commit 768859513a
2 changed files with 26 additions and 9 deletions

View file

@ -37,7 +37,7 @@ pub enum Frame {
Close(Option<CloseReason>),
}
#[derive(Debug)]
#[derive(Debug, Copy, Clone)]
/// WebSockets protocol codec
pub struct Codec {
max_size: usize,

View file

@ -177,9 +177,26 @@ where
inner: ContextParts::new(mb.sender_producer()),
messages: VecDeque::new(),
};
ctx.add_stream(WsStream::new(stream));
ctx.add_stream(WsStream::new(stream, Codec::new()));
WebsocketContextFut::new(ctx, actor, mb)
WebsocketContextFut::new(ctx, actor, mb, Codec::new())
}
#[inline]
/// Create a new Websocket context from a request, an actor, and a codec
pub fn with_codec<S>(actor: A, stream: S, codec: Codec) -> 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, codec));
WebsocketContextFut::new(ctx, actor, mb, codec)
}
/// Create a new Websocket context
@ -197,11 +214,11 @@ where
inner: ContextParts::new(mb.sender_producer()),
messages: VecDeque::new(),
};
ctx.add_stream(WsStream::new(stream));
ctx.add_stream(WsStream::new(stream, Codec::new()));
let act = f(&mut ctx);
WebsocketContextFut::new(ctx, act, mb)
WebsocketContextFut::new(ctx, act, mb, Codec::new())
}
}
@ -288,11 +305,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>, codec: Codec) -> Self {
let fut = ContextFut::new(ctx, act, mailbox);
WebsocketContextFut {
fut,
encoder: Codec::new(),
encoder: codec,
buf: BytesMut::new(),
closed: false,
}
@ -353,10 +370,10 @@ impl<S> WsStream<S>
where
S: Stream<Item = Bytes, Error = PayloadError>,
{
fn new(stream: S) -> Self {
fn new(stream: S, codec: Codec) -> Self {
Self {
stream,
decoder: Codec::new(),
decoder: codec,
buf: BytesMut::new(),
closed: false,
}