WebSocketSharpResponse.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 SocketHttpListener.Net;
  11. using HttpListenerResponse = SocketHttpListener.Net.HttpListenerResponse;
  12. using IHttpResponse = MediaBrowser.Model.Services.IHttpResponse;
  13. using IRequest = MediaBrowser.Model.Services.IRequest;
  14. namespace Emby.Server.Implementations.HttpServer.SocketSharp
  15. {
  16. public class WebSocketSharpResponse : IHttpResponse
  17. {
  18. private readonly ILogger _logger;
  19. private readonly HttpListenerResponse _response;
  20. public WebSocketSharpResponse(ILogger logger, HttpListenerResponse response, IRequest request)
  21. {
  22. _logger = logger;
  23. this._response = response;
  24. Items = new Dictionary<string, object>();
  25. Request = request;
  26. }
  27. public IRequest Request { get; private set; }
  28. public bool UseBufferedStream { get; 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 string GetHeader(string name)
  60. {
  61. return _response.Headers[name];
  62. }
  63. public void Redirect(string url)
  64. {
  65. _response.Redirect(url);
  66. }
  67. public Stream OutputStream
  68. {
  69. get { return _response.OutputStream; }
  70. }
  71. public void Close()
  72. {
  73. if (!this.IsClosed)
  74. {
  75. this.IsClosed = true;
  76. try
  77. {
  78. CloseOutputStream(this._response);
  79. }
  80. catch (Exception ex)
  81. {
  82. _logger.ErrorException("Error closing HttpListener output stream", ex);
  83. }
  84. }
  85. }
  86. public void CloseOutputStream(HttpListenerResponse response)
  87. {
  88. try
  89. {
  90. var outputStream = response.OutputStream;
  91. // This is needed with compression
  92. if (outputStream is ResponseStream)
  93. {
  94. //if (!string.IsNullOrWhiteSpace(GetHeader("Content-Encoding")))
  95. {
  96. outputStream.Flush();
  97. }
  98. outputStream.Dispose();
  99. }
  100. response.Close();
  101. }
  102. catch (Exception ex)
  103. {
  104. _logger.ErrorException("Error in HttpListenerResponseWrapper: " + ex.Message, ex);
  105. }
  106. }
  107. public bool IsClosed
  108. {
  109. get;
  110. private set;
  111. }
  112. public void SetContentLength(long contentLength)
  113. {
  114. //you can happily set the Content-Length header in Asp.Net
  115. //but HttpListener will complain if you do - you have to set ContentLength64 on the response.
  116. //workaround: HttpListener throws "The parameter is incorrect" exceptions when we try to set the Content-Length header
  117. _response.ContentLength64 = contentLength;
  118. }
  119. public void SetCookie(Cookie cookie)
  120. {
  121. var cookieStr = AsHeaderValue(cookie);
  122. _response.Headers.Add("Set-Cookie", cookieStr);
  123. }
  124. public static string AsHeaderValue(Cookie cookie)
  125. {
  126. var defaultExpires = DateTime.MinValue;
  127. var path = cookie.Expires == defaultExpires
  128. ? "/"
  129. : cookie.Path ?? "/";
  130. var sb = new StringBuilder();
  131. sb.Append($"{cookie.Name}={cookie.Value};path={path}");
  132. if (cookie.Expires != defaultExpires)
  133. {
  134. sb.Append($";expires={cookie.Expires:R}");
  135. }
  136. if (!string.IsNullOrEmpty(cookie.Domain))
  137. {
  138. sb.Append($";domain={cookie.Domain}");
  139. }
  140. //else if (restrictAllCookiesToDomain != null)
  141. //{
  142. // sb.Append($";domain={restrictAllCookiesToDomain}");
  143. //}
  144. if (cookie.Secure)
  145. {
  146. sb.Append(";Secure");
  147. }
  148. if (cookie.HttpOnly)
  149. {
  150. sb.Append(";HttpOnly");
  151. }
  152. return sb.ToString();
  153. }
  154. public bool SendChunked
  155. {
  156. get { return _response.SendChunked; }
  157. set { _response.SendChunked = value; }
  158. }
  159. public bool KeepAlive { get; set; }
  160. public void ClearCookies()
  161. {
  162. }
  163. public Task TransmitFile(string path, long offset, long count, CancellationToken cancellationToken)
  164. {
  165. return _response.TransmitFile(path, offset, count, cancellationToken);
  166. }
  167. }
  168. }