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

Commit 29b8c3d

Browse files
committed
Addressed feedback.
1 parent 45346db commit 29b8c3d

File tree

7 files changed

+29
-78
lines changed

7 files changed

+29
-78
lines changed

src/Microsoft.AspNet.Mvc.Core/ActionResults/ContentResult.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ public class ContentResult : ActionResult
1515
{
1616
private readonly MediaTypeHeaderValue DefaultContentType = new MediaTypeHeaderValue("text/plain")
1717
{
18-
Encoding = Encoding.UTF8
18+
Encoding = Encodings.UTF8EncodingWithoutBOM
1919
};
2020

21+
/// <summary>
22+
/// Gets or set the content representing the body of the response.
23+
/// </summary>
2124
public string Content { get; set; }
2225

2326
/// <summary>
@@ -39,7 +42,7 @@ public override async Task ExecuteResultAsync([NotNull] ActionContext context)
3942
if (contentTypeHeader == null)
4043
{
4144
contentTypeHeader = DefaultContentType;
42-
encoding = DefaultContentType.Encoding;
45+
encoding = Encodings.UTF8EncodingWithoutBOM;
4346
}
4447
else
4548
{
@@ -48,10 +51,14 @@ public override async Task ExecuteResultAsync([NotNull] ActionContext context)
4851
// 1. Do not modify the user supplied content type
4952
// 2. Parse here to handle parameters apart from charset
5053
contentTypeHeader = MediaTypeHeaderValue.Parse(contentTypeHeader.ToString());
51-
contentTypeHeader.Encoding = Encoding.UTF8;
52-
}
54+
contentTypeHeader.Encoding = Encodings.UTF8EncodingWithoutBOM;
5355

54-
encoding = contentTypeHeader.Encoding;
56+
encoding = Encodings.UTF8EncodingWithoutBOM;
57+
}
58+
else
59+
{
60+
encoding = contentTypeHeader.Encoding;
61+
}
5562
}
5663

5764
response.ContentType = contentTypeHeader.ToString();

