WebSocketSharpResponse.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using MediaBrowser.Model.Logging;
  5. using ServiceStack;
  6. using ServiceStack.Host;
  7. using ServiceStack.Web;
  8. using HttpListenerResponse = WebSocketSharp.Net.HttpListenerResponse;
  9. namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
  10. {
  11. public class WebSocketSharpResponse : IHttpResponse
  12. {
  13. private readonly ILogger _logger;
  14. private readonly HttpListenerResponse response;
  15. public WebSocketSharpResponse(ILogger logger, HttpListenerResponse response)
  16. {
  17. _logger = logger;
  18. this.response = response;
  19. }
  20. public bool UseBufferedStream { get; set; }
  21. public object OriginalResponse
  22. {
  23. get { return response; }
  24. }
  25. public int StatusCode
  26. {
  27. get { return this.response.StatusCode; }
  28. set { this.response.StatusCode = value; }
  29. }
  30. public string StatusDescription
  31. {
  32. get { return this.response.StatusDescription; }
  33. set { this.response.StatusDescription = value; }
  34. }
  35. public string ContentType
  36. {
  37. get { return response.ContentType; }
  38. set { response.ContentType = value; }
  39. }
  40. public ICookies Cookies { get; set; }
  41. public void AddHeader(string name, string value)
  42. {
  43. if (string.Equals(name, "Content-Type", StringComparison.OrdinalIgnoreCase))
  44. {
  45. ContentType = value;
  46. return;
  47. }
  48. response.AddHeader(name, value);
  49. }
  50. public void Redirect(string url)
  51. {
  52. response.Redirect(url);
  53. }
  54. public Stream OutputStream
  55. {
  56. get { return response.OutputStream; }
  57. }
  58. public object Dto { get; set; }
  59. public void Write(string text)
  60. {
  61. try
  62. {
  63. var bOutput = System.Text.Encoding.UTF8.GetBytes(text);
  64. response.ContentLength64 = bOutput.Length;
  65. var outputStream = response.OutputStream;
  66. outputStream.Write(bOutput, 0, bOutput.Length);
  67. Close();
  68. }
  69. catch (Exception ex)
  70. {
  71. _logger.ErrorException("Could not WriteTextToResponse: " + ex.Message, ex);
  72. throw;
  73. }
  74. }
  75. public void Close()
  76. {
  77. if (!this.IsClosed)
  78. {
  79. this.IsClosed = true;
  80. try
  81. {
  82. this.response.CloseOutputStream(_logger);
  83. }
  84. catch (Exception ex)
  85. {
  86. _logger.ErrorException("Error closing HttpListener output stream", ex);
  87. }
  88. }
  89. }
  90. public void End()
  91. {
  92. Close();
  93. }
  94. public void Flush()
  95. {
  96. response.OutputStream.Flush();
  97. }
  98. public bool IsClosed
  99. {
  100. get;
  101. private set;
  102. }
  103. public void SetContentLength(long contentLength)
  104. {
  105. //you can happily set the Content-Length header in Asp.Net
  106. //but HttpListener will complain if you do - you have to set ContentLength64 on the response.
  107. //workaround: HttpListener throws "The parameter is incorrect" exceptions when we try to set the Content-Length header
  108. response.ContentLength64 = contentLength;
  109. }
  110. public void SetCookie(Cookie cookie)
  111. {
  112. var cookieStr = cookie.AsHeaderValue();
  113. response.Headers.Add(HttpHeaders.SetCookie, cookieStr);
  114. }
  115. public bool SendChunked
  116. {
  117. get { return response.SendChunked; }
  118. set { response.SendChunked = value; }
  119. }
  120. }
  121. }