WebSocketSharpResponse.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 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. CloseOutputStream(this._response);
  86. }
  87. catch (Exception ex)
  88. {
  89. _logger.ErrorException("Error closing HttpListener output stream", ex);
  90. }
  91. }
  92. }
  93. public void CloseOutputStream(HttpListenerResponse response)
  94. {
  95. try
  96. {
  97. var outputStream = response.OutputStream;
  98. // This is needed with compression
  99. outputStream.Flush();
  100. outputStream.Dispose();
  101. response.Close();
  102. }
  103. catch (Exception ex)
  104. {
  105. _logger.ErrorException("Error in HttpListenerResponseWrapper: " + ex.Message, ex);
  106. }
  107. }
  108. public bool IsClosed
  109. {
  110. get;
  111. private set;
  112. }
  113. public void SetContentLength(long contentLength)
  114. {
  115. //you can happily set the Content-Length header in Asp.Net
  116. //but HttpListener will complain if you do - you have to set ContentLength64 on the response.
  117. //workaround: HttpListener throws "The parameter is incorrect" exceptions when we try to set the Content-Length header
  118. _response.ContentLength64 = contentLength;
  119. }
  120. public void SetCookie(Cookie cookie)
  121. {
  122. var cookieStr = AsHeaderValue(cookie);
  123. _response.Headers.Add("Set-Cookie", cookieStr);
  124. }
  125. public static string AsHeaderValue(Cookie cookie)
  126. {
  127. var defaultExpires = DateTime.MinValue;
  128. var path = cookie.Expires == defaultExpires
  129. ? "/"
  130. : cookie.Path ?? "/";
  131. var sb = new StringBuilder();
  132. sb.Append($"{cookie.Name}={cookie.Value};path={path}");
  133. if (cookie.Expires != defaultExpires)
  134. {
  135. sb.Append($";expires={cookie.Expires:R}");
  136. }
  137. if (!string.IsNullOrEmpty(cookie.Domain))
  138. {
  139. sb.Append($";domain={cookie.Domain}");
  140. }
  141. //else if (restrictAllCookiesToDomain != null)
  142. //{
  143. // sb.Append($";domain={restrictAllCookiesToDomain}");
  144. //}
  145. if (cookie.Secure)
  146. {
  147. sb.Append(";Secure");
  148. }
  149. if (cookie.HttpOnly)
  150. {
  151. sb.Append(";HttpOnly");
  152. }
  153. return sb.ToString();
  154. }
  155. public bool SendChunked
  156. {
  157. get { return _response.SendChunked; }
  158. set { _response.SendChunked = value; }
  159. }
  160. public bool KeepAlive { get; set; }
  161. public void ClearCookies()
  162. {
  163. }
  164. public Task TransmitFile(string path, long offset, long count, FileShareMode fileShareMode, CancellationToken cancellationToken)
  165. {
  166. return _response.TransmitFile(path, offset, count, fileShareMode, cancellationToken);
  167. }
  168. }
  169. }