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

Commit 6ef8be9

Browse files
committed
[Fixes #2541] Use custom stream writer for Xml output formatters
1 parent 22f1881 commit 6ef8be9

File tree

6 files changed

+18
-23
lines changed

6 files changed

+18
-23
lines changed

src/Microsoft.AspNet.Mvc.Xml/XmlDataContractSerializerOutputFormatter.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ protected virtual DataContractSerializer CreateSerializer([NotNull] Type type)
158158
public virtual XmlWriter CreateXmlWriter([NotNull] Stream writeStream,
159159
[NotNull] XmlWriterSettings xmlWriterSettings)
160160
{
161-
return XmlWriter.Create(writeStream, xmlWriterSettings);
161+
return XmlWriter.Create(
162+
new HttpResponseStreamWriter(writeStream, xmlWriterSettings.Encoding),
163+
xmlWriterSettings);
162164
}
163165

164166
/// <inheritdoc />
@@ -167,10 +169,7 @@ public override Task WriteResponseBodyAsync([NotNull] OutputFormatterContext con
167169
var tempWriterSettings = WriterSettings.Clone();
168170
tempWriterSettings.Encoding = context.SelectedEncoding;
169171

170-
var innerStream = context.HttpContext.Response.Body;
171-
172-
using (var outputStream = new NonDisposableStream(innerStream))
173-
using (var xmlWriter = CreateXmlWriter(outputStream, tempWriterSettings))
172+
using (var xmlWriter = CreateXmlWriter(context.HttpContext.Response.Body, tempWriterSettings))
174173
{
175174
var obj = context.Object;
176175
var runtimeType = obj?.GetType();

src/Microsoft.AspNet.Mvc.Xml/XmlSerializerOutputFormatter.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ protected virtual XmlSerializer CreateSerializer([NotNull] Type type)
133133
public virtual XmlWriter CreateXmlWriter([NotNull] Stream writeStream,
134134
[NotNull] XmlWriterSettings xmlWriterSettings)
135135
{
136-
return XmlWriter.Create(writeStream, xmlWriterSettings);
136+
return XmlWriter.Create(
137+
new HttpResponseStreamWriter(writeStream, xmlWriterSettings.Encoding),
138+
xmlWriterSettings);
137139
}
138140

139141
/// <inheritdoc />
@@ -144,10 +146,7 @@ public override Task WriteResponseBodyAsync([NotNull] OutputFormatterContext con
144146
var tempWriterSettings = WriterSettings.Clone();
145147
tempWriterSettings.Encoding = context.SelectedEncoding;
146148

147-
var innerStream = context.HttpContext.Response.Body;
148-
149-
using (var outputStream = new NonDisposableStream(innerStream))
150-
using (var xmlWriter = CreateXmlWriter(outputStream, tempWriterSettings))
149+
using (var xmlWriter = CreateXmlWriter(context.HttpContext.Response.Body, tempWriterSettings))
151150
{
152151
var obj = context.Object;
153152
var runtimeType = obj?.GetType();

test/Microsoft.AspNet.Mvc.Xml.Test/XmlDataContractSerializerOutputFormatterTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,9 @@ public async Task WriteAsync_WritesUTF16Output()
272272
var body = outputFormatterContext.HttpContext.Response.Body;
273273
body.Position = 0;
274274

275-
var content = new StreamReader(body).ReadToEnd();
275+
var content = new StreamReader(
276+
body,
277+
new UnicodeEncoding(bigEndian: false, byteOrderMark: false, throwOnInvalidBytes: true)).ReadToEnd();
276278
XmlAssert.Equal(expectedOutput, content);
277279
}
278280

test/Microsoft.AspNet.Mvc.Xml.Test/XmlSerializerOutputFormatterTest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,9 @@ public async Task XmlSerializerOutputFormatterWritesUTF16Output()
227227
// Assert
228228
var body = outputFormatterContext.HttpContext.Response.Body;
229229
body.Position = 0;
230-
231-
var content = new StreamReader(body).ReadToEnd();
230+
var content = new StreamReader(
231+
body,
232+
new UnicodeEncoding(bigEndian: false, byteOrderMark: false, throwOnInvalidBytes: true)).ReadToEnd();
232233
XmlAssert.Equal(expectedOutput, content);
233234
}
234235

test/WebSites/ContentNegotiationWebSite/VCardFormatter_V3.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Text;
88
using System.Threading.Tasks;
99
using ContentNegotiationWebSite.Models;
10+
using Microsoft.AspNet.Http;
1011
using Microsoft.AspNet.Mvc;
1112
using Microsoft.AspNet.Mvc.Internal;
1213
using Microsoft.Net.Http.Headers;
@@ -39,11 +40,7 @@ public override async Task WriteResponseBodyAsync(OutputFormatterContext context
3940
builder.AppendLine();
4041
builder.AppendLine("END:VCARD");
4142

42-
var responseStream = new NonDisposableStream(context.HttpContext.Response.Body);
43-
using (var writer = new StreamWriter(responseStream, context.SelectedEncoding, bufferSize: 1024))
44-
{
45-
await writer.WriteAsync(builder.ToString());
46-
}
43+
await context.HttpContext.Response.WriteAsync(builder.ToString(), context.SelectedEncoding);
4744
}
4845
}
4946
}

test/WebSites/ContentNegotiationWebSite/VCardFormatter_V4.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Text;
88
using System.Threading.Tasks;
99
using ContentNegotiationWebSite.Models;
10+
using Microsoft.AspNet.Http;
1011
using Microsoft.AspNet.Mvc;
1112
using Microsoft.AspNet.Mvc.Internal;
1213
using Microsoft.Net.Http.Headers;
@@ -42,11 +43,7 @@ public override async Task WriteResponseBodyAsync(OutputFormatterContext context
4243
builder.AppendLine();
4344
builder.AppendLine("END:VCARD");
4445

45-
var responseStream = new NonDisposableStream(context.HttpContext.Response.Body);
46-
using (var writer = new StreamWriter(responseStream, context.SelectedEncoding, bufferSize: 1024))
47-
{
48-
await writer.WriteAsync(builder.ToString());
49-
}
46+
await context.HttpContext.Response.WriteAsync(builder.ToString(), context.SelectedEncoding);
5047
}
5148
}
5249
}

0 commit comments

Comments
 (0)