WebSocketSharpResponse.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Emby.Server.Implementations;
  11. using MediaBrowser.Model.IO;
  12. using MediaBrowser.Model.Services;
  13. using Microsoft.AspNetCore.Http;
  14. using Microsoft.Extensions.Logging;
  15. using IHttpResponse = MediaBrowser.Model.Services.IHttpResponse;
  16. using IRequest = MediaBrowser.Model.Services.IRequest;
  17. namespace Emby.Server.Implementations.SocketSharp
  18. {
  19. public class WebSocketSharpResponse : IHttpResponse
  20. {
  21. private readonly ILogger _logger;
  22. private readonly HttpResponse _response;
  23. public WebSocketSharpResponse(ILogger logger, HttpResponse response, IRequest request)
  24. {
  25. _logger = logger;
  26. this._response = response;
  27. Items = new Dictionary<string, object>();
  28. Request = request;
  29. }
  30. public IRequest Request { get; private set; }
  31. public Dictionary<string, object> Items { get; private set; }
  32. public object OriginalResponse => _response;
  33. public int StatusCode
  34. {
  35. get => this._response.StatusCode;
  36. set => this._response.StatusCode = value;
  37. }
  38. public string StatusDescription { get; set; }
  39. public string ContentType
  40. {
  41. get => _response.ContentType;
  42. set => _response.ContentType = value;
  43. }
  44. public QueryParamCollection Headers => new QueryParamCollection(_response.Headers);
  45. private static string AsHeaderValue(Cookie cookie)
  46. {
  47. DateTime defaultExpires = DateTime.MinValue;
  48. var path = cookie.Expires == defaultExpires
  49. ? "/"
  50. : cookie.Path ?? "/";
  51. var sb = new StringBuilder();
  52. sb.Append($"{cookie.Name}={cookie.Value};path={path}");
  53. if (cookie.Expires != defaultExpires)
  54. {
  55. sb.Append($";expires={cookie.Expires:R}");
  56. }
  57. if (!string.IsNullOrEmpty(cookie.Domain))
  58. {
  59. sb.Append($";domain={cookie.Domain}");
  60. }
  61. if (cookie.Secure)
  62. {
  63. sb.Append(";Secure");
  64. }
  65. if (cookie.HttpOnly)
  66. {
  67. sb.Append(";HttpOnly");
  68. }
  69. return sb.ToString();
  70. }
  71. public void AddHeader(string name, string value)
  72. {
  73. if (string.Equals(name, "Content-Type", StringComparison.OrdinalIgnoreCase))
  74. {
  75. ContentType = value;
  76. return;
  77. }
  78. _response.Headers.Add(name, value);
  79. }
  80. public string GetHeader(string name)
  81. {
  82. return _response.Headers[name];
  83. }
  84. public void Redirect(string url)
  85. {
  86. _response.Redirect(url);
  87. }
  88. public Stream OutputStream => _response.Body;
  89. public void Close()
  90. {
  91. if (!this.IsClosed)
  92. {
  93. this.IsClosed = true;
  94. try
  95. {
  96. var response = this._response;
  97. var outputStream = response.Body;
  98. // This is needed with compression
  99. outputStream.Flush();
  100. outputStream.Dispose();
  101. }
  102. catch (SocketException)
  103. {
  104. }
  105. catch (Exception ex)
  106. {
  107. _logger.LogError(ex, "Error in HttpListenerResponseWrapper");
  108. }
  109. }
  110. }
  111. public bool IsClosed
  112. {
  113. get;
  114. private set;
  115. }
  116. public void SetCookie(Cookie cookie)
  117. {
  118. var cookieStr = AsHeaderValue(cookie);
  119. _response.Headers.Add("Set-Cookie", cookieStr);
  120. }
  121. public bool SendChunked { get; set; }
  122. public bool KeepAlive { get; set; }
  123. public void ClearCookies()
  124. {
  125. }
  126. const int StreamCopyToBufferSize = 81920;
  127. public async Task TransmitFile(string path, long offset, long count, FileShareMode fileShareMode, IFileSystem fileSystem, IStreamHelper streamHelper, CancellationToken cancellationToken)
  128. {
  129. // TODO
  130. // return _response.TransmitFile(path, offset, count, fileShareMode, cancellationToken);
  131. var allowAsync = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
  132. //if (count <= 0)
  133. //{
  134. // allowAsync = true;
  135. //}
  136. var fileOpenOptions = FileOpenOptions.SequentialScan;
  137. if (allowAsync)
  138. {
  139. fileOpenOptions |= FileOpenOptions.Asynchronous;
  140. }
  141. // use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
  142. using (var fs = fileSystem.GetFileStream(path, FileOpenMode.Open, FileAccessMode.Read, fileShareMode, fileOpenOptions))
  143. {
  144. if (offset > 0)
  145. {
  146. fs.Position = offset;
  147. }
  148. if (count > 0)
  149. {
  150. await streamHelper.CopyToAsync(fs, OutputStream, count, cancellationToken).ConfigureAwait(false);
  151. }
  152. else
  153. {
  154. await fs.CopyToAsync(OutputStream, StreamCopyToBufferSize, cancellationToken).ConfigureAwait(false);
  155. }
  156. }
  157. }
  158. }
  159. }