WebSocketSharpResponse.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Model.IO;
  10. using MediaBrowser.Model.Services;
  11. using Microsoft.Extensions.Logging;
  12. using HttpListenerResponse = SocketHttpListener.Net.HttpListenerResponse;
  13. using IHttpResponse = MediaBrowser.Model.Services.IHttpResponse;
  14. using IRequest = MediaBrowser.Model.Services.IRequest;
  15. namespace Jellyfin.SocketSharp
  16. {
  17. public class WebSocketSharpResponse : IHttpResponse
  18. {
  19. private readonly ILogger _logger;
  20. private readonly HttpListenerResponse _response;
  21. public WebSocketSharpResponse(ILogger logger, HttpListenerResponse response, IRequest request)
  22. {
  23. _logger = logger;
  24. this._response = response;
  25. Items = new Dictionary<string, object>();
  26. Request = request;
  27. }
  28. public IRequest Request { get; private set; }
  29. public Dictionary<string, object> Items { get; private set; }
  30. public object OriginalResponse
  31. {
  32. get { return _response; }
  33. }
  34. public int StatusCode
  35. {
  36. get { return this._response.StatusCode; }
  37. set { this._response.StatusCode = value; }
  38. }
  39. public string StatusDescription
  40. {
  41. get { return this._response.StatusDescription; }
  42. set { this._response.StatusDescription = value; }
  43. }
  44. public string ContentType
  45. {
  46. get { return _response.ContentType; }
  47. set { _response.ContentType = value; }
  48. }
  49. //public ICookies Cookies { get; set; }
  50. public void AddHeader(string name, string value)
  51. {
  52. if (string.Equals(name, "Content-Type", StringComparison.OrdinalIgnoreCase))
  53. {
  54. ContentType = value;
  55. return;
  56. }
  57. _response.AddHeader(name, value);
  58. }
  59. public QueryParamCollection Headers
  60. {
  61. get
  62. {
  63. return _response.Headers;
  64. }
  65. }
  66. public string GetHeader(string name)
  67. {
  68. return _response.Headers[name];
  69. }
  70. public void Redirect(string url)
  71. {
  72. _response.Redirect(url);
  73. }
  74. public Stream OutputStream
  75. {
  76. get { return _response.OutputStream; }
  77. }
  78. public void Close()
  79. {
  80. if (!this.IsClosed)
  81. {
  82. this.IsClosed = true;
  83. try
  84. {
  85. var response = this._response;
  86. var outputStream = response.OutputStream;
  87. // This is needed with compression
  88. outputStream.Flush();
  89. outputStream.Dispose();
  90. response.Close();
  91. }
  92. catch (SocketException)
  93. {
  94. }
  95. catch (Exception ex)
  96. {
  97. _logger.LogError(ex, "Error in HttpListenerResponseWrapper");
  98. }
  99. }
  100. }
  101. public bool IsClosed
  102. {
  103. get;
  104. private set;
  105. }
  106. public void SetContentLength(long contentLength)
  107. {
  108. //you can happily set the Content-Length header in Asp.Net
  109. //but HttpListener will complain if you do - you have to set ContentLength64 on the response.
  110. //workaround: HttpListener throws "The parameter is incorrect" exceptions when we try to set the Content-Length header
  111. _response.ContentLength64 = contentLength;
  112. }
  113. public void SetCookie(Cookie cookie)
  114. {
  115. var cookieStr = AsHeaderValue(cookie);
  116. _response.Headers.Add("Set-Cookie", cookieStr);
  117. }
  118. public static string AsHeaderValue(Cookie cookie)
  119. {
  120. var defaultExpires = DateTime.MinValue;
  121. var path = cookie.Expires == defaultExpires
  122. ? "/"
  123. : cookie.Path ?? "/";
  124. var sb = new StringBuilder();
  125. sb.Append($"{cookie.Name}={cookie.Value};path={path}");
  126. if (cookie.Expires != defaultExpires)
  127. {
  128. sb.Append($";expires={cookie.Expires:R}");
  129. }
  130. if (!string.IsNullOrEmpty(cookie.Domain))
  131. {
  132. sb.Append($";domain={cookie.Domain}");
  133. }
  134. //else if (restrictAllCookiesToDomain != null)
  135. //{
  136. // sb.Append($";domain={restrictAllCookiesToDomain}");
  137. //}
  138. if (cookie.Secure)
  139. {
  140. sb.Append(";Secure");
  141. }
  142. if (cookie.HttpOnly)
  143. {
  144. sb.Append(";HttpOnly");
  145. }
  146. return sb.ToString();
  147. }
  148. public bool SendChunked
  149. {
  150. get { return _response.SendChunked; }
  151. set { _response.SendChunked = value; }
  152. }
  153. public bool KeepAlive { get; set; }
  154. public void ClearCookies()
  155. {
  156. }
  157. public Task TransmitFile(string path, long offset, long count, FileShareMode fileShareMode, CancellationToken cancellationToken)
  158. {
  159. return _response.TransmitFile(path, offset, count, fileShareMode, cancellationToken);
  160. }
  161. }
  162. }