Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit b003c58

Browse files
committed
Consolidate OutputStream implementation
Move Windows implementation to shared, then use that from managed, which means moving the core of the managed implementation to a private EnsureResponseStream helper.
1 parent a3db731 commit b003c58

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

src/System.Net.HttpListener/src/System/Net/HttpListenerResponse.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Diagnostics;
6+
using System.IO;
67
using System.Text;
78

89
namespace System.Net
@@ -11,6 +12,7 @@ public sealed unsafe partial class HttpListenerResponse : IDisposable
1112
{
1213
private CookieCollection _cookies;
1314
private bool _keepAlive = true;
15+
private HttpResponseStream _responseStream;
1416
private string _statusDescription;
1517
private WebHeaderCollection _webHeaders = new WebHeaderCollection();
1618

@@ -62,6 +64,16 @@ public bool KeepAlive
6264
}
6365
}
6466

67+
public Stream OutputStream
68+
{
69+
get
70+
{
71+
CheckDisposed();
72+
EnsureResponseStream();
73+
return _responseStream;
74+
}
75+
}
76+
6577
public string RedirectLocation
6678
{
6779
get => Headers[HttpResponseHeader.Location];

src/System.Net.HttpListener/src/System/Net/Managed/HttpListenerResponse.Managed.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public sealed partial class HttpListenerResponse : IDisposable
3939
{
4040
private long _contentLength;
4141
private bool _clSet;
42-
private HttpResponseStream _outputStream;
4342
private Version _version = HttpVersion.Version11;
4443
private int _statusCode = 200;
4544
private bool _chunked;
@@ -70,13 +69,11 @@ public long ContentLength64
7069
}
7170
}
7271

73-
public Stream OutputStream
72+
private void EnsureResponseStream()
7473
{
75-
get
74+
if (_responseStream == null)
7675
{
77-
if (_outputStream == null)
78-
_outputStream = _context.Connection.GetResponseStream();
79-
return _outputStream;
76+
_responseStream = _context.Connection.GetResponseStream();
8077
}
8178
}
8279

@@ -275,8 +272,7 @@ internal void SendHeaders(bool closing, MemoryStream ms, bool isWebSocketHandsha
275272
writer.Write(headers_str);
276273
writer.Flush();
277274
int preamble = encoding.GetPreamble().Length;
278-
if (_outputStream == null)
279-
_outputStream = _context.Connection.GetResponseStream();
275+
EnsureResponseStream();
280276

281277
/* Assumes that the ms was at position 0 */
282278
ms.Position = preamble;

src/System.Net.HttpListener/src/System/Net/Windows/HttpListenerResponse.Windows.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ private enum ResponseState
2525

2626
private ResponseState _responseState;
2727

28-
private HttpResponseStream _responseStream;
2928
private long _contentLength;
3029
private BoundaryType _boundaryType;
3130
private Interop.HttpApi.HTTP_RESPONSE _nativeResponse;
@@ -53,16 +52,6 @@ internal HttpListenerResponse(HttpListenerContext httpContext) : this()
5352

5453
private HttpListenerRequest HttpListenerRequest => HttpListenerContext.Request;
5554

56-
public Stream OutputStream
57-
{
58-
get
59-
{
60-
CheckDisposed();
61-
EnsureResponseStream();
62-
return _responseStream;
63-
}
64-
}
65-
6655
public int StatusCode
6756
{
6857
get => _nativeResponse.StatusCode;

src/System.Net.HttpListener/tests/HttpListenerResponseTests.Headers.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ public async Task ContentType_SetAfterHeadersSent_DoesNothing()
126126
Assert.DoesNotContain("Content-Type", clientResponse);
127127
}
128128

129-
// The managed implementation should throw an ObjectDisposedException getting OutputStream when disposed.
130-
[ConditionalFact(nameof(Helpers) + "." + nameof(Helpers.IsWindowsImplementation))] // [ActiveIssue(19971, TestPlatforms.AnyUnix)]
129+
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))]
131130
public async Task OutputStream_GetDisposed_ThrowsObjectDisposedException()
132131
{
133132
HttpListenerResponse response = await GetResponse();

0 commit comments

Comments
 (0)