WebSocketSharpResponse.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 SetContentLength(long contentLength)
  117. {
  118. // you can happily set the Content-Length header in Asp.Net
  119. // but HttpListener will complain if you do - you have to set ContentLength64 on the response.
  120. // workaround: HttpListener throws "The parameter is incorrect" exceptions when we try to set the Content-Length header
  121. //_response.ContentLength64 = contentLength;
  122. }
  123. public void SetCookie(Cookie cookie)
  124. {
  125. var cookieStr = AsHeaderValue(cookie);
  126. _response.Headers.Add("Set-Cookie", cookieStr);
  127. }
  128. public bool SendChunked { get; set; }
  129. public bool KeepAlive { get; set; }
  130. public void ClearCookies()
  131. {
  132. }
  133. const int StreamCopyToBufferSize = 81920;
  134. public async Task TransmitFile(string path, long offset, long count, FileShareMode fileShareMode, IFileSystem fileSystem, IStreamHelper streamHelper, CancellationToken cancellationToken)
  135. {
  136. // TODO
  137. // return _response.TransmitFile(path, offset, count, fileShareMode, cancellationToken);
  138. var allowAsync = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
  139. //if (count <= 0)
  140. //{
  141. // allowAsync = true;
  142. //}
  143. var fileOpenOptions = FileOpenOptions.SequentialScan;
  144. if (allowAsync)
  145. {
  146. fileOpenOptions |= FileOpenOptions.Asynchronous;
  147. }
  148. // use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
  149. using (var fs = fileSystem.GetFileStream(path, FileOpenMode.Open, FileAccessMode.Read, fileShareMode, fileOpenOptions))
  150. {
  151. if (offset > 0)
  152. {
  153. fs.Position = offset;
  154. }
  155. if (count > 0)
  156. {
  157. await streamHelper.CopyToAsync(fs, OutputStream, count, cancellationToken).ConfigureAwait(false);
  158. }
  159. else
  160. {
  161. await fs.CopyToAsync(OutputStream, StreamCopyToBufferSize, cancellationToken).ConfigureAwait(false);
  162. }
  163. }
  164. }
  165. }
  166. }