WebSocketSharpResponse.cs 4.0 KB

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