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

Commit f17970b

Browse files
committed
Addressed feedback
1 parent 1073014 commit f17970b

File tree

3 files changed

+59
-36
lines changed

3 files changed

+59
-36
lines changed

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Text;
56
using System.Threading.Tasks;
67
using Microsoft.AspNet.Http;
@@ -13,7 +14,7 @@ public class ContentResult : ActionResult
1314
{
1415
private readonly MediaTypeHeaderValue DefaultContentType = new MediaTypeHeaderValue("text/plain")
1516
{
16-
Encoding = Encodings.UTF8EncodingWithoutBOM
17+
Charset = Encodings.UTF8EncodingWithoutBOM.WebName
1718
};
1819

1920
public string Content { get; set; }
@@ -29,31 +30,44 @@ public override async Task ExecuteResultAsync([NotNull] ActionContext context)
2930
{
3031
var response = context.HttpContext.Response;
3132

32-
// Encoding property on MediaTypeHeaderValue does not return the exact encoding instance that
33-
// is set, so any settings(for example: BOM) on it will be lost when retrieving the value.
34-
// In the scenario where the user does not supply encoding, we want to use UTF8 without BOM and
35-
// for this reason do not rely on Encoding property.
36-
MediaTypeHeaderValue contentTypeHeader;
33+
MediaTypeHeaderValue contentTypeHeader = ContentType;
3734
Encoding encoding;
38-
if (ContentType == null)
35+
if(contentTypeHeader == null)
3936
{
40-
encoding = Encodings.UTF8EncodingWithoutBOM;
4137
contentTypeHeader = DefaultContentType;
38+
encoding = Encodings.UTF8EncodingWithoutBOM;
4239
}
4340
else
4441
{
45-
if (ContentType.Encoding == null)
42+
if(string.IsNullOrEmpty(contentTypeHeader.Charset))
4643
{
4744
encoding = Encodings.UTF8EncodingWithoutBOM;
45+
4846
// 1. Do not modify the user supplied content type
4947
// 2. Parse here to handle parameters apart from charset
50-
contentTypeHeader = MediaTypeHeaderValue.Parse(ContentType.ToString());
51-
contentTypeHeader.Encoding = encoding;
48+
contentTypeHeader = MediaTypeHeaderValue.Parse(contentTypeHeader.ToString());
49+
contentTypeHeader.Charset = encoding.WebName;
5250
}
5351
else
5452
{
55-
encoding = ContentType.Encoding;
56-
contentTypeHeader = ContentType;
53+
if (string.Equals(
54+
contentTypeHeader.Charset,
55+
Encodings.UTF8EncodingWithoutBOM.WebName,
56+
StringComparison.OrdinalIgnoreCase))
57+
{
58+
encoding = Encodings.UTF8EncodingWithoutBOM;
59+
}
60+
else if (string.Equals(
61+
contentTypeHeader.Charset,
62+
Encodings.UTF16EncodingLittleEndian.WebName,
63+
StringComparison.OrdinalIgnoreCase))
64+
{
65+
encoding = Encodings.UTF16EncodingLittleEndian;
66+
}
67+
else
68+
{
69+
encoding = Encoding.GetEncoding(contentTypeHeader.Charset);
70+
}
5771
}
5872
}
5973

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

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.IO;
56
using System.Text;
67
using System.Threading.Tasks;
@@ -18,7 +19,7 @@ public static class ViewExecutor
1819
private const int BufferSize = 1024;
1920
private static readonly MediaTypeHeaderValue DefaultContentType = new MediaTypeHeaderValue("text/html")
2021
{
21-
Encoding = Encodings.UTF8EncodingWithoutBOM
22+
Charset = Encodings.UTF8EncodingWithoutBOM.WebName
2223
};
2324

2425
/// <summary>
@@ -37,43 +38,52 @@ public static async Task ExecuteAsync([NotNull] IView view,
3738
{
3839
var response = actionContext.HttpContext.Response;
3940

40-
// Encoding property on MediaTypeHeaderValue does not return the exact encoding instance that
41-
// is set, so any settings(for example: BOM) on it will be lost when retrieving the value.
42-
// In the scenario where the user does not supply encoding, we want to use UTF8 without BOM and
43-
// for this reason do not rely on Encoding property.
44-
MediaTypeHeaderValue contentTypeHeader;
41+
MediaTypeHeaderValue contentTypeHeader = contentType;
4542
Encoding encoding;
46-
if (contentType == null)
43+
if (contentTypeHeader == null)
4744
{
48-
encoding = Encodings.UTF8EncodingWithoutBOM;
4945
contentTypeHeader = DefaultContentType;
46+
encoding = Encodings.UTF8EncodingWithoutBOM;
5047
}
5148
else
5249
{
53-
if (contentType.Encoding == null)
50+
if (string.IsNullOrEmpty(contentTypeHeader.Charset))
5451
{
5552
encoding = Encodings.UTF8EncodingWithoutBOM;
53+
5654
// 1. Do not modify the user supplied content type
5755
// 2. Parse here to handle parameters apart from charset
58-
contentTypeHeader = MediaTypeHeaderValue.Parse(contentType.ToString());
59-
contentTypeHeader.Encoding = encoding;
56+
contentTypeHeader = MediaTypeHeaderValue.Parse(contentTypeHeader.ToString());
57+
contentTypeHeader.Charset = encoding.WebName;
6058
}
6159
else
6260
{
63-
encoding = contentType.Encoding;
64-
contentTypeHeader = contentType;
61+
if(string.Equals(
62+
contentTypeHeader.Charset,
63+
Encodings.UTF8EncodingWithoutBOM.WebName,
64+
StringComparison.OrdinalIgnoreCase))
65+
{
66+
encoding = Encodings.UTF8EncodingWithoutBOM;
67+
}
68+
else if(string.Equals(
69+
contentTypeHeader.Charset,
70+
Encodings.UTF16EncodingLittleEndian.WebName,
71+
StringComparison.OrdinalIgnoreCase))
72+
{
73+
encoding = Encodings.UTF16EncodingLittleEndian;
74+
}
75+
else
76+
{
77+
encoding = Encoding.GetEncoding(contentTypeHeader.Charset);
78+
}
6579
}
6680
}
6781

6882
response.ContentType = contentTypeHeader.ToString();
6983

7084
var wrappedStream = new StreamWrapper(response.Body);
7185

72-
using (var writer = new StreamWriter(
73-
wrappedStream,
74-
encoding,
75-
BufferSize,
76-
leaveOpen: true))
86+
using (var writer = new StreamWriter(wrappedStream, encoding, BufferSize, leaveOpen: true))
7787
{
7888
try
7989
{

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,17 @@ public static TheoryData<MediaTypeHeaderValue, string, byte[]> ViewExecutorSetsC
3838
new byte[] { 97, 98, 99, 100 }
3939
},
4040
{
41-
MediaTypeHeaderValue.Parse("text/foo;p1=p1-value"),
41+
MediaTypeHeaderValue.Parse("text/foo; p1=p1-value"),
4242
"text/foo; p1=p1-value; charset=utf-8",
4343
new byte[] { 97, 98, 99, 100 }
4444
},
4545
{
46-
// This is with BOM
47-
new MediaTypeHeaderValue("text/foo") { Encoding = Encoding.UTF8 },
46+
new MediaTypeHeaderValue("text/foo") { Charset = "utf-8" },
4847
"text/foo; charset=utf-8",
49-
new byte[] { 239, 187, 191, 97, 98, 99, 100 }
48+
new byte[] { 97, 98, 99, 100 }
5049
},
5150
{
52-
new MediaTypeHeaderValue("text/foo") {Encoding = Encoding.ASCII },
51+
new MediaTypeHeaderValue("text/foo") { Charset = "us-ascii" },
5352
"text/foo; charset=us-ascii",
5453
new byte[] { 97, 98, 99, 100 }
5554
}

0 commit comments

Comments
 (0)