WebSocketSharpResponse.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.Server.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 QueryParamCollection Headers => _response.Headers;
  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 => _response.OutputStream;
  65. public void Close()
  66. {
  67. if (!this.IsClosed)
  68. {
  69. this.IsClosed = true;
  70. try
  71. {
  72. var response = this._response;
  73. var outputStream = response.OutputStream;
  74. // This is needed with compression
  75. outputStream.Flush();
  76. outputStream.Dispose();
  77. response.Close();
  78. }
  79. catch (SocketException)
  80. {
  81. }
  82. catch (Exception ex)
  83. {
  84. _logger.LogError(ex, "Error in HttpListenerResponseWrapper");
  85. }
  86. }
  87. }
  88. public bool IsClosed
  89. {
  90. get;
  91. private set;
  92. }
  93. public void SetContentLength(long contentLength)
  94. {
  95. // you can happily set the Content-Length header in Asp.Net
  96. // but HttpListener will complain if you do - you have to set ContentLength64 on the response.
  97. // workaround: HttpListener throws "The parameter is incorrect" exceptions when we try to set the Content-Length header
  98. _response.ContentLength64 = contentLength;
  99. }
  100. public void SetCookie(Cookie cookie)
  101. {
  102. var cookieStr = AsHeaderValue(cookie);
  103. _response.Headers.Add("Set-Cookie", cookieStr);
  104. }
  105. public static string AsHeaderValue(Cookie cookie)
  106. {
  107. var defaultExpires = DateTime.MinValue;
  108. var path = cookie.Expires == defaultExpires
  109. ? "/"
  110. : cookie.Path ?? "/";
  111. var sb = new StringBuilder();
  112. sb.Append($"{cookie.Name}={cookie.Value};path={path}");
  113. if (cookie.Expires != defaultExpires)
  114. {
  115. sb.Append($";expires={cookie.Expires:R}");
  116. }
  117. if (!string.IsNullOrEmpty(cookie.Domain))
  118. {
  119. sb.Append($";domain={cookie.Domain}");
  120. }
  121. if (cookie.Secure)
  122. {
  123. sb.Append(";Secure");
  124. }
  125. if (cookie.HttpOnly)
  126. {
  127. sb.Append(";HttpOnly");
  128. }
  129. return sb.ToString();
  130. }
  131. public bool SendChunked
  132. {
  133. get => _response.SendChunked;
  134. set => _response.SendChunked = value;
  135. }
  136. public bool KeepAlive { get; set; }
  137. public void ClearCookies()
  138. {
  139. }
  140. public Task TransmitFile(string path, long offset, long count, FileShareMode fileShareMode, CancellationToken cancellationToken)
  141. {
  142. return _response.TransmitFile(path, offset, count, fileShareMode, cancellationToken);
  143. }
  144. }
  145. }