ResponseHelper.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Emby.Server.Implementations.HttpServer;
  9. using MediaBrowser.Model.Services;
  10. namespace Emby.Server.Implementations.Services
  11. {
  12. public static class ResponseHelper
  13. {
  14. public static async Task WriteToResponse(IResponse response, IRequest request, object result, CancellationToken cancellationToken)
  15. {
  16. if (result == null)
  17. {
  18. if (response.StatusCode == (int)HttpStatusCode.OK)
  19. {
  20. response.StatusCode = (int)HttpStatusCode.NoContent;
  21. }
  22. response.SetContentLength(0);
  23. return;
  24. }
  25. var httpResult = result as IHttpResult;
  26. if (httpResult != null)
  27. {
  28. httpResult.RequestContext = request;
  29. request.ResponseContentType = httpResult.ContentType ?? request.ResponseContentType;
  30. }
  31. var defaultContentType = request.ResponseContentType;
  32. if (httpResult != null)
  33. {
  34. if (httpResult.RequestContext == null)
  35. httpResult.RequestContext = request;
  36. response.StatusCode = httpResult.Status;
  37. response.StatusDescription = httpResult.StatusCode.ToString();
  38. if (string.IsNullOrEmpty(httpResult.ContentType))
  39. {
  40. httpResult.ContentType = defaultContentType;
  41. }
  42. response.ContentType = httpResult.ContentType;
  43. if (httpResult.Cookies != null)
  44. {
  45. var httpRes = response as IHttpResponse;
  46. if (httpRes != null)
  47. {
  48. foreach (var cookie in httpResult.Cookies)
  49. {
  50. httpRes.SetCookie(cookie);
  51. }
  52. }
  53. }
  54. }
  55. var responseOptions = result as IHasHeaders;
  56. if (responseOptions != null)
  57. {
  58. foreach (var responseHeaders in responseOptions.Headers)
  59. {
  60. if (string.Equals(responseHeaders.Key, "Content-Length", StringComparison.OrdinalIgnoreCase))
  61. {
  62. response.SetContentLength(long.Parse(responseHeaders.Value));
  63. continue;
  64. }
  65. response.AddHeader(responseHeaders.Key, responseHeaders.Value);
  66. }
  67. }
  68. //ContentType='text/html' is the default for a HttpResponse
  69. //Do not override if another has been set
  70. if (response.ContentType == null || response.ContentType == "text/html")
  71. {
  72. response.ContentType = defaultContentType;
  73. }
  74. if (new HashSet<string> { "application/json", }.Contains(response.ContentType))
  75. {
  76. response.ContentType += "; charset=utf-8";
  77. }
  78. var asyncStreamWriter = result as IAsyncStreamWriter;
  79. if (asyncStreamWriter != null)
  80. {
  81. await asyncStreamWriter.WriteToAsync(response.OutputStream, cancellationToken).ConfigureAwait(false);
  82. return;
  83. }
  84. var streamWriter = result as IStreamWriter;
  85. if (streamWriter != null)
  86. {
  87. streamWriter.WriteTo(response.OutputStream);
  88. return;
  89. }
  90. var fileWriter = result as FileWriter;
  91. if (fileWriter != null)
  92. {
  93. await fileWriter.WriteToAsync(response, cancellationToken).ConfigureAwait(false);
  94. return;
  95. }
  96. var stream = result as Stream;
  97. if (stream != null)
  98. {
  99. using (stream)
  100. {
  101. await stream.CopyToAsync(response.OutputStream).ConfigureAwait(false);
  102. return;
  103. }
  104. }
  105. var bytes = result as byte[];
  106. if (bytes != null)
  107. {
  108. response.ContentType = "application/octet-stream";
  109. response.SetContentLength(bytes.Length);
  110. await response.OutputStream.WriteAsync(bytes, 0, bytes.Length, cancellationToken).ConfigureAwait(false);
  111. return;
  112. }
  113. var responseText = result as string;
  114. if (responseText != null)
  115. {
  116. bytes = Encoding.UTF8.GetBytes(responseText);
  117. response.SetContentLength(bytes.Length);
  118. await response.OutputStream.WriteAsync(bytes, 0, bytes.Length, cancellationToken).ConfigureAwait(false);
  119. return;
  120. }
  121. await WriteObject(request, result, response).ConfigureAwait(false);
  122. }
  123. public static async Task WriteObject(IRequest request, object result, IResponse response)
  124. {
  125. var contentType = request.ResponseContentType;
  126. var serializer = RequestHelper.GetResponseWriter(HttpListenerHost.Instance, contentType);
  127. using (var ms = new MemoryStream())
  128. {
  129. serializer(result, ms);
  130. ms.Position = 0;
  131. response.SetContentLength(ms.Length);
  132. await ms.CopyToAsync(response.OutputStream).ConfigureAwait(false);
  133. }
  134. //serializer(result, outputStream);
  135. }
  136. }
  137. }