WebSocketSharpResponse.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using MediaBrowser.Model.Logging;
  6. using ServiceStack;
  7. using ServiceStack.Host;
  8. using HttpListenerResponse = SocketHttpListener.Net.HttpListenerResponse;
  9. using IHttpResponse = MediaBrowser.Model.Services.IHttpResponse;
  10. using IRequest = MediaBrowser.Model.Services.IRequest;
  11. namespace MediaBrowser.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 object Dto { get; set; }
  69. public void Write(string text)
  70. {
  71. var bOutput = System.Text.Encoding.UTF8.GetBytes(text);
  72. _response.ContentLength64 = bOutput.Length;
  73. var outputStream = _response.OutputStream;
  74. outputStream.Write(bOutput, 0, bOutput.Length);
  75. Close();
  76. }
  77. public void Close()
  78. {
  79. if (!this.IsClosed)
  80. {
  81. this.IsClosed = true;
  82. try
  83. {
  84. this._response.CloseOutputStream(_logger);
  85. }
  86. catch (Exception ex)
  87. {
  88. _logger.ErrorException("Error closing HttpListener output stream", ex);
  89. }
  90. }
  91. }
  92. public void End()
  93. {
  94. Close();
  95. }
  96. public void Flush()
  97. {
  98. _response.OutputStream.Flush();
  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 = cookie.AsHeaderValue();
  115. _response.Headers.Add(HttpHeaders.SetCookie, cookieStr);
  116. }
  117. public bool SendChunked
  118. {
  119. get { return _response.SendChunked; }
  120. set { _response.SendChunked = value; }
  121. }
  122. public bool KeepAlive { get; set; }
  123. public void ClearCookies()
  124. {
  125. }
  126. }
  127. }