WebSocketSharpResponse.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 MediaBrowser.Model.IO;
  9. using MediaBrowser.Model.Logging;
  10. using MediaBrowser.Model.Services;
  11. using SocketHttpListener.Net;
  12. using HttpListenerResponse = SocketHttpListener.Net.HttpListenerResponse;
  13. using IHttpResponse = MediaBrowser.Model.Services.IHttpResponse;
  14. using IRequest = MediaBrowser.Model.Services.IRequest;
  15. namespace Emby.Server.Implementations.HttpServer.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 bool UseBufferedStream { get; set; }
  30. public Dictionary<string, object> Items { get; private set; }
  31. public object OriginalResponse
  32. {
  33. get { return _response; }
  34. }
  35. public int StatusCode
  36. {
  37. get { return this._response.StatusCode; }
  38. set { this._response.StatusCode = value; }
  39. }
  40. public string StatusDescription
  41. {
  42. get { return this._response.StatusDescription; }
  43. set { this._response.StatusDescription = value; }
  44. }
  45. public string ContentType
  46. {
  47. get { return _response.ContentType; }
  48. set { _response.ContentType = value; }
  49. }
  50. //public ICookies Cookies { get; set; }
  51. public void AddHeader(string name, string value)
  52. {
  53. if (string.Equals(name, "Content-Type", StringComparison.OrdinalIgnoreCase))
  54. {
  55. ContentType = value;
  56. return;
  57. }
  58. _response.AddHeader(name, value);
  59. }
  60. public QueryParamCollection Headers
  61. {
  62. get
  63. {
  64. return _response.Headers;
  65. }
  66. }
  67. public string GetHeader(string name)
  68. {
  69. return _response.Headers[name];
  70. }
  71. public void Redirect(string url)
  72. {
  73. _response.Redirect(url);
  74. }
  75. public Stream OutputStream
  76. {
  77. get { return _response.OutputStream; }
  78. }
  79. public void Close()
  80. {
  81. if (!this.IsClosed)
  82. {
  83. this.IsClosed = true;
  84. try
  85. {
  86. CloseOutputStream(this._response);
  87. }
  88. catch (Exception ex)
  89. {
  90. _logger.ErrorException("Error closing HttpListener output stream", ex);
  91. }
  92. }
  93. }
  94. public void CloseOutputStream(HttpListenerResponse response)
  95. {
  96. try
  97. {
  98. var outputStream = response.OutputStream;
  99. // This is needed with compression
  100. if (outputStream is ResponseStream)
  101. {
  102. //if (!string.IsNullOrWhiteSpace(GetHeader("Content-Encoding")))
  103. {
  104. outputStream.Flush();
  105. }
  106. outputStream.Dispose();
  107. }
  108. response.Close();
  109. }
  110. catch (Exception ex)
  111. {
  112. _logger.ErrorException("Error in HttpListenerResponseWrapper: " + ex.Message, ex);
  113. }
  114. }
  115. public bool IsClosed
  116. {
  117. get;
  118. private set;
  119. }
  120. public void SetContentLength(long contentLength)
  121. {
  122. //you can happily set the Content-Length header in Asp.Net
  123. //but HttpListener will complain if you do - you have to set ContentLength64 on the response.
  124. //workaround: HttpListener throws "The parameter is incorrect" exceptions when we try to set the Content-Length header
  125. _response.ContentLength64 = contentLength;
  126. }
  127. public void SetCookie(Cookie cookie)
  128. {
  129. var cookieStr = AsHeaderValue(cookie);
  130. _response.Headers.Add("Set-Cookie", cookieStr);
  131. }
  132. public static string AsHeaderValue(Cookie cookie)
  133. {
  134. var defaultExpires = DateTime.MinValue;
  135. var path = cookie.Expires == defaultExpires
  136. ? "/"
  137. : cookie.Path ?? "/";
  138. var sb = new StringBuilder();
  139. sb.Append($"{cookie.Name}={cookie.Value};path={path}");
  140. if (cookie.Expires != defaultExpires)
  141. {
  142. sb.Append($";expires={cookie.Expires:R}");
  143. }
  144. if (!string.IsNullOrEmpty(cookie.Domain))
  145. {
  146. sb.Append($";domain={cookie.Domain}");
  147. }
  148. //else if (restrictAllCookiesToDomain != null)
  149. //{
  150. // sb.Append($";domain={restrictAllCookiesToDomain}");
  151. //}
  152. if (cookie.Secure)
  153. {
  154. sb.Append(";Secure");
  155. }
  156. if (cookie.HttpOnly)
  157. {
  158. sb.Append(";HttpOnly");
  159. }
  160. return sb.ToString();
  161. }
  162. public bool SendChunked
  163. {
  164. get { return _response.SendChunked; }
  165. set { _response.SendChunked = value; }
  166. }
  167. public bool KeepAlive { get; set; }
  168. public void ClearCookies()
  169. {
  170. }
  171. public Task TransmitFile(string path, long offset, long count, FileShareMode fileShareMode, CancellationToken cancellationToken)
  172. {
  173. return _response.TransmitFile(path, offset, count, fileShareMode, cancellationToken);
  174. }
  175. }
  176. }