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. private static string AsHeaderValue(Cookie cookie)
  48. {
  49. DateTime defaultExpires = DateTime.MinValue;
  50. var path = cookie.Expires == defaultExpires
  51. ? "/"
  52. : cookie.Path ?? "/";
  53. var sb = new StringBuilder();
  54. sb.Append($"{cookie.Name}={cookie.Value};path={path}");
  55. if (cookie.Expires != defaultExpires)
  56. {
  57. sb.Append($";expires={cookie.Expires:R}");
  58. }
  59. if (!string.IsNullOrEmpty(cookie.Domain))
  60. {
  61. sb.Append($";domain={cookie.Domain}");
  62. }
  63. if (cookie.Secure)
  64. {
  65. sb.Append(";Secure");
  66. }
  67. if (cookie.HttpOnly)
  68. {
  69. sb.Append(";HttpOnly");
  70. }
  71. return sb.ToString();
  72. }
  73. public void AddHeader(string name, string value)
  74. {
  75. if (string.Equals(name, "Content-Type", StringComparison.OrdinalIgnoreCase))
  76. {
  77. ContentType = value;
  78. return;
  79. }
  80. _response.AddHeader(name, value);
  81. }
  82. public string GetHeader(string name)
  83. {
  84. return _response.Headers[name];
  85. }
  86. public void Redirect(string url)
  87. {
  88. _response.Redirect(url);
  89. }
  90. public Stream OutputStream => _response.OutputStream;
  91. public void Close()
  92. {
  93. if (!this.IsClosed)
  94. {
  95. this.IsClosed = true;
  96. try
  97. {
  98. var response = this._response;
  99. var outputStream = response.OutputStream;
  100. // This is needed with compression
  101. outputStream.Flush();
  102. outputStream.Dispose();
  103. response.Close();
  104. }
  105. catch (SocketException)
  106. {
  107. }
  108. catch (Exception ex)
  109. {
  110. _logger.LogError(ex, "Error in HttpListenerResponseWrapper");
  111. }
  112. }
  113. }
  114. public bool IsClosed
  115. {
  116. get;
  117. private set;
  118. }
  119. public void SetContentLength(long contentLength)
  120. {
  121. // you can happily set the Content-Length header in Asp.Net
  122. // but HttpListener will complain if you do - you have to set ContentLength64 on the response.
  123. // workaround: HttpListener throws "The parameter is incorrect" exceptions when we try to set the Content-Length header
  124. _response.ContentLength64 = contentLength;
  125. }
  126. public void SetCookie(Cookie cookie)
  127. {
  128. var cookieStr = AsHeaderValue(cookie);
  129. _response.Headers.Add("Set-Cookie", cookieStr);
  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. }