WebSocketSharpResponse.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Model.IO;
  10. using MediaBrowser.Model.Services;
  11. using Microsoft.Extensions.Logging;
  12. using HttpListenerResponse = SocketHttpListener.Net.HttpListenerResponse;
  13. using IHttpResponse = MediaBrowser.Model.Services.IHttpResponse;
  14. using IRequest = MediaBrowser.Model.Services.IRequest;
  15. namespace Jellyfin.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 => _response;
  31. public int StatusCode
  32. {
  33. get => this._response.StatusCode;
  34. set => this._response.StatusCode = value;
  35. }
  36. public string StatusDescription
  37. {
  38. get => this._response.StatusDescription;
  39. set => this._response.StatusDescription = value;
  40. }
  41. public string ContentType
  42. {
  43. get => _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 QueryParamCollection Headers => _response.Headers;
  57. public string GetHeader(string name)
  58. {
  59. return _response.Headers[name];
  60. }
  61. public void Redirect(string url)
  62. {
  63. _response.Redirect(url);
  64. }
  65. public Stream OutputStream => _response.OutputStream;
  66. public void Close()
  67. {
  68. if (!this.IsClosed)
  69. {
  70. this.IsClosed = true;
  71. try
  72. {
  73. var response = this._response;
  74. var outputStream = response.OutputStream;
  75. // This is needed with compression
  76. outputStream.Flush();
  77. outputStream.Dispose();
  78. response.Close();
  79. }
  80. catch (SocketException)
  81. {
  82. }
  83. catch (Exception ex)
  84. {
  85. _logger.LogError(ex, "Error in HttpListenerResponseWrapper");
  86. }
  87. }
  88. }
  89. public bool IsClosed
  90. {
  91. get;
  92. private set;
  93. }
  94. public void SetContentLength(long contentLength)
  95. {
  96. //you can happily set the Content-Length header in Asp.Net
  97. //but HttpListener will complain if you do - you have to set ContentLength64 on the response.
  98. //workaround: HttpListener throws "The parameter is incorrect" exceptions when we try to set the Content-Length header
  99. _response.ContentLength64 = contentLength;
  100. }
  101. public void SetCookie(Cookie cookie)
  102. {
  103. var cookieStr = AsHeaderValue(cookie);
  104. _response.Headers.Add("Set-Cookie", cookieStr);
  105. }
  106. public static string AsHeaderValue(Cookie cookie)
  107. {
  108. var defaultExpires = DateTime.MinValue;
  109. var path = cookie.Expires == defaultExpires
  110. ? "/"
  111. : cookie.Path ?? "/";
  112. var sb = new StringBuilder();
  113. sb.Append($"{cookie.Name}={cookie.Value};path={path}");
  114. if (cookie.Expires != defaultExpires)
  115. {
  116. sb.Append($";expires={cookie.Expires:R}");
  117. }
  118. if (!string.IsNullOrEmpty(cookie.Domain))
  119. {
  120. sb.Append($";domain={cookie.Domain}");
  121. }
  122. //else if (restrictAllCookiesToDomain != null)
  123. //{
  124. // sb.Append($";domain={restrictAllCookiesToDomain}");
  125. //}
  126. if (cookie.Secure)
  127. {
  128. sb.Append(";Secure");
  129. }
  130. if (cookie.HttpOnly)
  131. {
  132. sb.Append(";HttpOnly");
  133. }
  134. return sb.ToString();
  135. }
  136. public bool SendChunked
  137. {
  138. get => _response.SendChunked;
  139. set => _response.SendChunked = value;
  140. }
  141. public bool KeepAlive { get; set; }
  142. public void ClearCookies()
  143. {
  144. }
  145. public Task TransmitFile(string path, long offset, long count, FileShareMode fileShareMode, CancellationToken cancellationToken)
  146. {
  147. return _response.TransmitFile(path, offset, count, fileShareMode, cancellationToken);
  148. }
  149. }
  150. }