@@ -14,6 +14,10 @@ use crate::ws;
14
14
use crate :: ws:: receiver:: Receiver as ReceiverTrait ;
15
15
use crate :: ws:: receiver:: { DataFrameIterator , MessageIterator } ;
16
16
17
+ const DEFAULT_MAX_DATAFRAME_SIZE : usize =1024 * 1024 * 100 ;
18
+ const DEFAULT_MAX_MESSAGE_SIZE : usize =1024 * 1024 * 200 ;
19
+ const MAX_DATAFRAMES_IN_ONE_MESSAGE : usize =1024 * 1024 ;
20
+
17
21
/// This reader bundles an existing stream with a parsing algorithm.
18
22
/// It is used by the client in its `.split()` function as the reading component.
19
23
pub struct Reader < R >
@@ -74,14 +78,33 @@ where
74
78
pub struct Receiver {
75
79
buffer : Vec < DataFrame > ,
76
80
mask : bool ,
81
+ // u32s instead uf usizes to economize used memory by this struct
82
+ max_dataframe_size : u32 ,
83
+ max_message_size : u32 ,
77
84
}
78
85
79
86
impl Receiver {
80
87
/// Create a new Receiver using the specified Reader.
88
+ ///
89
+ /// Uses built-in limits for dataframe and message sizes.
81
90
pub fn new ( mask : bool ) ->Receiver {
91
+ Receiver :: new_with_limits ( mask, DEFAULT_MAX_DATAFRAME_SIZE , DEFAULT_MAX_MESSAGE_SIZE )
92
+ }
93
+
94
+ /// Create a new Receiver using the specified Reader, with configurable limits
95
+ ///
96
+ /// Sizes should not be larger than `u32::MAX`.
97
+ ///
98
+ /// Note that `max_message_size` denotes message size where no new dataframes would be read,
99
+ /// so actual maximum message size is larger.
100
+ pub fn new_with_limits ( mask : bool , max_dataframe_size : usize , max_message_size : usize ) ->Receiver {
101
+ let max_dataframe_size: u32 = max_dataframe_size. min ( u32:: MAX as usize ) as u32 ;
102
+ let max_message_size: u32 = max_message_size. min ( u32:: MAX as usize ) as u32 ;
82
103
Receiver {
83
104
buffer : Vec :: new ( ) ,
84
105
mask,
106
+ max_dataframe_size,
107
+ max_message_size,
85
108
}
86
109
}
87
110
}
@@ -96,14 +119,15 @@ impl ws::Receiver for Receiver {
96
119
where
97
120
R : Read ,
98
121
{
99
- DataFrame :: read_dataframe ( reader, self . mask )
122
+ DataFrame :: read_dataframe_with_limit ( reader, self . mask , self . max_dataframe_size as usize )
100
123
}
101
124
102
125
/// Returns the data frames that constitute one message.
103
126
fn recv_message_dataframes < R > ( & mut self , reader : & mut R ) ->WebSocketResult < Vec < DataFrame > >
104
127
where
105
128
R : Read ,
106
129
{
130
+ let mut current_message_length: usize =self . buffer . iter ( ) . map ( |x|x. data . len ( ) ) . sum ( ) ;
107
131
let mut finished =if self . buffer . is_empty ( ) {
108
132
let first =self . recv_dataframe ( reader) ?;
109
133
@@ -114,6 +138,7 @@ impl ws::Receiver for Receiver {
114
138
}
115
139
116
140
let finished = first. finished ;
141
+ current_message_length += first. data . len ( ) ;
117
142
self . buffer . push ( first) ;
118
143
finished
119
144
} else {
@@ -126,7 +151,10 @@ impl ws::Receiver for Receiver {
126
151
127
152
match next. opcode as u8 {
128
153
// Continuation opcode
129
- 0 =>self . buffer . push ( next) ,
154
+ 0 =>{
155
+ current_message_length += next. data . len ( ) ;
156
+ self . buffer . push ( next)
157
+ }
130
158
// Control frame
131
159
8 ..=15 =>{
132
160
return Ok ( vec ! [ next] ) ;
@@ -138,6 +166,19 @@ impl ws::Receiver for Receiver {
138
166
) ) ;
139
167
}
140
168
}
169
+
170
+ if !finished{
171
+ if self . buffer . len ( ) >=MAX_DATAFRAMES_IN_ONE_MESSAGE {
172
+ return Err ( WebSocketError :: ProtocolError (
173
+ "Exceeded count of data frames in one WebSocket message" ,
174
+ ) ) ;
175
+ }
176
+ if current_message_length >=self . max_message_size as usize {
177
+ return Err ( WebSocketError :: ProtocolError (
178
+ "Exceeded maximum WebSocket message size" ,
179
+ ) ) ;
180
+ }
181
+ }
141
182
}
142
183
143
184
Ok ( :: std:: mem:: replace ( & mut self . buffer , Vec :: new ( ) ) )