HttpResponse.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. using SocketHttpListener.Net;
  11. namespace SocketHttpListener
  12. {
  13. internal class HttpResponse : HttpBase
  14. {
  15. #region Private Fields
  16. private string _code;
  17. private string _reason;
  18. #endregion
  19. #region Private Constructors
  20. private HttpResponse(string code, string reason, Version version, QueryParamCollection headers)
  21. : base(version, headers)
  22. {
  23. _code = code;
  24. _reason = reason;
  25. }
  26. #endregion
  27. #region Internal Constructors
  28. internal HttpResponse(HttpStatusCode code)
  29. : this(code, code.GetDescription())
  30. {
  31. }
  32. internal HttpResponse(HttpStatusCode code, string reason)
  33. : this(((int)code).ToString(), reason, HttpVersion.Version11, new QueryParamCollection())
  34. {
  35. Headers["Server"] = "websocket-sharp/1.0";
  36. }
  37. #endregion
  38. #region Public Properties
  39. public CookieCollection Cookies
  40. {
  41. get
  42. {
  43. return GetCookies(Headers, true);
  44. }
  45. }
  46. private CookieCollection GetCookies(QueryParamCollection headers, bool response)
  47. {
  48. var name = response ? "Set-Cookie" : "Cookie";
  49. return headers == null || !headers.Contains(name)
  50. ? new CookieCollection()
  51. : CookieHelper.Parse(headers[name], response);
  52. }
  53. public bool IsProxyAuthenticationRequired
  54. {
  55. get
  56. {
  57. return _code == "407";
  58. }
  59. }
  60. public bool IsUnauthorized
  61. {
  62. get
  63. {
  64. return _code == "401";
  65. }
  66. }
  67. public bool IsWebSocketResponse
  68. {
  69. get
  70. {
  71. var headers = Headers;
  72. return ProtocolVersion > HttpVersion.Version10 &&
  73. _code == "101" &&
  74. headers.Contains("Upgrade", "websocket") &&
  75. headers.Contains("Connection", "Upgrade");
  76. }
  77. }
  78. public string Reason
  79. {
  80. get
  81. {
  82. return _reason;
  83. }
  84. }
  85. public string StatusCode
  86. {
  87. get
  88. {
  89. return _code;
  90. }
  91. }
  92. #endregion
  93. #region Internal Methods
  94. internal static HttpResponse CreateCloseResponse(HttpStatusCode code)
  95. {
  96. var res = new HttpResponse(code);
  97. res.Headers["Connection"] = "close";
  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. }