HttpResponseExtensionsInternal.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //Copyright (c) Service Stack LLC. All Rights Reserved.
  2. //License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
  3. using System;
  4. using System.IO;
  5. using System.Net;
  6. using System.Threading.Tasks;
  7. using System.Collections.Generic;
  8. using System.Threading;
  9. using MediaBrowser.Model.Services;
  10. using ServiceStack.Host;
  11. namespace ServiceStack
  12. {
  13. public static class HttpResponseExtensionsInternal
  14. {
  15. public static async Task<bool> WriteToOutputStream(IResponse response, object result)
  16. {
  17. var asyncStreamWriter = result as IAsyncStreamWriter;
  18. if (asyncStreamWriter != null)
  19. {
  20. await asyncStreamWriter.WriteToAsync(response.OutputStream, CancellationToken.None).ConfigureAwait(false);
  21. return true;
  22. }
  23. var streamWriter = result as IStreamWriter;
  24. if (streamWriter != null)
  25. {
  26. streamWriter.WriteTo(response.OutputStream);
  27. return true;
  28. }
  29. var stream = result as Stream;
  30. if (stream != null)
  31. {
  32. using (stream)
  33. {
  34. await stream.CopyToAsync(response.OutputStream).ConfigureAwait(false);
  35. return true;
  36. }
  37. }
  38. var bytes = result as byte[];
  39. if (bytes != null)
  40. {
  41. response.ContentType = "application/octet-stream";
  42. response.SetContentLength(bytes.Length);
  43. await response.OutputStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
  44. return true;
  45. }
  46. return false;
  47. }
  48. /// <summary>
  49. /// End a ServiceStack Request with no content
  50. /// </summary>
  51. public static void EndRequestWithNoContent(this IResponse httpRes)
  52. {
  53. if (httpRes.StatusCode == (int)HttpStatusCode.OK)
  54. {
  55. httpRes.StatusCode = (int)HttpStatusCode.NoContent;
  56. }
  57. httpRes.SetContentLength(0);
  58. }
  59. public static Task WriteToResponse(this IResponse httpRes, IRequest httpReq, object result)
  60. {
  61. if (result == null)
  62. {
  63. httpRes.EndRequestWithNoContent();
  64. return Task.FromResult(true);
  65. }
  66. var httpResult = result as IHttpResult;
  67. if (httpResult != null)
  68. {
  69. httpResult.RequestContext = httpReq;
  70. httpReq.ResponseContentType = httpResult.ContentType ?? httpReq.ResponseContentType;
  71. return httpRes.WriteToResponseInternal(httpResult, httpReq);
  72. }
  73. return httpRes.WriteToResponseInternal(result, httpReq);
  74. }
  75. /// <summary>
  76. /// Writes to response.
  77. /// Response headers are customizable by implementing IHasHeaders an returning Dictionary of Http headers.
  78. /// </summary>
  79. /// <param name="response">The response.</param>
  80. /// <param name="result">Whether or not it was implicity handled by ServiceStack's built-in handlers.</param>
  81. /// <param name="request">The serialization context.</param>
  82. /// <returns></returns>
  83. private static async Task WriteToResponseInternal(this IResponse response, object result, IRequest request)
  84. {
  85. var defaultContentType = request.ResponseContentType;
  86. var httpResult = result as IHttpResult;
  87. if (httpResult != null)
  88. {
  89. if (httpResult.RequestContext == null)
  90. httpResult.RequestContext = request;
  91. response.StatusCode = httpResult.Status;
  92. response.StatusDescription = httpResult.StatusCode.ToString();
  93. if (string.IsNullOrEmpty(httpResult.ContentType))
  94. {
  95. httpResult.ContentType = defaultContentType;
  96. }
  97. response.ContentType = httpResult.ContentType;
  98. if (httpResult.Cookies != null)
  99. {
  100. var httpRes = response as IHttpResponse;
  101. if (httpRes != null)
  102. {
  103. foreach (var cookie in httpResult.Cookies)
  104. {
  105. httpRes.SetCookie(cookie);
  106. }
  107. }
  108. }
  109. }
  110. var responseOptions = result as IHasHeaders;
  111. if (responseOptions != null)
  112. {
  113. foreach (var responseHeaders in responseOptions.Headers)
  114. {
  115. if (responseHeaders.Key == "Content-Length")
  116. {
  117. response.SetContentLength(long.Parse(responseHeaders.Value));
  118. continue;
  119. }
  120. response.AddHeader(responseHeaders.Key, responseHeaders.Value);
  121. }
  122. }
  123. //ContentType='text/html' is the default for a HttpResponse
  124. //Do not override if another has been set
  125. if (response.ContentType == null || response.ContentType == "text/html")
  126. {
  127. response.ContentType = defaultContentType;
  128. }
  129. if (new HashSet<string> { "application/json", }.Contains(response.ContentType))
  130. {
  131. response.ContentType += "; charset=utf-8";
  132. }
  133. var writeToOutputStreamResult = await WriteToOutputStream(response, result).ConfigureAwait(false);
  134. if (writeToOutputStreamResult)
  135. {
  136. response.Flush(); //required for Compression
  137. return;
  138. }
  139. var responseText = result as string;
  140. if (responseText != null)
  141. {
  142. if (response.ContentType == null || response.ContentType == "text/html")
  143. response.ContentType = defaultContentType;
  144. response.Write(responseText);
  145. return;
  146. }
  147. var serializer = ContentTypes.Instance.GetResponseSerializer(defaultContentType);
  148. serializer(result, response);
  149. }
  150. }
  151. }