From 6865bb53530abbf703b590dd39cfa27274b03107 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 12 Nov 2016 02:45:06 -0500 Subject: [PATCH] update responses --- ServiceStack/Host/ContentTypes.cs | 2 +- .../HttpResponseExtensionsInternal.cs | 35 +++++++++++++------ ServiceStack/HttpResult.cs | 2 +- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/ServiceStack/Host/ContentTypes.cs b/ServiceStack/Host/ContentTypes.cs index 8840e7c8b..f7734a36b 100644 --- a/ServiceStack/Host/ContentTypes.cs +++ b/ServiceStack/Host/ContentTypes.cs @@ -18,7 +18,7 @@ namespace ServiceStack.Host serializer(response, responseStream); } - private Action GetStreamSerializer(string contentType) + public static Action GetStreamSerializer(string contentType) { switch (GetRealContentType(contentType)) { diff --git a/ServiceStack/HttpResponseExtensionsInternal.cs b/ServiceStack/HttpResponseExtensionsInternal.cs index 44b790f5f..318d62429 100644 --- a/ServiceStack/HttpResponseExtensionsInternal.cs +++ b/ServiceStack/HttpResponseExtensionsInternal.cs @@ -15,19 +15,19 @@ namespace ServiceStack { public static class HttpResponseExtensionsInternal { - public static async Task WriteToOutputStream(IResponse response, Stream outputStream, object result) + public static async Task WriteToOutputStream(IResponse response, object result) { var asyncStreamWriter = result as IAsyncStreamWriter; if (asyncStreamWriter != null) { - await asyncStreamWriter.WriteToAsync(outputStream, CancellationToken.None).ConfigureAwait(false); + await asyncStreamWriter.WriteToAsync(response.OutputStream, CancellationToken.None).ConfigureAwait(false); return true; } var streamWriter = result as IStreamWriter; if (streamWriter != null) { - streamWriter.WriteTo(outputStream); + streamWriter.WriteTo(response.OutputStream); return true; } @@ -36,7 +36,7 @@ namespace ServiceStack { using (stream) { - await stream.CopyToAsync(outputStream).ConfigureAwait(false); + await stream.CopyToAsync(response.OutputStream).ConfigureAwait(false); return true; } } @@ -47,7 +47,7 @@ namespace ServiceStack response.ContentType = "application/octet-stream"; response.SetContentLength(bytes.Length); - await outputStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false); + await response.OutputStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false); return true; } @@ -152,9 +152,7 @@ namespace ServiceStack response.ContentType += "; charset=utf-8"; } - var outputStream = response.OutputStream; - - var writeToOutputStreamResult = await WriteToOutputStream(response, outputStream, result).ConfigureAwait(false); + var writeToOutputStreamResult = await WriteToOutputStream(response, result).ConfigureAwait(false); if (writeToOutputStreamResult) { return; @@ -167,11 +165,28 @@ namespace ServiceStack response.ContentType = defaultContentType; var bytes = Encoding.UTF8.GetBytes(responseText); - await outputStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false); + await response.OutputStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false); return; } - ContentTypes.Instance.SerializeToStream(request, result, outputStream); + await WriteObject(request, result, response).ConfigureAwait(false); + } + + public static async Task WriteObject(IRequest request, object result, IResponse response) + { + var contentType = request.ResponseContentType; + var serializer = ContentTypes.GetStreamSerializer(contentType); + + using (var ms = new MemoryStream()) + { + serializer(result, ms); + + ms.Position = 0; + response.SetContentLength(ms.Length); + await ms.CopyToAsync(response.OutputStream).ConfigureAwait(false); + } + + //serializer(result, outputStream); } } } diff --git a/ServiceStack/HttpResult.cs b/ServiceStack/HttpResult.cs index e25002b3e..3f86ffdf7 100644 --- a/ServiceStack/HttpResult.cs +++ b/ServiceStack/HttpResult.cs @@ -55,7 +55,7 @@ namespace ServiceStack return; } - ContentTypes.Instance.SerializeToStream(this.RequestContext, this.Response, responseStream); + await HttpResponseExtensionsInternal.WriteObject(this.RequestContext, this.Response, response).ConfigureAwait(false); } } }