HttpResponse.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Specialized;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. using HttpStatusCode = SocketHttpListener.Net.HttpStatusCode;
  7. using HttpVersion = SocketHttpListener.Net.HttpVersion;
  8. using System.Linq;
  9. using MediaBrowser.Model.Services;
  10. namespace SocketHttpListener
  11. {
  12. internal class HttpResponse : HttpBase
  13. {
  14. #region Private Fields
  15. private string _code;
  16. private string _reason;
  17. #endregion
  18. #region Private Constructors
  19. private HttpResponse(string code, string reason, Version version, QueryParamCollection headers)
  20. : base(version, headers)
  21. {
  22. _code = code;
  23. _reason = reason;
  24. }
  25. #endregion
  26. #region Internal Constructors
  27. internal HttpResponse(HttpStatusCode code)
  28. : this(code, code.GetDescription())
  29. {
  30. }
  31. internal HttpResponse(HttpStatusCode code, string reason)
  32. : this(((int)code).ToString(), reason, HttpVersion.Version11, new QueryParamCollection())
  33. {
  34. Headers["Server"] = "websocket-sharp/1.0";
  35. }
  36. #endregion
  37. #region Public Properties
  38. public CookieCollection Cookies
  39. {
  40. get
  41. {
  42. return Headers.GetCookies(true);
  43. }
  44. }
  45. public bool IsProxyAuthenticationRequired
  46. {
  47. get
  48. {
  49. return _code == "407";
  50. }
  51. }
  52. public bool IsUnauthorized
  53. {
  54. get
  55. {
  56. return _code == "401";
  57. }
  58. }
  59. public bool IsWebSocketResponse
  60. {
  61. get
  62. {
  63. var headers = Headers;
  64. return ProtocolVersion > HttpVersion.Version10 &&
  65. _code == "101" &&
  66. headers.Contains("Upgrade", "websocket") &&
  67. headers.Contains("Connection", "Upgrade");
  68. }
  69. }
  70. public string Reason
  71. {
  72. get
  73. {
  74. return _reason;
  75. }
  76. }
  77. public string StatusCode
  78. {
  79. get
  80. {
  81. return _code;
  82. }
  83. }
  84. #endregion
  85. #region Internal Methods
  86. internal static HttpResponse CreateCloseResponse(HttpStatusCode code)
  87. {
  88. var res = new HttpResponse(code);
  89. res.Headers["Connection"] = "close";
  90. return res;
  91. }
  92. internal static HttpResponse CreateWebSocketResponse()
  93. {
  94. var res = new HttpResponse(HttpStatusCode.SwitchingProtocols);
  95. var headers = res.Headers;
  96. headers["Upgrade"] = "websocket";
  97. headers["Connection"] = "Upgrade";
  98. return res;
  99. }
  100. #endregion
  101. #region Public Methods
  102. public void SetCookies(CookieCollection cookies)
  103. {
  104. if (cookies == null || cookies.Count == 0)
  105. return;
  106. var headers = Headers;
  107. var sorted = cookies.OfType<Cookie>().OrderBy(i => i.Name).ToList();
  108. foreach (var cookie in sorted)
  109. headers.Add("Set-Cookie", cookie.ToString());
  110. }
  111. public override string ToString()
  112. {
  113. var output = new StringBuilder(64);
  114. output.AppendFormat("HTTP/{0} {1} {2}{3}", ProtocolVersion, _code, _reason, CrLf);
  115. var headers = Headers;
  116. foreach (var key in headers.Keys)
  117. output.AppendFormat("{0}: {1}{2}", key, headers[key], CrLf);
  118. output.Append(CrLf);
  119. var entity = EntityBody;
  120. if (entity.Length > 0)
  121. output.Append(entity);
  122. return output.ToString();
  123. }
  124. #endregion
  125. }
  126. }