From 6ef8be92ff7fbae610e3b03a93c399a0c1abae3d Mon Sep 17 00:00:00 2001 From: Kiran Challa Date: Tue, 12 May 2015 17:37:00 -0700 Subject: [PATCH] [Fixes #2541] Use custom stream writer for Xml output formatters --- .../XmlDataContractSerializerOutputFormatter.cs | 9 ++++----- .../XmlSerializerOutputFormatter.cs | 9 ++++----- .../XmlDataContractSerializerOutputFormatterTest.cs | 4 +++- .../XmlSerializerOutputFormatterTest.cs | 5 +++-- .../ContentNegotiationWebSite/VCardFormatter_V3.cs | 7 ++----- .../ContentNegotiationWebSite/VCardFormatter_V4.cs | 7 ++----- 6 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Xml/XmlDataContractSerializerOutputFormatter.cs b/src/Microsoft.AspNet.Mvc.Xml/XmlDataContractSerializerOutputFormatter.cs index 2303aa3ecb..1755d630aa 100644 --- a/src/Microsoft.AspNet.Mvc.Xml/XmlDataContractSerializerOutputFormatter.cs +++ b/src/Microsoft.AspNet.Mvc.Xml/XmlDataContractSerializerOutputFormatter.cs @@ -158,7 +158,9 @@ namespace Microsoft.AspNet.Mvc.Xml public virtual XmlWriter CreateXmlWriter([NotNull] Stream writeStream, [NotNull] XmlWriterSettings xmlWriterSettings) { - return XmlWriter.Create(writeStream, xmlWriterSettings); + return XmlWriter.Create( + new HttpResponseStreamWriter(writeStream, xmlWriterSettings.Encoding), + xmlWriterSettings); } /// @@ -167,10 +169,7 @@ namespace Microsoft.AspNet.Mvc.Xml var tempWriterSettings = WriterSettings.Clone(); tempWriterSettings.Encoding = context.SelectedEncoding; - var innerStream = context.HttpContext.Response.Body; - - using (var outputStream = new NonDisposableStream(innerStream)) - using (var xmlWriter = CreateXmlWriter(outputStream, tempWriterSettings)) + using (var xmlWriter = CreateXmlWriter(context.HttpContext.Response.Body, tempWriterSettings)) { var obj = context.Object; var runtimeType = obj?.GetType(); diff --git a/src/Microsoft.AspNet.Mvc.Xml/XmlSerializerOutputFormatter.cs b/src/Microsoft.AspNet.Mvc.Xml/XmlSerializerOutputFormatter.cs index 11fb2fdb65..3d57a3c47e 100644 --- a/src/Microsoft.AspNet.Mvc.Xml/XmlSerializerOutputFormatter.cs +++ b/src/Microsoft.AspNet.Mvc.Xml/XmlSerializerOutputFormatter.cs @@ -133,7 +133,9 @@ namespace Microsoft.AspNet.Mvc.Xml public virtual XmlWriter CreateXmlWriter([NotNull] Stream writeStream, [NotNull] XmlWriterSettings xmlWriterSettings) { - return XmlWriter.Create(writeStream, xmlWriterSettings); + return XmlWriter.Create( + new HttpResponseStreamWriter(writeStream, xmlWriterSettings.Encoding), + xmlWriterSettings); } /// @@ -144,10 +146,7 @@ namespace Microsoft.AspNet.Mvc.Xml var tempWriterSettings = WriterSettings.Clone(); tempWriterSettings.Encoding = context.SelectedEncoding; - var innerStream = context.HttpContext.Response.Body; - - using (var outputStream = new NonDisposableStream(innerStream)) - using (var xmlWriter = CreateXmlWriter(outputStream, tempWriterSettings)) + using (var xmlWriter = CreateXmlWriter(context.HttpContext.Response.Body, tempWriterSettings)) { var obj = context.Object; var runtimeType = obj?.GetType(); diff --git a/test/Microsoft.AspNet.Mvc.Xml.Test/XmlDataContractSerializerOutputFormatterTest.cs b/test/Microsoft.AspNet.Mvc.Xml.Test/XmlDataContractSerializerOutputFormatterTest.cs index 087d66b04d..8aa3d8506f 100644 --- a/test/Microsoft.AspNet.Mvc.Xml.Test/XmlDataContractSerializerOutputFormatterTest.cs +++ b/test/Microsoft.AspNet.Mvc.Xml.Test/XmlDataContractSerializerOutputFormatterTest.cs @@ -272,7 +272,9 @@ namespace Microsoft.AspNet.Mvc.Xml var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; - var content = new StreamReader(body).ReadToEnd(); + var content = new StreamReader( + body, + new UnicodeEncoding(bigEndian: false, byteOrderMark: false, throwOnInvalidBytes: true)).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); } diff --git a/test/Microsoft.AspNet.Mvc.Xml.Test/XmlSerializerOutputFormatterTest.cs b/test/Microsoft.AspNet.Mvc.Xml.Test/XmlSerializerOutputFormatterTest.cs index 7c8764afe2..07500d95e0 100644 --- a/test/Microsoft.AspNet.Mvc.Xml.Test/XmlSerializerOutputFormatterTest.cs +++ b/test/Microsoft.AspNet.Mvc.Xml.Test/XmlSerializerOutputFormatterTest.cs @@ -227,8 +227,9 @@ namespace Microsoft.AspNet.Mvc.Xml // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; - - var content = new StreamReader(body).ReadToEnd(); + var content = new StreamReader( + body, + new UnicodeEncoding(bigEndian: false, byteOrderMark: false, throwOnInvalidBytes: true)).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); } diff --git a/test/WebSites/ContentNegotiationWebSite/VCardFormatter_V3.cs b/test/WebSites/ContentNegotiationWebSite/VCardFormatter_V3.cs index a6fb17ccda..1f645d2b69 100644 --- a/test/WebSites/ContentNegotiationWebSite/VCardFormatter_V3.cs +++ b/test/WebSites/ContentNegotiationWebSite/VCardFormatter_V3.cs @@ -7,6 +7,7 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; using ContentNegotiationWebSite.Models; +using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc.Internal; using Microsoft.Net.Http.Headers; @@ -39,11 +40,7 @@ namespace ContentNegotiationWebSite builder.AppendLine(); builder.AppendLine("END:VCARD"); - var responseStream = new NonDisposableStream(context.HttpContext.Response.Body); - using (var writer = new StreamWriter(responseStream, context.SelectedEncoding, bufferSize: 1024)) - { - await writer.WriteAsync(builder.ToString()); - } + await context.HttpContext.Response.WriteAsync(builder.ToString(), context.SelectedEncoding); } } } \ No newline at end of file diff --git a/test/WebSites/ContentNegotiationWebSite/VCardFormatter_V4.cs b/test/WebSites/ContentNegotiationWebSite/VCardFormatter_V4.cs index ad901892da..676519b24f 100644 --- a/test/WebSites/ContentNegotiationWebSite/VCardFormatter_V4.cs +++ b/test/WebSites/ContentNegotiationWebSite/VCardFormatter_V4.cs @@ -7,6 +7,7 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; using ContentNegotiationWebSite.Models; +using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc.Internal; using Microsoft.Net.Http.Headers; @@ -42,11 +43,7 @@ namespace ContentNegotiationWebSite builder.AppendLine(); builder.AppendLine("END:VCARD"); - var responseStream = new NonDisposableStream(context.HttpContext.Response.Body); - using (var writer = new StreamWriter(responseStream, context.SelectedEncoding, bufferSize: 1024)) - { - await writer.WriteAsync(builder.ToString()); - } + await context.HttpContext.Response.WriteAsync(builder.ToString(), context.SelectedEncoding); } } } \ No newline at end of file