WebSocketSharpResponse.cs 5.4 KB

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