WebSocketSharpResponse.cs 5.2 KB

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