28
28
import io .rsocket .RSocket ;
29
29
import io .rsocket .exceptions .ConnectionErrorException ;
30
30
import io .rsocket .exceptions .Exceptions ;
31
+ import io .rsocket .fragmentation .FragmentationUtils ;
31
32
import io .rsocket .frame .CancelFrameFlyweight ;
32
33
import io .rsocket .frame .ErrorFrameFlyweight ;
33
34
import io .rsocket .frame .FrameHeaderFlyweight ;
@@ -88,6 +89,7 @@ class RSocketRequester implements RSocket {
88
89
private final IntObjectMap <Subscription > senders ;
89
90
private final IntObjectMap <Processor <Payload , Payload >> receivers ;
90
91
private final UnboundedProcessor <ByteBuf > sendProcessor ;
92
+ private final int mtu ;
91
93
private final RequesterLeaseHandler leaseHandler ;
92
94
private final ByteBufAllocator allocator ;
93
95
private final KeepAliveFramesAcceptor keepAliveFramesAcceptor ;
@@ -99,6 +101,7 @@ class RSocketRequester implements RSocket {
99
101
PayloadDecoder payloadDecoder ,
100
102
Consumer <Throwable > errorConsumer ,
101
103
StreamIdSupplier streamIdSupplier ,
104
+ int mtu ,
102
105
int keepAliveTickPeriod ,
103
106
int keepAliveAckTimeout ,
104
107
@ Nullable KeepAliveHandler keepAliveHandler ,
@@ -108,6 +111,7 @@ class RSocketRequester implements RSocket {
108
111
this .payloadDecoder = payloadDecoder ;
109
112
this .errorConsumer = errorConsumer ;
110
113
this .streamIdSupplier = streamIdSupplier ;
114
+ this .mtu = mtu ;
111
115
this .leaseHandler = leaseHandler ;
112
116
this .senders = new SynchronizedIntObjectHashMap <>();
113
117
this .receivers = new SynchronizedIntObjectHashMap <>();
@@ -186,6 +190,11 @@ private Mono<Void> handleFireAndForget(Payload payload) {
186
190
return Mono .error (err );
187
191
}
188
192
193
+ if (!FragmentationUtils .isValid (this .mtu , payload )) {
194
+ payload .release ();
195
+ return Mono .error (new IllegalArgumentException ("Too big Payload size" ));
196
+ }
197
+
189
198
final int streamId = streamIdSupplier .nextStreamId (receivers );
190
199
191
200
return UnicastMonoEmpty .newInstance (
@@ -210,6 +219,11 @@ private Mono<Payload> handleRequestResponse(final Payload payload) {
210
219
return Mono .error (err );
211
220
}
212
221
222
+ if (!FragmentationUtils .isValid (this .mtu , payload )) {
223
+ payload .release ();
224
+ return Mono .error (new IllegalArgumentException ("Too big Payload size" ));
225
+ }
226
+
213
227
int streamId = streamIdSupplier .nextStreamId (receivers );
214
228
final UnboundedProcessor <ByteBuf > sendProcessor = this .sendProcessor ;
215
229
@@ -255,6 +269,11 @@ private Flux<Payload> handleRequestStream(final Payload payload) {
255
269
return Flux .error (err );
256
270
}
257
271
272
+ if (!FragmentationUtils .isValid (this .mtu , payload )) {
273
+ payload .release ();
274
+ return Flux .error (new IllegalArgumentException ("Too big Payload size" ));
275
+ }
276
+
258
277
int streamId = streamIdSupplier .nextStreamId (receivers );
259
278
260
279
final UnboundedProcessor <ByteBuf > sendProcessor = this .sendProcessor ;
@@ -317,6 +336,12 @@ private Flux<Payload> handleChannel(Flux<Payload> request) {
317
336
(s , flux ) -> {
318
337
Payload payload = s .get ();
319
338
if (payload != null ) {
339
+ if (!FragmentationUtils .isValid (mtu , payload )) {
340
+ payload .release ();
341
+ final IllegalArgumentException t = new IllegalArgumentException ("Too big Payload size" );
342
+ errorConsumer .accept (t );
343
+ return Mono .error (t );
344
+ }
320
345
return handleChannel (payload , flux .skip (1 ));
321
346
} else {
322
347
return flux ;
@@ -341,6 +366,16 @@ protected void hookOnSubscribe(Subscription subscription) {
341
366
342
367
@ Override
343
368
protected void hookOnNext (Payload payload ) {
369
+ if (!FragmentationUtils .isValid (mtu , payload )) {
370
+ payload .release ();
371
+ cancel ();
372
+ final IllegalArgumentException t = new IllegalArgumentException ("Too big Payload size" );
373
+ errorConsumer .accept (t );
374
+ // no need to send any errors.
375
+ sendProcessor .onNext (CancelFrameFlyweight .encode (allocator , streamId ));
376
+ receiver .onError (t );
377
+ return ;
378
+ }
344
379
final ByteBuf frame =
345
380
PayloadFrameFlyweight .encode (allocator , streamId , false , false , true , payload );
346
381
0 commit comments