src/Microsoft.AspNet.Mvc.Core/ActionResults/FileContentResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class FileContentResult : FileResult
2525
/// </summary>
2626
/// <param name="fileContents">The bytes that represent the file contents.</param>
2727
/// <param name="contentType">The Content-Type header of the response.</param>
28-
public FileContentResult([NotNull] byte[] fileContents, string contentType)
28+
public FileContentResult([NotNull] byte[] fileContents, [NotNull] string contentType)
2929
: this(fileContents, new MediaTypeHeaderValue(contentType))
3030
{
3131
}
@@ -37,7 +37,7 @@ public FileContentResult([NotNull] byte[] fileContents, string contentType)
3737
/// </summary>
3838
/// <param name="fileContents">The bytes that represent the file contents.</param>
3939
/// <param name="contentType">The Content-Type header of the response.</param>
40-
public FileContentResult([NotNull] byte[] fileContents, MediaTypeHeaderValue contentType)
40+
public FileContentResult([NotNull] byte[] fileContents, [NotNull] MediaTypeHeaderValue contentType)
4141
: base(contentType)
4242
{
4343
FileContents = fileContents;

src/Microsoft.AspNet.Mvc.Core/ActionResults/FilePathResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class FilePathResult : FileResult
3434
/// <param name="fileName">The path to the file. The path must be an absolute
3535
/// path. Relative and virtual paths are not supported.</param>
3636
/// <param name="contentType">The Content-Type header of the response.</param>
37-
public FilePathResult([NotNull] string fileName, string contentType)
37+
public FilePathResult([NotNull] string fileName, [NotNull] string contentType)
3838
: this(fileName, new MediaTypeHeaderValue(contentType))
3939
{
4040
}
@@ -47,7 +47,7 @@ public FilePathResult([NotNull] string fileName, string contentType)
4747
/// <param name="fileName">The path to the file. The path must be an absolute
4848
/// path. Relative and virtual paths are not supported.</param>
4949
/// <param name="contentType">The Content-Type header of the response.</param>
50-
public FilePathResult([NotNull] string fileName, MediaTypeHeaderValue contentType)
50+
public FilePathResult([NotNull] string fileName, [NotNull] MediaTypeHeaderValue contentType)
5151
: base(contentType)
5252
{
5353
FileName = fileName;

src/Microsoft.AspNet.Mvc.Core/ActionResults/FileResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected FileResult([NotNull] string contentType)
3131

3232
/// <summary>
3333
/// Creates a new <see cref="FileResult"/> instance with
34-
/// the provided <paramref name="Microsoft.Net.Http.Headers.MediaTypeHeaderValue"/>.
34+
/// the provided <paramref name="contentType"/>.
3535
/// </summary>
3636
/// <param name="contentType">The Content-Type header of the response.</param>
3737
protected FileResult([NotNull] MediaTypeHeaderValue contentType)

src/Microsoft.AspNet.Mvc.Core/ActionResults/FileStreamResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class FileStreamResult : FileResult
2929
/// </summary>
3030
/// <param name="fileStream">The stream with the file.</param>
3131
/// <param name="contentType">The Content-Type header of the response.</param>
32-
public FileStreamResult([NotNull] Stream fileStream, string contentType)
32+
public FileStreamResult([NotNull] Stream fileStream, [NotNull] string contentType)
3333
: this(fileStream, new MediaTypeHeaderValue(contentType))
3434
{
3535
}
@@ -41,7 +41,7 @@ public FileStreamResult([NotNull] Stream fileStream, string contentType)
4141
/// </summary>
4242
/// <param name="fileStream">The stream with the file.</param>
4343
/// <param name="contentType">The Content-Type header of the response.</param>
44-
public FileStreamResult([NotNull] Stream fileStream, MediaTypeHeaderValue contentType)
44+
public FileStreamResult([NotNull] Stream fileStream, [NotNull] MediaTypeHeaderValue contentType)
4545
: base(contentType)
4646
{
4747
FileStream = fileStream;

src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewExecutor.cs

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static class ViewExecutor
1919
private const int BufferSize = 1024;
2020
private static readonly MediaTypeHeaderValue DefaultContentType = new MediaTypeHeaderValue("text/html")
2121
{
22-
Encoding = Encoding.UTF8
22+
Encoding = Encodings.UTF8EncodingWithoutBOM
2323
};
2424

2525
/// <summary>
@@ -43,7 +43,7 @@ public static async Task ExecuteAsync([NotNull] IView view,
4343
if (contentTypeHeader == null)
4444
{
4545
contentTypeHeader = DefaultContentType;
46-
encoding = DefaultContentType.Encoding;
46+
encoding = Encodings.UTF8EncodingWithoutBOM;
4747
}
4848
else
4949
{
@@ -52,24 +52,21 @@ public static async Task ExecuteAsync([NotNull] IView view,
5252
// 1. Do not modify the user supplied content type
5353
// 2. Parse here to handle parameters apart from charset
5454
contentTypeHeader = MediaTypeHeaderValue.Parse(contentTypeHeader.ToString());
55-
contentTypeHeader.Encoding = Encoding.UTF8;
56-
}
55+
contentTypeHeader.Encoding = Encodings.UTF8EncodingWithoutBOM;
5756

58-
encoding = contentTypeHeader.Encoding;
57+
encoding = Encodings.UTF8EncodingWithoutBOM;
58+
}
59+
else
60+
{
61+
encoding = contentTypeHeader.Encoding;
62+
}
5963
}
6064

6165
response.ContentType = contentTypeHeader.ToString();
6266

6367
var wrappedStream = new StreamWrapper(response.Body);
6468

65-
// StreamWriter writes the preamble or BOM bytes to the response if the encoding requires it.
66-
// Since preamble bytes are unnecessary when generating dynamic content, wrap the original encoding
67-
// to avoid writing the preamble bytes.
68-
using (var writer = new StreamWriter(
69-
wrappedStream,
70-
new ResponseEncodingWrapper(encoding),
71-
BufferSize,
72-
leaveOpen: true))
69+
using (var writer = new StreamWriter(wrappedStream, encoding, BufferSize, leaveOpen: true))
7370
{
7471
try
7572
{
@@ -161,53 +158,5 @@ public override void Write(byte[] buffer, int offset, int count)
161158
}
162159
}
163160
}
164-
165-
/// <summary>
166-
/// Encoding wrapper which makes preamble to be not required.
167-
/// </summary>
168-
private class ResponseEncodingWrapper : Encoding
169-
{
170-
private readonly Encoding _encoding;
171-
172-
public ResponseEncodingWrapper(Encoding innerEncoding)
173-
{
174-
_encoding = innerEncoding;
175-
}
176-
177-
public override int GetByteCount(char[] chars, int index, int count)
178-
{
179-
return _encoding.GetByteCount(chars, index, count);
180-
}
181-
182-
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
183-
{
184-
return _encoding.GetBytes(chars, charIndex, charCount, bytes, byteIndex);
185-
}
186-
187-
public override int GetCharCount(byte[] bytes, int index, int count)
188-
{
189-
return _encoding.GetCharCount(bytes, index, count);
190-
}
191-
192-
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
193-
{
194-
return _encoding.GetChars(bytes, byteIndex, byteCount, chars, charIndex);
195-
}
196-
197-
public override int GetMaxByteCount(int charCount)
198-
{
199-
return _encoding.GetMaxByteCount(charCount);
200-
}
201-
202-
public override int GetMaxCharCount(int byteCount)
203-
{
204-
return _encoding.GetMaxByteCount(byteCount);
205-
}
206-
207-
public override byte[] GetPreamble()
208-
{
209-
return new byte[] { };
210-
}
211-
}
212161
}
213162
}

test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/ViewExecutorTest.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ public static TheoryData<MediaTypeHeaderValue, string, byte[]> ViewExecutorSetsC
4242
"text/foo; p1=p1-value; charset=utf-8",
4343
new byte[] { 97, 98, 99, 100 }
4444
},
45-
{
46-
new MediaTypeHeaderValue("text/foo") { Charset = "utf-8" },
47-
"text/foo; charset=utf-8",
48-
new byte[] { 97, 98, 99, 100 }
49-
},
5045
{
5146
new MediaTypeHeaderValue("text/foo") { Charset = "us-ascii" },
5247
"text/foo; charset=us-ascii",

0 commit comments

Comments
 (0)