ResponseHelper.cs 5.0 KB

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