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

Commit bfe3363

Browse files
hughbestephentoub
authored andcommitted
Make HttpListenerResponse.ContentEncoding a Nop in the managed implementation to match Windows (#19766)
* Cleanup some code in HttpListenerResponse * Make HttpListenerResponse.ContentEncoding a Nop in the managed implementation to match Windows * Address PR feedback * Dispose of the HttpResponseMessage in HttpListener tests
1 parent 986c401 commit bfe3363

File tree

6 files changed

+105
-172
lines changed

6 files changed

+105
-172
lines changed

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

Lines changed: 7 additions & 20 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.Text;
67

78
namespace System.Net
89
{
@@ -13,10 +14,7 @@ public sealed unsafe partial class HttpListenerResponse : IDisposable
1314

1415
public WebHeaderCollection Headers
1516
{
16-
get
17-
{
18-
return _webHeaders;
19-
}
17+
get => _webHeaders;
2018
set
2119
{
2220
_webHeaders = new WebHeaderCollection();
@@ -27,20 +25,12 @@ public WebHeaderCollection Headers
2725
}
2826
}
2927

28+
public Encoding ContentEncoding { get; set; }
29+
3030
public CookieCollection Cookies
3131
{
32-
get
33-
{
34-
if (_cookies == null)
35-
{
36-
_cookies = new CookieCollection();
37-
}
38-
return _cookies;
39-
}
40-
set
41-
{
42-
_cookies = value;
43-
}
32+
get => _cookies ?? (_cookies = new CookieCollection());
33+
set => _cookies = value;
4434
}
4535

4636
public void AddHeader(string name, string value)
@@ -91,9 +81,6 @@ public void SetCookie(Cookie cookie)
9181
}
9282
}
9383

94-
void IDisposable.Dispose()
95-
{
96-
Dispose();
97-
}
84+
void IDisposable.Dispose() => Dispose();
9885
}
9986
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ public void SendError(string msg, int status)
450450
else
451451
str = string.Format("<h1>{0}</h1>", description);
452452

453-
byte[] error = _context.Response.ContentEncoding.GetBytes(str);
453+
byte[] error = Encoding.Default.GetBytes(str);
454454
response.Close(error, false);
455455
}
456456
catch

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

Lines changed: 13 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ namespace System.Net
3838
public sealed partial class HttpListenerResponse : IDisposable
3939
{
4040
private bool _disposed;
41-
private Encoding _contentEncoding;
4241
private long _contentLength;
4342
private bool _clSet;
4443
private string _contentType;
@@ -59,37 +58,11 @@ internal HttpListenerResponse(HttpListenerContext context)
5958
_context = context;
6059
}
6160

62-
internal bool ForceCloseChunked
63-
{
64-
get { return _forceCloseChunked; }
65-
}
66-
67-
public Encoding ContentEncoding
68-
{
69-
get
70-
{
71-
if (_contentEncoding == null)
72-
{
73-
_contentEncoding = Encoding.Default;
74-
}
75-
76-
return _contentEncoding;
77-
}
78-
set
79-
{
80-
if (_disposed)
81-
throw new ObjectDisposedException(GetType().ToString());
82-
83-
if (_headersSent)
84-
throw new InvalidOperationException(SR.net_cannot_change_after_headers);
85-
86-
_contentEncoding = value;
87-
}
88-
}
61+
internal bool ForceCloseChunked => _forceCloseChunked;
8962

9063
public long ContentLength64
9164
{
92-
get { return _contentLength; }
65+
get => _contentLength;
9366
set
9467
{
9568
if (_disposed)
@@ -108,7 +81,7 @@ public long ContentLength64
10881

10982
public string ContentType
11083
{
111-
get { return _contentType; }
84+
get => _contentType;
11285
set
11386
{
11487
if (_disposed)
@@ -123,7 +96,7 @@ public string ContentType
12396

12497
public bool KeepAlive
12598
{
126-
get { return _keepAlive; }
99+
get => _keepAlive;
127100
set
128101
{
129102
if (_disposed)
@@ -148,7 +121,7 @@ public Stream OutputStream
148121

149122
public Version ProtocolVersion
150123
{
151-
get { return _version; }
124+
get => _version;
152125
set
153126
{
154127
if (_disposed)
@@ -169,7 +142,7 @@ public Version ProtocolVersion
169142

170143
public string RedirectLocation
171144
{
172-
get { return _location; }
145+
get => _location;
173146
set
174147
{
175148
if (_disposed)
@@ -184,7 +157,7 @@ public string RedirectLocation
184157

185158
public bool SendChunked
186159
{
187-
get { return _chunked; }
160+
get => _chunked;
188161
set
189162
{
190163
if (_disposed)
@@ -199,7 +172,7 @@ public bool SendChunked
199172

200173
public int StatusCode
201174
{
202-
get { return _statusCode; }
175+
get => _statusCode;
203176
set
204177
{
205178
if (_disposed)
@@ -217,17 +190,11 @@ public int StatusCode
217190

218191
public string StatusDescription
219192
{
220-
get { return _statusDescription; }
221-
set
222-
{
223-
_statusDescription = value;
224-
}
193+
get => _statusDescription;
194+
set => _statusDescription = value;
225195
}
226196

227-
private void Dispose()
228-
{
229-
Close(true);
230-
}
197+
private void Dispose() => Close(true);
231198

232199
public void Close()
233200
{
@@ -301,23 +268,11 @@ private bool FindCookie(Cookie cookie)
301268

302269
internal void SendHeaders(bool closing, MemoryStream ms, bool isWebSocketHandshake = false)
303270
{
304-
Encoding encoding = _contentEncoding;
305-
if (encoding == null)
306-
encoding = Encoding.Default;
307-
308271
if (!isWebSocketHandshake)
309272
{
310273
if (_contentType != null)
311274
{
312-
if (_contentEncoding != null && _contentType.IndexOf(HttpHeaderStrings.Charset, StringComparison.Ordinal) == -1)
313-
{
314-
string enc_name = _contentEncoding.WebName;
315-
_webHeaders.Set(HttpKnownHeaderNames.ContentType, _contentType + "; " + HttpHeaderStrings.Charset + enc_name);
316-
}
317-
else
318-
{
319-
_webHeaders.Set(HttpKnownHeaderNames.ContentType, _contentType);
320-
}
275+
_webHeaders.Set(HttpKnownHeaderNames.ContentType, _contentType);
321276
}
322277

323278
if (_webHeaders[HttpKnownHeaderNames.Server] == null)
@@ -397,6 +352,7 @@ internal void SendHeaders(bool closing, MemoryStream ms, bool isWebSocketHandsha
397352
}
398353
}
399354

355+
Encoding encoding = Encoding.Default;
400356
StreamWriter writer = new StreamWriter(ms, encoding, 256);
401357
writer.Write("HTTP/{0} {1} {2}\r\n", _version, _statusCode, _statusDescription);
402358
string headers_str = FormatHeaders(_webHeaders);

0 commit comments

Comments
 (0)