-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathStreamImpl.cs
1051 lines (963 loc) · 42.1 KB
/
StreamImpl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Http2.Hpack;
using Http2.Internal;
namespace Http2
{
/// <summary>
/// Implementation of a HTTP/2 stream
/// </summary>
internal class StreamImpl : IStream
{
/// <summary>The ID of the stream</summary>
public uint Id { get; }
/// <summary>Returns the current state of the stream</summary>
public StreamState State
{
get
{
lock (stateMutex)
{
return this.state;
}
}
}
private readonly Connection connection;
private StreamState state;
private readonly object stateMutex = new object();
/// Allows only a single write at a time
private SemaphoreSlim writeMutex = new SemaphoreSlim(1);
private bool headersSent = false;
private bool dataSent = false;
private enum HeaderReceptionState : byte
{
ReceivedNoHeaders,
ReceivedInformationalHeaders,
ReceivedAllHeaders,
}
private HeaderReceptionState headersReceived =
HeaderReceptionState.ReceivedNoHeaders;
private bool dataReceived = false;
// A trailersReceived field is not necessary, since receiving trailers
// moves the state to HalfClosedRemote
private List<HeaderField> inHeaders;
private List<HeaderField> inTrailers;
// Semaphores for unblocking async access operations
private readonly AsyncManualResetEvent readDataPossible =
new AsyncManualResetEvent(false);
private readonly AsyncManualResetEvent readHeadersPossible =
new AsyncManualResetEvent(false);
private readonly AsyncManualResetEvent readTrailersPossible =
new AsyncManualResetEvent(false);
private long declaredInContentLength = -1;
private long totalInData = 0;
private int totalReceiveWindow;
private int receiveWindow;
/// Intrusive linked list item for receive buffer queue
private class ReceiveQueueItem
{
public byte[] Buffer;
public int Offset;
public int Count;
public ReceiveQueueItem Next;
public ReceiveQueueItem(ArraySegment<byte> segment)
{
this.Buffer = segment.Array;
this.Offset = segment.Offset;
this.Count = segment.Count;
}
}
private ReceiveQueueItem receiveQueueHead = null;
private int ReceiveQueueLength
{
get
{
int len = 0;
var item = receiveQueueHead;
while (item != null)
{
len += item.Count;
item = item.Next;
}
return len;
}
}
/// Reusable empty list of headers
private static readonly HeaderField[] EmptyHeaders = new HeaderField[0];
public StreamImpl(
Connection connection,
uint streamId,
StreamState state,
int receiveWindow)
{
this.connection = connection;
this.Id = streamId;
// In case we are on the server side and the client opens a stream
// the expected state is Open and we get headers.
// TODO: Or are we only Idle and get headers soon after that?
// In case we are on the client side we are idle and need
// to send headers before doing anything.
this.state = state;
this.receiveWindow = receiveWindow;
this.totalReceiveWindow = receiveWindow;
}
private async Task SendHeaders(
IEnumerable<HeaderField> headers, bool endOfStream)
{
var fhh = new FrameHeader {
StreamId = this.Id,
Type = FrameType.Headers,
// EndOfHeaders will be auto-set
Flags = endOfStream ? (byte)HeadersFrameFlags.EndOfStream : (byte)0,
};
var res = await connection.writer.WriteHeaders(fhh, headers);
if (res != ConnectionWriter.WriteResult.Success)
{
// TODO: Improve the exception
throw new Exception("Can not write to stream");
}
}
public Task WriteHeadersAsync(
IEnumerable<HeaderField> headers, bool endOfStream)
{
HeaderValidationResult hvr;
// TODO: For push promises other validates might need to be used
if (connection.IsServer) hvr = HeaderValidator.ValidateResponseHeaders(headers);
else hvr = HeaderValidator.ValidateRequestHeaders(headers);
if (hvr != HeaderValidationResult.Ok)
{
throw new Exception(hvr.ToString());
}
return WriteValidatedHeadersAsync(headers, endOfStream);
}
internal async Task WriteValidatedHeadersAsync(
IEnumerable<HeaderField> headers, bool endOfStream)
{
var removeStream = false;
await writeMutex.WaitAsync();
try
{
// Check what already has been sent
lock (stateMutex)
{
// Check if data has already been sent.
// Headers may be only sent in front of all data.
if (dataSent)
{
throw new Exception("Attempted to write headers after data");
}
if (headersSent)
{
// TODO: Allow for multiple header packets or not?
// It seems it is required for informational headers to work
// However we might check later on if a status code different
// from 100 was already sent and if yes don't allow further
// headers to be sent.
}
headersSent = true;
switch (state)
{
case StreamState.Idle:
state = StreamState.Open;
break;
case StreamState.ReservedLocal:
state = StreamState.HalfClosedRemote;
break;
case StreamState.Reset:
throw new StreamResetException();
// TODO: Check if the other stream states are covered
// by the dataSent/headersSent logic.
// At least in order for a stream to be closed headers
// need to be sent. With push promises it might be different
}
if (state == StreamState.Open && endOfStream)
{
state = StreamState.HalfClosedLocal;
}
else if (state == StreamState.HalfClosedRemote && endOfStream)
{
state = StreamState.Closed;
removeStream = true;
}
}
await SendHeaders(headers, endOfStream); // TODO: Use result
}
finally
{
writeMutex.Release();
if (removeStream)
{
connection.UnregisterStream(this);
}
}
}
public async Task WriteTrailersAsync(IEnumerable<HeaderField> headers)
{
HeaderValidationResult hvr = HeaderValidator.ValidateTrailingHeaders(headers);
// TODO: For push promises other validates might need to be used
if (hvr != HeaderValidationResult.Ok)
{
throw new Exception(hvr.ToString());
}
var removeStream = false;
await writeMutex.WaitAsync();
try
{
// Check what already has been sent
lock (stateMutex)
{
if (!dataSent)
{
throw new Exception("Attempted to write trailers without data");
}
switch (state)
{
case StreamState.Open:
state = StreamState.HalfClosedLocal;
break;
case StreamState.HalfClosedRemote:
state = StreamState.HalfClosedRemote;
state = StreamState.Closed;
removeStream = true;
break;
case StreamState.Idle:
case StreamState.ReservedRemote:
case StreamState.HalfClosedLocal:
case StreamState.Closed:
throw new Exception("Invalid state for sending trailers");
case StreamState.Reset:
throw new StreamResetException();
case StreamState.ReservedLocal:
// We can't be in here if we already have data sent
throw new Exception("Unexpected state: ReservedLocal after data sent");
}
}
await SendHeaders(headers, true); // TODO: Use result
}
finally
{
writeMutex.Release();
if (removeStream)
{
connection.UnregisterStream(this);
}
}
}
public void Cancel()
{
var writeResetTask = Reset(ErrorCode.Cancel, false);
// We don't really need to care about this task.
// Even if it fails the stream will be reset anyway internally.
// And failing most likely occured because of a dead connection.
// The only helpful thing could be attaching a continuation for
// logging
}
public void Dispose()
{
// Disposing a stream is equivalent to resetting it
Cancel();
}
internal ValueTask<ConnectionWriter.WriteResult> Reset(
ErrorCode errorCode, bool fromRemote)
{
ValueTask<ConnectionWriter.WriteResult> writeResetTask =
new ValueTask<ConnectionWriter.WriteResult>(
ConnectionWriter.WriteResult.Success);
lock (stateMutex)
{
if (state == StreamState.Reset || state == StreamState.Closed)
{
// Already reset or fully closed
return writeResetTask;
}
state = StreamState.Reset;
// Free the receive queue
var head = receiveQueueHead;
receiveQueueHead = null;
FreeReceiveQueue(head);
}
if (connection.logger != null)
{
connection.logger.LogTrace(
"Resetted stream {0} with error code {1}",
Id, errorCode);
}
// Remark: Even if we are here in IDLE state we need to send the
// RESET frame. The reason for this is that if we receive a header
// for a new stream which is invalid a StreamImpl instance will be
// created and put into IDLE state. The header processing will fail
// and Reset will get called. As the remote thinks we are in Open
// state we must send a RST_STREAM.
if (!fromRemote)
{
// Send a reset frame with the given error code
var fh = new FrameHeader
{
StreamId = this.Id,
Type = FrameType.ResetStream,
Flags = 0,
};
var resetData = new ResetFrameData
{
ErrorCode = errorCode
};
writeResetTask = connection.writer.WriteResetStream(fh, resetData);
}
else
{
// If we don't send a notification we still have to unregister
// from the writer in order to cancel pending writes
connection.writer.RemoveStream(this.Id);
}
// Unregister from the connection
// If this has happened from the remote side the connection will
// already have performed this
if (!fromRemote)
{
this.connection.UnregisterStream(this);
}
// Unblock all waiters
readDataPossible.Set();
readTrailersPossible.Set();
readHeadersPossible.Set();
return writeResetTask;
}
public async ValueTask<StreamReadResult> ReadAsync(ArraySegment<byte> buffer)
{
while (true)
{
await readDataPossible;
int windowUpdateAmount = 0;
StreamReadResult result = new StreamReadResult();
bool hasResult = false;
lock (stateMutex)
{
if (state == StreamState.Reset)
{
throw new StreamResetException();
}
var streamClosedFromRemote =
state == StreamState.Closed || state == StreamState.HalfClosedRemote;
if (receiveQueueHead != null)
{
// Copy as much data as possible from internal queue into
// user buffer
var offset = buffer.Offset;
var count = buffer.Count;
while (receiveQueueHead != null && count > 0)
{
// Copy data from receive buffer to target
var toCopy = Math.Min(receiveQueueHead.Count, count);
Array.Copy(
receiveQueueHead.Buffer, receiveQueueHead.Offset,
buffer.Array, offset,
toCopy);
offset += toCopy;
count -= toCopy;
if (toCopy == receiveQueueHead.Count)
{
connection.config.BufferPool.Return(
receiveQueueHead.Buffer);
receiveQueueHead = receiveQueueHead.Next;
}
else
{
receiveQueueHead.Offset += toCopy;
receiveQueueHead.Count -= toCopy;
break;
}
}
// Calculate whether we should send a window update frame
// after the read is complete.
// Only need to do this if the stream has not yet ended
if (!streamClosedFromRemote)
{
var isFree = totalReceiveWindow - ReceiveQueueLength;
var possibleWindowUpdate = isFree - receiveWindow;
if (possibleWindowUpdate >= (totalReceiveWindow/2))
{
windowUpdateAmount = possibleWindowUpdate;
receiveWindow += windowUpdateAmount;
if (connection.logger != null &&
connection.logger.IsEnabled(LogLevel.Trace))
{
connection.logger.LogTrace(
"Incoming flow control window update:\n" +
" Stream {0} window: {1} -> {2}",
Id, receiveWindow - windowUpdateAmount, receiveWindow);
}
}
}
result = new StreamReadResult{
BytesRead = offset - buffer.Offset,
EndOfStream = false,
};
hasResult = true;
if (receiveQueueHead == null && !streamClosedFromRemote)
{
// If all data was consumed the next read must be blocked
// until more data comes in or the stream gets closed or reset
readDataPossible.Reset();
}
}
else if (streamClosedFromRemote)
{
// Deliver a notification that the stream was closed
result = new StreamReadResult{
BytesRead = 0,
EndOfStream = true,
};
hasResult = true;
}
}
if (hasResult)
{
if (windowUpdateAmount > 0)
{
// We need to send a window update frame before delivering
// the result
await SendWindowUpdate(windowUpdateAmount);
}
return result;
}
}
}
/// <summary>
/// Sends a window update frame for this stream
/// </summary>
private async ValueTask<object> SendWindowUpdate(int amount)
{
// Send the header
var fh = new FrameHeader {
StreamId = this.Id,
Type = FrameType.WindowUpdate,
Flags = 0,
};
var updateData = new WindowUpdateData
{
WindowSizeIncrement = amount,
};
try
{
await this.connection.writer.WriteWindowUpdate(fh, updateData);
}
catch (Exception)
{
// We ignore errors on sending window updates since they are
// not important to the reading task, which is the request
// handling task.
// An error means the writer is dead, which again means
// window updates are no longer necessary
}
return null;
}
public Task WriteAsync(ArraySegment<byte> buffer)
{
return WriteAsync(buffer, false);
}
public async Task WriteAsync(ArraySegment<byte> buffer, bool endOfStream = false)
{
var removeStream = false;
await writeMutex.WaitAsync();
try
{
lock (stateMutex)
{
// Check the current stream state
if (state == StreamState.Reset)
{
throw new StreamResetException();
}
else if (state != StreamState.Open && state != StreamState.HalfClosedRemote)
{
throw new Exception("Attempt to write data in invalid stream state");
}
else if (state == StreamState.Open && endOfStream)
{
state = StreamState.HalfClosedLocal;
}
else if (state == StreamState.HalfClosedRemote && endOfStream)
{
state = StreamState.Closed;
removeStream = true;
}
// Besides state check also check if headers have already been sent
// StreamState.Open could mean we only have received them
if (!this.headersSent)
{
throw new Exception("Attempted to write data before headers");
}
// Remark: There's no need to check whether trailers have already
// been sent as writing trailers will (half)close the stream,
// which is checked for
dataSent = true; // Set a flag do disallow following headers
}
// Remark:
// As we hold the writeMutex nobody can close the stream or send trailers
// in between.
// The only thing that may happen is that the stream get's reset in between,
// which would be reported through the ConnectionWriter to us
var fh = new FrameHeader {
StreamId = this.Id,
Type = FrameType.Data,
Flags = endOfStream ? ((byte)DataFrameFlags.EndOfStream) : (byte)0,
};
var res = await connection.writer.WriteData(fh, buffer);
if (res == ConnectionWriter.WriteResult.StreamResetError)
{
throw new StreamResetException();
}
else if (res != ConnectionWriter.WriteResult.Success)
{
throw new Exception("Can not write to stream"); // TODO: Improve me
}
}
finally
{
writeMutex.Release();
if (removeStream)
{
connection.UnregisterStream(this);
}
}
}
public Task CloseAsync()
{
return this.WriteAsync(Constants.EmptyByteArray, true);
}
public async Task<IEnumerable<HeaderField>> ReadHeadersAsync()
{
await readHeadersPossible;
IEnumerable<HeaderField> result = null;
lock (stateMutex)
{
if (state == StreamState.Reset)
{
throw new StreamResetException();
}
if (inHeaders != null)
{
result = inHeaders;
// If the headers which are read are the informatianal headers
// we reset the received headers, so that they can be
// replaced by the real headers later on.
if (result.IsInformationalHeaders())
{
inHeaders = null;
readHeadersPossible.Reset();
}
}
else result = EmptyHeaders;
}
return result;
}
public async Task<IEnumerable<HeaderField>> ReadTrailersAsync()
{
await readTrailersPossible;
IEnumerable<HeaderField> result = null;
lock (stateMutex)
{
if (state == StreamState.Reset)
{
throw new StreamResetException();
}
if (inTrailers != null) result = inTrailers;
else result = EmptyHeaders;
}
return result;
}
/// <summary>
/// Processes the reception of incoming headers
/// </summary>
public Http2Error? ProcessHeaders(
CompleteHeadersFrameData headers)
{
var wakeupDataWaiter = false;
var wakeupHeaderWaiter = false;
var wakeupTrailerWaiter = false;
var removeStream = false;
lock (stateMutex)
{
// Header frames are not valid in all states
switch (state)
{
case StreamState.ReservedLocal:
case StreamState.ReservedRemote:
// Push promises are currently not implemented
// So this needs to be reviewed later on
// Currently we should never encounter this state
return new Http2Error
{
StreamId = Id,
Code = ErrorCode.InternalError,
Message = "Received header frame in uncovered push promise state",
};
case StreamState.Idle:
case StreamState.Open:
case StreamState.HalfClosedLocal:
// Open can mean we have already received headers
// (in case we are a server) or not (in case we are
// a client and only have sent headers)
// If headers were already received before there must be
// a data frame in between and these are trailers.
// An exception is if we are client, where we can
// receive informational headers and normal headers.
// This requires no data in between. These header must
// contain a 1xy status code.
// Trailers must have the EndOfStream flag set and must
// always follow after a data frame.
if (headersReceived != HeaderReceptionState.ReceivedAllHeaders)
{
// We are receiving headers
HeaderValidationResult hvr;
if (connection.IsServer)
{
hvr = HeaderValidator.ValidateRequestHeaders(headers.Headers);
}
else
{
hvr = HeaderValidator.ValidateResponseHeaders(headers.Headers);
}
if (hvr != HeaderValidationResult.Ok)
{
return new Http2Error
{
StreamId = Id,
Code = ErrorCode.ProtocolError,
Message = "Received invalid headers",
};
}
if (!connection.config.IsServer &&
headers.Headers.IsInformationalHeaders())
{
// Clients support the reception of informational headers.
// If this is only an informational header we might
// receive additional headers later on.
headersReceived =
HeaderReceptionState.ReceivedInformationalHeaders;
}
else
{
// Servers don't support informational headers at all.
// And if we are client and directly receive response
// headers it's also fine.
headersReceived =
HeaderReceptionState.ReceivedAllHeaders;
}
wakeupHeaderWaiter = true;
// TODO: Uncompress cookie headers here?
declaredInContentLength = headers.Headers.GetContentLength();
inHeaders = headers.Headers;
}
else if (!dataReceived)
{
// We already have received headers, so this should
// be trailers. However there was no DATA frame in
// between, so this is simply invalid.
return new Http2Error
{
StreamId = Id,
Code = ErrorCode.ProtocolError,
Message = "Received trailers without headers",
};
}
else
{
// These are trailers
// trailers must have end of stream set. It is not
// valid to receive multiple trailers
if (!headers.EndOfStream)
{
return new Http2Error
{
StreamId = Id,
Code = ErrorCode.ProtocolError,
Message = "Received trailers without EndOfStream flag",
};
}
var hvr = HeaderValidator.ValidateTrailingHeaders(headers.Headers);
if (hvr != HeaderValidationResult.Ok)
{
return new Http2Error
{
StreamId = Id,
Code = ErrorCode.ProtocolError,
Message = "Received invalid trailers",
};
}
// If content-length was set we must also validate
// it against the received dataamount here
if (declaredInContentLength >= 0 &&
declaredInContentLength != totalInData)
{
return new Http2Error
{
StreamId = Id,
Code = ErrorCode.ProtocolError,
Message =
"Length of DATA frames does not match content-length",
};
}
wakeupTrailerWaiter = true;
inTrailers = headers.Headers;
}
// Handle state changes that are caused by HEADERS frame
if (state == StreamState.Idle)
{
state = StreamState.Open;
}
if (headers.EndOfStream)
{
if (state == StreamState.HalfClosedLocal)
{
state = StreamState.Closed;
removeStream = true;
}
else // Must be Open, since Idle moves to Open
{
state = StreamState.HalfClosedRemote;
}
wakeupTrailerWaiter = true;
wakeupDataWaiter = true;
}
break;
case StreamState.HalfClosedRemote:
case StreamState.Closed:
// Received a header frame for a stream that was
// already closed from remote side.
// That's not valid
return new Http2Error
{
Code = ErrorCode.StreamClosed,
StreamId = Id,
Message = "Received headers for closed stream",
};
case StreamState.Reset:
// The stream was already reset
// What we really should do here depends on the previous state,
// which is not stored for efficiency. If we reset the
// stream late headers are ok. If the remote resetted it
// this is a protocol error for the stream.
// As it does not really matter just ignore the frame.
break;
default:
throw new Exception("Unhandled stream state");
}
}
// Wakeup any blocked calls that are waiting on headers or end of stream
if (wakeupHeaderWaiter)
{
readHeadersPossible.Set();
}
if (wakeupDataWaiter)
{
readDataPossible.Set();
}
if (wakeupTrailerWaiter)
{
readTrailersPossible.Set();
}
if (removeStream)
{
connection.UnregisterStream(this);
}
return null;
}
/// <summary>
/// Processes the reception of a DATA frame.
/// The connection is responsible for checking the maximum frame length
/// before calling this function.
/// </summary>
public Http2Error? PushBuffer(
ArraySegment<byte> buffer,
bool endOfStream,
out bool tookBufferOwnership)
{
tookBufferOwnership = false;
var wakeupDataWaiter = false;
var wakeupTrailerWaiter = false;
var removeStream = false;
lock (stateMutex)
{
// Data frames are not valid in all states
switch (state)
{
case StreamState.ReservedLocal:
case StreamState.ReservedRemote:
// Push promises are currently not implemented
// At the moment these should already been
// rejected in the Connection.
// This needs to be reviewed later on
throw new NotImplementedException();
case StreamState.Open:
case StreamState.HalfClosedLocal:
if (headersReceived != HeaderReceptionState.ReceivedAllHeaders)
{
// Received DATA without HEADERS before.
// State Open can also mean we only have sent
// headers but not received them.
// Therefore checking the state alone isn't sufficient.
return new Http2Error
{
StreamId = Id,
Code = ErrorCode.ProtocolError,
Message = "Received data before all headers",
};
}
// Only enqueue DATA frames with a real content length
// of at least 1 byte, otherwise the reader is needlessly
// woken up.
// Empty DATA frames are also not checked against the
// flow control window, which means they are valid even
// in case of a negative flow control window (which is
// not possible in the current state of the implementation).
// However we still treat empty DATA frames as a valid
// separator between HEADERS and trailing HEADERS.
if (buffer.Count > 0)
{
// Check if the flow control window is exceeded
if (buffer.Count > receiveWindow)
{
return new Http2Error
{
StreamId = Id,
Code = ErrorCode.FlowControlError,
Message = "Received window exceeded",
};
}
if (connection.logger != null &&
connection.logger.IsEnabled(LogLevel.Trace))
{
connection.logger.LogTrace(
"Incoming flow control window update:\n" +
" Stream {0} window: {1} -> {2}",
Id, receiveWindow, receiveWindow - buffer.Count);
}
receiveWindow -= buffer.Count;
// Enqueue the data at the end of the receive queue
// TODO: Instead of appending buffers with only small
// content on the end of each other we might to concat
// the buffers together, which avoids the worst-case
// scenario: The remote side sending us lots of 1byte
// DATA frames, where we need a queue item for each
// one.
var newItem = new ReceiveQueueItem(buffer);
EnqueueReceiveQueueItem(newItem);
wakeupDataWaiter = true;
tookBufferOwnership = true;
}
// Allow trailing headers afterwards
dataReceived = true;
// Check if data matches declared content-length
// TODO: What should be done if the declared
// content-length was invalid (-2)?
totalInData += buffer.Count;
if (endOfStream &&
declaredInContentLength >= 0 &&
declaredInContentLength != totalInData)
{
return new Http2Error
{
StreamId = Id,
Code = ErrorCode.ProtocolError,
Message =
"Length of DATA frames does not match content-length",
};
}
// All checks OK so far, wakeup waiter and handle state changes
// Handle state changes that are caused by DATA frames
if (endOfStream)
{
if (state == StreamState.HalfClosedLocal)
{
state = StreamState.Closed;
removeStream = true;
}
else // Open
{
state = StreamState.HalfClosedRemote;
}
wakeupTrailerWaiter = true;
wakeupDataWaiter = true;
}
break;
case StreamState.Idle:
case StreamState.HalfClosedRemote:
case StreamState.Closed:
// Received a DATA frame for a stream that was
// already closed or not properly opened from remote side.
// That's not valid
return new Http2Error
{
StreamId = Id,
Code = ErrorCode.StreamClosed,
Message = "Received data for closed stream",
};
case StreamState.Reset:
// The stream was already reset
// What we really should do here depends on the previous state,
// which is not stored for efficiency. If we reset the
// stream late headers are ok. If the remote resetted it
// this is a protocol error for the stream.
// As it does not really matter just ignore the frame.
break;
default:
throw new Exception("Unhandled stream state");
}
}
// Wakeup any blocked calls that are waiting on data or end of stream
if (wakeupDataWaiter)
{
// Wakeup any blocked call that waits for headers to get available
readDataPossible.Set();
}