Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit de5dd4c

Browse files
committed
Merge branch 'benaadams/trim-streams' into dev
2 parents d0dca75 + 59bfb9b commit de5dd4c

File tree

2 files changed

+60
-72
lines changed

2 files changed

+60
-72
lines changed

src/Microsoft.AspNet.Server.Kestrel/Http/FrameRequestStream.cs

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
1111
public class FrameRequestStream : Stream
1212
{
1313
private readonly MessageBody _body;
14-
private bool _stopped;
15-
private bool _aborted;
14+
private StreamState _state;
1615

1716
public FrameRequestStream(MessageBody body)
1817
{
@@ -52,29 +51,15 @@ public override void SetLength(long value)
5251

5352
public override int Read(byte[] buffer, int offset, int count)
5453
{
55-
if (_stopped)
56-
{
57-
throw new ObjectDisposedException(nameof(FrameRequestStream));
58-
}
59-
if (_aborted)
60-
{
61-
throw new IOException("The request has been aborted.");
62-
}
54+
ValidateState();
6355

6456
return ReadAsync(buffer, offset, count).GetAwaiter().GetResult();
6557
}
6658

6759
#if NET451
6860
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
6961
{
70-
if (_stopped)
71-
{
72-
throw new ObjectDisposedException(nameof(FrameRequestStream));
73-
}
74-
if (_aborted)
75-
{
76-
throw new IOException("The request has been aborted.");
77-
}
62+
ValidateState();
7863

7964
var task = ReadAsync(buffer, offset, count, CancellationToken.None, state);
8065
if (callback != null)
@@ -91,14 +76,7 @@ public override int EndRead(IAsyncResult asyncResult)
9176

9277
private Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken, object state)
9378
{
94-
if (_stopped)
95-
{
96-
throw new ObjectDisposedException(nameof(FrameRequestStream));
97-
}
98-
if (_aborted)
99-
{
100-
throw new IOException("The request has been aborted.");
101-
}
79+
ValidateState();
10280

10381
var tcs = new TaskCompletionSource<int>(state);
10482
var task = _body.ReadAsync(new ArraySegment<byte>(buffer, offset, count), cancellationToken);
@@ -124,14 +102,7 @@ private Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationTo
124102

125103
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
126104
{
127-
if (_stopped)
128-
{
129-
throw new ObjectDisposedException(nameof(FrameRequestStream));
130-
}
131-
if (_aborted)
132-
{
133-
throw new IOException("The request has been aborted.");
134-
}
105+
ValidateState();
135106

136107
return _body.ReadAsync(new ArraySegment<byte>(buffer, offset, count), cancellationToken);
137108
}
@@ -145,14 +116,37 @@ public void StopAcceptingReads()
145116
{
146117
// Can't use dispose (or close) as can be disposed too early by user code
147118
// As exampled in EngineTests.ZeroContentLengthNotSetAutomaticallyForCertainStatusCodes
148-
_stopped = true;
119+
_state = StreamState.Disposed;
149120
}
150121

151122
public void Abort()
152123
{
153124
// We don't want to throw an ODE until the app func actually completes.
154125
// If the request is aborted, we throw an IOException instead.
155-
_aborted = true;
126+
if (_state != StreamState.Disposed)
127+
{
128+
_state = StreamState.Aborted;
129+
}
130+
}
131+
132+
private void ValidateState()
133+
{
134+
switch (_state)
135+
{
136+
case StreamState.Open:
137+
return;
138+
case StreamState.Disposed:
139+
throw new ObjectDisposedException(nameof(FrameRequestStream));
140+
case StreamState.Aborted:
141+
throw new IOException("The request has been aborted.");
142+
}
143+
}
144+
145+
private enum StreamState
146+
{
147+
Open,
148+
Disposed,
149+
Aborted
156150
}
157151
}
158152
}

src/Microsoft.AspNet.Server.Kestrel/Http/FrameResponseStream.cs

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
1111
class FrameResponseStream : Stream
1212
{
1313
private readonly FrameContext _context;
14-
private bool _stopped;
15-
private bool _aborted;
14+
private StreamState _state;
1615

1716
public FrameResponseStream(FrameContext context)
1817
{
@@ -37,28 +36,14 @@ public override long Length
3736

3837
public override void Flush()
3938
{
40-
if (_stopped)
41-
{
42-
throw new ObjectDisposedException(nameof(FrameResponseStream));
43-
}
44-
if (_aborted)
45-
{
46-
throw new IOException("The request has been aborted.");
47-
}
39+
ValidateState();
4840

4941
_context.FrameControl.Flush();
5042
}
5143

5244
public override Task FlushAsync(CancellationToken cancellationToken)
5345
{
54-
if (_stopped)
55-
{
56-
throw new ObjectDisposedException(nameof(FrameResponseStream));
57-
}
58-
if (_aborted)
59-
{
60-
throw new IOException("The request has been aborted.");
61-
}
46+
ValidateState();
6247

6348
return _context.FrameControl.FlushAsync(cancellationToken);
6449
}
@@ -80,28 +65,14 @@ public override int Read(byte[] buffer, int offset, int count)
8065

8166
public override void Write(byte[] buffer, int offset, int count)
8267
{
83-
if (_stopped)
84-
{
85-
throw new ObjectDisposedException(nameof(FrameResponseStream));
86-
}
87-
if (_aborted)
88-
{
89-
throw new IOException("The request has been aborted.");
90-
}
68+
ValidateState();
9169

9270
_context.FrameControl.Write(new ArraySegment<byte>(buffer, offset, count));
9371
}
9472

9573
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
9674
{
97-
if (_stopped)
98-
{
99-
throw new ObjectDisposedException(nameof(FrameResponseStream));
100-
}
101-
if (_aborted)
102-
{
103-
throw new IOException("The request has been aborted.");
104-
}
75+
ValidateState();
10576

10677
return _context.FrameControl.WriteAsync(new ArraySegment<byte>(buffer, offset, count), cancellationToken);
10778
}
@@ -110,14 +81,37 @@ public void StopAcceptingWrites()
11081
{
11182
// Can't use dispose (or close) as can be disposed too early by user code
11283
// As exampled in EngineTests.ZeroContentLengthNotSetAutomaticallyForCertainStatusCodes
113-
_stopped = true;
84+
_state = StreamState.Disposed;
11485
}
11586

11687
public void Abort()
11788
{
11889
// We don't want to throw an ODE until the app func actually completes.
11990
// If the request is aborted, we throw an IOException instead.
120-
_aborted = true;
91+
if (_state != StreamState.Disposed)
92+
{
93+
_state = StreamState.Aborted;
94+
}
95+
}
96+
97+
private void ValidateState()
98+
{
99+
switch (_state)
100+
{
101+
case StreamState.Open:
102+
return;
103+
case StreamState.Disposed:
104+
throw new ObjectDisposedException(nameof(FrameResponseStream));
105+
case StreamState.Aborted:
106+
throw new IOException("The request has been aborted.");
107+
}
108+
}
109+
110+
private enum StreamState
111+
{
112+
Open,
113+
Disposed,
114+
Aborted
121115
}
122116
}
123117
}

0 commit comments

Comments
 (0)